summaryrefslogtreecommitdiff
path: root/test/test-window.c
blob: dfb7a4e5e8454eea3c2ff1161da39005e66ca8ce (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
/* Copyright 2013 Endless Mobile, Inc. */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <endless/endless.h>
#include "endless/eostopbar-private.h"
#include "endless/eosmainarea-private.h"

#include "run-tests.h"

#define EXPECTED_TOP_BAR_HEIGHT 32
#define EXPECTED_NULL_APPLICATION_ERRMSG \
  "*In order to create a window, you must have an application for it to " \
  "connect to.*"

static void
test_assign_application (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));

  g_assert(EOS_APPLICATION (app)
           == EOS_APPLICATION (gtk_window_get_application (GTK_WINDOW (win))));

  gtk_widget_destroy (win);
}

static void
test_application_not_null (GApplication *app)
{
  /* Unix-only test */
  if (g_test_trap_fork(0 /* timeout */, G_TEST_TRAP_SILENCE_STDERR))
    {
      GtkWidget *win = eos_window_new (NULL);
      gtk_widget_destroy (win);
      exit (0);
    }

  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr (EXPECTED_NULL_APPLICATION_ERRMSG);

  g_application_quit (app); /* No window, so otherwise won't quit */
}

static void
test_screen_size (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));
  GdkRectangle screen_size, window_size;
  GdkScreen *default_screen = gdk_screen_get_default ();
  gint monitor = 0;

  /* If more than one monitor, find out which one to use */
  if (gdk_screen_get_n_monitors (default_screen) != 1)
    {
      GdkWindow *gdkwindow;

      /* Realize the window so that its GdkWindow is not NULL */
      gtk_widget_realize (GTK_WIDGET (win));
      gdkwindow = gtk_widget_get_window (GTK_WIDGET (win));
      monitor = gdk_screen_get_monitor_at_window (default_screen, gdkwindow);
    }

  gdk_screen_get_monitor_workarea (default_screen, monitor, &screen_size);

  gtk_widget_show_now (GTK_WIDGET (win));

  while (gtk_events_pending ())
    gtk_main_iteration ();

  gtk_widget_get_allocation (GTK_WIDGET (win), &window_size);

  g_assert_cmpint (screen_size.width, ==, window_size.width);
  g_assert_cmpint (screen_size.height, ==, window_size.height);

  gtk_widget_destroy (win);
}

static void
test_has_top_bar (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));
  GtkWidget *top_bar = container_find_descendant_with_type (GTK_CONTAINER (win), EOS_TYPE_TOP_BAR);
  g_assert (top_bar != NULL);
  g_assert (EOS_IS_TOP_BAR (top_bar));

  gtk_widget_destroy (win);
}

static void
test_has_main_area (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));
  GtkWidget *main_area = container_find_descendant_with_type (GTK_CONTAINER (win), EOS_TYPE_MAIN_AREA);
  g_assert (main_area != NULL);
  g_assert (EOS_IS_MAIN_AREA (main_area));

  gtk_widget_destroy (win);
}

static void
test_has_default_page_manager (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));

  EosPageManager *pm = eos_window_get_page_manager (EOS_WINDOW (win));
  g_assert (pm != NULL);

  g_object_get (win, "page-manager", &pm, NULL);
  g_assert (pm != NULL);

  gtk_widget_destroy (win);
}

static void
test_get_set_page_manager (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));

  EosPageManager *orig_pm = eos_window_get_page_manager (EOS_WINDOW (win));
  EosPageManager *new_pm = EOS_PAGE_MANAGER (eos_page_manager_new ());

  g_assert (orig_pm != new_pm);
  eos_window_set_page_manager(EOS_WINDOW (win), new_pm);
  EosPageManager *test_pm = eos_window_get_page_manager (EOS_WINDOW (win));
  g_assert (new_pm == test_pm);

  gtk_widget_destroy (win);
}

