Don't require leading TAB in wxAcceleratorEntry::FromString().
[wxWidgets.git] / tests / menu / accelentry.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/menu/accelentry.cpp
3 // Purpose: wxAcceleratorEntry unit test
4 // Author: Vadim Zeitlin
5 // Created: 2010-12-03
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 Vadim Zeitlin
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 #endif // WX_PRECOMP
22
23 #include "wx/accel.h"
24 #include "wx/scopedptr.h"
25
26 class AccelEntryTestCase : public CppUnit::TestCase
27 {
28 public:
29 AccelEntryTestCase() {}
30
31 private:
32 CPPUNIT_TEST_SUITE( AccelEntryTestCase );
33 CPPUNIT_TEST( Create );
34 CPPUNIT_TEST( ToFromString );
35 CPPUNIT_TEST_SUITE_END();
36
37 void Create();
38 void ToFromString();
39
40 wxDECLARE_NO_COPY_CLASS(AccelEntryTestCase);
41 };
42
43 // register in the unnamed registry so that these tests are run by default
44 CPPUNIT_TEST_SUITE_REGISTRATION( AccelEntryTestCase );
45
46 // also include in it's own registry so that these tests can be run alone
47 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AccelEntryTestCase, "AccelEntryTestCase" );
48
49 namespace
50 {
51
52 void CheckAccelEntry(const wxAcceleratorEntry& accel, int keycode, int flags)
53 {
54 CPPUNIT_ASSERT_EQUAL( keycode, accel.GetKeyCode() );
55 CPPUNIT_ASSERT_EQUAL( flags, accel.GetFlags() );
56 }
57
58 } // anonymous namespace
59
60 void AccelEntryTestCase::Create()
61 {
62 wxScopedPtr<wxAcceleratorEntry>
63 pa(wxAcceleratorEntry::Create("Foo\tCtrl+Z"));
64 CPPUNIT_ASSERT( pa );
65 CPPUNIT_ASSERT( pa->IsOk() );
66
67 CheckAccelEntry(*pa, 'Z', wxACCEL_CTRL);
68
69
70 pa.reset(wxAcceleratorEntry::Create("Shift-Q"));
71 CPPUNIT_ASSERT( pa );
72 CPPUNIT_ASSERT( pa->IsOk() );
73
74 CheckAccelEntry(*pa, 'Q', wxACCEL_SHIFT);
75
76
77 pa.reset(wxAcceleratorEntry::Create("bloordyblop"));
78 CPPUNIT_ASSERT( !pa );
79 }
80
81 void AccelEntryTestCase::ToFromString()
82 {
83 wxAcceleratorEntry a(wxACCEL_ALT, 'X');
84 CPPUNIT_ASSERT_EQUAL( "Alt+X", a.ToString() );
85
86 CPPUNIT_ASSERT( a.FromString("Alt+Shift+F1") );
87 CheckAccelEntry(a, WXK_F1, wxACCEL_ALT | wxACCEL_SHIFT);
88
89 CPPUNIT_ASSERT( !a.FromString("bloordyblop") );
90 }