From dc256eb989c49d3942bd0381b99fa6228eaa77b9 Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Mon, 25 Jun 2018 20:23:31 -0500 Subject: [PATCH] Implement tests for on_change and on_kill_focus. --- src/GUI/OptionsGroup/Field.hpp | 5 +- src/test/GUI/test_field_checkbox.cpp | 108 +++++++++++++++++++-------- 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/src/GUI/OptionsGroup/Field.hpp b/src/GUI/OptionsGroup/Field.hpp index 2a9b1aad2..203d3172e 100644 --- a/src/GUI/OptionsGroup/Field.hpp +++ b/src/GUI/OptionsGroup/Field.hpp @@ -11,8 +11,9 @@ public: ui_window = new wxCheckBox(parent, checkid, ""); _check = dynamic_cast(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 on_change {nullptr}; std::function 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()); } } diff --git a/src/test/GUI/test_field_checkbox.cpp b/src/test/GUI/test_field_checkbox.cpp index 85cc111a0..111855a88 100644 --- a/src/test/GUI/test_field_checkbox.cpp +++ b/src/test/GUI/test_field_checkbox.cpp @@ -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(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(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();