summaryrefslogtreecommitdiff
path: root/gui/tests/participant_editor.cpp
blob: 8f6f7d3acd4228d07b3e7880822e8a90e4812cef (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * SPDX-FileCopyrightText: 2025 Felix Morgner <felix.morgner@gmail.com>
 * SPDX-License-Identifier: LGPL-2.1-only
 */

#include "participant_editor.hpp"

#include "gtk-test.hpp"
#include "messages.hpp"

#include <turnsmm/participant.hpp>

#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>

#include <glib/gi18n.h>

#include <adwaitamm/applicationwindow.hpp>
#include <adwaitamm/comborow.hpp>
#include <glibmm/i18n.h>
#include <glibmm/ustring.h>
#include <gtkmm/stringobject.h>

#include <clocale>
#include <format>

namespace Turns::gui::tests
{

  namespace
  {
    auto constexpr static default_name{"Test Participant"};
    auto constexpr static default_priority{5.15f};
    auto constexpr static default_disposition{Participant::Disposition::Friendly};
  }  // namespace

  SCENARIO("Creating a participant editor", "[gui][windows][lifetime]")
  {
    auto window = Adwaita::ApplicationWindow{gtk_test::application};
    auto locale = GENERATE("en_US.UTF-8", "de_CH.UTF-8", "de_DE.UTF-8", "de_AT.UTF-8");

    GIVEN(std::format("An active locale of {}", locale))
    {
      setlocale(LC_ALL, locale);

      AND_GIVEN("a participant editor constructed without a participant")
      {
        auto instance = ParticipantEditor{nullptr};
        instance.present(&window);

        THEN("its title is message::add_participant")
        {
          REQUIRE(instance.get_title() == _(message::add_participant));
        }

        THEN("its name is empty")
        {
          REQUIRE(instance.get_name().empty());
        }

        THEN("its priority is 0")
        {
          REQUIRE(instance.get_priority() == 0.0f);
        }

        THEN("its disposition is neutral")
        {
          REQUIRE(instance.get_disposition() == Participant::Disposition::Neutral);
        }

        THEN("its participant is NULL")
        {
          REQUIRE(instance.get_participant() == nullptr);
        }

        THEN("its disposition combo row has 4 elements")
        {
          auto row = instance.get_widget<Adwaita::ComboRow>("disposition");
          REQUIRE(row->get_model()->get_n_items() == 4);
          auto model = row->get_model();
          auto index = GENERATE(0, 1, 2, 3);

          AND_THEN(std::format("the row's entry at index {} is translated accordingly", index))
          {
            auto object = model->get_typed_object<Gtk::StringObject>(index);
            auto text = object->get_string();

            switch (static_cast<Participant::Disposition>(index))
            {
            case Participant::Disposition::Neutral:
              REQUIRE(text == _("Neutral"));
              break;
            case Participant::Disposition::Friendly:
              REQUIRE(text == _("Friendly"));
              break;
            case Participant::Disposition::Hostile:
              REQUIRE(text == _("Hostile"));
              break;
            case Participant::Disposition::Secret:
              REQUIRE(text == _("Secret"));
              break;
            }
          }
        }

        WHEN("a participant is set")
        {
          auto participant = Participant::create(default_name, default_priority, default_disposition);
          instance.set_participant(participant);

          THEN("its title is message::edit_participant")
          {
            REQUIRE(instance.get_title() == _(message::edit_participant));
          }

          THEN("its name is the same as the participant's")
          {
            REQUIRE(instance.get_name() == participant->get_name());
          }

          THEN("its priority is the same as the participant's")
          {
            REQUIRE(instance.get_priority() == participant->get_priority());
          }

          THEN("its disposition is the same as the participant's")
          {
            REQUIRE(instance.get_disposition() == participant->get_disposition());
          }

          THEN("its participant is the participant")
          {
            REQUIRE(instance.get_participant() == participant);
          }

          THEN("changing the participant's name also changes the name in the editor")
          {
            auto new_value = "Changed Name";
            participant->set_name(new_value);
            REQUIRE(instance.get_name() == new_value);
          }

          THEN("changing the participant's disposition also changes the disposition in the editor")
          {
            auto new_value = Participant::Disposition::Hostile;
            participant->set_disposition(new_value);
            REQUIRE(instance.get_disposition() == new_value);
          }

          THEN("changing the participant's priority also changes the priority in the editor")
          {
            auto new_value = -10.0f;
            participant->set_priority(new_value);
            REQUIRE(instance.get_priority() == new_value);
          }

          THEN("changing the name in the editor also changes the participant's name")
          {
            auto new_value = "Editor Changed Name";
            instance.set_name(new_value);
            REQUIRE(participant->get_name() == new_value);
          }

          THEN("changing the disposition in the editor also changes the participant's disposition")
          {
            auto new_value = Participant::Disposition::Secret;
            instance.set_disposition(new_value);
            REQUIRE(participant->get_disposition() == new_value);
          }

          THEN("changing the priority in the editor also changes the participant's priority")
          {
            auto new_value = 100.0f;
            instance.set_priority(new_value);
            REQUIRE(participant->get_priority() == new_value);
          }
        }

        instance.close();
      }
    }
  }
}

  //   auto participant = Participant::create(default_name, default_priority, default_disposition);
  //   auto instance = std::make_shared<ParticipantEditor>(nullptr);
  //   auto window = Gtk::Window{};

  //   SECTION("allows binding to the finished signal")
  //   {
  //     REQUIRE((instance->signal_finished().connect([](auto, auto, auto) {})).connected());
  //   }
  // }