accelentry.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/menu/accelentry.cpp
  3. // Purpose: wxAcceleratorEntry unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2010-12-03
  6. // Copyright: (c) 2010 Vadim Zeitlin
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #endif // WX_PRECOMP
  17. #include "wx/accel.h"
  18. #include "wx/scopedptr.h"
  19. class AccelEntryTestCase : public CppUnit::TestCase
  20. {
  21. public:
  22. AccelEntryTestCase() {}
  23. private:
  24. CPPUNIT_TEST_SUITE( AccelEntryTestCase );
  25. CPPUNIT_TEST( Create );
  26. CPPUNIT_TEST( ToFromString );
  27. CPPUNIT_TEST_SUITE_END();
  28. void Create();
  29. void ToFromString();
  30. wxDECLARE_NO_COPY_CLASS(AccelEntryTestCase);
  31. };
  32. // register in the unnamed registry so that these tests are run by default
  33. CPPUNIT_TEST_SUITE_REGISTRATION( AccelEntryTestCase );
  34. // also include in its own registry so that these tests can be run alone
  35. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AccelEntryTestCase, "AccelEntryTestCase" );
  36. namespace
  37. {
  38. void CheckAccelEntry(const wxAcceleratorEntry& accel, int keycode, int flags)
  39. {
  40. CPPUNIT_ASSERT_EQUAL( keycode, accel.GetKeyCode() );
  41. CPPUNIT_ASSERT_EQUAL( flags, accel.GetFlags() );
  42. }
  43. } // anonymous namespace
  44. void AccelEntryTestCase::Create()
  45. {
  46. wxScopedPtr<wxAcceleratorEntry>
  47. pa(wxAcceleratorEntry::Create("Foo\tCtrl+Z"));
  48. CPPUNIT_ASSERT( pa );
  49. CPPUNIT_ASSERT( pa->IsOk() );
  50. CheckAccelEntry(*pa, 'Z', wxACCEL_CTRL);
  51. // There must be a TAB in the string passed to Create()
  52. pa.reset(wxAcceleratorEntry::Create("Shift-Q"));
  53. CPPUNIT_ASSERT( !pa );
  54. pa.reset(wxAcceleratorEntry::Create("Bar\tShift-Q"));
  55. CPPUNIT_ASSERT( pa );
  56. CPPUNIT_ASSERT( pa->IsOk() );
  57. CheckAccelEntry(*pa, 'Q', wxACCEL_SHIFT);
  58. pa.reset(wxAcceleratorEntry::Create("bloordyblop"));
  59. CPPUNIT_ASSERT( !pa );
  60. }
  61. void AccelEntryTestCase::ToFromString()
  62. {
  63. wxAcceleratorEntry a(wxACCEL_ALT, 'X');
  64. CPPUNIT_ASSERT_EQUAL( "Alt+X", a.ToString() );
  65. CPPUNIT_ASSERT( a.FromString("Alt+Shift+F1") );
  66. CheckAccelEntry(a, WXK_F1, wxACCEL_ALT | wxACCEL_SHIFT);
  67. CPPUNIT_ASSERT( !a.FromString("bloordyblop") );
  68. }