static void
test_prop_page_manager (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));

  EosPageManager *orig_pm;
  g_object_get(win, "page-manager", &orig_pm, NULL);
  EosPageManager *new_pm = EOS_PAGE_MANAGER (eos_page_manager_new ());

  g_assert (orig_pm != new_pm);
  g_object_set(win, "page-manager", new_pm, NULL);
  EosPageManager *test_pm;
  g_object_get(win, "page-manager", &test_pm, NULL);
  g_assert (new_pm == test_pm);

  gtk_widget_destroy (win);
}

static void
test_main_area_widgets_visibility (GApplication *app)
{
  GtkWidget *win = eos_window_new (EOS_APPLICATION (app));
  EosPageManager *pm = eos_window_get_page_manager (EOS_WINDOW (win));
  GtkWidget *main_area = container_find_descendant_with_type (GTK_CONTAINER (win), EOS_TYPE_MAIN_AREA);

  GtkWidget *page0 = gtk_label_new ("no-no");
  GtkWidget *page1 = gtk_label_new ("yes-no");
  GtkWidget *page2 = gtk_label_new ("no-yes");
  GtkWidget *page3 = gtk_label_new ("yes-yes");

  GtkWidget *toolbox1 = gtk_label_new ("toolbox1");
  GtkWidget *toolbox3 = gtk_label_new ("toolbox3");

  gtk_container_add (GTK_CONTAINER (pm), page0);
  gtk_container_add_with_properties (GTK_CONTAINER (pm), page1,
                                     "custom-toolbox-widget", toolbox1,
                                     NULL);
  gtk_container_add_with_properties (GTK_CONTAINER (pm), page2,
                                     "page-actions", TRUE,
                                     NULL);
  gtk_container_add_with_properties (GTK_CONTAINER (pm), page3,
                                     "custom-toolbox-widget", toolbox3,
                                     "page-actions", TRUE,
                                     NULL);

  GtkWidget *tb;
  gboolean actions;
  EosMainArea *ma = EOS_MAIN_AREA (main_area);

  eos_page_manager_set_visible_page (pm, page0);
  tb = eos_main_area_get_toolbox (ma);
  actions = eos_main_area_get_actions (ma);
  g_assert (tb == NULL);
  g_assert (actions == FALSE);

  eos_page_manager_set_visible_page (pm, page1);
  tb = eos_main_area_get_toolbox (ma);
  actions = eos_main_area_get_actions (ma);
  g_assert (tb == toolbox1);
  g_assert (actions == FALSE);

  eos_page_manager_set_visible_page (pm, page2);
  tb = eos_main_area_get_toolbox (ma);
  actions = eos_main_area_get_actions (ma);
  g_assert (tb == NULL);
  g_assert (actions == TRUE);

  eos_page_manager_set_visible_page (pm, page3);
  tb = eos_main_area_get_toolbox (ma);
  actions = eos_main_area_get_actions (ma);
  g_assert (tb == toolbox3);
  g_assert (actions == TRUE);

  gtk_widget_destroy (win);
}

void
add_window_tests (void)
{
  ADD_APP_WINDOW_TEST ("/window/assign-application", test_assign_application);
  ADD_APP_WINDOW_TEST ("/window/application-not-null",
                       test_application_not_null);
  ADD_APP_WINDOW_TEST ("/window/screen-size", test_screen_size);
  ADD_APP_WINDOW_TEST ("/window/has-top-bar", test_has_top_bar);
  ADD_APP_WINDOW_TEST ("/window/has-main-area", test_has_main_area);
  ADD_APP_WINDOW_TEST ("/window/has-default-page-manager",
                       test_has_default_page_manager);
  ADD_APP_WINDOW_TEST ("/window/get-set-page-manager",
                       test_get_set_page_manager);
  ADD_APP_WINDOW_TEST ("/window/prop-page-manager", test_prop_page_manager);
  ADD_APP_WINDOW_TEST ("/window/main-area-widgets-visibility",
                       test_main_area_widgets_visibility);
}