testableframe.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: testableframe.cpp
  3. // Purpose: An improved wxFrame for unit-testing
  4. // Author: Steven Lamerton
  5. // Copyright: (c) 2010 Steven Lamerton
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // For compilers that support precompilation, includes "wx/wx.h".
  9. #include "testprec.h"
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. #include "wx/app.h"
  14. #include "testableframe.h"
  15. wxTestableFrame::wxTestableFrame() : wxFrame(NULL, wxID_ANY, "Test Frame")
  16. {
  17. Move(2000, 200);
  18. }
  19. void wxTestableFrame::OnEvent(wxEvent& evt)
  20. {
  21. m_count[evt.GetEventType()]++;
  22. if(! evt.IsCommandEvent() )
  23. evt.Skip();
  24. }
  25. int wxTestableFrame::GetEventCount(wxEventType type)
  26. {
  27. return m_count[type];
  28. }
  29. void wxTestableFrame::ClearEventCount(wxEventType type)
  30. {
  31. m_count[type] = 0;
  32. }
  33. EventCounter::EventCounter(wxWindow* win, wxEventType type) : m_type(type),
  34. m_win(win)
  35. {
  36. m_frame = wxStaticCast(wxTheApp->GetTopWindow(), wxTestableFrame);
  37. m_win->Connect(m_type, wxEventHandler(wxTestableFrame::OnEvent),
  38. NULL, m_frame);
  39. }
  40. EventCounter::~EventCounter()
  41. {
  42. m_win->Disconnect(m_type, wxEventHandler(wxTestableFrame::OnEvent),
  43. NULL, m_frame);
  44. //This stops spurious counts from previous tests
  45. Clear();
  46. m_frame = NULL;
  47. m_win = NULL;
  48. }