summaryrefslogtreecommitdiff
path: root/test/test-page-manager.c
blob: b1d862fe0e59fa2d8d69fc0852cc61b13f9f1e1e (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <gtk/gtk.h>
#include <endless/endless.h>

#include "run-tests.h"

#define PAGE1_NAME "page1"
#define PAGE2_NAME "page2"
#define PAGE3_NAME "page3"
#define EXPECTED_PAGE_NAME PAGE2_NAME
#define EXPECTED_CHANGED_PAGE_NAME "changed-name"
#define DUPLICATE_PAGE_NAME "duplicate-name"
#define EXPECTED_DUPLICATE_PAGE_NAME_ERRMSG "*Not setting page name to \"" \
  DUPLICATE_PAGE_NAME "\", because page manager already contains a page by " \
  "that name*"
#define ADD_PAGE_MANAGER_TEST(path, test_func) \
  g_test_add ((path), PageManagerFixture, NULL, \
              pm_fixture_setup, (test_func), pm_fixture_teardown)
#define ADD_EMPTY_PAGE_MANAGER_TEST(path, test_func) \
  g_test_add ((path), PageManagerFixture, NULL, \
              empty_pm_fixture_setup, (test_func), pm_fixture_teardown);

typedef struct
{
  GtkWidget *pm;
  GtkWidget *page1;
  GtkWidget *page2;
  GtkWidget *page3;
} PageManagerFixture;

static void
pm_fixture_setup (PageManagerFixture *fixture,
                  gconstpointer       unused)
{
  fixture->pm = eos_page_manager_new ();
  fixture->page1 = gtk_label_new ("1");
  fixture->page2 = gtk_label_new ("2");
  fixture->page3 = gtk_label_new ("3");
  gtk_container_add_with_properties (GTK_CONTAINER (fixture->pm),
                                     fixture->page1,
                                     "name", PAGE1_NAME,
                                     NULL);
  gtk_container_add_with_properties (GTK_CONTAINER (fixture->pm),
                                     fixture->page2,
                                     "name", PAGE2_NAME,
                                     NULL);
  gtk_container_add_with_properties (GTK_CONTAINER (fixture->pm),
                                     fixture->page3,
                                     "name", PAGE3_NAME,
                                     NULL);
}

static void
empty_pm_fixture_setup (PageManagerFixture *fixture,
                        gconstpointer       unused)
{
  fixture->pm = eos_page_manager_new ();
}

static void
pm_fixture_teardown (PageManagerFixture *fixture,
                     gconstpointer       unused)
{
  gtk_widget_destroy (fixture->pm);
}

static void
test_pm_get_set_visible_page (PageManagerFixture *fixture,
                              gconstpointer       unused)
{
  GtkWidget *visible_page;
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page != fixture->page2);
  eos_page_manager_set_visible_page (EOS_PAGE_MANAGER (fixture->pm),
                                     fixture->page2);
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == fixture->page2);
}

static void
test_pm_prop_visible_page (PageManagerFixture *fixture,
                           gconstpointer       unused)
{
  GtkWidget *visible_page;
  g_object_get (fixture->pm, "visible-page", &visible_page, NULL);
  g_assert (visible_page != fixture->page2);
  g_object_set (fixture->pm, "visible-page", fixture->page2, NULL);
  g_object_get (fixture->pm, "visible-page", &visible_page, NULL);
  g_assert (visible_page == fixture->page2);
}

static void
test_pm_get_set_visible_page_name (PageManagerFixture *fixture,
                                   gconstpointer       unused)
{
  const gchar *name;
  name = eos_page_manager_get_visible_page_name (EOS_PAGE_MANAGER (fixture->pm));
  g_assert_cmpstr (name, !=, EXPECTED_PAGE_NAME);
  eos_page_manager_set_visible_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                          EXPECTED_PAGE_NAME);
  name = eos_page_manager_get_visible_page_name (EOS_PAGE_MANAGER (fixture->pm));
  g_assert_cmpstr (name, ==, EXPECTED_PAGE_NAME);
}

static void
test_pm_prop_visible_page_name (PageManagerFixture *fixture,
                                gconstpointer       unused)
{
  gchar *name;
  g_object_get (fixture->pm, "visible-page-name", &name, NULL);
  g_assert_cmpstr (name, !=, EXPECTED_PAGE_NAME);
  g_free (name);
  g_object_set (fixture->pm, "visible-page-name", EXPECTED_PAGE_NAME, NULL);
  g_object_get (fixture->pm, "visible-page-name", &name, NULL);
  g_assert_cmpstr (name, ==, EXPECTED_PAGE_NAME);
  g_free (name);
}

