]> git.saurik.com Git - wxWidgets.git/blame - tests/controls/treectrltest.cpp
paint events cannot be constructed outside a native redraw in OSX
[wxWidgets.git] / tests / controls / treectrltest.cpp
CommitLineData
fa97ee24
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/controls/treectrltest.cpp
3// Purpose: wxTreeCtrl unit test
4// Author: Vadim Zeitlin
5// Created: 2008-11-26
6// RCS-ID: $Id$
7// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14#include "testprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/app.h"
22#endif // WX_PRECOMP
23
24#include "wx/treectrl.h"
25
26// ----------------------------------------------------------------------------
27// test class
28// ----------------------------------------------------------------------------
29
30class TreeCtrlTestCase : public CppUnit::TestCase
31{
32public:
33 TreeCtrlTestCase() { }
34
35 virtual void setUp();
36 virtual void tearDown();
37
38private:
39 CPPUNIT_TEST_SUITE( TreeCtrlTestCase );
40 CPPUNIT_TEST( HasChildren );
756c98b0
VZ
41 CPPUNIT_TEST( SelectItemSingle );
42 CPPUNIT_TEST( PseudoTest_MultiSelect );
43 CPPUNIT_TEST( SelectItemMulti );
fa97ee24
VZ
44 CPPUNIT_TEST( PseudoTest_SetHiddenRoot );
45 CPPUNIT_TEST( HasChildren );
46 CPPUNIT_TEST_SUITE_END();
47
48 void HasChildren();
756c98b0
VZ
49 void SelectItemSingle();
50 void SelectItemMulti();
51 void PseudoTest_MultiSelect() { ms_multiSelect = true; }
fa97ee24
VZ
52 void PseudoTest_SetHiddenRoot() { ms_hiddenRoot = true; }
53
756c98b0 54 static bool ms_multiSelect;
fa97ee24
VZ
55 static bool ms_hiddenRoot;
56
756c98b0 57 // the tree control itself
fa97ee24
VZ
58 wxTreeCtrl *m_tree;
59
756c98b0
VZ
60 // and some of its items
61 wxTreeItemId m_root,
62 m_child1,
63 m_child2,
64 m_grandchild;
65
fa97ee24
VZ
66 DECLARE_NO_COPY_CLASS(TreeCtrlTestCase)
67};
68
69// register in the unnamed registry so that these tests are run by default
70CPPUNIT_TEST_SUITE_REGISTRATION( TreeCtrlTestCase );
71
72// also include in it's own registry so that these tests can be run alone
73CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TreeCtrlTestCase, "TreeCtrlTestCase" );
74
75// ----------------------------------------------------------------------------
76// test initialization
77// ----------------------------------------------------------------------------
78
756c98b0 79bool TreeCtrlTestCase::ms_multiSelect = false;
fa97ee24
VZ
80bool TreeCtrlTestCase::ms_hiddenRoot = false;
81
82void TreeCtrlTestCase::setUp()
83{
84 m_tree = new wxTreeCtrl(wxTheApp->GetTopWindow());
756c98b0
VZ
85
86 if ( ms_multiSelect )
87 m_tree->ToggleWindowStyle(wxTR_MULTIPLE);
88
fa97ee24
VZ
89 if ( ms_hiddenRoot )
90 m_tree->ToggleWindowStyle(wxTR_HIDE_ROOT); // actually set it
756c98b0
VZ
91
92 m_root = m_tree->AddRoot("root");
93 m_child1 = m_tree->AppendItem(m_root, "child1");
94 m_child2 = m_tree->AppendItem(m_root, "child2");
95 m_grandchild = m_tree->AppendItem(m_child1, "grandchild");
fa97ee24
VZ
96}
97
98void TreeCtrlTestCase::tearDown()
99{
100 delete m_tree;
101 m_tree = NULL;
756c98b0
VZ
102
103 m_root =
104 m_child1 =
105 m_child2 =
106 m_grandchild = wxTreeItemId();
fa97ee24
VZ
107}
108
109// ----------------------------------------------------------------------------
110// the tests themselves
111// ----------------------------------------------------------------------------
112
113void TreeCtrlTestCase::HasChildren()
114{
756c98b0
VZ
115 CPPUNIT_ASSERT( m_tree->HasChildren(m_root) );
116 CPPUNIT_ASSERT( m_tree->HasChildren(m_child1) );
117 CPPUNIT_ASSERT( !m_tree->HasChildren(m_child2) );
118 CPPUNIT_ASSERT( !m_tree->HasChildren(m_grandchild) );
119}
120
121void TreeCtrlTestCase::SelectItemSingle()
122{
123 // this test should be only ran in single-selection control
124 CPPUNIT_ASSERT( !m_tree->HasFlag(wxTR_MULTIPLE) );
125
126 // initially nothing is selected
127 CPPUNIT_ASSERT( !m_tree->IsSelected(m_child1) );
128
129 // selecting an item should make it selected
130 m_tree->SelectItem(m_child1);
131 CPPUNIT_ASSERT( m_tree->IsSelected(m_child1) );
132
133 // selecting it again shouldn't change anything
134 m_tree->SelectItem(m_child1);
135 CPPUNIT_ASSERT( m_tree->IsSelected(m_child1) );
136
137 // selecting another item should switch the selection to it
138 m_tree->SelectItem(m_child2);
139 CPPUNIT_ASSERT( !m_tree->IsSelected(m_child1) );
140 CPPUNIT_ASSERT( m_tree->IsSelected(m_child2) );
141
142 // selecting it again still shouldn't change anything
143 m_tree->SelectItem(m_child2);
144 CPPUNIT_ASSERT( !m_tree->IsSelected(m_child1) );
145 CPPUNIT_ASSERT( m_tree->IsSelected(m_child2) );
146
147 // deselecting an item should remove the selection entirely
148 m_tree->UnselectItem(m_child2);
149 CPPUNIT_ASSERT( !m_tree->IsSelected(m_child1) );
150 CPPUNIT_ASSERT( !m_tree->IsSelected(m_child2) );
151}
152
153void TreeCtrlTestCase::SelectItemMulti()
154{
155 // this test should be only ran in multi-selection control
156 CPPUNIT_ASSERT( m_tree->HasFlag(wxTR_MULTIPLE) );
157
158 // initially nothing is selected
159 CPPUNIT_ASSERT( !m_tree->IsSelected(m_child1) );
160
161 // selecting an item should make it selected
162 m_tree->SelectItem(m_child1);
163 CPPUNIT_ASSERT( m_tree->IsSelected(m_child1) );
164
165 // selecting it again shouldn't change anything
166 m_tree->SelectItem(m_child1);
167 CPPUNIT_ASSERT( m_tree->IsSelected(m_child1) );
168
169 // selecting another item shouldn't deselect the previously selected one
170 m_tree->SelectItem(m_child2);
171 CPPUNIT_ASSERT( m_tree->IsSelected(m_child1) );
172 CPPUNIT_ASSERT( m_tree->IsSelected(m_child2) );
173
174 // selecting it again still shouldn't change anything
175 m_tree->SelectItem(m_child2);
176 CPPUNIT_ASSERT( m_tree->IsSelected(m_child1) );
177 CPPUNIT_ASSERT( m_tree->IsSelected(m_child2) );
178
179 // deselecting one of the items should leave the others selected
180 m_tree->UnselectItem(m_child1);
181 CPPUNIT_ASSERT( !m_tree->IsSelected(m_child1) );
182 CPPUNIT_ASSERT( m_tree->IsSelected(m_child2) );
fa97ee24
VZ
183}
184