baseplate.ui.tests.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <planet/debug/ui.hpp>
#include <planet/ostream.hpp>
#include <planet/ui/baseplate.hpp>
#include <planet/ui/layout.column.hpp>
#include <planet/ui/layout.row.hpp>
#include <felspar/test.hpp>


namespace {


    using constrained_type = planet::ui::constrained2d<float>;

    constexpr planet::ui::reflowable::constrained_type screen{
            {40, 0, 40}, {30, 0, 30}};


    auto const suite = felspar::testsuite("baseplate");


    auto const ordering = suite.test("ordering", [](auto check, auto &log) {
        planet::ui::panel panel;
        planet::ui::baseplate bp;
        planet::debug::button<> btn{log};

        check(bp.widget_count()) == 0u;

        btn.add_to(bp, panel);

        check(bp.widget_count()) == 0u;

        check([&]() {
            btn.draw();
        }).throws(std::logic_error{"Reflowable position has not been set"});

        check([&]() {
            btn.move_to(
                    {.screen = screen},
                    {{15, 20}, planet::affine::extents2d{40, 30}});
        }).throws(std::logic_error{"Reflowable constraints have not been set"});
    });


    auto const events = suite.test(
            "events",
            [](auto check, auto &log) {
                planet::ui::baseplate bp;
                planet::ui::panel panel;
                planet::debug::button<> btn{log};

                btn.add_to(bp, panel);
                btn.reflow({.screen = screen}, screen);
                btn.move_to(
                        {.screen = screen},
                        {{15, 20}, planet::affine::extents2d{4, 3}});
                btn.draw();
                check(btn.clicks) == 0u;

                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::down,
                         .location = {18, 21},
                         .clicks = 0});
                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::up,
                         .location = {18, 21},
                         .clicks = 1});
                check(btn.position())
                        == planet::affine::rectangle2d{
                                {15, 20}, planet::affine::extents2d{4, 3}};
                check(btn.clicks) == 1u;

                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::down,
                         .location = {8, 21}});
                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::up,
                         .location = {8, 21}});
                check(btn.clicks) == 1u;

                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::down,
                         .location = {18, 21}});
                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::up,
                         .location = {18, 21},
                         .clicks = 1});
                check(btn.clicks) == 2u;
            },
            [](auto check, auto &log) {
                planet::ui::baseplate bp;
                planet::ui::panel panel;
                auto ui = planet::ui::column{std::tuple{
                        planet::ui::row{std::tuple{planet::debug::button{
                                planet::debug::fixed_element{log, {4, 3}}}}}}};

                auto &btn = std::get<0>(std::get<0>(ui.items).items);
                btn.add_to(bp, panel);
                ui.reflow({.screen = screen}, screen);
                ui.move_to(
                        {.screen = screen},
                        {{15, 20}, planet::affine::extents2d{4, 3}});
                ui.draw();

                log << "btn position: " << btn.position() << '\n';

                check(btn.wants_focus()) == true;

                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::down,
                         .location = {16, 21}});
                bp.events.mouse.push(
                        {.button = planet::events::button::left,
                         .action = planet::events::action::up,
                         .location = {16, 21},
                         .clicks = 1});
                check(btn.clicks) == 1u;
            });


}