static void
test_pm_get_set_page_name (PageManagerFixture *fixture,
                           gconstpointer       unused)
{
  const gchar *name;
  name = eos_page_manager_get_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                         fixture->page1);
  g_assert_cmpstr (name, ==, PAGE1_NAME);
  name = eos_page_manager_get_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                         fixture->page2);
  g_assert_cmpstr (name, ==, PAGE2_NAME);
  name = eos_page_manager_get_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                         fixture->page3);
  g_assert_cmpstr (name, ==, PAGE3_NAME);
  eos_page_manager_set_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                  fixture->page2,
                                  EXPECTED_CHANGED_PAGE_NAME);
  name = eos_page_manager_get_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                         fixture->page2);
  g_assert_cmpstr (name, ==, EXPECTED_CHANGED_PAGE_NAME);
}

static void
test_pm_child_prop_name (PageManagerFixture *fixture,
                         gconstpointer       unused)
{
  gchar *name;
  gtk_container_child_get (GTK_CONTAINER (fixture->pm), fixture->page1,
                           "name", &name,
                           NULL);
  g_assert_cmpstr (name, ==, PAGE1_NAME);
  g_free (name);
  gtk_container_child_get (GTK_CONTAINER (fixture->pm), fixture->page2,
                           "name", &name,
                           NULL);
  g_assert_cmpstr (name, ==, PAGE2_NAME);
  g_free (name);
  gtk_container_child_get (GTK_CONTAINER (fixture->pm), fixture->page3,
                           "name", &name,
                           NULL);
  g_assert_cmpstr (name, ==, PAGE3_NAME);
  g_free (name);
  gtk_container_child_set (GTK_CONTAINER (fixture->pm), fixture->page2,
                           "name", EXPECTED_CHANGED_PAGE_NAME,
                           NULL);
  gtk_container_child_get (GTK_CONTAINER (fixture->pm), fixture->page2,
                           "name", &name,
                           NULL);
  g_assert_cmpstr (name, ==, EXPECTED_CHANGED_PAGE_NAME);
  g_free (name);
}

static void
test_pm_page_no_name (PageManagerFixture *fixture,
                      gconstpointer       unused)
{
  const gchar *name_get;
  gchar *name_prop;
  GtkWidget *new_page = gtk_label_new("new");
  gtk_container_add (GTK_CONTAINER (fixture->pm), new_page);
  name_get = eos_page_manager_get_page_name (EOS_PAGE_MANAGER (fixture->pm), new_page);
  g_assert (name_get == NULL);
  gtk_container_child_get (GTK_CONTAINER (fixture->pm), new_page,
                           "name", &name_prop,
                           NULL);
  g_assert (name_prop == NULL);
}

static void
test_pm_set_page_no_name (PageManagerFixture *fixture,
                          gconstpointer       unused)
{
  const gchar *name;
  eos_page_manager_set_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                  fixture->page1,
                                  NULL);
  name = eos_page_manager_get_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                         fixture->page1);
  g_assert (name == NULL);
  name = eos_page_manager_get_visible_page_name (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (name == NULL);
}

static void
test_pm_remove_page_behavior (PageManagerFixture *fixture,
                              gconstpointer       unused)
{
  GtkWidget *visible_page;
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == fixture->page1);
  gtk_container_remove (GTK_CONTAINER (fixture->pm), fixture->page3);
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == fixture->page1);
  gtk_container_remove (GTK_CONTAINER (fixture->pm), fixture->page2);
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == fixture->page1);
  gtk_container_remove (GTK_CONTAINER (fixture->pm), fixture->page1);
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == NULL);
}

static void
test_pm_remove_page_undefined_behavior (PageManagerFixture *fixture,
                                        gconstpointer       unused)
{
  GtkWidget *visible_page;
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == fixture->page1);
  gtk_container_remove (GTK_CONTAINER (fixture->pm), fixture->page1);
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page != fixture->page1);
}

static void
test_pm_remove_page_by_name (PageManagerFixture *fixture,
                             gconstpointer       unused)
{
  GList *pages = gtk_container_get_children (GTK_CONTAINER (fixture->pm));
  guint length = g_list_length (pages);
  g_list_free (pages);

  eos_page_manager_remove_page_by_name (EOS_PAGE_MANAGER (fixture->pm),
                                        PAGE2_NAME);
  pages = gtk_container_get_children (GTK_CONTAINER (fixture->pm));
  g_assert_cmpuint (g_list_length (pages), ==, length - 1);
  g_assert (g_list_find (pages, fixture->page1) != NULL);
  g_assert (g_list_find (pages, fixture->page2) == NULL);
  g_assert (g_list_find (pages, fixture->page3) != NULL);
  g_list_free (pages);
}

