1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
///////////////////////////////////////////////////////////////////////////////
// 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"
#include "testpersistence.h"
#ifndef WX_PRECOMP
#include "wx/frame.h"
#ifdef __WXGTK__
#include "wx/stopwatch.h"
#endif // __WXGTK__
#endif // WX_PRECOMP
#include "wx/persist/toplevel.h"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
#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]")
{
const wxPoint pos(100, 150);
const wxSize size(450, 350);
// Save the frame geometry.
{
wxFrame* const frame = CreatePersistenceTestFrame();
// Set the geometry before saving.
frame->SetPosition(pos);
frame->SetSize(size);
CHECK(wxPersistenceManager::Get().Register(frame));
// Destroy the frame immediately, i.e. don't use Destroy() here.
delete frame;
// Test that the relevant keys have been stored correctly.
int val = -1;
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/x", &val));
CHECK(pos.x == val);
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/y", &val));
CHECK(pos.y == val);
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/w", &val));
CHECK(size.x == val);
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/h", &val));
CHECK(size.y == val);
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/Iconized", &val));
CHECK(0 == val);
CHECK(GetConfig().Read(FRAME_OPTIONS_PREFIX "/Maximized", &val));
CHECK(0 == val);
}
// Now try recreating the frame using the restored values.
bool checkIconized = true;
{
wxFrame* const frame = CreatePersistenceTestFrame();
// 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());
// Next try that restoring a minimized frame works correctly: for
// Iconize() to have effect, we must show the frame first.
frame->Iconize();
frame->Show();
#ifdef __WXGTK__
// When using Xvfb, the frame will never get iconized, presumably
// because there is no WM, so don't even bother waiting or warning.
if ( IsRunningUnderXVFB() )
{
checkIconized = false;
}
else
{
wxStopWatch sw;
while ( !frame->IsIconized() )
{
wxYield();
if ( sw.Time() > 500 )
{
// 500ms should be enough for the window to end up iconized.
WARN("Frame wasn't iconized as expected");
checkIconized = false;
break;
}
}
}
#endif // __WXGTK__
delete frame;
}
// Check geometry after restoring the minimized frame.
{
wxFrame* const frame = CreatePersistenceTestFrame();
CHECK(wxPersistenceManager::Get().RegisterAndRestore(frame));
// As above, we need to show the frame for it to be actually iconized.
frame->Show();
CHECK(!frame->IsMaximized());
if ( checkIconized )
{
#ifdef __WXGTK__
wxStopWatch sw;
while ( !frame->IsIconized() )
{
wxYield();
if ( sw.Time() > 500 )
{
INFO("Abandoning wait after " << sw.Time() << "ms");
break;
}
}
#endif // __WXGTK__
CHECK(frame->IsIconized());
}
frame->Restore();
CHECK(pos.x == frame->GetPosition().x);
CHECK(pos.y == frame->GetPosition().y);
CHECK(size.x == frame->GetSize().GetWidth());
CHECK(size.y == frame->GetSize().GetHeight());
// Next try that restoring a maximized frame works correctly: again,
// for it to be really maximized, it must be shown.
frame->Maximize();
frame->Show();
delete frame;
}
// Check geometry after restoring the maximized frame.
//
// This test currently fails under non-MSW platforms as they only save the
// maximized frame size, and its normal size is lost and can't be restored.
#ifdef __WXMSW__
{
wxFrame* const frame = CreatePersistenceTestFrame();
CHECK(wxPersistenceManager::Get().RegisterAndRestore(frame));
CHECK(frame->IsMaximized());
CHECK(!frame->IsIconized());
frame->Restore();
CHECK(pos.x == frame->GetPosition().x);
CHECK(pos.y == frame->GetPosition().y);
CHECK(size.x == frame->GetSize().GetWidth());
CHECK(size.y == frame->GetSize().GetHeight());
delete frame;
}
#endif // __WXMSW__
}
|