Use temporary wxConfig for persistence unit tests
This requires slightly more work, but ensures that we don't leave any traces of running the tests in the system registry or file system, as we can just call DeleteAll() on the config object after finishing with it.
This commit is contained in:
@@ -33,13 +33,12 @@
|
|||||||
#define DVC_COL_PREFIX DVC_PREFIX "/Columns/" DVC_COL
|
#define DVC_COL_PREFIX DVC_PREFIX "/Columns/" DVC_COL
|
||||||
#define DVC_SORT_PREFIX DVC_PREFIX "/Sorting"
|
#define DVC_SORT_PREFIX DVC_PREFIX "/Sorting"
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// tests themselves
|
// local helpers
|
||||||
// --------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// Note: The wxDataViewCtrl test currently uses the derivative class
|
// Create the control used for testing.
|
||||||
// wxDataViewListCtrl for convenience.
|
static wxDataViewCtrl* CreatePersistenceTestDVC()
|
||||||
TEST_CASE_METHOD(PersistenceTests, "wxPersistDVC", "[persist][wxDataViewCtrl]")
|
|
||||||
{
|
{
|
||||||
// We can't just destroy the control itself directly, we need to destroy
|
// We can't just destroy the control itself directly, we need to destroy
|
||||||
// its parent as only this will ensure that it gets wxWindowDestroyEvent
|
// its parent as only this will ensure that it gets wxWindowDestroyEvent
|
||||||
@@ -77,39 +76,51 @@ TEST_CASE_METHOD(PersistenceTests, "wxPersistDVC", "[persist][wxDataViewCtrl]")
|
|||||||
data.push_back("FFFF");
|
data.push_back("FFFF");
|
||||||
list->AppendItem(data);
|
list->AppendItem(data);
|
||||||
|
|
||||||
SECTION("Save")
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
// tests themselves
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Note: The wxDataViewCtrl test currently uses the derivative class
|
||||||
|
// wxDataViewListCtrl for convenience.
|
||||||
|
TEST_CASE_METHOD(PersistenceTests, "wxPersistDVC", "[persist][wxDataViewCtrl]")
|
||||||
|
{
|
||||||
{
|
{
|
||||||
|
wxDataViewCtrl* const list = CreatePersistenceTestDVC();
|
||||||
|
|
||||||
// Adjust the initial settings.
|
// Adjust the initial settings.
|
||||||
list->GetColumn(0)->SetWidth(150);
|
list->GetColumn(0)->SetWidth(150);
|
||||||
list->GetColumn(1)->SetWidth(250);
|
list->GetColumn(1)->SetWidth(250);
|
||||||
list->GetColumn(1)->SetSortOrder(false);
|
list->GetColumn(1)->SetSortOrder(false);
|
||||||
|
|
||||||
wxPersistenceManager::Get().Register(list);
|
CHECK(wxPersistenceManager::Get().Register(list));
|
||||||
|
|
||||||
delete parent;
|
// Deleting the control itself doesn't allow it to save its state as
|
||||||
|
// the wxEVT_DESTROY handler is called too late, so delete its parent
|
||||||
|
// (as would usually be the case) instead.
|
||||||
|
delete list->GetParent();
|
||||||
|
|
||||||
// Test that the relevant keys have been stored correctly.
|
// Test that the relevant keys have been stored correctly.
|
||||||
int val;
|
int val = -1;
|
||||||
wxString text;
|
wxString text;
|
||||||
|
|
||||||
const wxConfigBase* const conf = wxConfig::Get();
|
CHECK(GetConfig().Read(DVC_COL_PREFIX "1/Width", &val));
|
||||||
|
|
||||||
CHECK(conf->Read(DVC_COL_PREFIX "1/Width", &val));
|
|
||||||
CHECK(150 == val);
|
CHECK(150 == val);
|
||||||
|
|
||||||
CHECK(conf->Read(DVC_COL_PREFIX "2/Width", &val));
|
CHECK(GetConfig().Read(DVC_COL_PREFIX "2/Width", &val));
|
||||||
CHECK(250 == val);
|
CHECK(250 == val);
|
||||||
|
|
||||||
CHECK(conf->Read(DVC_SORT_PREFIX "/Column", &text));
|
CHECK(GetConfig().Read(DVC_SORT_PREFIX "/Column", &text));
|
||||||
CHECK(text == "Column #2");
|
CHECK(text == "Column #2");
|
||||||
|
|
||||||
CHECK(conf->Read(DVC_SORT_PREFIX "/Asc", &val));
|
CHECK(GetConfig().Read(DVC_SORT_PREFIX "/Asc", &val));
|
||||||
CHECK(0 == val);
|
CHECK(0 == val);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Restore")
|
|
||||||
{
|
{
|
||||||
EnableCleanup();
|
wxDataViewCtrl* const list = CreatePersistenceTestDVC();
|
||||||
|
|
||||||
// Test that the object was registered and restored.
|
// Test that the object was registered and restored.
|
||||||
CHECK(wxPersistenceManager::Get().RegisterAndRestore(list));
|
CHECK(wxPersistenceManager::Get().RegisterAndRestore(list));
|
||||||
@@ -119,5 +130,7 @@ TEST_CASE_METHOD(PersistenceTests, "wxPersistDVC", "[persist][wxDataViewCtrl]")
|
|||||||
CHECK(250 == list->GetColumn(1)->GetWidth());
|
CHECK(250 == list->GetColumn(1)->GetWidth());
|
||||||
CHECK(list->GetColumn(1)->IsSortKey());
|
CHECK(list->GetColumn(1)->IsSortKey());
|
||||||
CHECK(!list->GetColumn(1)->IsSortOrderAscending());
|
CHECK(!list->GetColumn(1)->IsSortOrderAscending());
|
||||||
|
|
||||||
|
delete list->GetParent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "wx/app.h"
|
#include "wx/app.h"
|
||||||
#include "wx/config.h"
|
#include "wx/config.h"
|
||||||
|
#include "wx/persist.h"
|
||||||
|
|
||||||
#define PO_PREFIX "/Persistent_Options"
|
#define PO_PREFIX "/Persistent_Options"
|
||||||
|
|
||||||
@@ -18,28 +19,49 @@ class PersistenceTests
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PersistenceTests()
|
PersistenceTests()
|
||||||
: m_cleanup(false)
|
: m_managerOld(&wxPersistenceManager::Get())
|
||||||
{
|
{
|
||||||
wxTheApp->SetAppName("wxPersistenceTests");
|
// Install our custom manager, using custom config object, for the test
|
||||||
|
// duration.
|
||||||
|
wxPersistenceManager::Set(m_manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The tests using this fixture should call this method when they don't
|
// Access the config object used for storing the settings.
|
||||||
// need the values saved into wxConfig any more.
|
const wxConfigBase& GetConfig() const
|
||||||
void EnableCleanup()
|
|
||||||
{
|
{
|
||||||
m_cleanup = true;
|
return *m_manager.GetConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
~PersistenceTests()
|
~PersistenceTests()
|
||||||
{
|
{
|
||||||
if ( m_cleanup )
|
wxPersistenceManager::Set(*m_managerOld);
|
||||||
{
|
|
||||||
wxConfig::Get()->DeleteGroup(PO_PREFIX);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_cleanup;
|
class TestPersistenceManager : public wxPersistenceManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TestPersistenceManager()
|
||||||
|
: m_config("PersistenceTests", "wxWidgets")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~TestPersistenceManager() wxOVERRIDE
|
||||||
|
{
|
||||||
|
m_config.DeleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
wxConfigBase* GetConfig() const wxOVERRIDE
|
||||||
|
{
|
||||||
|
return const_cast<wxConfig*>(&m_config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
wxConfig m_config;
|
||||||
|
};
|
||||||
|
|
||||||
|
wxPersistenceManager *m_managerOld;
|
||||||
|
TestPersistenceManager m_manager;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WX_TESTS_PERSIST_TESTPERSISTENCE_H
|
#endif // WX_TESTS_PERSIST_TESTPERSISTENCE_H
|
||||||
|
@@ -30,57 +30,66 @@
|
|||||||
|
|
||||||
#define FRAME_OPTIONS_PREFIX PO_PREFIX "/Window/frame"
|
#define FRAME_OPTIONS_PREFIX PO_PREFIX "/Window/frame"
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// local helpers
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Create the frame used for testing.
|
||||||
|
static wxFrame* CreatePersistenceTestFrame()
|
||||||
|
{
|
||||||
|
wxFrame* const frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, "wxTest");
|
||||||
|
frame->SetName("frame");
|
||||||
|
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// tests themselves
|
// tests themselves
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
TEST_CASE_METHOD(PersistenceTests, "wxPersistTLW", "[persist][tlw]")
|
TEST_CASE_METHOD(PersistenceTests, "wxPersistTLW", "[persist][tlw]")
|
||||||
{
|
{
|
||||||
// Create the objects to persist or restore.
|
|
||||||
wxFrame* const frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, "wxTest");
|
|
||||||
frame->SetName("frame");
|
|
||||||
|
|
||||||
const wxPoint pos(100, 150);
|
const wxPoint pos(100, 150);
|
||||||
const wxSize size(450, 350);
|
const wxSize size(450, 350);
|
||||||
|
|
||||||
SECTION("Save")
|
// Save the frame geometry.
|
||||||
{
|
{
|
||||||
|
wxFrame* const frame = CreatePersistenceTestFrame();
|
||||||
|
|
||||||
// Set the geometry before saving.
|
// Set the geometry before saving.
|
||||||
frame->SetPosition(pos);
|
frame->SetPosition(pos);
|
||||||
frame->SetSize(size);
|
frame->SetSize(size);
|
||||||
|
|
||||||
wxPersistenceManager::Get().Register(frame);
|
CHECK(wxPersistenceManager::Get().Register(frame));
|
||||||
|
|
||||||
// Destroy the frame immediately.
|
// Destroy the frame immediately, i.e. don't use Destroy() here.
|
||||||
delete frame;
|
delete frame;
|
||||||
|
|
||||||
// Test that the relevant keys have been stored correctly.
|
// Test that the relevant keys have been stored correctly.
|
||||||
int val;
|
int val = -1;
|
||||||
|
|
||||||
const wxConfigBase* const conf = wxConfig::Get();
|
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/x", &val));
|
||||||
|
|
||||||
CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/x", &val));
|
|
||||||
CHECK(pos.x == val);
|
CHECK(pos.x == val);
|
||||||
|
|
||||||
CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/y", &val));
|
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/y", &val));
|
||||||
CHECK(pos.y == val);
|
CHECK(pos.y == val);
|
||||||
|
|
||||||
CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/w", &val));
|
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/w", &val));
|
||||||
CHECK(size.x == val);
|
CHECK(size.x == val);
|
||||||
|
|
||||||
CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/h", &val));
|
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/h", &val));
|
||||||
CHECK(size.y == val);
|
CHECK(size.y == val);
|
||||||
|
|
||||||
CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/Iconized", &val));
|
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/Iconized", &val));
|
||||||
CHECK(0 == val);
|
CHECK(0 == val);
|
||||||
|
|
||||||
CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/Maximized", &val));
|
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/Maximized", &val));
|
||||||
CHECK(0 == val);
|
CHECK(0 == val);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Restore")
|
// Now try recreating the frame using the restored values.
|
||||||
{
|
{
|
||||||
EnableCleanup();
|
wxFrame* const frame = CreatePersistenceTestFrame();
|
||||||
|
|
||||||
// Test that the object was registered and restored.
|
// Test that the object was registered and restored.
|
||||||
CHECK(wxPersistenceManager::Get().RegisterAndRestore(frame));
|
CHECK(wxPersistenceManager::Get().RegisterAndRestore(frame));
|
||||||
@@ -91,5 +100,7 @@ TEST_CASE_METHOD(PersistenceTests, "wxPersistTLW", "[persist][tlw]")
|
|||||||
CHECK(size.y == frame->GetSize().GetHeight());
|
CHECK(size.y == frame->GetSize().GetHeight());
|
||||||
CHECK(!frame->IsMaximized());
|
CHECK(!frame->IsMaximized());
|
||||||
CHECK(!frame->IsIconized());
|
CHECK(!frame->IsIconized());
|
||||||
|
|
||||||
|
delete frame;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user