dialogtest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/dialogtest.cpp
  3. // Purpose: wxWindow unit test
  4. // Author: Vaclav Slavik
  5. // Created: 2012-08-30
  6. // Copyright: (c) 2012 Vaclav Slavik
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #ifdef __BORLANDC__
  10. #pragma hdrstop
  11. #endif
  12. #include "wx/testing.h"
  13. #ifdef HAVE_VARIADIC_MACROS
  14. #include "wx/msgdlg.h"
  15. #include "wx/filedlg.h"
  16. // This test suite tests helpers from wx/testing.h intended for testing of code
  17. // that calls modal dialogs. It does not test the implementation of wxWidgets'
  18. // dialogs.
  19. class ModalDialogsTestCase : public CppUnit::TestCase
  20. {
  21. public:
  22. ModalDialogsTestCase() { }
  23. private:
  24. CPPUNIT_TEST_SUITE( ModalDialogsTestCase );
  25. CPPUNIT_TEST( MessageDialog );
  26. CPPUNIT_TEST( FileDialog );
  27. CPPUNIT_TEST( CustomDialog );
  28. CPPUNIT_TEST_SUITE_END();
  29. void MessageDialog();
  30. void FileDialog();
  31. void CustomDialog();
  32. DECLARE_NO_COPY_CLASS(ModalDialogsTestCase)
  33. };
  34. // register in the unnamed registry so that these tests are run by default
  35. CPPUNIT_TEST_SUITE_REGISTRATION( ModalDialogsTestCase );
  36. // also include in its own registry so that these tests can be run alone
  37. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ModalDialogsTestCase, "ModalDialogsTestCase" );
  38. void ModalDialogsTestCase::MessageDialog()
  39. {
  40. int rc;
  41. wxTEST_DIALOG
  42. (
  43. rc = wxMessageBox("Should I fail?", "Question", wxYES|wxNO),
  44. wxExpectModal<wxMessageDialog>(wxNO),
  45. wxExpectModal<wxFileDialog>(wxGetCwd() + "/test.txt").Optional()
  46. );
  47. CPPUNIT_ASSERT_EQUAL(wxNO, rc);
  48. }
  49. void ModalDialogsTestCase::FileDialog()
  50. {
  51. wxFileDialog dlg(NULL);
  52. int rc;
  53. wxTEST_DIALOG
  54. (
  55. rc = dlg.ShowModal(),
  56. wxExpectModal<wxFileDialog>(wxGetCwd() + "/test.txt")
  57. );
  58. CPPUNIT_ASSERT_EQUAL((int)wxID_OK, rc);
  59. CPPUNIT_ASSERT_EQUAL("test.txt", dlg.GetFilename());
  60. }
  61. class MyDialog : public wxDialog
  62. {
  63. public:
  64. MyDialog(wxWindow *parent) : wxDialog(parent, wxID_ANY, "Entry"), m_value(-1)
  65. {
  66. // Dummy. Imagine it's a real dialog that shows some number-entry
  67. // controls.
  68. }
  69. int m_value;
  70. };
  71. template<>
  72. class wxExpectModal<MyDialog> : public wxExpectModalBase<MyDialog>
  73. {
  74. public:
  75. wxExpectModal(int valueToSet) : m_valueToSet(valueToSet) {}
  76. protected:
  77. virtual int OnInvoked(MyDialog *dlg) const
  78. {
  79. // Simulate the user entering the expected number:
  80. dlg->m_value = m_valueToSet;
  81. return wxID_OK;
  82. }
  83. int m_valueToSet;
  84. };
  85. void ModalDialogsTestCase::CustomDialog()
  86. {
  87. MyDialog dlg(NULL);
  88. wxTEST_DIALOG
  89. (
  90. dlg.ShowModal(),
  91. wxExpectModal<MyDialog>(42)
  92. );
  93. CPPUNIT_ASSERT_EQUAL( 42, dlg.m_value );
  94. }
  95. #endif // HAVE_VARIADIC_MACROS