Release 2.2.0 RC1
[hildon] / tests / check-hildon-scroll-area.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, or (at your option) any later version.
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/gtkliststore.h>
29 #include <gtk/gtkcellrenderer.h>
30 #include <gtk/gtktreeviewcolumn.h>
31 #include <gtk/gtktreemodel.h>
32 #include <gtk/gtkscrolledwindow.h>
33 #include <gtk/gtktreeview.h>
34 #include <gtk/gtkbox.h>
35 #include <gtk/gtkcellrenderertext.h>
36 #include <gtk/gtkbutton.h>
37 #include <gtk/gtkvbox.h>
38 #include <gtk/gtkfixed.h>
39 #include <gtk/gtklabel.h>
40 #include <glib/gprintf.h>
41 #include "test_suites.h"
42
43 #include <hildon/hildon-scroll-area.h>
44
45 enum
46   {
47     COL_NAME = 0,
48     COL_AGE,
49     NUM_COLS
50   } ;
51
52 /* -------------------- Fixtures -------------------- */
53
54 static void 
55 fx_setup_default_scroll_area ()
56 {
57   int argc = 0;
58
59   gtk_init(&argc, NULL);
60 }
61
62 /* -------------------- Test cases -------------------- */
63
64 /* ------------ Test case for scroll-area --------------*/
65
66 /**
67  * 
68  * Helper function used to create the tests, it creates a treemodel
69  * and inserts test data to it. This function it is used in the
70  * create_view_and_model function.
71  * 
72  */
73 static GtkTreeModel *
74 create_and_fill_model (void)
75 {
76   GtkListStore  *store;
77   GtkTreeIter    iter;
78   gint i = 0;  
79   store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_UINT);
80
81   for(i=0; i< 20; i++)
82     {
83       /* Append a row and fill in some data */
84       gtk_list_store_append (store, &iter);
85       gtk_list_store_set (store, &iter,
86                           COL_NAME, "Heinz El-Mann",
87                           COL_AGE, 51,
88                           -1);
89   
90       /* append another row and fill in some data */
91       gtk_list_store_append (store, &iter);
92       gtk_list_store_set (store, &iter,
93                           COL_NAME, "Jane Doe",
94                           COL_AGE, 23,
95                           -1);
96   
97       /* ... and a third row */
98       gtk_list_store_append (store, &iter);
99       gtk_list_store_set (store, &iter,
100                           COL_NAME, "Joe Bungop",
101                           COL_AGE, 91,
102                           -1);
103     }  
104   return GTK_TREE_MODEL (store);
105 }
106
107 /**
108  * 
109  * Helper function used to create the tests, it creates a treeview and
110  * a treemodel, it returns the treeview with the treemodel associated.
111  * 
112  */
113 static GtkWidget *
114 create_view_and_model (void)
115 {
116   GtkTreeViewColumn   *col;
117   GtkCellRenderer     *renderer;
118   GtkTreeModel        *model;
119   GtkWidget           *view;
120
121   view = gtk_tree_view_new ();
122
123   /* --- Column #1 --- */
124
125   renderer = gtk_cell_renderer_text_new ();
126   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
127                                                -1,      
128                                                "Name",  
129                                                renderer,
130                                                "text", COL_NAME,
131                                                NULL);
132
133   /* --- Column #2 --- */
134
135   col = gtk_tree_view_column_new();
136
137   renderer = gtk_cell_renderer_text_new ();
138   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
139                                                -1,      
140                                                "Age",  
141                                                renderer,
142                                                "text", COL_AGE,
143                                                NULL);
144
145   model = create_and_fill_model ();
146
147   gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
148
149   g_object_unref (model); /* destroy model automatically with view */
150
151   return view;
152 }
153
154 /**
155  * Purpose: Check the construction of the scroll-area
156  * Cases considered:
157  *    - Create a regular case of construction
158  *    - Create a scroll area with a label, it is not a proper case but it is 
159  *      returning a proper value
160  */
161 START_TEST (test_create_scroll_area_regular)
162 {
163   GtkWidget *button[2];
164   GtkWidget *box;
165   GtkWidget *scroll_window;
166   GtkWidget *treew;
167   GtkWidget *fixed;
168   GtkWidget *label;
169   gint i;
170
171   /* Test1: Create a regular case of construction*/
172   box = gtk_vbox_new(FALSE,0);
173   scroll_window = gtk_scrolled_window_new(NULL, NULL);
174
175   treew = create_view_and_model();
176
177   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
178                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
179   
180   for(i=0; i<2; i++)
181     button[i] = gtk_button_new_with_label("Junk button");
182
183   gtk_box_pack_start_defaults(GTK_BOX(box), button[0]);
184
185   fixed = hildon_scroll_area_new(scroll_window, treew);
186
187   /* Check the return value is a proper gtk_fixed object */
188   if (!GTK_FIXED(fixed))
189     {
190       gtk_widget_destroy (GTK_WIDGET (scroll_window));
191       gtk_widget_destroy (GTK_WIDGET (box));
192       gtk_widget_destroy (GTK_WIDGET (treew));
193       fail("hildon-scroll-area: Creation failed.");
194     }
195
196   gtk_box_pack_start_defaults(GTK_BOX(box), fixed);
197   gtk_box_pack_start_defaults(GTK_BOX(box), button[1]);
198
199   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll_window), box);
200
201   gtk_widget_destroy (GTK_WIDGET (scroll_window));
202
203   /* Test2: Create a scroll area with a label, it is not a proper case but it is 
204      returning a proper value */
205   scroll_window = gtk_scrolled_window_new(NULL, NULL);
206
207   label = gtk_label_new("This is a label widget example");
208   fixed = hildon_scroll_area_new(scroll_window, label);
209
210   /* Check the return value is a fixed value when we try to create the scroll area with a label */
211   if (!GTK_FIXED(fixed)) 
212     {
213       gtk_widget_destroy (GTK_WIDGET (label));
214       gtk_widget_destroy (GTK_WIDGET (scroll_window));
215       fail("hildon-scroll-area: Creation with a label did not return a proper fixed object");
216     }
217   gtk_widget_destroy (GTK_WIDGET (label));
218   gtk_widget_destroy (GTK_WIDGET (scroll_window));
219 }
220 END_TEST
221
222 /**
223  * Purpose: Check the construction of the scroll-area
224  *          invalid values.
225  * Cases considered:
226  *    - Create with NULL widgets
227  *    - Create with actual invalid widget instead of the scrolled-window
228  *    - Create with actual invalid widget instead of the treeview
229  */
230 START_TEST (test_create_scroll_area_invalid)
231 {
232   GtkWidget *fixed;
233   GtkWidget *label;
234   GtkWidget *treew;
235   GtkWidget *scroll_window;
236
237   fixed = hildon_scroll_area_new(NULL, NULL);
238
239   /* Test1: Create with NULL widgets */
240   fail_if(fixed != NULL,
241           "hildon-scroll-area: Invalid creation did not return a NULL value.");
242
243   treew = create_view_and_model();
244   label = gtk_label_new("This is an invalid example widget");
245   fixed = hildon_scroll_area_new(label, treew);
246
247   /* Test2: Create with actual invalid widget instead of the scrolled-window */
248   if (GTK_FIXED(fixed)) 
249     {    
250       gtk_widget_destroy (GTK_WIDGET (label));
251       gtk_widget_destroy (GTK_WIDGET (treew));
252       fail ("hildon-scroll-area: Invalid creation did not return a NULL value when we set an invalid value in the first parameter.");
253     }
254   
255   gtk_widget_destroy (GTK_WIDGET (label));
256   gtk_widget_destroy (GTK_WIDGET (treew));
257
258   scroll_window = gtk_scrolled_window_new(NULL, NULL);
259   fixed = hildon_scroll_area_new(scroll_window, label);
260
261   /* Test3: Create with actual invalid widget instead of the treeview */
262   if (!GTK_FIXED(fixed))
263     {      
264       gtk_widget_destroy (GTK_WIDGET (scroll_window));
265       fail ("hildon-scroll-area: Invalid creation returned a NULL value when we set an invalid value in the second parameter.");
266     }
267   
268   gtk_widget_destroy (GTK_WIDGET (scroll_window));
269   
270 }
271 END_TEST
272
273
274 /* ---------- Suite creation ---------- */
275
276 Suite *create_hildon_scroll_area_suite()
277 {
278   /* Create the suite */
279   Suite *s = suite_create("HildonScrollArea");
280
281   /* Create test cases */
282   TCase *tc1 = tcase_create("create_scroll_area");
283
284   /* Create test case for scroll-area and add it to the suite */
285   tcase_add_checked_fixture(tc1, fx_setup_default_scroll_area, NULL);
286   tcase_add_test(tc1, test_create_scroll_area_regular);
287   tcase_add_test(tc1, test_create_scroll_area_invalid);
288   suite_add_tcase(s, tc1);
289
290   /* Return created suite */
291   return s;
292 }