1 /*
2 * This file is part of HexEditor plugin for Code::Blocks Studio
3 * Copyright (C) 2008-2009 Bartlomiej Swiecki
4 *
5 * HexEditor plugin is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * HexEditor pluging is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with HexEditor. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * $Revision: 10288 $
19 * $Id: TestCasesDlg.cpp 10288 2015-05-15 10:58:29Z jenslody $
20 * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/plugins/contrib/HexEditor/TestCasesDlg.cpp $
21 */
22
23 #include "TestCasesDlg.h"
24
25 //(*InternalHeaders(TestCasesDlg)
26 #include <wx/string.h>
27 #include <wx/intl.h>
28 //*)
29
30 //(*IdInit(TestCasesDlg)
31 const long TestCasesDlg::ID_LISTBOX1 = wxNewId();
32 const long TestCasesDlg::ID_BUTTON1 = wxNewId();
33 const long TestCasesDlg::ID_TIMER1 = wxNewId();
34 //*)
35
BEGIN_EVENT_TABLE(TestCasesDlg,wxScrollingDialog)36 BEGIN_EVENT_TABLE(TestCasesDlg,wxScrollingDialog)
37 //(*EventTable(TestCasesDlg)
38 //*)
39 END_EVENT_TABLE()
40
41 TestCasesDlg::TestCasesDlg(wxWindow* parent, TestCasesBase& tests): m_Tests( tests ), m_Thread( 0 )
42 {
43 m_Tests.InitOutput( *this );
44 BuildContent(parent);
45 }
46
BuildContent(wxWindow * parent)47 void TestCasesDlg::BuildContent(wxWindow* parent)
48 {
49 //(*Initialize(TestCasesDlg)
50 wxBoxSizer* BoxSizer1;
51 wxStaticBoxSizer* StaticBoxSizer1;
52
53 Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
54 BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
55 StaticBoxSizer1 = new wxStaticBoxSizer(wxVERTICAL, this, _("Test log:"));
56 ListBox1 = new wxListBox(this, ID_LISTBOX1, wxDefaultPosition, wxSize(410,268), 0, 0, 0, wxDefaultValidator, _T("ID_LISTBOX1"));
57 StaticBoxSizer1->Add(ListBox1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 5);
58 Button1 = new wxButton(this, ID_BUTTON1, _("Stop"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
59 StaticBoxSizer1->Add(Button1, 0, wxBOTTOM|wxLEFT|wxRIGHT|wxALIGN_RIGHT, 5);
60 BoxSizer1->Add(StaticBoxSizer1, 1, wxALL|wxEXPAND, 5);
61 SetSizer(BoxSizer1);
62 Timer1.SetOwner(this, ID_TIMER1);
63 Timer1.Start(50, false);
64 BoxSizer1->Fit(this);
65 BoxSizer1->SetSizeHints(this);
66
67 Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TestCasesDlg::OnButton1Click);
68 Connect(ID_TIMER1,wxEVT_TIMER,(wxObjectEventFunction)&TestCasesDlg::OnTimer1Trigger);
69 Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&TestCasesDlg::OnClose);
70 //*)
71
72 m_Running = true;
73 m_StopRequest = false;
74 m_BtnChanged = false;
75
76 // Start the thread
77 m_Thread = new MyThread( this );
78 m_Thread->Create();
79 m_Thread->Run();
80 }
81
~TestCasesDlg()82 TestCasesDlg::~TestCasesDlg()
83 {
84 //(*Destroy(TestCasesDlg)
85 //*)
86 m_Thread->Wait();
87 delete m_Thread;
88 }
89
Entry()90 int TestCasesDlg::Entry()
91 {
92 m_Result = m_Tests.PerformTests();
93 m_Running = false;
94 return 0;
95 }
96
StopTest()97 bool TestCasesDlg::StopTest()
98 {
99 // Don't have to lock anything since
100 // we only send one bit of information
101 return m_StopRequest;
102 }
103
AddLog(const wxString & logLine)104 void TestCasesDlg::AddLog( const wxString& logLine )
105 {
106 wxCriticalSectionLocker lock( m_Section );
107 m_NewLogs.Add( logLine );
108 }
109
OnButton1Click(wxCommandEvent &)110 void TestCasesDlg::OnButton1Click(wxCommandEvent& /*event*/)
111 {
112 if ( m_Running )
113 {
114 m_StopRequest = true;
115 Button1->Disable();
116 AddLog( _T("Cancelled by the user") );
117 }
118 else if ( m_BtnChanged )
119 {
120 EndDialog( wxID_OK );
121 }
122 }
123
OnTimer1Trigger(wxTimerEvent &)124 void TestCasesDlg::OnTimer1Trigger(wxTimerEvent& /*event*/)
125 {
126 if ( !m_Running && !m_BtnChanged )
127 {
128 m_BtnChanged = true;
129 Button1->Enable();
130 Button1->SetLabel( _("Close") );
131 }
132
133 wxCriticalSectionLocker lock( m_Section );
134 if ( !m_NewLogs.IsEmpty() )
135 {
136 ListBox1->Append( m_NewLogs );
137 m_NewLogs.clear();
138
139 ListBox1->SetFirstItem( ListBox1->GetCount() - 1 );
140 }
141 }
142
OnClose(wxCloseEvent & event)143 void TestCasesDlg::OnClose(wxCloseEvent& event)
144 {
145 if ( m_Running && event.CanVeto() )
146 {
147 event.Veto();
148 }
149 else
150 {
151 m_StopRequest = true;
152 event.Skip();
153 }
154 }
155