mirror of
https://git.mirrors.martin98.com/https://github.com/slic3r/Slic3r.git
synced 2025-08-14 06:05:53 +08:00
Implement tests for on_change and on_kill_focus.
This commit is contained in:
parent
8d352aed1c
commit
dc256eb989
@ -11,8 +11,9 @@ public:
|
||||
ui_window = new wxCheckBox(parent, checkid, "");
|
||||
_check = dynamic_cast<wxCheckBox*>(ui_window);
|
||||
_check->Bind(wxEVT_CHECKBOX, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); });
|
||||
_check->Bind(wxEVT_KILL_FOCUS, [this](wxFocusEvent& e) { this->on_kill_focus(""); e.Skip(); });
|
||||
}
|
||||
~UI_Checkbox() { wxDELETE(ui_window); ui_window = _check = nullptr; }
|
||||
~UI_Checkbox() { wxDELETE(_check); ui_window = _check = nullptr; }
|
||||
/// Function to call when the contents of this change.
|
||||
std::function<void (const std::string&, bool value)> on_change {nullptr};
|
||||
std::function<void (const std::string&)> on_kill_focus {nullptr};
|
||||
@ -34,7 +35,7 @@ private:
|
||||
wxCheckBox* _check {nullptr};
|
||||
|
||||
void _on_change(std::string opt_id) {
|
||||
if (!this->disable_change_event) {
|
||||
if (!this->disable_change_event && this->on_change != nullptr) {
|
||||
this->on_change(opt_id, this->get_bool());
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/app.h"
|
||||
#include "wx/sizer.h"
|
||||
#include "wx/checkbox.h"
|
||||
#include "wx/uiaction.h"
|
||||
#endif // WX_PRECOMP
|
||||
@ -9,51 +10,92 @@
|
||||
#include "testableframe.h"
|
||||
#include "OptionsGroup/Field.hpp"
|
||||
using namespace Slic3r::GUI;
|
||||
SCENARIO( "GUI Checkbox option items fire their on_change event when clicked and appropriate." ) {
|
||||
|
||||
SCENARIO( "GUI Checkbox option items fire their on_kill_focus when focus leaves the checkbox." ) {
|
||||
wxTestableFrame* old = dynamic_cast<wxTestableFrame*>(wxTheApp->GetTopWindow());
|
||||
old->Destroy();
|
||||
wxTheApp->SetTopWindow(new wxTestableFrame());
|
||||
wxUIActionSimulator sim;
|
||||
wxMilliSleep(500);
|
||||
GIVEN( "A checkbox field item and disable_change = false") {
|
||||
auto* exec_counter = new int(0);
|
||||
GIVEN( "A checkbox field item exists on a window") {
|
||||
auto exec_counter {0};
|
||||
auto* test_field {new UI_Checkbox(wxTheApp->GetTopWindow(), wxID_ANY)};
|
||||
|
||||
auto killfunc {[&exec_counter](const std::string& opt_id) { exec_counter += 1; }};
|
||||
|
||||
test_field->on_kill_focus = killfunc;
|
||||
wxTheApp->GetTopWindow()->Show();
|
||||
wxTheApp->GetTopWindow()->Fit();
|
||||
test_field->disable_change_event = false;
|
||||
auto changefunc {[exec_counter](const std::string& opt_id, bool value) { *exec_counter += 1; }};
|
||||
test_field->on_change = changefunc;
|
||||
|
||||
WHEN ( "the box is checked") {
|
||||
*exec_counter = 0;
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
WHEN ( "focus leaves the checkbox") {
|
||||
exec_counter = 0;
|
||||
|
||||
test_field->check()->SetFocus();
|
||||
wxMilliSleep(500);
|
||||
|
||||
auto ev {wxFocusEvent(wxEVT_KILL_FOCUS, test_field->check()->GetId())};
|
||||
ev.SetEventObject(test_field->check());
|
||||
test_field->check()->ProcessWindowEvent(ev);
|
||||
wxYield();
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
THEN ( "on_change is executed.") {
|
||||
REQUIRE(*exec_counter == 1);
|
||||
}
|
||||
}
|
||||
WHEN ( "the box is unchecked") {
|
||||
*exec_counter = 0;
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
wxYield();
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
THEN ( "on_change is executed.") {
|
||||
REQUIRE(*exec_counter == 1);
|
||||
wxMilliSleep(500);
|
||||
THEN( "on_focus_kill is executed.") {
|
||||
REQUIRE(exec_counter == 1);
|
||||
}
|
||||
}
|
||||
delete test_field;
|
||||
}
|
||||
}
|
||||
|
||||
SCENARIO( "GUI Checkbox option items fire their on_change event when clicked and appropriate." ) {
|
||||
wxUIActionSimulator sim;
|
||||
wxTestableFrame* old = dynamic_cast<wxTestableFrame*>(wxTheApp->GetTopWindow());
|
||||
old->Destroy();
|
||||
wxTheApp->SetTopWindow(new wxTestableFrame());
|
||||
wxMilliSleep(500);
|
||||
GIVEN( "A checkbox field item and disable_change = false") {
|
||||
auto exec_counter {0};
|
||||
|
||||
auto changefunc {[&exec_counter](const std::string& opt_id, bool value) { exec_counter += 1; }};
|
||||
auto* test_field {new UI_Checkbox(wxTheApp->GetTopWindow(), wxID_ANY)};
|
||||
|
||||
test_field->disable_change_event = false;
|
||||
test_field->on_change = changefunc;
|
||||
|
||||
wxTheApp->GetTopWindow()->Show();
|
||||
wxTheApp->GetTopWindow()->Fit();
|
||||
|
||||
WHEN ( "the box is checked") {
|
||||
exec_counter = 0;
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
wxYield();
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
THEN ( "on_change is executed.") {
|
||||
REQUIRE(exec_counter == 1);
|
||||
}
|
||||
}
|
||||
WHEN ( "the box is unchecked") {
|
||||
exec_counter = 0;
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
wxYield();
|
||||
sim.MouseMove(test_field->check()->GetScreenPosition() + wxPoint(10, 10));
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
sim.MouseClick(wxMOUSE_BTN_LEFT);
|
||||
wxYield();
|
||||
THEN ( "on_change is executed.") {
|
||||
REQUIRE(exec_counter == 1);
|
||||
}
|
||||
}
|
||||
delete test_field;
|
||||
delete exec_counter;
|
||||
}
|
||||
GIVEN( "A checkbox field item and disable_change = true") {
|
||||
int exec_counter {0};
|
||||
auto exec_counter {0};
|
||||
auto changefunc {[&exec_counter] (const std::string& opt_id, bool value) { exec_counter++; }};
|
||||
auto* test_field {new Slic3r::GUI::UI_Checkbox(wxTheApp->GetTopWindow(), wxID_ANY)};
|
||||
wxTheApp->GetTopWindow()->Show();
|
||||
|
Loading…
x
Reference in New Issue
Block a user