From c75e0babcc236d691b25d6b7e149a063c9fb09ed Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Mon, 21 Aug 2017 22:12:56 +0100 Subject: [PATCH 01/13] Implement persistence for wxDataViewListCtrl --- include/wx/persist/dataview.h | 124 ++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 include/wx/persist/dataview.h diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h new file mode 100644 index 0000000000..950a7707ee --- /dev/null +++ b/include/wx/persist/dataview.h @@ -0,0 +1,124 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/persist/dataview.h +// Purpose: Persistence support for wxDataViewListCtrl +// Author: wxWidgets Team +// Created: 2017-08-21 +// Copyright: (c) 2017 wxWidgets.org +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PERSIST_DATAVIEW_H_ +#define _WX_PERSIST_DATAVIEW_H_ + +#include "wx/persist/window.h" +#include "wx/dataview.h" + +// ---------------------------------------------------------------------------- +// String constants used by wxPersistentDataViewListCtrl. +// ---------------------------------------------------------------------------- + +#define wxPERSIST_DVLC_KIND "DataViewList" + +#define wxPERSIST_DVLC_COL_PREFIX "Column" + +#define wxPERSIST_DVLC_WIDTH "ColWidth" +#define wxPERSIST_DVLC_SORT_KEY "SortKey" +#define wxPERSIST_DVLC_SORT_ASC "SortAsc" + +// ---------------------------------------------------------------------------- +// wxPersistentDataViewListCtrl: Saves and restores user modified column widths +// and single column sort order. +// +// Future improvements could be to save and restore column order if the user +// has changed it and multicolumn sorts. +// ---------------------------------------------------------------------------- + +class wxPersistentDataViewListCtrl : + public wxPersistentWindow +{ +public: + wxPersistentDataViewListCtrl(wxDataViewListCtrl* control) + : wxPersistentWindow(control) + { + } + + virtual void Save() const wxOVERRIDE + { + wxDataViewListCtrl* const control = Get(); + + int sortColumn = -1; + + for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) + { + // Create a prefix string to identify each column. + wxString columnPrefix; + columnPrefix.Printf("%s %d ", wxPERSIST_DVLC_COL_PREFIX, col); + + // Save the width of each column. + int width = control->GetColumn(col)->GetWidth(); + SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, width); + + // Check if this column is the current sort key. + if ( control->GetColumn(col)->IsSortKey() ) + sortColumn = col; + } + + // Note: The current implementation does not save and restore multi- + // column sort keys. + if (control->IsMultiColumnSortAllowed()) + return; + + // Save the current sort key. + SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn); + + // Save the sort direction. + if ( sortColumn > -1 ) + { + bool sortAsc = + control->GetColumn(sortColumn)->IsSortOrderAscending(); + SaveValue(wxPERSIST_DVLC_SORT_ASC, sortAsc); + } + } + + virtual bool Restore() wxOVERRIDE + { + wxDataViewListCtrl* const control = Get(); + + for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) + { + // Create a prefix string to identify each column. + wxString columnPrefix = + wxString::Format("%s %d ", wxPERSIST_DVLC_COL_PREFIX, col); + + // Restore the column width. + int width; + if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_WIDTH, &width) ) + control->GetColumn(col)->SetWidth(width); + } + + // Restore the sort key and order. + int sortColumn; + if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && + sortColumn > -1 && + sortColumn < (int)control->GetColumnCount() ) + { + bool sortAsc = true; + + RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); + control->GetColumn(sortColumn)->SetSortOrder(sortAsc); + } + return true; + } + + virtual wxString GetKind() const wxOVERRIDE + { + return wxPERSIST_DVLC_KIND; + } +}; + +inline wxPersistentObject *wxCreatePersistentObject(wxDataViewListCtrl* control) +{ + return new wxPersistentDataViewListCtrl(control); +} + +#endif // _WX_PERSIST_DATAVIEW_H_ From 384d9f9043669cd35504ba1204f07ea76ec4885c Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Mon, 21 Aug 2017 22:54:21 +0100 Subject: [PATCH 02/13] Minor documentation changes for wxPersistentObject. --- interface/wx/persist.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/interface/wx/persist.h b/interface/wx/persist.h index a60fd1f42c..d703e720b0 100644 --- a/interface/wx/persist.h +++ b/interface/wx/persist.h @@ -292,13 +292,14 @@ protected: bool SaveValue(const wxString& name, T value) const; /** - Restore the value saved by Save(). + Restore a value saved by SaveValue(). @param name - The same name as was used by Save(). + The same name as was used by SaveValue(). @param value - Non-@NULL pointer which will be filled with the value if it was - read successfully or not modified if it wasn't. + Non-@NULL pointer to the same type that was passed to SaveValue(). + The pointed to object will be filled with the saved value if it + was read successfully or not modified otherwise. @return @true if the value was successfully read or @false if it was not found or an error occurred. From 9cad6d191897545c240025f5e685d16894935117 Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Tue, 22 Aug 2017 13:50:08 +0100 Subject: [PATCH 03/13] Make persistence store keys shorter and without spaces --- include/wx/persist/dataview.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index 950a7707ee..52afdc1bdc 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -21,7 +21,7 @@ #define wxPERSIST_DVLC_COL_PREFIX "Column" -#define wxPERSIST_DVLC_WIDTH "ColWidth" +#define wxPERSIST_DVLC_WIDTH "Width" #define wxPERSIST_DVLC_SORT_KEY "SortKey" #define wxPERSIST_DVLC_SORT_ASC "SortAsc" @@ -52,7 +52,7 @@ public: { // Create a prefix string to identify each column. wxString columnPrefix; - columnPrefix.Printf("%s %d ", wxPERSIST_DVLC_COL_PREFIX, col); + columnPrefix.Printf("%s%d", wxPERSIST_DVLC_COL_PREFIX, col); // Save the width of each column. int width = control->GetColumn(col)->GetWidth(); @@ -88,7 +88,7 @@ public: { // Create a prefix string to identify each column. wxString columnPrefix = - wxString::Format("%s %d ", wxPERSIST_DVLC_COL_PREFIX, col); + wxString::Format("%s%d", wxPERSIST_DVLC_COL_PREFIX, col); // Restore the column width. int width; @@ -106,6 +106,7 @@ public: RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); control->GetColumn(sortColumn)->SetSortOrder(sortAsc); + control->GetModel()->Resort(); } return true; } From 110505543fad7909ec339381cde40735554bc170 Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 00:14:11 +0100 Subject: [PATCH 04/13] Organise persistence keys within subgroups and store columns by title rather than index. --- include/wx/persist/dataview.h | 116 +++++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 38 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index 52afdc1bdc..1394ec7289 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -19,11 +19,32 @@ #define wxPERSIST_DVLC_KIND "DataViewList" -#define wxPERSIST_DVLC_COL_PREFIX "Column" +#define wxPERSIST_DVLC_COLUMNS "Columns" +#define wxPERSIST_DVLC_HIDDEN "Hidden" +#define wxPERSIST_DVLC_POS "Position" +#define wxPERSIST_DVLC_TITLE "Title" #define wxPERSIST_DVLC_WIDTH "Width" -#define wxPERSIST_DVLC_SORT_KEY "SortKey" -#define wxPERSIST_DVLC_SORT_ASC "SortAsc" + +#define wxPERSIST_DVLC_SORT_KEY "Sorting/Column" +#define wxPERSIST_DVLC_SORT_ASC "Sorting/Asc" + +// ---------------------------------------------------------------------------- +// Helper function to search for a column by its title. +// ---------------------------------------------------------------------------- +wxDataViewColumn* GetColumnByTitle(wxDataViewCtrl* control, + const wxString& name) +{ + wxASSERT(control); + + for ( unsigned int col = 0; col < control->GetColumnCount(); col++) + { + if ( control->GetColumn(col)->GetTitle() == name) + return control->GetColumn(col); + } + + return NULL; +} // ---------------------------------------------------------------------------- // wxPersistentDataViewListCtrl: Saves and restores user modified column widths @@ -33,34 +54,39 @@ // has changed it and multicolumn sorts. // ---------------------------------------------------------------------------- -class wxPersistentDataViewListCtrl : - public wxPersistentWindow +class wxPersistentDataViewCtrl : public wxPersistentWindow { public: - wxPersistentDataViewListCtrl(wxDataViewListCtrl* control) - : wxPersistentWindow(control) + wxPersistentDataViewCtrl(wxDataViewCtrl* control) + : wxPersistentWindow(control) { } virtual void Save() const wxOVERRIDE { - wxDataViewListCtrl* const control = Get(); + wxDataViewCtrl* const control = Get(); - int sortColumn = -1; + wxDataViewColumn* sortColumn = NULL; for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) { + wxDataViewColumn* column = control->GetColumn(col); + wxASSERT(column); + // Create a prefix string to identify each column. wxString columnPrefix; - columnPrefix.Printf("%s%d", wxPERSIST_DVLC_COL_PREFIX, col); + columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS, + column->GetTitle()); + + // Save the column attributes. + SaveValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, column->IsHidden()); + SaveValue(columnPrefix + wxPERSIST_DVLC_POS, + control->GetColumnPosition(column)); + SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, column->GetWidth()); - // Save the width of each column. - int width = control->GetColumn(col)->GetWidth(); - SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, width); - // Check if this column is the current sort key. - if ( control->GetColumn(col)->IsSortKey() ) - sortColumn = col; + if ( column->IsSortKey() ) + sortColumn = column; } // Note: The current implementation does not save and restore multi- @@ -68,45 +94,59 @@ public: if (control->IsMultiColumnSortAllowed()) return; - // Save the current sort key. - SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn); - - // Save the sort direction. - if ( sortColumn > -1 ) + // Save the sort key and direction if there is a valid sort. + if ( sortColumn ) { - bool sortAsc = - control->GetColumn(sortColumn)->IsSortOrderAscending(); - SaveValue(wxPERSIST_DVLC_SORT_ASC, sortAsc); + SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn->GetTitle()); + SaveValue(wxPERSIST_DVLC_SORT_ASC, + sortColumn->IsSortOrderAscending()); } } virtual bool Restore() wxOVERRIDE { - wxDataViewListCtrl* const control = Get(); + wxDataViewCtrl* const control = Get(); + wxDataViewColumn* column; for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) { - // Create a prefix string to identify each column. - wxString columnPrefix = - wxString::Format("%s%d", wxPERSIST_DVLC_COL_PREFIX, col); - + column = control->GetColumn(col); + wxASSERT(column); + + // Create a prefix string to identify each column within the + // persistence store (columns are stored by title). The persistence + // store benignly handles cases where the title is not found. + wxString columnPrefix; + columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS, + column->GetTitle()); + + // Restore column hidden status. + bool hidden; + if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, &hidden) ) + column->SetHidden(hidden); + // Restore the column width. int width; if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_WIDTH, &width) ) - control->GetColumn(col)->SetWidth(width); + column->SetWidth(width); + + // TODO: Set the column's view position. } // Restore the sort key and order. - int sortColumn; + wxString sortColumn; if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && - sortColumn > -1 && - sortColumn < (int)control->GetColumnCount() ) + sortColumn != "" ) { bool sortAsc = true; + column = GetColumnByTitle(control, sortColumn); - RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); - control->GetColumn(sortColumn)->SetSortOrder(sortAsc); - control->GetModel()->Resort(); + if ( column ) + { + RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); + column->SetSortOrder(sortAsc); + control->GetModel()->Resort(); + } } return true; } @@ -117,9 +157,9 @@ public: } }; -inline wxPersistentObject *wxCreatePersistentObject(wxDataViewListCtrl* control) +inline wxPersistentObject *wxCreatePersistentObject(wxDataViewCtrl* control) { - return new wxPersistentDataViewListCtrl(control); + return new wxPersistentDataViewCtrl(control); } #endif // _WX_PERSIST_DATAVIEW_H_ From 326b76e649252258e7682e12961f2fbc0930541e Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 00:21:02 +0100 Subject: [PATCH 05/13] Only attempt to update the sort order if the control has a model attached. --- include/wx/persist/dataview.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index 1394ec7289..d0a1ad622f 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -133,10 +133,12 @@ public: // TODO: Set the column's view position. } - // Restore the sort key and order. + // Restore the sort key and order if there is a valid model and sort + // criteria. wxString sortColumn; - if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && - sortColumn != "" ) + if ( control->GetModel() && + RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && + sortColumn != "" ) { bool sortAsc = true; column = GetColumnByTitle(control, sortColumn); @@ -145,6 +147,8 @@ public: { RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); column->SetSortOrder(sortAsc); + + // Resort the control based on the new sort criteria. control->GetModel()->Resort(); } } From 24b8f3a32df391a8fc10e87b476080c0d8c5d01a Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 19:27:47 +0100 Subject: [PATCH 06/13] Update documentation to include wxDataViewCtrl in the list of controls supported by the default persistent objects implementation. --- docs/doxygen/overviews/persistence.h | 2 ++ interface/wx/persist.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/docs/doxygen/overviews/persistence.h b/docs/doxygen/overviews/persistence.h index 396dddd587..a353ab6a7c 100644 --- a/docs/doxygen/overviews/persistence.h +++ b/docs/doxygen/overviews/persistence.h @@ -50,6 +50,8 @@ Currently the following classes are supported: - wxTopLevelWindow (and hence wxFrame and wxDialog) - wxBookCtrlBase (i.e. wxNotebook, wxListbook, wxToolbook and wxChoicebook) +- wxDataViewCtrl and its derivatives + (i.e. wxDataViewListCtrl and wxDataViewTreeCtrl) - wxTreebook To automatically save and restore the properties of the windows of classes diff --git a/interface/wx/persist.h b/interface/wx/persist.h index d703e720b0..683f6296b8 100644 --- a/interface/wx/persist.h +++ b/interface/wx/persist.h @@ -204,6 +204,9 @@ protected: /** Base class for persistent object adapters. + See @ref overview_persistence for an overview of persistent objects within + wxWidgets. + wxWidgets persistence framework is non-intrusive, i.e. can work with the classes which have no relationship to nor knowledge of it. To allow this, an intermediate persistence adapter is used: this is just a simple object From 63dead6350751be483484c05aabc94449e4fa60e Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 22:37:27 +0100 Subject: [PATCH 07/13] Add a simple test case for persistence of wxDataViewListCtrl (should test wxDataViewCtrl too) and wxFrame (should test wxTopLevelWindow). Further tests can be added in due course. --- tests/persist/persistence.cpp | 235 ++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 tests/persist/persistence.cpp diff --git a/tests/persist/persistence.cpp b/tests/persist/persistence.cpp new file mode 100644 index 0000000000..2be909b01c --- /dev/null +++ b/tests/persist/persistence.cpp @@ -0,0 +1,235 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/persistence.cpp +// Purpose: wxPersistentObjects unit test +// Author: wxWidgets Team +// Created: 2017-08-23 +// Copyright: (c) 2017 wxWidgets Team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/config.h" + #include "wx/dataview.h" + #include "wx/frame.h" + #include "wx/persist/dataview.h" + #include "wx/persist/toplevel.h" +#endif // WX_PRECOMP + +// ---------------------------------------------------------------------------- +// macros +// ---------------------------------------------------------------------------- + +#define APP_NAME "cpptest" +#define PO_PREFIX "/Persistent_Options" +#define DVLC_PREFIX PO_PREFIX "/DataViewList/dvlc" +#define DVLC_COL "Column #" +#define DVLC_COL_PREFIX DVLC_PREFIX "/Columns/" DVLC_COL +#define DVLC_SORT_PREFIX DVLC_PREFIX "/Sorting" +#define DVLC_FRAME_PREFIX PO_PREFIX "/Window/frame" + +// -------------------------------------------------------------------------- +// test class +// -------------------------------------------------------------------------- + +class PersistenceTestCase : public CppUnit::TestCase +{ +public: + PersistenceTestCase() {} + + virtual void setUp(); + virtual void tearDown(); + +private: + CPPUNIT_TEST_SUITE( PersistenceTestCase ); + CPPUNIT_TEST( FrameSaveTest ); + CPPUNIT_TEST( FrameRestoreTest ); + CPPUNIT_TEST( DVLCSaveTest ); + CPPUNIT_TEST( DVLCRestoreTest ); + CPPUNIT_TEST_SUITE_END(); + + void FrameSaveTest(); + void FrameRestoreTest(); + void DVLCSaveTest(); + void DVLCRestoreTest(); + + wxDataViewListCtrl* m_list; + wxFrame* m_frame; + + wxDECLARE_NO_COPY_CLASS(PersistenceTestCase); +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( PersistenceTestCase ); + +// also include in its own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PersistenceTestCase, "PersistenceTestCase" ); + +void PersistenceTestCase::setUp() +{ + wxConfigBase* conf = wxConfig::Get(); + conf->SetAppName("PersistTest"); + conf->SetVendorName("wxWidgets"); + + // Clear any pre-existing settings. + conf->DeleteGroup("/Persistence_Options"); + conf->Flush(); + + // Create the objects to persist. + m_frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, + "Persistence Test", + wxDefaultPosition, wxSize(400,400)); + + m_list = new wxDataViewListCtrl(m_frame, wxID_ANY, + wxDefaultPosition, + wxDLG_UNIT(m_frame, wxSize(-1,-1)), + wxDV_ROW_LINES|wxDV_SINGLE); + + // Define names so the objects can be found using wxConfig. + m_frame->SetName("frame"); + m_list->SetName("dvlc"); + + // Add some columns to the DVLC. + m_list->AppendTextColumn(DVLC_COL "1", + wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, + wxDATAVIEW_COL_RESIZABLE | + wxDATAVIEW_COL_REORDERABLE | + wxDATAVIEW_COL_SORTABLE); + m_list->AppendTextColumn(DVLC_COL "2", + wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, + wxDATAVIEW_COL_RESIZABLE | + wxDATAVIEW_COL_REORDERABLE | + wxDATAVIEW_COL_SORTABLE); + + // Populate with DVLC data. + wxVector data; + + data.push_back("AAAA"); + data.push_back("BBBB"); + m_list->AppendItem(data); + + data.clear(); + data.push_back("CCCC"); + data.push_back("DDDD"); + m_list->AppendItem(data); + + data.clear(); + data.push_back("EEEE"); + data.push_back("FFFF"); + m_list->AppendItem(data); +} + +void PersistenceTestCase::tearDown() +{ + +} + +void PersistenceTestCase::FrameSaveTest() +{ + // Adjust the initial settings. + m_frame->SetPosition(wxPoint(100, 150)); + m_frame->SetSize(wxSize(450, 350)); + + // Get default managers. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + wxConfigBase* conf = wxConfig::Get(); + + PM.Register(m_frame); + + // Destroy the frame immediately. + m_frame->wxWindowBase::Destroy(); + + // Test that the relevant keys have been stored correctly. + int val; + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/x", &val)); + CPPUNIT_ASSERT_EQUAL(100, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/y", &val)); + CPPUNIT_ASSERT_EQUAL(150, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/h", &val)); + CPPUNIT_ASSERT_EQUAL(350, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/w", &val)); + CPPUNIT_ASSERT_EQUAL(450, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/Iconized", &val)); + CPPUNIT_ASSERT_EQUAL(0, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/Maximized", &val)); + CPPUNIT_ASSERT_EQUAL(0, val); +} + +void PersistenceTestCase::FrameRestoreTest() +{ + // Get default manager. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + + // Test that the objects are registered and can be restored. + CPPUNIT_ASSERT(PM.RegisterAndRestore(m_frame)); + + CPPUNIT_ASSERT_EQUAL(100, m_frame->GetPosition().x); + CPPUNIT_ASSERT_EQUAL(150, m_frame->GetPosition().y); + CPPUNIT_ASSERT_EQUAL(350, m_frame->GetSize().GetHeight()); + CPPUNIT_ASSERT_EQUAL(450, m_frame->GetSize().GetWidth()); + CPPUNIT_ASSERT(!m_frame->IsMaximized()); + CPPUNIT_ASSERT(!m_frame->IsIconized()); +} + +void PersistenceTestCase::DVLCSaveTest() +{ + // Adjust the initial settings. + m_list->GetColumn(0)->SetWidth(150); + m_list->GetColumn(1)->SetWidth(250); + m_list->GetColumn(1)->SetSortOrder(false); + + // Get default managers. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + wxConfigBase* conf = wxConfig::Get(); + + PM.Register(m_list); + + // Destroy the frame containing the DVLC immediately. + m_frame->wxWindowBase::Destroy(); + + // Test that the relevant keys have been stored correctly. + int val; + wxString text; + + CPPUNIT_ASSERT(conf->Read(DVLC_COL_PREFIX "1/Width", &val)); + CPPUNIT_ASSERT_EQUAL(150, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_COL_PREFIX "2/Width", &val)); + CPPUNIT_ASSERT_EQUAL(250, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_SORT_PREFIX "/Column", &text)); + CPPUNIT_ASSERT_EQUAL("Column #2", text); + + CPPUNIT_ASSERT(conf->Read(DVLC_SORT_PREFIX "/Asc", &val)); + CPPUNIT_ASSERT_EQUAL(0, val); +} + +void PersistenceTestCase::DVLCRestoreTest() +{ + // Get default manager. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + + // Test that the objects are registered and can be restored. + CPPUNIT_ASSERT(PM.RegisterAndRestore(m_list)); + + // Test that the correct values were restored. + CPPUNIT_ASSERT_EQUAL(150, m_list->GetColumn(0)->GetWidth()); + CPPUNIT_ASSERT_EQUAL(250, m_list->GetColumn(1)->GetWidth()); + CPPUNIT_ASSERT(m_list->GetColumn(1)->IsSortKey()); + CPPUNIT_ASSERT(!m_list->GetColumn(1)->IsSortOrderAscending()); +} From 9d090fc9e903dc9f6faa93746e3fd2de3270757f Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 23:21:10 +0100 Subject: [PATCH 08/13] Rename all objects to 'DataViewCtrl' and 'DVC' instead of 'DataViewListCtrl' and 'DVLC' to reflect the change of implementation at the base class. --- include/wx/persist/dataview.h | 46 +++++++++++++++++------------------ tests/persist/persistence.cpp | 26 ++++++++++++-------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index d0a1ad622f..1e099e8df2 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////// // Name: wx/persist/dataview.h -// Purpose: Persistence support for wxDataViewListCtrl +// Purpose: Persistence support for wxDataViewCtrl and its derivatives // Author: wxWidgets Team // Created: 2017-08-21 // Copyright: (c) 2017 wxWidgets.org @@ -14,20 +14,20 @@ #include "wx/dataview.h" // ---------------------------------------------------------------------------- -// String constants used by wxPersistentDataViewListCtrl. +// String constants used by wxPersistentDataViewCtrl. // ---------------------------------------------------------------------------- -#define wxPERSIST_DVLC_KIND "DataViewList" +#define wxPERSIST_DVC_KIND "DataView" -#define wxPERSIST_DVLC_COLUMNS "Columns" +#define wxPERSIST_DVC_COLUMNS "Columns" -#define wxPERSIST_DVLC_HIDDEN "Hidden" -#define wxPERSIST_DVLC_POS "Position" -#define wxPERSIST_DVLC_TITLE "Title" -#define wxPERSIST_DVLC_WIDTH "Width" +#define wxPERSIST_DVC_HIDDEN "Hidden" +#define wxPERSIST_DVC_POS "Position" +#define wxPERSIST_DVC_TITLE "Title" +#define wxPERSIST_DVC_WIDTH "Width" -#define wxPERSIST_DVLC_SORT_KEY "Sorting/Column" -#define wxPERSIST_DVLC_SORT_ASC "Sorting/Asc" +#define wxPERSIST_DVC_SORT_KEY "Sorting/Column" +#define wxPERSIST_DVC_SORT_ASC "Sorting/Asc" // ---------------------------------------------------------------------------- // Helper function to search for a column by its title. @@ -47,7 +47,7 @@ wxDataViewColumn* GetColumnByTitle(wxDataViewCtrl* control, } // ---------------------------------------------------------------------------- -// wxPersistentDataViewListCtrl: Saves and restores user modified column widths +// wxPersistentDataViewCtrl: Saves and restores user modified column widths // and single column sort order. // // Future improvements could be to save and restore column order if the user @@ -75,14 +75,14 @@ public: // Create a prefix string to identify each column. wxString columnPrefix; - columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS, + columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS, column->GetTitle()); // Save the column attributes. - SaveValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, column->IsHidden()); - SaveValue(columnPrefix + wxPERSIST_DVLC_POS, + SaveValue(columnPrefix + wxPERSIST_DVC_HIDDEN, column->IsHidden()); + SaveValue(columnPrefix + wxPERSIST_DVC_POS, control->GetColumnPosition(column)); - SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, column->GetWidth()); + SaveValue(columnPrefix + wxPERSIST_DVC_WIDTH, column->GetWidth()); // Check if this column is the current sort key. if ( column->IsSortKey() ) @@ -97,8 +97,8 @@ public: // Save the sort key and direction if there is a valid sort. if ( sortColumn ) { - SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn->GetTitle()); - SaveValue(wxPERSIST_DVLC_SORT_ASC, + SaveValue(wxPERSIST_DVC_SORT_KEY, sortColumn->GetTitle()); + SaveValue(wxPERSIST_DVC_SORT_ASC, sortColumn->IsSortOrderAscending()); } } @@ -117,17 +117,17 @@ public: // persistence store (columns are stored by title). The persistence // store benignly handles cases where the title is not found. wxString columnPrefix; - columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS, + columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS, column->GetTitle()); // Restore column hidden status. bool hidden; - if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, &hidden) ) + if ( RestoreValue(columnPrefix + wxPERSIST_DVC_HIDDEN, &hidden) ) column->SetHidden(hidden); // Restore the column width. int width; - if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_WIDTH, &width) ) + if ( RestoreValue(columnPrefix + wxPERSIST_DVC_WIDTH, &width) ) column->SetWidth(width); // TODO: Set the column's view position. @@ -137,7 +137,7 @@ public: // criteria. wxString sortColumn; if ( control->GetModel() && - RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && + RestoreValue(wxPERSIST_DVC_SORT_KEY, &sortColumn) && sortColumn != "" ) { bool sortAsc = true; @@ -145,7 +145,7 @@ public: if ( column ) { - RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); + RestoreValue(wxPERSIST_DVC_SORT_ASC, &sortAsc); column->SetSortOrder(sortAsc); // Resort the control based on the new sort criteria. @@ -157,7 +157,7 @@ public: virtual wxString GetKind() const wxOVERRIDE { - return wxPERSIST_DVLC_KIND; + return wxPERSIST_DVC_KIND; } }; diff --git a/tests/persist/persistence.cpp b/tests/persist/persistence.cpp index 2be909b01c..492633d591 100644 --- a/tests/persist/persistence.cpp +++ b/tests/persist/persistence.cpp @@ -6,11 +6,15 @@ // Copyright: (c) 2017 wxWidgets Team /////////////////////////////////////////////////////////////////////////////// +// Note: The wxDataViewCtrl test currently uses the derivative class +// wxDataViewListCtrl for convenience. + // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- #include "testprec.h" + #include #ifdef __BORLANDC__ #pragma hdrstop @@ -31,7 +35,7 @@ #define APP_NAME "cpptest" #define PO_PREFIX "/Persistent_Options" -#define DVLC_PREFIX PO_PREFIX "/DataViewList/dvlc" +#define DVLC_PREFIX PO_PREFIX "/DataView/dvlc" #define DVLC_COL "Column #" #define DVLC_COL_PREFIX DVLC_PREFIX "/Columns/" DVLC_COL #define DVLC_SORT_PREFIX DVLC_PREFIX "/Sorting" @@ -44,10 +48,20 @@ class PersistenceTestCase : public CppUnit::TestCase { public: - PersistenceTestCase() {} + PersistenceTestCase() + { + suite_setUp(); + } virtual void setUp(); virtual void tearDown(); + + void suite_setUp() + { + wxTheApp->SetAppName("PersistTest"); + wxConfig::Get()->DeleteGroup("/Persistent_Options"); + wxConfig::Get()->Flush(); + } private: CPPUNIT_TEST_SUITE( PersistenceTestCase ); @@ -76,14 +90,6 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PersistenceTestCase, "PersistenceTestCase void PersistenceTestCase::setUp() { - wxConfigBase* conf = wxConfig::Get(); - conf->SetAppName("PersistTest"); - conf->SetVendorName("wxWidgets"); - - // Clear any pre-existing settings. - conf->DeleteGroup("/Persistence_Options"); - conf->Flush(); - // Create the objects to persist. m_frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, "Persistence Test", From 072a9c20a3e6aa15129eec6052539df2afe9b3fd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 15 Dec 2017 00:35:45 +0100 Subject: [PATCH 09/13] Add new wx/persist/dataview.h to the build system In particular, this ensures that it gets installed by "make install" under Unix. --- Makefile.in | 1 + build/bakefiles/files.bkl | 1 + build/files | 1 + build/msw/wx_core.vcxproj | 1 + build/msw/wx_core.vcxproj.filters | 3 +++ build/msw/wx_vc7_core.vcproj | 3 +++ build/msw/wx_vc8_core.vcproj | 4 ++++ build/msw/wx_vc9_core.vcproj | 4 ++++ 8 files changed, 18 insertions(+) diff --git a/Makefile.in b/Makefile.in index 07e50b8ac3..3a49998522 100644 --- a/Makefile.in +++ b/Makefile.in @@ -3964,6 +3964,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS = \ wx/paper.h \ wx/persist.h \ wx/persist/bookctrl.h \ + wx/persist/dataview.h \ wx/persist/splitter.h \ wx/persist/toplevel.h \ wx/persist/treebook.h \ diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index bd05d5a1dd..850ba226f1 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -1145,6 +1145,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! wx/paper.h wx/persist.h wx/persist/bookctrl.h + wx/persist/dataview.h wx/persist/splitter.h wx/persist/toplevel.h wx/persist/treebook.h diff --git a/build/files b/build/files index f10f704f47..19ade45977 100644 --- a/build/files +++ b/build/files @@ -1066,6 +1066,7 @@ GUI_CMN_HDR = wx/paper.h wx/persist.h wx/persist/bookctrl.h + wx/persist/dataview.h wx/persist/splitter.h wx/persist/toplevel.h wx/persist/treebook.h diff --git a/build/msw/wx_core.vcxproj b/build/msw/wx_core.vcxproj index d530592aae..40cf55da63 100644 --- a/build/msw/wx_core.vcxproj +++ b/build/msw/wx_core.vcxproj @@ -1383,6 +1383,7 @@ + diff --git a/build/msw/wx_core.vcxproj.filters b/build/msw/wx_core.vcxproj.filters index eecc9366e6..d8057768a5 100644 --- a/build/msw/wx_core.vcxproj.filters +++ b/build/msw/wx_core.vcxproj.filters @@ -1723,6 +1723,9 @@ Common Headers + + Common Headers + Common Headers diff --git a/build/msw/wx_vc7_core.vcproj b/build/msw/wx_vc7_core.vcproj index 9c9bfb0294..2427c2944c 100644 --- a/build/msw/wx_vc7_core.vcproj +++ b/build/msw/wx_vc7_core.vcproj @@ -2054,6 +2054,9 @@ + + diff --git a/build/msw/wx_vc8_core.vcproj b/build/msw/wx_vc8_core.vcproj index bbfe94eaf8..ec5944ffc8 100644 --- a/build/msw/wx_vc8_core.vcproj +++ b/build/msw/wx_vc8_core.vcproj @@ -3240,6 +3240,10 @@ RelativePath="..\..\include\wx\dataview.h" > + + diff --git a/build/msw/wx_vc9_core.vcproj b/build/msw/wx_vc9_core.vcproj index 3a399a7c42..09e025ae63 100644 --- a/build/msw/wx_vc9_core.vcproj +++ b/build/msw/wx_vc9_core.vcproj @@ -3236,6 +3236,10 @@ RelativePath="..\..\include\wx\dataview.h" > + + From 4e82f60b8a95b16adb4ff37b2d6678ff5e8e6e55 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 15 Dec 2017 00:39:16 +0100 Subject: [PATCH 10/13] Add documentation for wxPersistentDataViewCtrl Documentation is trivial, but better than nothing. --- interface/wx/persist/dataview.h | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 interface/wx/persist/dataview.h diff --git a/interface/wx/persist/dataview.h b/interface/wx/persist/dataview.h new file mode 100644 index 0000000000..b98896879b --- /dev/null +++ b/interface/wx/persist/dataview.h @@ -0,0 +1,42 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/persist/dataview.h +// Purpose: interface of wxPersistentDataViewCtrl +// Author: Vadim Zeitlin +// Copyright: (c) 2009 Vadim Zeitlin +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +/** + Persistence adapter for wxDataViewCtrl. + + This adapter handles wxDataViewCtrl column widths and sort order. + + @since 3.1.1 + */ +class wxPersistentDataViewCtrl : public wxPersistentWindow +{ +public: + /** + Constructor. + + @param control The associated control. + */ + wxPersistentDataViewCtrl(wxDataViewCtrl* control); + + /** + Save the current column widths and sort order. + */ + void Save() const override; + + /** + Restore the column widths and sort order. + + The wxDataViewCtrl must be initialized before calling this function, i.e. + all of its columns should be already added to it -- otherwise restoring + their width would have no effect. + */ + bool Restore() override; +}; + +/// Overload allowing persistence adapter creation for wxDataViewCtrl objects. +wxPersistentObject *wxCreatePersistentObject(wxDataViewCtrl *book); From 036870ab35d08b97c7d09bd0f7830e1d081228ae Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 15 Dec 2017 00:43:24 +0100 Subject: [PATCH 11/13] Split wxPersistenceManager-related tests and actually build them It seems better to organize the tests in different files and just provide a common fixture-like class to reuse functionality. Also use this as an opportunity to rewrite the tests to use Catch directly instead of using CppUnit-compatible macros. Finally, actually build these tests as part of the test suite. --- tests/Makefile.in | 8 + tests/makefile.bcc | 8 + tests/makefile.gcc | 8 + tests/makefile.vc | 8 + tests/persist/persistence.cpp | 229 ---------------------------- tests/persistence/dataview.cpp | 123 +++++++++++++++ tests/persistence/testpersistence.h | 45 ++++++ tests/persistence/tlw.cpp | 95 ++++++++++++ tests/test.bkl | 2 + tests/test_gui.vcxproj | 2 + tests/test_gui.vcxproj.filters | 8 +- tests/test_vc7_test_gui.vcproj | 6 + tests/test_vc8_test_gui.vcproj | 8 + tests/test_vc9_test_gui.vcproj | 8 + 14 files changed, 328 insertions(+), 230 deletions(-) delete mode 100644 tests/persist/persistence.cpp create mode 100644 tests/persistence/dataview.cpp create mode 100644 tests/persistence/testpersistence.h create mode 100644 tests/persistence/tlw.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index 09456fcfb3..f8a086d08b 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -250,6 +250,8 @@ TEST_GUI_OBJECTS = \ test_gui_safearrayconverttest.o \ test_gui_settings.o \ test_gui_socket.o \ + test_gui_tlw.o \ + test_gui_dataview.o \ test_gui_boxsizer.o \ test_gui_gridsizer.o \ test_gui_wrapsizer.o \ @@ -1015,6 +1017,12 @@ test_gui_settings.o: $(srcdir)/misc/settings.cpp $(TEST_GUI_ODEP) test_gui_socket.o: $(srcdir)/net/socket.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/net/socket.cpp +test_gui_tlw.o: $(srcdir)/persistence/tlw.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/persistence/tlw.cpp + +test_gui_dataview.o: $(srcdir)/persistence/dataview.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/persistence/dataview.cpp + test_gui_boxsizer.o: $(srcdir)/sizers/boxsizer.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/boxsizer.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index d09d849d64..2466423dad 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -236,6 +236,8 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_safearrayconverttest.obj \ $(OBJS)\test_gui_settings.obj \ $(OBJS)\test_gui_socket.obj \ + $(OBJS)\test_gui_tlw.obj \ + $(OBJS)\test_gui_dataview.obj \ $(OBJS)\test_gui_boxsizer.obj \ $(OBJS)\test_gui_gridsizer.obj \ $(OBJS)\test_gui_wrapsizer.obj \ @@ -1068,6 +1070,12 @@ $(OBJS)\test_gui_settings.obj: .\misc\settings.cpp $(OBJS)\test_gui_socket.obj: .\net\socket.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\net\socket.cpp +$(OBJS)\test_gui_tlw.obj: .\persistence\tlw.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\persistence\tlw.cpp + +$(OBJS)\test_gui_dataview.obj: .\persistence\dataview.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\persistence\dataview.cpp + $(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 15b7c210c4..6ff71503e1 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -231,6 +231,8 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_safearrayconverttest.o \ $(OBJS)\test_gui_settings.o \ $(OBJS)\test_gui_socket.o \ + $(OBJS)\test_gui_tlw.o \ + $(OBJS)\test_gui_dataview.o \ $(OBJS)\test_gui_boxsizer.o \ $(OBJS)\test_gui_gridsizer.o \ $(OBJS)\test_gui_wrapsizer.o \ @@ -1045,6 +1047,12 @@ $(OBJS)\test_gui_settings.o: ./misc/settings.cpp $(OBJS)\test_gui_socket.o: ./net/socket.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_gui_tlw.o: ./persistence/tlw.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + +$(OBJS)\test_gui_dataview.o: ./persistence/dataview.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_gui_boxsizer.o: ./sizers/boxsizer.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index e4aa4a03bb..9ccebcdd66 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -242,6 +242,8 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_safearrayconverttest.obj \ $(OBJS)\test_gui_settings.obj \ $(OBJS)\test_gui_socket.obj \ + $(OBJS)\test_gui_tlw.obj \ + $(OBJS)\test_gui_dataview.obj \ $(OBJS)\test_gui_boxsizer.obj \ $(OBJS)\test_gui_gridsizer.obj \ $(OBJS)\test_gui_wrapsizer.obj \ @@ -1247,6 +1249,12 @@ $(OBJS)\test_gui_settings.obj: .\misc\settings.cpp $(OBJS)\test_gui_socket.obj: .\net\socket.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\net\socket.cpp +$(OBJS)\test_gui_tlw.obj: .\persistence\tlw.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\persistence\tlw.cpp + +$(OBJS)\test_gui_dataview.obj: .\persistence\dataview.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\persistence\dataview.cpp + $(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp diff --git a/tests/persist/persistence.cpp b/tests/persist/persistence.cpp deleted file mode 100644 index 4382379a48..0000000000 --- a/tests/persist/persistence.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// Name: tests/persistence/persistence.cpp -// Purpose: wxPersistentObjects unit test -// Author: wxWidgets Team -// Created: 2017-08-23 -// Copyright: (c) 2017 wxWidgets Team -/////////////////////////////////////////////////////////////////////////////// - -// Note: The wxDataViewCtrl test currently uses the derivative class -// wxDataViewListCtrl for convenience. - -// ---------------------------------------------------------------------------- -// headers -// ---------------------------------------------------------------------------- - -#include "testprec.h" - -#ifdef __BORLANDC__ - #pragma hdrstop -#endif - -#ifndef WX_PRECOMP - #include "wx/app.h" - #include "wx/config.h" - #include "wx/dataview.h" - #include "wx/frame.h" - #include "wx/persist/dataview.h" - #include "wx/persist/toplevel.h" -#endif // WX_PRECOMP - -// ---------------------------------------------------------------------------- -// macros -// ---------------------------------------------------------------------------- - -#define APP_NAME "cpptest" -#define PO_PREFIX "/Persistent_Options" -#define DVLC_PREFIX PO_PREFIX "/DataView/dvlc" -#define DVLC_COL "Column #" -#define DVLC_COL_PREFIX DVLC_PREFIX "/Columns/" DVLC_COL -#define DVLC_SORT_PREFIX DVLC_PREFIX "/Sorting" -#define DVLC_FRAME_PREFIX PO_PREFIX "/Window/frame" - -// -------------------------------------------------------------------------- -// test class -// -------------------------------------------------------------------------- - -class PersistenceTestCase : public CppUnit::TestCase -{ -public: - PersistenceTestCase() - { - wxTheApp->SetAppName("PersistTest"); - wxConfig::Get()->DeleteGroup("/Persistent_Options"); - wxConfig::Get()->Flush(); - } - - virtual void setUp(); - -private: - CPPUNIT_TEST_SUITE( PersistenceTestCase ); - CPPUNIT_TEST( FrameSaveTest ); - CPPUNIT_TEST( FrameRestoreTest ); - CPPUNIT_TEST( DVLCSaveTest ); - CPPUNIT_TEST( DVLCRestoreTest ); - CPPUNIT_TEST_SUITE_END(); - - void FrameSaveTest(); - void FrameRestoreTest(); - void DVLCSaveTest(); - void DVLCRestoreTest(); - - wxDataViewListCtrl* m_list; - wxFrame* m_frame; - - wxDECLARE_NO_COPY_CLASS(PersistenceTestCase); -}; - -// register in the unnamed registry so that these tests are run by default -CPPUNIT_TEST_SUITE_REGISTRATION( PersistenceTestCase ); - -// also include in its own registry so that these tests can be run alone -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PersistenceTestCase, "PersistenceTestCase" ); - -void PersistenceTestCase::setUp() -{ - // Create the objects to persist. - m_frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, - "Persistence Test", - wxDefaultPosition, wxSize(400,400)); - - m_list = new wxDataViewListCtrl(m_frame, wxID_ANY, - wxDefaultPosition, - wxDLG_UNIT(m_frame, wxSize(-1,-1)), - wxDV_ROW_LINES|wxDV_SINGLE); - - // Define names so the objects can be found using wxConfig. - m_frame->SetName("frame"); - m_list->SetName("dvlc"); - - // Add some columns to the DVLC. - m_list->AppendTextColumn(DVLC_COL "1", - wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, - wxDATAVIEW_COL_RESIZABLE | - wxDATAVIEW_COL_REORDERABLE | - wxDATAVIEW_COL_SORTABLE); - m_list->AppendTextColumn(DVLC_COL "2", - wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, - wxDATAVIEW_COL_RESIZABLE | - wxDATAVIEW_COL_REORDERABLE | - wxDATAVIEW_COL_SORTABLE); - - // Populate with DVLC data. - wxVector data; - - data.push_back("AAAA"); - data.push_back("BBBB"); - m_list->AppendItem(data); - - data.clear(); - data.push_back("CCCC"); - data.push_back("DDDD"); - m_list->AppendItem(data); - - data.clear(); - data.push_back("EEEE"); - data.push_back("FFFF"); - m_list->AppendItem(data); -} - -void PersistenceTestCase::FrameSaveTest() -{ - // Adjust the initial settings. - m_frame->SetPosition(wxPoint(100, 150)); - m_frame->SetSize(wxSize(450, 350)); - - // Get default managers. - wxPersistenceManager& pm = wxPersistenceManager::Get(); - wxConfigBase* conf = wxConfig::Get(); - - pm.Register(m_frame); - - // Destroy the frame immediately. - m_frame->wxWindowBase::Destroy(); - - // Test that the relevant keys have been stored correctly. - int val; - - CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/x", &val)); - CPPUNIT_ASSERT_EQUAL(100, val); - - CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/y", &val)); - CPPUNIT_ASSERT_EQUAL(150, val); - - CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/h", &val)); - CPPUNIT_ASSERT_EQUAL(350, val); - - CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/w", &val)); - CPPUNIT_ASSERT_EQUAL(450, val); - - CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/Iconized", &val)); - CPPUNIT_ASSERT_EQUAL(0, val); - - CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/Maximized", &val)); - CPPUNIT_ASSERT_EQUAL(0, val); -} - -void PersistenceTestCase::FrameRestoreTest() -{ - // Get default manager. - wxPersistenceManager& pm = wxPersistenceManager::Get(); - - // Test that the objects are registered and can be restored. - CPPUNIT_ASSERT(pm.RegisterAndRestore(m_frame)); - - CPPUNIT_ASSERT_EQUAL(100, m_frame->GetPosition().x); - CPPUNIT_ASSERT_EQUAL(150, m_frame->GetPosition().y); - CPPUNIT_ASSERT_EQUAL(350, m_frame->GetSize().GetHeight()); - CPPUNIT_ASSERT_EQUAL(450, m_frame->GetSize().GetWidth()); - CPPUNIT_ASSERT(!m_frame->IsMaximized()); - CPPUNIT_ASSERT(!m_frame->IsIconized()); -} - -void PersistenceTestCase::DVLCSaveTest() -{ - // Adjust the initial settings. - m_list->GetColumn(0)->SetWidth(150); - m_list->GetColumn(1)->SetWidth(250); - m_list->GetColumn(1)->SetSortOrder(false); - - // Get default managers. - wxPersistenceManager& pm = wxPersistenceManager::Get(); - wxConfigBase* conf = wxConfig::Get(); - - pm.Register(m_list); - - // Destroy the frame containing the DVLC immediately. - m_frame->wxWindowBase::Destroy(); - - // Test that the relevant keys have been stored correctly. - int val; - wxString text; - - CPPUNIT_ASSERT(conf->Read(DVLC_COL_PREFIX "1/Width", &val)); - CPPUNIT_ASSERT_EQUAL(150, val); - - CPPUNIT_ASSERT(conf->Read(DVLC_COL_PREFIX "2/Width", &val)); - CPPUNIT_ASSERT_EQUAL(250, val); - - CPPUNIT_ASSERT(conf->Read(DVLC_SORT_PREFIX "/Column", &text)); - CPPUNIT_ASSERT_EQUAL("Column #2", text); - - CPPUNIT_ASSERT(conf->Read(DVLC_SORT_PREFIX "/Asc", &val)); - CPPUNIT_ASSERT_EQUAL(0, val); -} - -void PersistenceTestCase::DVLCRestoreTest() -{ - // Get default manager. - wxPersistenceManager& pm = wxPersistenceManager::Get(); - - // Test that the objects are registered and can be restored. - CPPUNIT_ASSERT(pm.RegisterAndRestore(m_list)); - - // Test that the correct values were restored. - CPPUNIT_ASSERT_EQUAL(150, m_list->GetColumn(0)->GetWidth()); - CPPUNIT_ASSERT_EQUAL(250, m_list->GetColumn(1)->GetWidth()); - CPPUNIT_ASSERT(m_list->GetColumn(1)->IsSortKey()); - CPPUNIT_ASSERT(!m_list->GetColumn(1)->IsSortOrderAscending()); -} diff --git a/tests/persistence/dataview.cpp b/tests/persistence/dataview.cpp new file mode 100644 index 0000000000..da43a3f6c2 --- /dev/null +++ b/tests/persistence/dataview.cpp @@ -0,0 +1,123 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/persistence/dataview.cpp +// Purpose: wxDataViewCtrl persistence support unit tests +// Author: wxWidgets Team +// Created: 2017-08-23 +// Copyright: (c) 2017 wxWidgets Team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#include "testpersistence.h" + +#ifndef WX_PRECOMP + #include "wx/dataview.h" +#endif // WX_PRECOMP + +#include "wx/persist/dataview.h" + +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- + +#define DVC_PREFIX PO_PREFIX "/DataView/dvc" +#define DVC_COL "Column #" +#define DVC_COL_PREFIX DVC_PREFIX "/Columns/" DVC_COL +#define DVC_SORT_PREFIX DVC_PREFIX "/Sorting" + +// -------------------------------------------------------------------------- +// tests themselves +// -------------------------------------------------------------------------- + +// Note: The wxDataViewCtrl test currently uses the derivative class +// wxDataViewListCtrl for convenience. +TEST_CASE_METHOD(PersistenceTests, "wxPersistDVC", "[persist][wxDataViewCtrl]") +{ + // We can't just destroy the control itself directly, we need to destroy + // its parent as only this will ensure that it gets wxWindowDestroyEvent + // from which its state will be saved. + wxWindow* const parent = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY); + wxDataViewListCtrl* const list = new wxDataViewListCtrl(parent, wxID_ANY); + list->SetName("dvc"); + + // Add some columns to the DVC. + list->AppendTextColumn(DVC_COL "1", + wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, + wxDATAVIEW_COL_RESIZABLE | + wxDATAVIEW_COL_REORDERABLE | + wxDATAVIEW_COL_SORTABLE); + list->AppendTextColumn(DVC_COL "2", + wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, + wxDATAVIEW_COL_RESIZABLE | + wxDATAVIEW_COL_REORDERABLE | + wxDATAVIEW_COL_SORTABLE); + + // Populate with DVC data. + wxVector data; + + data.push_back("AAAA"); + data.push_back("BBBB"); + list->AppendItem(data); + + data.clear(); + data.push_back("CCCC"); + data.push_back("DDDD"); + list->AppendItem(data); + + data.clear(); + data.push_back("EEEE"); + data.push_back("FFFF"); + list->AppendItem(data); + + SECTION("Save") + { + // Adjust the initial settings. + list->GetColumn(0)->SetWidth(150); + list->GetColumn(1)->SetWidth(250); + list->GetColumn(1)->SetSortOrder(false); + + wxPersistenceManager::Get().Register(list); + + delete parent; + + // Test that the relevant keys have been stored correctly. + int val; + wxString text; + + const wxConfigBase* const conf = wxConfig::Get(); + + CHECK(conf->Read(DVC_COL_PREFIX "1/Width", &val)); + CHECK(150 == val); + + CHECK(conf->Read(DVC_COL_PREFIX "2/Width", &val)); + CHECK(250 == val); + + CHECK(conf->Read(DVC_SORT_PREFIX "/Column", &text)); + CHECK(text == "Column #2"); + + CHECK(conf->Read(DVC_SORT_PREFIX "/Asc", &val)); + CHECK(0 == val); + } + + SECTION("Restore") + { + EnableCleanup(); + + // Test that the object was registered and restored. + CHECK(wxPersistenceManager::Get().RegisterAndRestore(list)); + + // Test that the correct values were restored. + CHECK(150 == list->GetColumn(0)->GetWidth()); + CHECK(250 == list->GetColumn(1)->GetWidth()); + CHECK(list->GetColumn(1)->IsSortKey()); + CHECK(!list->GetColumn(1)->IsSortOrderAscending()); + } +} diff --git a/tests/persistence/testpersistence.h b/tests/persistence/testpersistence.h new file mode 100644 index 0000000000..5c8196ec94 --- /dev/null +++ b/tests/persistence/testpersistence.h @@ -0,0 +1,45 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/persistence/testpersistence.h +// Purpose: Fixture for wxPersistentObject unit tests +// Author: wxWidgets Team +// Created: 2017-08-23 +// Copyright: (c) 2017 wxWidgets Team +/////////////////////////////////////////////////////////////////////////////// + +#ifndef WX_TESTS_PERSIST_TESTPERSISTENCE_H +#define WX_TESTS_PERSIST_TESTPERSISTENCE_H + +#include "wx/app.h" +#include "wx/config.h" + +#define PO_PREFIX "/Persistent_Options" + +class PersistenceTests +{ +public: + PersistenceTests() + : m_cleanup(false) + { + wxTheApp->SetAppName("wxPersistenceTests"); + } + + // The tests using this fixture should call this method when they don't + // need the values saved into wxConfig any more. + void EnableCleanup() + { + m_cleanup = true; + } + + ~PersistenceTests() + { + if ( m_cleanup ) + { + wxConfig::Get()->DeleteGroup(PO_PREFIX); + } + } + +private: + bool m_cleanup; +}; + +#endif // WX_TESTS_PERSIST_TESTPERSISTENCE_H diff --git a/tests/persistence/tlw.cpp b/tests/persistence/tlw.cpp new file mode 100644 index 0000000000..86b55cffdf --- /dev/null +++ b/tests/persistence/tlw.cpp @@ -0,0 +1,95 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/persistence/persistence.cpp +// Purpose: wxTLW persistence support unit test +// Author: wxWidgets Team +// Created: 2017-08-23 +// Copyright: (c) 2017 wxWidgets Team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#include "testpersistence.h" + +#ifndef WX_PRECOMP + #include "wx/frame.h" +#endif // WX_PRECOMP + +#include "wx/persist/toplevel.h" + +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- + +#define FRAME_OPTIONS_PREFIX PO_PREFIX "/Window/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") + { + // Set the geometry before saving. + frame->SetPosition(pos); + frame->SetSize(size); + + wxPersistenceManager::Get().Register(frame); + + // Destroy the frame immediately. + delete frame; + + // Test that the relevant keys have been stored correctly. + int val; + + const wxConfigBase* const conf = wxConfig::Get(); + + CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/x", &val)); + CHECK(pos.x == val); + + CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/y", &val)); + CHECK(pos.y == val); + + CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/w", &val)); + CHECK(size.x == val); + + CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/h", &val)); + CHECK(size.y == val); + + CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/Iconized", &val)); + CHECK(0 == val); + + CHECK(conf->Read(FRAME_OPTIONS_PREFIX "/Maximized", &val)); + CHECK(0 == val); + } + + SECTION("Restore") + { + EnableCleanup(); + + // Test that the object was registered and restored. + CHECK(wxPersistenceManager::Get().RegisterAndRestore(frame)); + + CHECK(pos.x == frame->GetPosition().x); + CHECK(pos.y == frame->GetPosition().y); + CHECK(size.x == frame->GetSize().GetWidth()); + CHECK(size.y == frame->GetSize().GetHeight()); + CHECK(!frame->IsMaximized()); + CHECK(!frame->IsIconized()); + } +} diff --git a/tests/test.bkl b/tests/test.bkl index e6b7194890..95007b4836 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -261,6 +261,8 @@ applications. --> net/socket.cpp + persistence/tlw.cpp + persistence/dataview.cpp sizers/boxsizer.cpp sizers/gridsizer.cpp sizers/wrapsizer.cpp diff --git a/tests/test_gui.vcxproj b/tests/test_gui.vcxproj index c6810356d2..af4d68b517 100644 --- a/tests/test_gui.vcxproj +++ b/tests/test_gui.vcxproj @@ -545,6 +545,8 @@ + + diff --git a/tests/test_gui.vcxproj.filters b/tests/test_gui.vcxproj.filters index 169cb53ca1..40ab41a51d 100644 --- a/tests/test_gui.vcxproj.filters +++ b/tests/test_gui.vcxproj.filters @@ -227,6 +227,12 @@ Source Files + + Source Files + + + Source Files + Source Files @@ -293,4 +299,4 @@ Resource Files - \ No newline at end of file + diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index 9db79de191..484c4776ac 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -355,6 +355,9 @@ + + @@ -559,6 +562,9 @@ + + diff --git a/tests/test_vc8_test_gui.vcproj b/tests/test_vc8_test_gui.vcproj index ba44877479..15fda65a98 100644 --- a/tests/test_vc8_test_gui.vcproj +++ b/tests/test_vc8_test_gui.vcproj @@ -910,6 +910,10 @@ RelativePath=".\config\config.cpp" > + + @@ -1214,6 +1218,10 @@ RelativePath=".\controls\textentrytest.cpp" > + + diff --git a/tests/test_vc9_test_gui.vcproj b/tests/test_vc9_test_gui.vcproj index 557797b046..6e99dc8de7 100644 --- a/tests/test_vc9_test_gui.vcproj +++ b/tests/test_vc9_test_gui.vcproj @@ -882,6 +882,10 @@ RelativePath=".\config\config.cpp" > + + @@ -1186,6 +1190,10 @@ RelativePath=".\controls\textentrytest.cpp" > + + From e3575d1f9c6371927508600cab227043acc4a71e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 15 Dec 2017 14:43:43 +0100 Subject: [PATCH 12/13] 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; } } From ed23de08c7880ed9759fa4b3d1e435dcd2ede59a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 15 Dec 2017 15:26:32 +0100 Subject: [PATCH 13/13] Minor clean up of wxPersistentDataViewCtrl Add another helper function, make variables const when possible, fix some small style problems. No real changes. --- include/wx/persist/dataview.h | 74 +++++++++++++++-------------------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index 5be0feaa5f..c5f212b2ee 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -20,8 +20,6 @@ #define wxPERSIST_DVC_KIND "DataView" -#define wxPERSIST_DVC_COLUMNS "Columns" - #define wxPERSIST_DVC_HIDDEN "Hidden" #define wxPERSIST_DVC_POS "Position" #define wxPERSIST_DVC_TITLE "Title" @@ -30,28 +28,6 @@ #define wxPERSIST_DVC_SORT_KEY "Sorting/Column" #define wxPERSIST_DVC_SORT_ASC "Sorting/Asc" -// ---------------------------------------------------------------------------- -// Helper function to search for a column by its title. -// ---------------------------------------------------------------------------- - -namespace wxPrivate -{ - -inline -wxDataViewColumn* -GetColumnByTitle(wxDataViewCtrl* control, const wxString& name) -{ - for ( unsigned int col = 0; col < control->GetColumnCount(); col++) - { - if ( control->GetColumn(col)->GetTitle() == name ) - return control->GetColumn(col); - } - - return NULL; -} - -} // namespace wxPrivate - // ---------------------------------------------------------------------------- // wxPersistentDataViewCtrl: Saves and restores user modified column widths // and single column sort order. @@ -72,22 +48,19 @@ public: { wxDataViewCtrl* const control = Get(); - wxDataViewColumn* sortColumn = NULL; + const wxDataViewColumn* sortColumn = NULL; for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) { - wxDataViewColumn* column = control->GetColumn(col); - wxASSERT(column); + const wxDataViewColumn* const column = control->GetColumn(col); // Create a prefix string to identify each column. - wxString columnPrefix; - columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS, - column->GetTitle()); + const wxString columnPrefix = MakeColumnPrefix(column); // Save the column attributes. SaveValue(columnPrefix + wxPERSIST_DVC_HIDDEN, column->IsHidden()); SaveValue(columnPrefix + wxPERSIST_DVC_POS, - control->GetColumnPosition(column)); + control->GetColumnPosition(column)); SaveValue(columnPrefix + wxPERSIST_DVC_WIDTH, column->GetWidth()); // Check if this column is the current sort key. @@ -97,7 +70,7 @@ public: // Note: The current implementation does not save and restore multi- // column sort keys. - if (control->IsMultiColumnSortAllowed()) + if ( control->IsMultiColumnSortAllowed() ) return; // Save the sort key and direction if there is a valid sort. @@ -105,26 +78,22 @@ public: { SaveValue(wxPERSIST_DVC_SORT_KEY, sortColumn->GetTitle()); SaveValue(wxPERSIST_DVC_SORT_ASC, - sortColumn->IsSortOrderAscending()); + sortColumn->IsSortOrderAscending()); } } virtual bool Restore() wxOVERRIDE { wxDataViewCtrl* const control = Get(); - wxDataViewColumn* column; for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) { - column = control->GetColumn(col); - wxASSERT(column); + wxDataViewColumn* const column = control->GetColumn(col); // Create a prefix string to identify each column within the // persistence store (columns are stored by title). The persistence // store benignly handles cases where the title is not found. - wxString columnPrefix; - columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS, - column->GetTitle()); + const wxString columnPrefix = MakeColumnPrefix(column); // Restore column hidden status. bool hidden; @@ -144,12 +113,10 @@ public: wxString sortColumn; if ( control->GetModel() && RestoreValue(wxPERSIST_DVC_SORT_KEY, &sortColumn) && - sortColumn != "" ) + !sortColumn.empty() ) { bool sortAsc = true; - column = wxPrivate::GetColumnByTitle(control, sortColumn); - - if ( column ) + if ( wxDataViewColumn* column = GetColumnByTitle(control, sortColumn) ) { RestoreValue(wxPERSIST_DVC_SORT_ASC, &sortAsc); column->SetSortOrder(sortAsc); @@ -158,6 +125,7 @@ public: control->GetModel()->Resort(); } } + return true; } @@ -165,6 +133,26 @@ public: { return wxPERSIST_DVC_KIND; } + +private: + // Return a (slash-terminated) prefix for the column-specific entries. + static wxString MakeColumnPrefix(const wxDataViewColumn* column) + { + return wxString::Format("/Columns/%s/", column->GetTitle()); + } + + // Return the column with the given title or NULL. + static wxDataViewColumn* + GetColumnByTitle(wxDataViewCtrl* control, const wxString& title) + { + for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) + { + if ( control->GetColumn(col)->GetTitle() == title ) + return control->GetColumn(col); + } + + return NULL; + } }; inline wxPersistentObject *wxCreatePersistentObject(wxDataViewCtrl* control)