| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: testableframe.cpp |
| 3 | // Purpose: An improved wxFrame for unit-testing |
| 4 | // Author: Steven Lamerton |
| 5 | // RCS-ID: $Id$ |
| 6 | // Copyright: (c) 2010 Steven Lamerton |
| 7 | // Licence: wxWidgets licence |
| 8 | /////////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx/wx.h". |
| 11 | #include "testprec.h" |
| 12 | |
| 13 | #ifdef __BORLANDC__ |
| 14 | #pragma hdrstop |
| 15 | #endif |
| 16 | |
| 17 | #include "wx/app.h" |
| 18 | #include "testableframe.h" |
| 19 | |
| 20 | wxTestableFrame::wxTestableFrame() : wxFrame(NULL, wxID_ANY, "Test Frame") |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | void wxTestableFrame::OnEvent(wxEvent& evt) |
| 25 | { |
| 26 | m_count[evt.GetEventType()]++; |
| 27 | |
| 28 | if(! evt.IsCommandEvent() ) |
| 29 | evt.Skip(); |
| 30 | } |
| 31 | |
| 32 | int wxTestableFrame::GetEventCount(wxEventType type) |
| 33 | { |
| 34 | if (type == wxEVT_ANY) |
| 35 | { |
| 36 | //Get the total event count |
| 37 | long total = 0; |
| 38 | |
| 39 | for(wxLongToLongHashMap::iterator iter = m_count.begin(); |
| 40 | iter != m_count.end(); |
| 41 | iter++) |
| 42 | { |
| 43 | total += iter->second; |
| 44 | iter->second = 0; |
| 45 | } |
| 46 | |
| 47 | return total; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | long count = m_count[type]; |
| 52 | m_count[type] = 0; |
| 53 | return count; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void wxTestableFrame::ClearEventCount(wxEventType type) |
| 58 | { |
| 59 | m_count[type] = 0; |
| 60 | } |
| 61 | |
| 62 | EventCounter::EventCounter(wxWindow* win, wxEventType type) : m_type(type), |
| 63 | m_win(win) |
| 64 | |
| 65 | { |
| 66 | m_frame = wxStaticCast(wxTheApp->GetTopWindow(), |
| 67 | wxTestableFrame); |
| 68 | |
| 69 | m_win->Connect(m_type, |
| 70 | wxEventHandler(wxTestableFrame::OnEvent), |
| 71 | NULL, |
| 72 | m_frame); |
| 73 | } |
| 74 | |
| 75 | EventCounter::~EventCounter() |
| 76 | { |
| 77 | m_win->Disconnect(m_type, |
| 78 | wxEventHandler(wxTestableFrame::OnEvent), |
| 79 | NULL, |
| 80 | m_frame); |
| 81 | |
| 82 | //This stops spurious counts from previous tests |
| 83 | m_frame->ClearEventCount(m_type); |
| 84 | |
| 85 | m_frame = NULL; |
| 86 | m_win = NULL; |
| 87 | } |