static void
test_pm_duplicate_page_name (PageManagerFixture *fixture,
                             gconstpointer unused)
{
  eos_page_manager_set_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                  fixture->page1,
                                  DUPLICATE_PAGE_NAME);
  /* Should not complain */
  eos_page_manager_set_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                  fixture->page1,
                                  DUPLICATE_PAGE_NAME);

  g_test_expect_message (TEST_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                         EXPECTED_DUPLICATE_PAGE_NAME_ERRMSG);
  eos_page_manager_set_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                  fixture->page2,
                                  DUPLICATE_PAGE_NAME);
  g_test_assert_expected_messages ();

  const gchar *name = eos_page_manager_get_page_name (EOS_PAGE_MANAGER (fixture->pm),
                                                      fixture->page2);
  g_assert_cmpstr (name, !=, DUPLICATE_PAGE_NAME);
}

static void
test_empty_pm_visible_page (PageManagerFixture *fixture,
                            gconstpointer       unused)
{
  GtkWidget *visible_page_get, *visible_page_prop;
  visible_page_get = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page_get == NULL);
  g_object_get (fixture->pm, "visible-page", &visible_page_prop, NULL);
  g_assert (visible_page_prop == NULL);
}

static void
test_empty_pm_visible_page_name (PageManagerFixture *fixture,
                                 gconstpointer       unused)
{
  const gchar *name_get;
  gchar *name_prop;
  name_get = eos_page_manager_get_visible_page_name (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (name_get == NULL);
  g_object_get (fixture->pm, "visible-page-name", &name_prop, NULL);
  g_assert (name_prop == NULL);
}

static void
test_empty_pm_add_page_behavior (PageManagerFixture *fixture,
                                 gconstpointer       unused)
{
  GtkWidget *visible_page;
  GtkWidget *page1 = gtk_label_new ("page1");
  GtkWidget *page2 = gtk_label_new ("page2");
  gtk_container_add (GTK_CONTAINER (fixture->pm), page1);
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == page1);
  gtk_container_add (GTK_CONTAINER (fixture->pm), page2);
  visible_page = eos_page_manager_get_visible_page (EOS_PAGE_MANAGER (fixture->pm));
  g_assert (visible_page == page1); /* Not page2! */
}

void
add_page_manager_tests (void)
{
  ADD_PAGE_MANAGER_TEST ("/page-manager/get-set-visible-page",
                         test_pm_get_set_visible_page);
  ADD_PAGE_MANAGER_TEST ("/page-manager/prop-visible-page",
                         test_pm_prop_visible_page);
  ADD_PAGE_MANAGER_TEST ("/page-manager/get-set-visible-page-name",
                         test_pm_get_set_visible_page_name);
  ADD_PAGE_MANAGER_TEST ("/page-manager/prop-visible-page-name",
                         test_pm_prop_visible_page_name);
  ADD_PAGE_MANAGER_TEST ("/page-manager/get-set-page-name",
                         test_pm_get_set_page_name);
  ADD_PAGE_MANAGER_TEST ("/page-manager/child-prop-name",
                         test_pm_child_prop_name);
  ADD_PAGE_MANAGER_TEST ("/page-manager/page-no-name", test_pm_page_no_name);
  ADD_PAGE_MANAGER_TEST ("/page-manager/set-page-no-name",
                         test_pm_set_page_no_name);
  ADD_PAGE_MANAGER_TEST ("/page-manager/remove-page-by-name",
                         test_pm_remove_page_by_name);
  ADD_PAGE_MANAGER_TEST ("/page-manager/duplicate-page-name",
                         test_pm_duplicate_page_name);
  ADD_EMPTY_PAGE_MANAGER_TEST ("/page-manager/empty-visible-page",
                               test_empty_pm_visible_page);
  ADD_EMPTY_PAGE_MANAGER_TEST ("/page-manager/empty-visible-page-name",
                               test_empty_pm_visible_page_name);
  ADD_EMPTY_PAGE_MANAGER_TEST ("/page-manager/add-page-behavior",
                               test_empty_pm_add_page_behavior);

  /* Disabled until https://bugzilla.gnome.org/show_bug.cgi?id=699756 is fixed
  [endlessm/eos-sdk#67] */
  if (FALSE)
    {
      ADD_PAGE_MANAGER_TEST ("/page-manager/remove-page-behavior",
                           test_pm_remove_page_behavior);
      ADD_PAGE_MANAGER_TEST ("/page-manager/remove-page-undefined-behavior",
                             test_pm_remove_page_undefined_behavior);
    }
}