Files
MuditaOS/module-gui/test/test-catch/test-gui-callbacks.cpp
Marcin Smoczyński a9cb37d7f4 tests: add proper catch2 handling
Add proper catch2 support:
 - add catch2 as a submodule
 - add cmake function to add test easily
 - discover each test in test binary
 - remove copies of catch.hpp
 - do not try to process tests for rt1051

Do not test internals of utf8, only public interface (nasty bugs could
occur otherwise).

Signed-off-by: Marcin Smoczyński <smoczynski.marcin@gmail.com>
2020-06-24 10:38:14 +02:00

109 lines
3.4 KiB
C++

#include <catch2/catch.hpp>
#include <module-gui/gui/widgets/Item.hpp>
#include <mock/TestWindow.hpp>
// there is strict order on which callbacks should be called
TEST_CASE("gui::Item on input flow test")
{
bool success = false;
auto item = gui::Item();
SECTION("gui item onInput callback called")
{
success = false;
item.inputCallback = [&success](gui::Item &, const gui::InputEvent &inputEvent) -> bool {
success = true;
return true;
};
// trigger callback
item.onInput(gui::InputEvent({}));
// test callback called
REQUIRE(success == true);
}
SECTION("gui item resize called")
{
success = false;
item.dimensionChangedCallback = [&success](gui::Item &, void *data) -> bool {
success = true;
return true;
};
// trigger callback
item.setSize(item.getWidth(), item.getHeight());
// test callback called
REQUIRE(success == true);
}
}
#include <module-gui/gui/widgets/Label.hpp>
TEST_CASE("gui::Window on input flow test")
{
auto win = gui::TestWindow("Callback test window");
bool success = false;
SECTION("gui item onInput callback called")
{
success = false;
win.inputCallback = [&success](gui::Item &, const gui::InputEvent &inputEvent) -> bool {
success = true;
return true;
};
// trigger callback
win.onInput(gui::InputEvent({}));
// test callback called
REQUIRE(success == true);
}
SECTION("gui item - no input handling, no navigation")
{
win.inputCallback = nullptr;
win.onInput(gui::InputEvent({}));
REQUIRE(success == false);
}
auto l1 = new gui::Label(&win, 0, 0, 0, 0, "Test 1");
auto l2 = new gui::Label(&win, 0, 0, 0, 0, "Test 2");
SECTION("gui item - no input handling with navigation")
{
bool focus_acquired_l1 = false;
bool focus_acquired_l2 = false;
bool l1_input_handled = false;
l1->setNavigationItem({gui::NavigationDirection::DOWN}, l2);
l1->focusChangedCallback = [&focus_acquired_l1](gui::Item &it) -> bool {
if (it.focus) {
focus_acquired_l1 = true;
}
return true;
};
l1->inputCallback = [&l1_input_handled](gui::Item &, const gui::InputEvent &inputEvent) -> bool {
if (inputEvent.keyCode == gui::KeyCode::KEY_TORCH) {
l1_input_handled = true;
return true;
}
return false;
};
l2->setNavigationItem({gui::NavigationDirection::DOWN}, l1);
l2->focusChangedCallback = [&focus_acquired_l2](gui::Item &it) -> bool {
if (it.focus) {
focus_acquired_l2 = true;
}
return true;
};
win.setFocusItem(l1);
focus_acquired_l2 = false;
win.onInput(gui::InputEvent({}, gui::InputEvent::State::keyReleasedShort, gui::KeyCode::KEY_DOWN));
REQUIRE(focus_acquired_l2);
focus_acquired_l1 = false;
win.onInput(gui::InputEvent({}, gui::InputEvent::State::keyReleasedShort, gui::KeyCode::KEY_DOWN));
REQUIRE(focus_acquired_l1);
win.onInput(gui::InputEvent({}, gui::InputEvent::State::keyReleasedShort, gui::KeyCode::KEY_TORCH));
REQUIRE(l1_input_handled);
}
}