Adding some clean stuff for tests/. Removing old out crap.
[hildon] / tests / check-hildon-window.c
1 /*
2  * This file is a part of hildon tests
3  *
4  * Copyright (C) 2006, 2007 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <stdlib.h>
26 #include <check.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkhbox.h>
29 #include <gtk/gtkentry.h>
30 #include <gtk/gtkviewport.h>
31 #include <gtk/gtkscrolledwindow.h>
32 #include "test_suites.h"
33 #include "check_utils.h"
34
35 #include "hildon-window.h"
36
37
38 /* -------------------- Fixtures -------------------- */
39
40 static HildonWindow *window = NULL;
41
42 static void
43 fx_setup_default_window ()
44 {
45   int argc = 0;
46   gtk_init(&argc, NULL);
47
48   window = HILDON_WINDOW(hildon_window_new());
49   /* Check window object has been created properly */
50   fail_if(!HILDON_IS_WINDOW(window),
51           "hildon-window: Creation failed.");
52
53   show_all_test_window(GTK_WIDGET(window));
54 }
55
56 static void
57 fx_teardown_default_window ()
58 {
59   gtk_widget_destroy(GTK_WIDGET(window));
60 }
61
62
63 /* -------------------- Test cases -------------------- */
64
65 /* ----- Test case for add_with_scrollbar -----*/
66
67 /**
68  * Purpose: Test regular usage
69  * Cases considered:
70  *    - Add a non viewport child
71  *    - Add a viewport child
72  */
73 START_TEST (test_add_with_scrollbar_regular)
74 {
75   GtkWidget *box;
76   GtkWidget *scrolled_window;
77   GtkWidget *viewport;
78   GList *children, *first;
79
80   /* Test1: Check addition of a non viewport child */
81   box = gtk_hbox_new(FALSE, 0);
82   hildon_window_add_with_scrollbar(window, box);
83
84   show_all_test_window(GTK_WIDGET(window));
85
86   children = gtk_container_get_children(GTK_CONTAINER(window));
87   first = g_list_first(children);
88   fail_if(first == NULL,
89           "hildon-window: Added a hbox with scrollbar, but window has not children");
90   fail_if (!GTK_IS_SCROLLED_WINDOW(first->data),
91            "hildon-window: Added a hbox with scrollbar, but retrieved child is not a scrolled window");
92   scrolled_window = GTK_WIDGET(first->data);
93   g_list_free(children);
94   children = gtk_container_get_children(GTK_CONTAINER(scrolled_window));
95   first = g_list_first(children);
96   fail_if(first == NULL,
97           "hildon-window: Added a hbox with scrollbar, but scrolled window has not children");
98   fail_if (!GTK_IS_VIEWPORT(first->data),
99            "hildon-window: Added a hbox with scrollbar, but retrieved child from the scrolled window is not a viewport");  
100   viewport = GTK_WIDGET(first->data);
101   g_list_free(children);
102   children = gtk_container_get_children(GTK_CONTAINER(viewport));
103   first = g_list_first(children);
104   fail_if(first == NULL,
105           "hildon-window: Added a hbox with scrollbar, but viewport child of the scrolled window has not children");
106   fail_if (!GTK_IS_HBOX(first->data),
107            "hildon-window: Added a hbox with scrollbar, but retrieved child from the viewport of the scrolled window is not a hbox");
108   g_list_free(children);
109   gtk_container_remove(GTK_CONTAINER(window), scrolled_window);
110
111   /* Test2: Check addition of a viewport child */
112   box = gtk_hbox_new(FALSE, 0);
113   viewport = gtk_viewport_new(GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 100, 10, 10, 1000)),
114                               GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 100, 10, 10, 1000)));
115   gtk_container_add(GTK_CONTAINER(viewport), box);
116   hildon_window_add_with_scrollbar(window, viewport);
117
118   show_all_test_window(GTK_WIDGET(window));
119
120   children = gtk_container_get_children(GTK_CONTAINER(window));
121   first = g_list_first(children);
122   fail_if(first == NULL,
123           "hildon-window: Added a hbox inside a viewport with scrollbar, but window has not children");
124   fail_if (!GTK_IS_SCROLLED_WINDOW(first->data),
125            "hildon-window: Added a hbox inside a viewport with scrollbar, but retrieved child is not a scrolled window");
126   scrolled_window = GTK_WIDGET(first->data);
127   g_list_free(children);
128   children = gtk_container_get_children(GTK_CONTAINER(scrolled_window));
129   first = g_list_first(children);
130   fail_if(first == NULL,
131           "hildon-window: Added a hbox inside a viewportwith scrollbar, but scrolled window has not children");
132   fail_if (!GTK_IS_VIEWPORT(first->data),
133            "hildon-window: Added a hbox inside a viewport with scrollbar, but retrieved child from the scrolled window is not a viewport");
134   viewport = GTK_WIDGET(first->data);
135   g_list_free(children);
136   children = gtk_container_get_children(GTK_CONTAINER(viewport));
137   first = g_list_first(children);
138   fail_if(first == NULL,
139           "hildon-window: Added a hbox inside a viewport with scrollbar, but viewport child of the scrolled window has not children");
140   fail_if (!GTK_IS_HBOX(first->data),
141               "hildon-window: Added a hbox inside a viewport with scrollbar, but retrieved child from the viewport of the scrolled window is not a hbox");
142   g_list_free(children);
143   gtk_container_remove(GTK_CONTAINER(window), scrolled_window);
144 }
145 END_TEST
146
147 /**
148  * Purpose: Check invalid values
149  * Cases considered:
150  *    - Add a NULL child
151  *    - Add a child to a NULL window
152  *    - Add a child with a parent already set
153  */
154 START_TEST (test_add_with_scrollbar_invalid)
155 {
156   GList *children, *first;
157   GtkWidget *hbox;
158   GtkWidget *entry;
159
160   /* Test1: Add NULL object */
161   hildon_window_add_with_scrollbar(window, NULL);
162   children = gtk_container_get_children(GTK_CONTAINER(window));
163   first = g_list_first(children);
164   fail_if (first != NULL,
165               "hildon-window: Addition of a NULL child is allowed");
166   g_list_free(children);
167
168   /* Test2: Add to a NULL window */
169   hildon_window_add_with_scrollbar(NULL, gtk_hbox_new(FALSE, 0));
170
171   /* Test3: Add a widget with a parent already set */
172   hbox = gtk_hbox_new(FALSE, 0);
173   entry = gtk_entry_new();
174   gtk_container_add(GTK_CONTAINER(hbox), entry);
175   hildon_window_add_with_scrollbar(window, entry);
176
177   show_all_test_window(GTK_WIDGET(window));
178
179   children = gtk_container_get_children(GTK_CONTAINER(window));
180   first = g_list_first(children);
181   fail_if (first != NULL,
182               "hildon-window: Addition of a child with parent already set is allowed");
183   g_list_free(children);
184 }
185 END_TEST
186
187 /* ---------- Suite creation ---------- */
188
189 Suite *create_hildon_window_suite()
190 {
191   /* Create the suite */
192   Suite *s = suite_create("HildonWindow");
193
194   /* Create test cases */
195   TCase *tc1 = tcase_create("add_with_scrollbar");
196
197   /* Create test case for add_with_scrollbar and add it to the suite */
198   tcase_add_checked_fixture(tc1, fx_setup_default_window, fx_teardown_default_window);
199   tcase_add_test(tc1, test_add_with_scrollbar_regular);
200   tcase_add_test(tc1, test_add_with_scrollbar_invalid);
201   suite_add_tcase (s, tc1);
202
203   /* Return created suite */
204   return s;
205 }