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.
This commit is contained in:
Vadim Zeitlin
2017-12-15 00:43:24 +01:00
parent 4e82f60b8a
commit 036870ab35
14 changed files with 328 additions and 230 deletions

View File

@@ -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<wxVariant> 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());
}
}

View File

@@ -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

95
tests/persistence/tlw.cpp Normal file
View File

@@ -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());
}
}