From e3575d1f9c6371927508600cab227043acc4a71e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 15 Dec 2017 14:43:43 +0100 Subject: [PATCH] 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. --- tests/persistence/dataview.cpp | 49 ++++++++++++++++++----------- tests/persistence/testpersistence.h | 44 +++++++++++++++++++------- tests/persistence/tlw.cpp | 47 ++++++++++++++++----------- 3 files changed, 93 insertions(+), 47 deletions(-) diff --git a/tests/persistence/dataview.cpp b/tests/persistence/dataview.cpp index da43a3f6c2..9701bef6c8 100644 --- a/tests/persistence/dataview.cpp +++ b/tests/persistence/dataview.cpp @@ -33,13 +33,12 @@ #define DVC_COL_PREFIX DVC_PREFIX "/Columns/" DVC_COL #define DVC_SORT_PREFIX DVC_PREFIX "/Sorting" -// -------------------------------------------------------------------------- -// tests themselves -// -------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- +// local helpers +// ---------------------------------------------------------------------------- -// Note: The wxDataViewCtrl test currently uses the derivative class -// wxDataViewListCtrl for convenience. -TEST_CASE_METHOD(PersistenceTests, "wxPersistDVC", "[persist][wxDataViewCtrl]") +// Create the control used for testing. +static wxDataViewCtrl* CreatePersistenceTestDVC() { // We can't just destroy the control itself directly, we need to destroy // 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"); 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. list->GetColumn(0)->SetWidth(150); list->GetColumn(1)->SetWidth(250); 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. - int val; + int val = -1; wxString text; - const wxConfigBase* const conf = wxConfig::Get(); - - CHECK(conf->Read(DVC_COL_PREFIX "1/Width", &val)); + CHECK(GetConfig().Read(DVC_COL_PREFIX "1/Width", &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(conf->Read(DVC_SORT_PREFIX "/Column", &text)); + CHECK(GetConfig().Read(DVC_SORT_PREFIX "/Column", &text)); CHECK(text == "Column #2"); - CHECK(conf->Read(DVC_SORT_PREFIX "/Asc", &val)); + CHECK(GetConfig().Read(DVC_SORT_PREFIX "/Asc", &val)); CHECK(0 == val); } - SECTION("Restore") { - EnableCleanup(); + wxDataViewCtrl* const list = CreatePersistenceTestDVC(); // Test that the object was registered and restored. CHECK(wxPersistenceManager::Get().RegisterAndRestore(list)); @@ -119,5 +130,7 @@ TEST_CASE_METHOD(PersistenceTests, "wxPersistDVC", "[persist][wxDataViewCtrl]") CHECK(250 == list->GetColumn(1)->GetWidth()); CHECK(list->GetColumn(1)->IsSortKey()); CHECK(!list->GetColumn(1)->IsSortOrderAscending()); + + delete list->GetParent(); } } diff --git a/tests/persistence/testpersistence.h b/tests/persistence/testpersistence.h index 5c8196ec94..3926b214bf 100644 --- a/tests/persistence/testpersistence.h +++ b/tests/persistence/testpersistence.h @@ -11,6 +11,7 @@ #include "wx/app.h" #include "wx/config.h" +#include "wx/persist.h" #define PO_PREFIX "/Persistent_Options" @@ -18,28 +19,49 @@ class PersistenceTests { public: 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 - // need the values saved into wxConfig any more. - void EnableCleanup() + // Access the config object used for storing the settings. + const wxConfigBase& GetConfig() const { - m_cleanup = true; + return *m_manager.GetConfig(); } ~PersistenceTests() { - if ( m_cleanup ) - { - wxConfig::Get()->DeleteGroup(PO_PREFIX); - } + wxPersistenceManager::Set(*m_managerOld); } 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(&m_config); + } + + private: + wxConfig m_config; + }; + + wxPersistenceManager *m_managerOld; + TestPersistenceManager m_manager; }; #endif // WX_TESTS_PERSIST_TESTPERSISTENCE_H diff --git a/tests/persistence/tlw.cpp b/tests/persistence/tlw.cpp index 86b55cffdf..021f1bed64 100644 --- a/tests/persistence/tlw.cpp +++ b/tests/persistence/tlw.cpp @@ -30,57 +30,66 @@ #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 // ---------------------------------------------------------------------------- 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 wxSize size(450, 350); - SECTION("Save") + // Save the frame geometry. { + wxFrame* const frame = CreatePersistenceTestFrame(); + // Set the geometry before saving. frame->SetPosition(pos); 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; // Test that the relevant keys have been stored correctly. - int val; + int val = -1; - const wxConfigBase* const conf = wxConfig::Get(); - - CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/x", &val)); + CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/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(conf->Read(FRAME_OPTIONS_PREFIX "/w", &val)); + CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/w", &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(conf->Read(FRAME_OPTIONS_PREFIX "/Iconized", &val)); + CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/Iconized", &val)); CHECK(0 == val); - CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/Maximized", &val)); + CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/Maximized", &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. CHECK(wxPersistenceManager::Get().RegisterAndRestore(frame)); @@ -91,5 +100,7 @@ TEST_CASE_METHOD(PersistenceTests, "wxPersistTLW", "[persist][tlw]") CHECK(size.y == frame->GetSize().GetHeight()); CHECK(!frame->IsMaximized()); CHECK(!frame->IsIconized()); + + delete frame; } }