]> git.saurik.com Git - wxWidgets.git/blob - tests/config/config.cpp
simplify the code for extended flags handling fixing a rare bug with wxSTAY_ON_TOP...
[wxWidgets.git] / tests / config / config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/config/config.cpp
3 // Purpose: wxConfig unit test
4 // Author: Marcin Wojdyr
5 // Created: 2007-07-07
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Marcin Wojdyr
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // see also tests/fileconf/fileconftest.cpp for wxFileConfig specific tests
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 #include "testprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #endif // WX_PRECOMP
25
26 #include "wx/config.h"
27 #include "wx/colour.h"
28
29 // --------------------------------------------------------------------------
30 // test class
31 // --------------------------------------------------------------------------
32
33 class ConfigTestCase : public CppUnit::TestCase
34 {
35 public:
36 ConfigTestCase() {}
37
38 private:
39 CPPUNIT_TEST_SUITE( ConfigTestCase );
40 CPPUNIT_TEST( ReadWriteLocalTest );
41 CPPUNIT_TEST( RecordingDefaultsTest );
42 CPPUNIT_TEST_SUITE_END();
43
44 void ReadWriteLocalTest();
45 void RecordingDefaultsTest();
46 void ReadValues(wxConfig *config, bool has_values);
47
48 DECLARE_NO_COPY_CLASS(ConfigTestCase)
49 };
50
51 // register in the unnamed registry so that these tests are run by default
52 CPPUNIT_TEST_SUITE_REGISTRATION( ConfigTestCase );
53
54 // also include in it's own registry so that these tests can be run alone
55 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ConfigTestCase, "ConfigTestCase" );
56
57 void ConfigTestCase::ReadWriteLocalTest()
58 {
59 wxString app = wxT("wxConfigTestCase");
60 wxString vendor = wxT("wxWidgets");
61 wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
62 wxCONFIG_USE_LOCAL_FILE);
63 config->DeleteAll();
64 config->Write(wxT("string1"), wxT("abc"));
65 config->Write(wxT("string2"), wxString(wxT("def")));
66 config->Write(wxT("int1"), 123);
67 config->Write(wxString(wxT("long1")), 234L);
68 config->Write(wxT("double1"), 345.67);
69 config->Write(wxT("bool1"), true);
70 config->Write(wxT("color1"), wxColour(11,22,33,44));
71 config->Flush();
72 delete config;
73
74 config = new wxConfig(app, vendor, wxT(""), wxT(""),
75 wxCONFIG_USE_LOCAL_FILE);
76 wxString string1 = config->Read(wxT("string1"));
77 CPPUNIT_ASSERT_EQUAL( string1, wxString(wxT("abc")) );
78 string1 = config->Read(wxT("string1"), wxT("defaultvalue"));
79 CPPUNIT_ASSERT_EQUAL( string1, wxString(wxT("abc")) );
80
81 wxString string2;
82 bool r = config->Read(wxT("string2"), &string2);
83 CPPUNIT_ASSERT( r );
84 CPPUNIT_ASSERT_EQUAL( string2, wxString(wxT("def")) );
85
86 r = config->Read(wxT("string2"), &string2, wxT("defaultvalue"));
87 CPPUNIT_ASSERT( r );
88 CPPUNIT_ASSERT_EQUAL( string2, wxString(wxT("def")) );
89
90 int int1 = config->Read(wxT("int1"), 5);
91 CPPUNIT_ASSERT_EQUAL( int1, 123 );
92
93 long long1;
94 r = config->Read(wxT("long1"), &long1);
95 CPPUNIT_ASSERT( r );
96 CPPUNIT_ASSERT_EQUAL( long1, 234L );
97
98 CPPUNIT_ASSERT( config->ReadLong(wxT("long1"), 0) == 234 );
99
100 double double1;
101 r = config->Read(wxT("double1"), &double1);
102 CPPUNIT_ASSERT( r );
103 CPPUNIT_ASSERT_EQUAL( double1, 345.67 );
104
105 CPPUNIT_ASSERT( config->ReadDouble(wxT("double1"), 0) == double1 );
106
107 bool bool1;
108 r = config->Read(wxT("foo"), &bool1); // there is no "foo" key
109 CPPUNIT_ASSERT( !r );
110
111 r = config->Read(wxT("bool1"), &bool1);
112 CPPUNIT_ASSERT( r );
113 CPPUNIT_ASSERT_EQUAL( bool1, true );
114
115 CPPUNIT_ASSERT( config->ReadBool(wxT("bool1"), false) == bool1 );
116
117 wxColour color1;
118 r = config->Read(wxT("color1"), &color1);
119 CPPUNIT_ASSERT( r );
120 CPPUNIT_ASSERT( color1 == wxColour(11,22,33,44) );
121
122 CPPUNIT_ASSERT( config->ReadObject(wxT("color1"), wxNullColour) == color1 );
123
124 config->DeleteAll();
125 delete config;
126 }
127
128 void ConfigTestCase::ReadValues(wxConfig *config, bool has_values)
129 {
130 bool r;
131 wxString string1 = config->Read(wxT("string1"), wxT("abc"));
132 wxString string2 = config->Read(wxT("string2"), wxString(wxT("def")));
133 wxString string3, string4;
134 r = config->Read(wxT("string3"), &string3, wxT("abc"));
135 CPPUNIT_ASSERT_EQUAL( r, has_values );
136 r = config->Read(wxT("string4"), &string4, wxString(wxT("def")));
137 CPPUNIT_ASSERT_EQUAL( r, has_values );
138 int int1;
139 r = config->Read(wxT("int1"), &int1, 123);
140 CPPUNIT_ASSERT_EQUAL( r, has_values );
141 int int2 = config->Read(wxT("int2"), 1234);
142 CPPUNIT_ASSERT_EQUAL( int2, 1234 );
143 long long1;
144 r = config->Read(wxString(wxT("long1")), &long1, 234L);
145 CPPUNIT_ASSERT_EQUAL( r, has_values );
146 double double1;
147 r = config->Read(wxT("double1"), &double1, 345.67);
148 CPPUNIT_ASSERT_EQUAL( r, has_values );
149 bool bool1;
150 r = config->Read(wxT("bool1"), &bool1, true);
151 CPPUNIT_ASSERT_EQUAL( r, has_values );
152 wxColour color1;
153 r = config->Read(wxT("color1"), &color1, wxColour(11,22,33,44));
154 CPPUNIT_ASSERT_EQUAL( r, has_values );
155 }
156
157
158 void ConfigTestCase::RecordingDefaultsTest()
159 {
160 wxString app = wxT("wxConfigTestCaseRD");
161 wxString vendor = wxT("wxWidgets");
162 wxConfig *config = new wxConfig(app, vendor, wxT(""), wxT(""),
163 wxCONFIG_USE_LOCAL_FILE);
164 config->DeleteAll();
165 config->SetRecordDefaults(false); // by default it is false
166 ReadValues(config, false);
167 CPPUNIT_ASSERT_EQUAL( (int) config->GetNumberOfEntries(), 0 );
168 config->SetRecordDefaults(true);
169 ReadValues(config, false);
170 CPPUNIT_ASSERT_EQUAL( (int) config->GetNumberOfEntries(), 10 );
171 ReadValues(config, true);
172 config->DeleteAll();
173 delete config;
174 }
175
176