Removed unused data
[wxWidgets.git] / samples / propgrid / tests.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/propgrid/tests.cpp
3 // Purpose: wxPropertyGrid tests
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2007-05-16
7 // RCS-ID: $Id$
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21
22 #include <wx/propgrid/propgrid.h>
23 #include <wx/propgrid/advprops.h>
24 #include <wx/propgrid/manager.h>
25
26 #include "propgrid.h"
27 #include "sampleprops.h"
28
29
30 // -----------------------------------------------------------------------
31 // wxTestCustomFlagsProperty
32 // -----------------------------------------------------------------------
33
34 //
35 // Test customizing wxColourProperty via subclassing
36 //
37 // * Includes custom colour entry.
38 // * Includes extra custom entry.
39 //
40 class MyColourProperty : public wxColourProperty
41 {
42 public:
43 MyColourProperty( const wxString& label = wxPG_LABEL,
44 const wxString& name = wxPG_LABEL,
45 const wxColour& value = *wxWHITE )
46 : wxColourProperty(label, name, value)
47 {
48 wxPGChoices colours;
49 colours.Add(wxT("White"));
50 colours.Add(wxT("Black"));
51 colours.Add(wxT("Red"));
52 colours.Add(wxT("Green"));
53 colours.Add(wxT("Blue"));
54 colours.Add(wxT("Custom"));
55 colours.Add(wxT("None"));
56 m_choices = colours;
57 SetIndex(0);
58 wxVariant variant;
59 variant << value;
60 SetValue(variant);
61 }
62
63 virtual ~MyColourProperty()
64 {
65 }
66
67 virtual wxColour GetColour( int index ) const
68 {
69 switch (index)
70 {
71 case 0: return *wxWHITE;
72 case 1: return *wxBLACK;
73 case 2: return *wxRED;
74 case 3: return *wxGREEN;
75 case 4: return *wxBLUE;
76 case 5:
77 // Return current colour for the custom entry
78 wxColour col;
79 if ( GetIndex() == GetCustomColourIndex() )
80 {
81 if ( m_value.IsNull() )
82 return col;
83 col << m_value;
84 return col;
85 }
86 return *wxWHITE;
87 };
88 return wxColour();
89 }
90
91 virtual wxString ColourToString( const wxColour& col, int index ) const
92 {
93 if ( index == (int)(m_choices.GetCount()-1) )
94 return wxT("");
95
96 return wxColourProperty::ColourToString(col, index);
97 }
98
99 virtual int GetCustomColourIndex() const
100 {
101 return m_choices.GetCount()-2;
102 }
103 };
104
105
106 void FormMain::AddTestProperties( wxPropertyGridPage* pg )
107 {
108 pg->Append( new MyColourProperty(wxT("CustomColourProperty"), wxPG_LABEL, *wxGREEN) );
109 pg->GetProperty(wxT("CustomColourProperty"))->SetFlag(wxPG_PROP_AUTO_UNSPECIFIED);
110 pg->SetPropertyEditor( wxT("CustomColourProperty"), wxPGEditor_ComboBox );
111
112 pg->SetPropertyHelpString(wxT("CustomColourProperty"),
113 wxT("This is a MyColourProperty from the sample app. ")
114 wxT("It is built by subclassing wxColourProperty."));
115 }
116
117 // -----------------------------------------------------------------------
118
119 void FormMain::OnDumpList( wxCommandEvent& WXUNUSED(event) )
120 {
121 wxVariant values = m_pPropGridManager->GetPropertyValues(wxT("list"), wxNullProperty, wxPG_INC_ATTRIBUTES);
122 wxString text = wxT("This only tests that wxVariant related routines do not crash.");
123 wxString t;
124
125 wxDialog* dlg = new wxDialog(this,-1,wxT("wxVariant Test"),
126 wxDefaultPosition,wxDefaultSize,wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER);
127
128 unsigned int i;
129 for ( i = 0; i < (unsigned int)values.GetCount(); i++ )
130 {
131 wxVariant& v = values[i];
132
133 wxString strValue = v.GetString();
134
135 #if wxCHECK_VERSION(2,8,0)
136 if ( v.GetName().EndsWith(wxT("@attr")) )
137 #else
138 if ( v.GetName().Right(5) == wxT("@attr") )
139 #endif
140 {
141 text += wxString::Format(wxT("Attributes:\n"));
142
143 unsigned int n;
144 for ( n = 0; n < (unsigned int)v.GetCount(); n++ )
145 {
146 wxVariant& a = v[n];
147
148 t.Printf(wxT(" atribute %i: name=\"%s\" (type=\"%s\" value=\"%s\")\n"),(int)n,
149 a.GetName().c_str(),a.GetType().c_str(),a.GetString().c_str());
150 text += t;
151 }
152 }
153 else
154 {
155 t.Printf(wxT("%i: name=\"%s\" type=\"%s\" value=\"%s\"\n"),(int)i,
156 v.GetName().c_str(),v.GetType().c_str(),strValue.c_str());
157 text += t;
158 }
159 }
160
161 // multi-line text editor dialog
162 const int spacing = 8;
163 wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL );
164 wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL );
165 wxTextCtrl* ed = new wxTextCtrl(dlg,11,text,
166 wxDefaultPosition,wxDefaultSize,wxTE_MULTILINE|wxTE_READONLY);
167 rowsizer->Add( ed, 1, wxEXPAND|wxALL, spacing );
168 topsizer->Add( rowsizer, 1, wxEXPAND, 0 );
169 rowsizer = new wxBoxSizer( wxHORIZONTAL );
170 const int butSzFlags =
171 wxALIGN_CENTRE_HORIZONTAL|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT;
172 rowsizer->Add( new wxButton(dlg,wxID_OK,wxT("Ok")),
173 0, butSzFlags, spacing );
174 topsizer->Add( rowsizer, 0, wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL, 0 );
175
176 dlg->SetSizer( topsizer );
177 topsizer->SetSizeHints( dlg );
178
179 dlg->SetSize(400,300);
180 dlg->Centre();
181 dlg->ShowModal();
182 }
183
184 // -----------------------------------------------------------------------
185
186 class TestRunner
187 {
188 public:
189
190 TestRunner( const wxString& name, wxPropertyGridManager* man, wxTextCtrl* ed, wxArrayString* errorMessages )
191 {
192 m_name = name;
193 m_man = man;
194 m_ed = ed;
195 m_errorMessages = errorMessages;
196 #ifdef __WXDEBUG__
197 m_preWarnings = wxPGGlobalVars->m_warnings;
198 #endif
199
200 if ( name != wxT("none") )
201 Msg(name+wxT("\n"));
202 }
203
204 ~TestRunner()
205 {
206 #ifdef __WXDEBUG__
207 int warningsOccurred = wxPGGlobalVars->m_warnings - m_preWarnings;
208 if ( warningsOccurred )
209 {
210 wxString s = wxString::Format(wxT("%i warnings occurred during test '%s'"), warningsOccurred, m_name.c_str());
211 m_errorMessages->push_back(s);
212 Msg(s);
213 }
214 #endif
215 }
216
217 void Msg( const wxString& text )
218 {
219 if ( m_ed )
220 {
221 m_ed->AppendText(text);
222 m_ed->AppendText(wxT("\n"));
223 }
224 wxLogDebug(text);
225 }
226
227 protected:
228 wxPropertyGridManager* m_man;
229 wxTextCtrl* m_ed;
230 wxArrayString* m_errorMessages;
231 wxString m_name;
232 #ifdef __WXDEBUG__
233 int m_preWarnings;
234 #endif
235 };
236
237
238 #define RT_START_TEST(TESTNAME) \
239 TestRunner tr(wxT(#TESTNAME), pgman, ed, &errorMessages);
240
241 #define RT_MSG(S) \
242 tr.Msg(S);
243
244 #define RT_FAILURE() \
245 { \
246 wxString s1 = wxString::Format(wxT("Test failure in tests.cpp, line %i."),__LINE__-1); \
247 errorMessages.push_back(s1); \
248 wxLogDebug(s1); \
249 failures++; \
250 }
251
252 #define RT_FAILURE_MSG(MSG) \
253 { \
254 wxString s1 = wxString::Format(wxT("Test failure in tests.cpp, line %i."),__LINE__-1); \
255 errorMessages.push_back(s1); \
256 wxLogDebug(s1); \
257 wxString s2 = wxString::Format(wxT("Message: %s"),MSG); \
258 errorMessages.push_back(s2); \
259 wxLogDebug(s2); \
260 failures++; \
261 }
262
263 #define RT_VALIDATE_VIRTUAL_HEIGHT(PROPS, EXTRATEXT) \
264 { \
265 unsigned int h1_ = PROPS->GetVirtualHeight(); \
266 unsigned int h2_ = PROPS->GetActualVirtualHeight(); \
267 if ( h1_ != h2_ ) \
268 { \
269 wxString s_ = wxString::Format(wxT("VirtualHeight = %i, should be %i (%s)"), h1_, h2_, EXTRATEXT.c_str()); \
270 RT_FAILURE_MSG(s_.c_str()); \
271 _failed_ = true; \
272 } \
273 else \
274 { \
275 _failed_ = false; \
276 } \
277 }
278
279
280 int gpiro_cmpfunc(const void* a, const void* b)
281 {
282 const wxPGProperty* p1 = (const wxPGProperty*) a;
283 const wxPGProperty* p2 = (const wxPGProperty*) b;
284 return (int) (((size_t)p1->GetClientData()) - ((size_t)p2->GetClientData()));
285 }
286
287 wxArrayPGProperty GetPropertiesInRandomOrder( wxPropertyGridInterface* props, int iterationFlags = wxPG_ITERATE_ALL )
288 {
289 wxArrayPGProperty arr;
290
291 wxPropertyGridIterator it;
292
293 for ( it = props->GetIterator(iterationFlags);
294 !it.AtEnd();
295 it++ )
296 {
297 wxPGProperty* p = *it;
298 size_t randomNumber = rand();
299 p->SetClientData(reinterpret_cast<void*>(randomNumber));
300 arr.push_back(p);
301 }
302
303 wxPGProperty** firstEntry = &arr[0];
304 qsort(firstEntry, arr.size(), sizeof(wxPGProperty*), gpiro_cmpfunc);
305
306 return arr;
307 }
308
309 bool FormMain::RunTests( bool fullTest, bool interactive )
310 {
311 wxString t;
312
313 wxPropertyGridManager* pgman = m_pPropGridManager;
314 wxPropertyGrid* pg;
315
316 size_t i;
317
318 pgman->ClearSelection();
319
320 int failures = 0;
321 bool _failed_ = false;
322 wxArrayString errorMessages;
323 wxDialog* dlg = NULL;
324
325 dlg = new wxDialog(this,-1,wxT("wxPropertyGrid Regression Tests"),
326 wxDefaultPosition,wxDefaultSize,wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER);
327
328 // multi-line text editor dialog
329 const int spacing = 8;
330 wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL );
331 wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL );
332 wxTextCtrl* ed = new wxTextCtrl(dlg,11,wxEmptyString,
333 wxDefaultPosition,wxDefaultSize,wxTE_MULTILINE|wxTE_READONLY);
334 rowsizer->Add( ed, 1, wxEXPAND|wxALL, spacing );
335 topsizer->Add( rowsizer, 1, wxEXPAND, 0 );
336 rowsizer = new wxBoxSizer( wxHORIZONTAL );
337 const int butSzFlags =
338 wxALIGN_CENTRE_HORIZONTAL|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT;
339 rowsizer->Add( new wxButton(dlg,wxID_OK,wxT("Ok")),
340 0, butSzFlags, spacing );
341 topsizer->Add( rowsizer, 0, wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL, 0 );
342
343 dlg->SetSizer( topsizer );
344 topsizer->SetSizeHints( dlg );
345
346 dlg->SetSize(400,300);
347 dlg->Move(wxSystemSettings::GetMetric(wxSYS_SCREEN_X)-dlg->GetSize().x,
348 wxSystemSettings::GetMetric(wxSYS_SCREEN_Y)-dlg->GetSize().y);
349 dlg->Show();
350
351 {
352 //
353 // Basic iterator tests
354 RT_START_TEST(GetIterator)
355
356 wxPGVIterator it;
357 int count;
358
359 count = 0;
360 for ( it = pgman->GetVIterator(wxPG_ITERATE_PROPERTIES);
361 !it.AtEnd();
362 it.Next() )
363 {
364 wxPGProperty* p = it.GetProperty();
365 if ( p->IsCategory() )
366 RT_FAILURE_MSG(wxString::Format(wxT("'%s' is a category (non-private child property expected)"),p->GetLabel().c_str()).c_str())
367 else if ( p->GetParent()->HasFlag(wxPG_PROP_AGGREGATE) )
368 RT_FAILURE_MSG(wxString::Format(wxT("'%s' is a private child (non-private child property expected)"),p->GetLabel().c_str()).c_str())
369 count++;
370 }
371
372 RT_MSG(wxString::Format(wxT("GetVIterator(wxPG_ITERATE_PROPERTIES) -> %i entries"), count));
373
374 count = 0;
375 for ( it = pgman->GetVIterator(wxPG_ITERATE_CATEGORIES);
376 !it.AtEnd();
377 it.Next() )
378 {
379 wxPGProperty* p = it.GetProperty();
380 if ( !p->IsCategory() )
381 RT_FAILURE_MSG(wxString::Format(wxT("'%s' is not a category (only category was expected)"),p->GetLabel().c_str()).c_str())
382 count++;
383 }
384
385 RT_MSG(wxString::Format(wxT("GetVIterator(wxPG_ITERATE_CATEGORIES) -> %i entries"), count));
386
387 count = 0;
388 for ( it = pgman->GetVIterator(wxPG_ITERATE_PROPERTIES|wxPG_ITERATE_CATEGORIES);
389 !it.AtEnd();
390 it.Next() )
391 {
392 wxPGProperty* p = it.GetProperty();
393 if ( p->GetParent()->HasFlag(wxPG_PROP_AGGREGATE) )
394 RT_FAILURE_MSG(wxString::Format(wxT("'%s' is a private child (non-private child property or category expected)"),p->GetLabel().c_str()).c_str())
395 count++;
396 }
397
398 RT_MSG(wxString::Format(wxT("GetVIterator(wxPG_ITERATE_PROPERTIES|wxPG_ITERATE_CATEGORIES) -> %i entries"), count));
399
400 count = 0;
401 for ( it = pgman->GetVIterator(wxPG_ITERATE_VISIBLE);
402 !it.AtEnd();
403 it.Next() )
404 {
405 wxPGProperty* p = it.GetProperty();
406 if ( (p->GetParent() != p->GetParentState()->DoGetRoot() && !p->GetParent()->IsExpanded()) )
407 RT_FAILURE_MSG(wxString::Format(wxT("'%s' had collapsed parent (only visible properties expected)"),p->GetLabel().c_str()).c_str())
408 else if ( p->HasFlag(wxPG_PROP_HIDDEN) )
409 RT_FAILURE_MSG(wxString::Format(wxT("'%s' was hidden (only visible properties expected)"),p->GetLabel().c_str()).c_str())
410 count++;
411 }
412
413 RT_MSG(wxString::Format(wxT("GetVIterator(wxPG_ITERATE_VISIBLE) -> %i entries"), count));
414 }
415
416 if ( fullTest )
417 {
418 // Test that setting focus to properties does not crash things
419 RT_START_TEST(SelectProperty)
420
421 wxPropertyGridIterator it;
422 size_t ind;
423
424 for ( ind=0; ind<pgman->GetPageCount(); ind++ )
425 {
426 wxPropertyGridPage* page = pgman->GetPage(ind);
427 pgman->SelectPage(page);
428
429 for ( it = page->GetIterator(wxPG_ITERATE_VISIBLE);
430 !it.AtEnd();
431 it++ )
432 {
433 wxPGProperty* p = *it;
434 RT_MSG(p->GetLabel());
435 pgman->GetGrid()->SelectProperty(p, true);
436 ::wxMilliSleep(150);
437 Update();
438 }
439 }
440 }
441
442 {
443 //
444 // Delete everything in reverse order
445 RT_START_TEST(DeleteProperty)
446
447 wxPGVIterator it;
448 wxArrayPGProperty array;
449
450 for ( it = pgman->GetVIterator(wxPG_ITERATE_ALL&~(wxPG_IT_CHILDREN(wxPG_PROP_AGGREGATE)));
451 !it.AtEnd();
452 it.Next() )
453 array.push_back(it.GetProperty());
454
455 wxArrayPGProperty::reverse_iterator it2;
456
457 for ( it2 = array.rbegin(); it2 != array.rend(); it2++ )
458 {
459 wxPGProperty* p = (wxPGProperty*)*it2;
460 RT_MSG(wxString::Format(wxT("Deleting '%s' ('%s')"),p->GetLabel().c_str(),p->GetName().c_str()));
461 pgman->DeleteProperty(p);
462 }
463
464 // Recreate grid
465 CreateGrid( -1, -1 );
466 pgman = m_pPropGridManager;
467 }
468
469 {
470 //
471 // Test property default values
472 RT_START_TEST(Default_Values)
473
474 wxPGVIterator it;
475
476 for ( it = pgman->GetVIterator(wxPG_ITERATE_PROPERTIES);
477 !it.AtEnd();
478 it.Next() )
479 {
480 wxPGProperty* p = it.GetProperty();
481 pgman->SetPropertyValue(p, p->GetDefaultValue());
482 }
483
484 // Recreate grid
485 CreateGrid( -1, -1 );
486 pgman = m_pPropGridManager;
487 }
488
489 {
490 RT_START_TEST(GetPropertyValues)
491
492 for ( i=0; i<3; i++ )
493 {
494 wxString text;
495
496 wxPropertyGridPage* page = pgman->GetPage(i);
497
498 wxVariant values = page->GetPropertyValues();
499
500 unsigned int i;
501 for ( i = 0; i < (unsigned int)values.GetCount(); i++ )
502 {
503 wxVariant& v = values[i];
504
505 t.Printf(wxT("%i: name=\"%s\" type=\"%s\"\n"),(int)i,
506 v.GetName().c_str(),v.GetType().c_str());
507
508 text += t;
509 }
510 ed->AppendText(text);
511 }
512 }
513
514 {
515 RT_START_TEST(SetPropertyValue_and_GetPropertyValue)
516
517 // In this section, mixed up usage of wxT("propname") and "propname"
518 // in wxPropertyGridInterface functions is intentional.
519 // Purpose is to test wxPGPropArgCls ctors.
520
521 //pg = (wxPropertyGrid*) NULL;
522
523 wxArrayString test_arrstr_1;
524 test_arrstr_1.Add(wxT("Apple"));
525 test_arrstr_1.Add(wxT("Orange"));
526 test_arrstr_1.Add(wxT("Lemon"));
527
528 wxArrayString test_arrstr_2;
529 test_arrstr_2.Add(wxT("Potato"));
530 test_arrstr_2.Add(wxT("Cabbage"));
531 test_arrstr_2.Add(wxT("Cucumber"));
532
533 wxArrayInt test_arrint_1;
534 test_arrint_1.Add(1);
535 test_arrint_1.Add(2);
536 test_arrint_1.Add(3);
537
538 wxArrayInt test_arrint_2;
539 test_arrint_2.Add(0);
540 test_arrint_2.Add(1);
541 test_arrint_2.Add(4);
542
543 #if wxUSE_DATETIME
544 wxDateTime dt1 = wxDateTime::Now();
545 dt1.SetYear(dt1.GetYear()-1);
546
547 wxDateTime dt2 = wxDateTime::Now();
548 dt2.SetYear(dt2.GetYear()-10);
549 #endif
550
551 #define FLAG_TEST_SET1 (wxCAPTION|wxCLOSE_BOX|wxSYSTEM_MENU|wxRESIZE_BORDER)
552 #define FLAG_TEST_SET2 (wxSTAY_ON_TOP|wxCAPTION|wxICONIZE|wxSYSTEM_MENU)
553
554 pgman->SetPropertyValue(wxT("StringProperty"),wxT("Text1"));
555 pgman->SetPropertyValue(wxT("IntProperty"),1024);
556 pgman->SetPropertyValue(wxT("FloatProperty"),1024.0000000001);
557 pgman->SetPropertyValue(wxT("BoolProperty"),FALSE);
558 pgman->SetPropertyValue(wxT("EnumProperty"),120);
559 pgman->SetPropertyValue(wxT("ArrayStringProperty"),test_arrstr_1);
560 wxColour emptyCol;
561 pgman->SetPropertyValue(wxT("ColourProperty"),emptyCol);
562 pgman->SetPropertyValue(wxT("ColourProperty"),(wxObject*)wxBLACK);
563 pgman->SetPropertyValue(wxT("Size"),WXVARIANT(wxSize(150,150)));
564 pgman->SetPropertyValue(wxT("Position"),WXVARIANT(wxPoint(150,150)));
565 pgman->SetPropertyValue(wxT("MultiChoiceProperty"),test_arrint_1);
566 #if wxUSE_DATETIME
567 pgman->SetPropertyValue(wxT("DateProperty"),dt1);
568 #endif
569
570 pgman->SelectPage(2);
571 pg = pgman->GetGrid();
572
573 if ( pg->GetPropertyValueAsString(wxT("StringProperty")) != wxT("Text1") )
574 RT_FAILURE();
575 if ( pg->GetPropertyValueAsInt(wxT("IntProperty")) != 1024 )
576 RT_FAILURE();
577 if ( pg->GetPropertyValueAsDouble(wxT("FloatProperty")) != 1024.0000000001 )
578 RT_FAILURE();
579 if ( pg->GetPropertyValueAsBool(wxT("BoolProperty")) != FALSE )
580 RT_FAILURE();
581 if ( pg->GetPropertyValueAsLong(wxT("EnumProperty")) != 120 )
582 RT_FAILURE();
583 if ( pg->GetPropertyValueAsArrayString(wxT("ArrayStringProperty")) != test_arrstr_1 )
584 RT_FAILURE();
585 wxColour col;
586 col << pgman->GetPropertyValue(wxT("ColourProperty"));
587 if ( col != *wxBLACK )
588 RT_FAILURE();
589 if ( wxSizeRefFromVariant(pg->GetPropertyValue(wxT("Size"))) != wxSize(150,150) )
590 RT_FAILURE();
591 if ( wxPointRefFromVariant(pg->GetPropertyValue(wxT("Position"))) != wxPoint(150,150) )
592 RT_FAILURE();
593 if ( !(pg->GetPropertyValueAsArrayInt(wxT("MultiChoiceProperty")) == test_arrint_1) )
594 RT_FAILURE();
595 #if wxUSE_DATETIME
596 if ( !(pg->GetPropertyValueAsDateTime(wxT("DateProperty")) == dt1) )
597 RT_FAILURE();
598 #endif
599
600 pgman->SetPropertyValue(wxT("IntProperty"),wxLL(10000000000));
601 if ( pg->GetPropertyValueAsLongLong(wxT("IntProperty")) != wxLL(10000000000) )
602 RT_FAILURE();
603
604 pg->SetPropertyValue(wxT("StringProperty"),wxT("Text2"));
605 pg->SetPropertyValue(wxT("IntProperty"),512);
606 pg->SetPropertyValue(wxT("FloatProperty"),512.0);
607 pg->SetPropertyValue(wxT("BoolProperty"),TRUE);
608 pg->SetPropertyValue(wxT("EnumProperty"),80);
609 pg->SetPropertyValue(wxT("ArrayStringProperty"),test_arrstr_2);
610 pg->SetPropertyValue(wxT("ColourProperty"),(wxObject*)wxWHITE);
611 pg->SetPropertyValue(wxT("Size"),WXVARIANT(wxSize(300,300)));
612 pg->SetPropertyValue(wxT("Position"),WXVARIANT(wxPoint(300,300)));
613 pg->SetPropertyValue(wxT("MultiChoiceProperty"),test_arrint_2);
614 #if wxUSE_DATETIME
615 pg->SetPropertyValue(wxT("DateProperty"),dt2);
616 #endif
617
618 //pg = (wxPropertyGrid*) NULL;
619
620 pgman->SelectPage(0);
621
622 if ( pgman->GetPropertyValueAsString(wxT("StringProperty")) != wxT("Text2") )
623 RT_FAILURE();
624 if ( pgman->GetPropertyValueAsInt(wxT("IntProperty")) != 512 )
625 RT_FAILURE();
626 if ( pgman->GetPropertyValueAsDouble(wxT("FloatProperty")) != 512.0 )
627 RT_FAILURE();
628 if ( pgman->GetPropertyValueAsBool(wxT("BoolProperty")) != TRUE )
629 RT_FAILURE();
630 if ( pgman->GetPropertyValueAsLong(wxT("EnumProperty")) != 80 )
631 RT_FAILURE();
632 if ( pgman->GetPropertyValueAsArrayString(wxT("ArrayStringProperty")) != test_arrstr_2 )
633 RT_FAILURE();
634 col << pgman->GetPropertyValue(wxT("ColourProperty"));
635 if ( col != *wxWHITE )
636 RT_FAILURE();
637 if ( wxSizeRefFromVariant(pgman->GetPropertyValue(wxT("Size"))) != wxSize(300,300) )
638 RT_FAILURE();
639 if ( wxPointRefFromVariant(pgman->GetPropertyValue(wxT("Position"))) != wxPoint(300,300) )
640 RT_FAILURE();
641 if ( !(pgman->GetPropertyValueAsArrayInt(wxT("MultiChoiceProperty")) == test_arrint_2) )
642 RT_FAILURE();
643 #if wxUSE_DATETIME
644 if ( !(pgman->GetPropertyValueAsDateTime(wxT("DateProperty")) == dt2) )
645 RT_FAILURE();
646 #endif
647
648 pgman->SetPropertyValue(wxT("IntProperty"),wxLL(-80000000000));
649 if ( pgman->GetPropertyValueAsLongLong(wxT("IntProperty")) != wxLL(-80000000000) )
650 RT_FAILURE();
651
652 //
653 // Flexible wx(U)LongLong << operator safety conformance tests
654 wxPGProperty* prop;
655 wxLongLong ll;
656 wxULongLong ull;
657
658 prop = pgman->GetProperty(wxT("IntProperty"));
659 prop->SetValue(128);
660 ll << prop->GetValue();
661 if ( ll != 128 )
662 RT_FAILURE();
663
664 prop->SetValue(WXVARIANT(wxLL(68719476736)));
665 ll << prop->GetValue();
666 if ( ll.GetValue() != wxLL(68719476736) )
667 RT_FAILURE();
668
669 #if wxUSE_LONGLONG_NATIVE
670 wxLongLong_t ll_t;
671 ll_t << prop->GetValue();
672 if ( ll_t != wxLL(68719476736) )
673 RT_FAILURE();
674 #endif
675
676 prop->SetValue(256);
677 ll << prop->GetValue();
678 if ( ll != 256 )
679 RT_FAILURE();
680
681 prop = pgman->GetProperty(wxT("UIntProperty"));
682 prop->SetValue(128);
683 ull << prop->GetValue();
684 if ( ull != 128 )
685 RT_FAILURE();
686
687 prop->SetValue(WXVARIANT(wxULL(68719476739)));
688 ull << prop->GetValue();
689 if ( ull.GetValue() != wxULL(68719476739) )
690 RT_FAILURE();
691
692 #if wxUSE_LONGLONG_NATIVE
693 wxULongLong_t ull_t;
694 ull_t << prop->GetValue();
695 if ( ull_t != wxLL(68719476739) )
696 RT_FAILURE();
697 #endif
698
699 prop->SetValue(256);
700 ull << prop->GetValue();
701 if ( ull != 256 )
702 RT_FAILURE();
703
704 // Make sure children of composite parent get updated as well
705 // Original string value: "Lamborghini Diablo SV; 5707; [300; 3.9; 8.6] 300000"
706
707 //
708 // This updates children as well
709 wxString nvs = "Lamborghini Diablo XYZ; 5707; [100; 3.9; 8.6] 3000002";
710 pgman->SetPropertyValue("Car", nvs);
711
712 if ( pgman->GetPropertyValueAsString("Car.Model") != "Lamborghini Diablo XYZ" )
713 {
714 wxLogDebug("Did not match: Car.Model=%s", pgman->GetPropertyValueAsString("Car.Model").c_str());
715 RT_FAILURE();
716 }
717
718 if ( pgman->GetPropertyValueAsInt("Car.Speeds.Max. Speed (mph)") != 100 )
719 {
720 wxLogDebug("Did not match: Car.Speeds.Max. Speed (mph)=%s", pgman->GetPropertyValueAsString("Car.Speeds.Max. Speed (mph)").c_str());
721 RT_FAILURE();
722 }
723
724 if ( pgman->GetPropertyValueAsInt("Car.Price ($)") != 3000002 )
725 {
726 wxLogDebug("Did not match: Car.Price ($)=%s", pgman->GetPropertyValueAsString("Car.Price ($)").c_str());
727 RT_FAILURE();
728 }
729 }
730
731 {
732 RT_START_TEST(SetPropertyValueUnspecified)
733
734 // Null variant setter tests
735 pgman->SetPropertyValueUnspecified(wxT("StringProperty"));
736 pgman->SetPropertyValueUnspecified(wxT("IntProperty"));
737 pgman->SetPropertyValueUnspecified(wxT("FloatProperty"));
738 pgman->SetPropertyValueUnspecified(wxT("BoolProperty"));
739 pgman->SetPropertyValueUnspecified(wxT("EnumProperty"));
740 pgman->SetPropertyValueUnspecified(wxT("ArrayStringProperty"));
741 pgman->SetPropertyValueUnspecified(wxT("ColourProperty"));
742 pgman->SetPropertyValueUnspecified(wxT("Size"));
743 pgman->SetPropertyValueUnspecified(wxT("Position"));
744 pgman->SetPropertyValueUnspecified(wxT("MultiChoiceProperty"));
745 #if wxUSE_DATETIME
746 pgman->SetPropertyValueUnspecified(wxT("DateProperty"));
747 #endif
748 }
749
750 {
751 RT_START_TEST(Attributes)
752
753 wxPGProperty* prop = pgman->GetProperty(wxT("StringProperty"));
754 prop->SetAttribute(wxT("Dummy Attribute"), (long)15);
755
756 if ( prop->GetAttribute(wxT("Dummy Attribute")).GetLong() != 15 )
757 RT_FAILURE();
758
759 prop->SetAttribute(wxT("Dummy Attribute"), wxVariant());
760
761 if ( !prop->GetAttribute(wxT("Dummy Attribute")).IsNull() )
762 RT_FAILURE();
763 }
764
765 {
766 wxPropertyGridPage* page1;
767 wxPropertyGridPage* page2;
768 wxPropertyGridPage* page3;
769 wxVariant pg1_values;
770 wxVariant pg2_values;
771 wxVariant pg3_values;
772
773 {
774 RT_START_TEST(GetPropertyValues)
775
776 page1 = pgman->GetPage(0);
777 pg1_values = page1->GetPropertyValues(wxT("Page1"),NULL,wxPG_KEEP_STRUCTURE);
778 page2 = pgman->GetPage(1);
779 pg2_values = page2->GetPropertyValues(wxT("Page2"),NULL,wxPG_KEEP_STRUCTURE);
780 page3 = pgman->GetPage(2);
781 pg3_values = page3->GetPropertyValues(wxT("Page3"),NULL,wxPG_KEEP_STRUCTURE);
782 }
783
784 {
785 RT_START_TEST(SetPropertyValues)
786
787 page1->SetPropertyValues(pg3_values);
788 page2->SetPropertyValues(pg1_values);
789 page3->SetPropertyValues(pg2_values);
790 }
791 }
792
793 if ( !(pgman->GetWindowStyleFlag()&wxPG_HIDE_CATEGORIES) )
794 {
795 RT_START_TEST(Collapse_and_GetFirstCategory_and_GetNextCategory)
796
797 for ( i=0; i<3; i++ )
798 {
799 wxPropertyGridPage* page = pgman->GetPage(i);
800
801 wxPropertyGridIterator it;
802
803 for ( it = page->GetIterator( wxPG_ITERATE_CATEGORIES );
804 !it.AtEnd();
805 it++ )
806 {
807 wxPGProperty* p = *it;
808
809 if ( !page->IsPropertyCategory(p) )
810 RT_FAILURE();
811
812 page->Collapse( p );
813
814 t.Printf(wxT("Collapsing: %s\n"),page->GetPropertyLabel(p).c_str());
815 ed->AppendText(t);
816 }
817 }
818 }
819
820 {
821 RT_START_TEST(Save_And_RestoreEditableState)
822
823 for ( i=0; i<3; i++ )
824 {
825 pgman->SelectPage(i);
826
827 wxString stringState = pgman->SaveEditableState();
828 bool res = pgman->RestoreEditableState(stringState);
829 if ( !res )
830 RT_FAILURE();
831 }
832 }
833
834 //if ( !(pgman->GetWindowStyleFlag()&wxPG_HIDE_CATEGORIES) )
835 {
836 RT_START_TEST(Expand_and_GetFirstCategory_and_GetNextCategory)
837
838 for ( i=0; i<3; i++ )
839 {
840 wxPropertyGridPage* page = pgman->GetPage(i);
841
842 wxPropertyGridIterator it;
843
844 for ( it = page->GetIterator( wxPG_ITERATE_CATEGORIES );
845 !it.AtEnd();
846 it++ )
847 {
848 wxPGProperty* p = *it;
849
850 if ( !page->IsPropertyCategory(p) )
851 RT_FAILURE();
852
853 page->Expand( p );
854
855 t.Printf(wxT("Expand: %s\n"),page->GetPropertyLabel(p).c_str());
856 ed->AppendText(t);
857 }
858 }
859 }
860
861 {
862 RT_START_TEST(Choice_Manipulation)
863
864 wxPGProperty* enumProp = pgman->GetProperty(wxT("EnumProperty"));
865
866 pgman->SelectPage(2);
867 pgman->SelectProperty(enumProp);
868 wxASSERT(pgman->GetGrid()->GetSelection() == enumProp);
869
870 const wxPGChoices& choices = enumProp->GetChoices();
871 int ind = enumProp->InsertChoice(wxT("New Choice"), choices.GetCount()/2);
872 enumProp->DeleteChoice(ind);
873
874 // Recreate the original grid
875 CreateGrid( -1, -1 );
876 pgman = m_pPropGridManager;
877 }
878
879 //if ( !(pgman->GetWindowStyleFlag()&wxPG_HIDE_CATEGORIES) )
880 {
881 RT_START_TEST(RandomCollapse)
882
883 // Select the most error prone page as visible.
884 pgman->SelectPage(1);
885
886 for ( i=0; i<3; i++ )
887 {
888 wxArrayPtrVoid arr;
889
890 wxPropertyGridPage* page = pgman->GetPage(i);
891
892 wxPropertyGridIterator it;
893
894 for ( it = page->GetIterator( wxPG_ITERATE_CATEGORIES );
895 !it.AtEnd();
896 it++ )
897 {
898 arr.Add((void*)*it);
899 }
900
901 if ( arr.GetCount() )
902 {
903 size_t n;
904
905 pgman->Collapse( (wxPGProperty*)arr.Item(0) );
906
907 for ( n=arr.GetCount()-1; n>0; n-- )
908 {
909 pgman->Collapse( (wxPGProperty*)arr.Item(n) );
910 }
911 }
912
913 }
914 }
915
916 {
917 RT_START_TEST(EnsureVisible)
918 pgman->EnsureVisible(wxT("Cell Colour"));
919 }
920
921 {
922 RT_START_TEST(SetPropertyBackgroundColour)
923 wxCommandEvent evt;
924 evt.SetInt(1); // IsChecked() will return TRUE.
925 evt.SetId(ID_COLOURSCHEME4);
926 OnCatColours(evt);
927 OnColourScheme(evt);
928 }
929
930 {
931 RT_START_TEST(ManagerClear)
932 pgman->Clear();
933
934 if ( pgman->GetPageCount() )
935 RT_FAILURE();
936
937 // Recreate the original grid
938 CreateGrid( -1, -1 );
939 pgman = m_pPropGridManager;
940 }
941
942 /*
943 {
944 // TODO: This test fails.
945 RT_START_TEST(SetSplitterPosition)
946
947 InitPanel();
948
949 const int trySplitterPos = 50;
950
951 int style = wxPG_AUTO_SORT; // wxPG_SPLITTER_AUTO_CENTER;
952 pgman = m_pPropGridManager =
953 new wxPropertyGridManager(m_panel, wxID_ANY,
954 wxDefaultPosition,
955 wxDefaultSize,
956 style );
957
958 PopulateGrid();
959 pgman->SetSplitterPosition(trySplitterPos);
960
961 if ( pgman->GetGrid()->GetSplitterPosition() != trySplitterPos )
962 RT_FAILURE_MSG(wxString::Format(wxT("Splitter position was %i (should have been %i)"),(int)pgman->GetGrid()->GetSplitterPosition(),trySplitterPos).c_str());
963
964 m_topSizer->Add( m_pPropGridManager, 1, wxEXPAND );
965 FinalizePanel();
966
967 wxSize sz = GetSize();
968 wxSize origSz = sz;
969 sz.x += 5;
970 sz.y += 5;
971
972 if ( pgman->GetGrid()->GetSplitterPosition() != trySplitterPos )
973 RT_FAILURE_MSG(wxString::Format(wxT("Splitter position was %i (should have been %i)"),(int)pgman->GetGrid()->GetSplitterPosition(),trySplitterPos).c_str());
974
975 SetSize(origSz);
976
977 // Recreate the original grid
978 CreateGrid( -1, -1 );
979 pgman = m_pPropGridManager;
980 }
981 */
982
983 {
984 RT_START_TEST(HideProperty)
985
986 wxPropertyGridPage* page = pgman->GetPage(0);
987
988 srand(0x1234);
989
990 wxArrayPGProperty arr1;
991
992 arr1 = GetPropertiesInRandomOrder(page);
993
994 if ( !_failed_ )
995 {
996 for ( i=0; i<arr1.size(); i++ )
997 {
998 wxPGProperty* p = arr1[i];
999 page->HideProperty(p, true);
1000
1001 wxString s = wxString::Format(wxT("HideProperty(%i, %s)"), i, p->GetLabel().c_str());
1002 RT_VALIDATE_VIRTUAL_HEIGHT(page, s)
1003 if ( _failed_ )
1004 break;
1005 }
1006 }
1007
1008 if ( !_failed_ )
1009 {
1010 wxArrayPGProperty arr2 = GetPropertiesInRandomOrder(page);
1011
1012 for ( i=0; i<arr2.size(); i++ )
1013 {
1014 wxPGProperty* p = arr2[i];
1015 page->HideProperty(p, false);
1016
1017 wxString s = wxString::Format(wxT("ShowProperty(%i, %s)"), i, p->GetLabel().c_str());
1018 RT_VALIDATE_VIRTUAL_HEIGHT(page, s)
1019 if ( _failed_ )
1020 break;
1021 }
1022 }
1023
1024 //
1025 // Let's do some more, for better consistency
1026 arr1 = GetPropertiesInRandomOrder(page);
1027
1028 if ( !_failed_ )
1029 {
1030 for ( i=0; i<arr1.size(); i++ )
1031 {
1032 wxPGProperty* p = arr1[i];
1033 page->HideProperty(p, true);
1034
1035 wxString s = wxString::Format(wxT("HideProperty(%i, %s)"), i, p->GetLabel().c_str());
1036 RT_VALIDATE_VIRTUAL_HEIGHT(page, s)
1037 if ( _failed_ )
1038 break;
1039 }
1040 }
1041
1042 if ( !_failed_ )
1043 {
1044 wxArrayPGProperty arr2 = GetPropertiesInRandomOrder(page);
1045
1046 for ( i=0; i<arr2.size(); i++ )
1047 {
1048 wxPGProperty* p = arr2[i];
1049 page->HideProperty(p, false);
1050
1051 wxString s = wxString::Format(wxT("ShowProperty(%i, %s)"), i, p->GetLabel().c_str());
1052 RT_VALIDATE_VIRTUAL_HEIGHT(page, s)
1053 if ( _failed_ )
1054 break;
1055 }
1056 }
1057
1058 //
1059 // Ok, this time only hide half of them
1060 arr1 = GetPropertiesInRandomOrder(page);
1061 arr1.resize(arr1.size()/2);
1062
1063 if ( !_failed_ )
1064 {
1065 for ( i=0; i<arr1.size(); i++ )
1066 {
1067 wxPGProperty* p = arr1[i];
1068 page->HideProperty(p, true);
1069
1070 wxString s = wxString::Format(wxT("HideProperty(%i, %s)"), i, p->GetLabel().c_str());
1071 RT_VALIDATE_VIRTUAL_HEIGHT(page, s)
1072 if ( _failed_ )
1073 break;
1074 }
1075 }
1076
1077 if ( !_failed_ )
1078 {
1079 wxArrayPGProperty arr2 = GetPropertiesInRandomOrder(page);
1080
1081 for ( i=0; i<arr2.size(); i++ )
1082 {
1083 wxPGProperty* p = arr2[i];
1084 page->HideProperty(p, false);
1085
1086 wxString s = wxString::Format(wxT("ShowProperty(%i, %s)"), i, p->GetLabel().c_str());
1087 RT_VALIDATE_VIRTUAL_HEIGHT(page, s)
1088 if ( _failed_ )
1089 break;
1090 }
1091 }
1092
1093 // Recreate the original grid
1094 CreateGrid( -1, -1 );
1095 pgman = m_pPropGridManager;
1096 }
1097
1098 if ( fullTest )
1099 {
1100 RT_START_TEST(MultipleColumns)
1101
1102 // Test with multiple columns
1103 // FIXME: Does not display changes.
1104 for ( i=3; i<12; i+=2 )
1105 {
1106 RT_MSG(wxString::Format(wxT("%i columns"),i));
1107 CreateGrid( -1, -1 );
1108 pgman = m_pPropGridManager;
1109 pgman->SetColumnCount(i);
1110 Refresh();
1111 Update();
1112 wxMilliSleep(500);
1113 }
1114 }
1115
1116 if ( fullTest )
1117 {
1118 RT_START_TEST(WindowStyles)
1119
1120 // Recreate grid with all possible (single) flags
1121 wxASSERT(wxPG_AUTO_SORT == 0x000000010);
1122
1123 for ( i=4; i<16; i++ )
1124 {
1125 int flag = 1<<i;
1126 RT_MSG(wxString::Format(wxT("Style: 0x%X"),flag));
1127 CreateGrid( flag, -1 );
1128 pgman = m_pPropGridManager;
1129 Update();
1130 wxMilliSleep(500);
1131 }
1132
1133 wxASSERT(wxPG_EX_INIT_NOCAT == 0x00001000);
1134
1135 for ( i=12; i<24; i++ )
1136 {
1137 int flag = 1<<i;
1138 RT_MSG(wxString::Format(wxT("ExStyle: 0x%X"),flag));
1139 CreateGrid( -1, flag );
1140 pgman = m_pPropGridManager;
1141 Update();
1142 wxMilliSleep(500);
1143 }
1144
1145 // Recreate the original grid
1146 CreateGrid( -1, -1 );
1147 pgman = m_pPropGridManager;
1148 }
1149
1150 RT_START_TEST(none)
1151
1152 bool retVal;
1153
1154 if ( failures || errorMessages.size() )
1155 {
1156 retVal = false;
1157
1158 wxString s;
1159 #ifdef __WXDEBUG__
1160 if ( failures )
1161 #endif
1162 s = wxString::Format(wxT("%i tests failed!!!"), failures);
1163 #ifdef __WXDEBUG__
1164 else
1165 s = wxString::Format(wxT("All tests were succesfull, but there were %i warnings!"), wxPGGlobalVars->m_warnings);
1166 #endif
1167 RT_MSG(s)
1168 for ( i=0; i<errorMessages.size(); i++ )
1169 RT_MSG(errorMessages[i])
1170 wxMessageBox(s, wxT("Some Tests Failed"));
1171 }
1172 else
1173 {
1174 RT_MSG(wxT("All tests succesfull"))
1175 retVal = true;
1176
1177 if ( !interactive )
1178 dlg->Close();
1179 }
1180
1181 pgman->SelectPage(0);
1182
1183 // Test may screw up the toolbar, so we need to refresh it.
1184 wxToolBar* toolBar = pgman->GetToolBar();
1185 if ( toolBar )
1186 toolBar->Refresh();
1187
1188 return retVal;
1189 }
1190
1191 // -----------------------------------------------------------------------