aboutsummaryrefslogtreecommitdiff
path: root/tests/check-hildon-scroll-area.c
blob: 1405cfffbe116e7315bdbf1fe5fb749a0d2fb1f2 (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
/*
 * This file is a part of hildon tests
 *
 * Copyright (C) 2006, 2007 Nokia Corporation, all rights reserved.
 *
 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <stdlib.h>
#include <check.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtkcellrenderer.h>
#include <gtk/gtktreeviewcolumn.h>
#include <gtk/gtktreemodel.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtktreeview.h>
#include <gtk/gtkbox.h>
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkfixed.h>
#include <gtk/gtklabel.h>
#include <glib/gprintf.h>
#include "test_suites.h"

#include <hildon/hildon-scroll-area.h>

enum
  {
    COL_NAME = 0,
    COL_AGE,
    NUM_COLS
  } ;

/* -------------------- Fixtures -------------------- */

static void 
fx_setup_default_scroll_area ()
{
  int argc = 0;

  gtk_init(&argc, NULL);
}

/* -------------------- Test cases -------------------- */

/* ------------ Test case for scroll-area --------------*/

/**
 * 
 * Helper function used to create the tests, it creates a treemodel
 * and inserts test data to it. This function it is used in the
 * create_view_and_model function.
 * 
 */
static GtkTreeModel *
create_and_fill_model (void)
{
  GtkListStore  *store;
  GtkTreeIter    iter;
  gint i = 0;  
  store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_UINT);

  for(i=0; i< 20; i++)
    {
      /* Append a row and fill in some data */
      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
                          COL_NAME, "Heinz El-Mann",
                          COL_AGE, 51,
                          -1);
  
      /* append another row and fill in some data */
      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
                          COL_NAME, "Jane Doe",
                          COL_AGE, 23,
                          -1);
  
      /* ... and a third row */
      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
                          COL_NAME, "Joe Bungop",
                          COL_AGE, 91,
                          -1);
    }  
  return GTK_TREE_MODEL (store);
}

/**
 * 
 * Helper function used to create the tests, it creates a treeview and
 * a treemodel, it returns the treeview with the treemodel associated.
 * 
 */
static GtkWidget *
create_view_and_model (void)
{
  GtkTreeViewColumn   *col;
  GtkCellRenderer     *renderer;
  GtkTreeModel        *model;
  GtkWidget           *view;

  view = gtk_tree_view_new ();

  /* --- Column #1 --- */

  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
                                               -1,      
                                               "Name",  
                                               renderer,
                                               "text", COL_NAME,
                                               NULL);

  /* --- Column #2 --- */

  col = gtk_tree_view_column_new();

  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
                                               -1,      
                                               "Age",  
                                               renderer,
                                               "text", COL_AGE,
                                               NULL);

  model = create_and_fill_model ();

  gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);

  g_object_unref (model); /* destroy model automatically with view */

  return view;
}

/**
 * Purpose: Check the construction of the scroll-area
 * Cases considered:
 *    - Create a regular case of construction
 *    - Create a scroll area with a label, it is not a proper case but it is 
 *      returning a proper value
 */
START_TEST (test_create_scroll_area_regular)
{
  GtkWidget *button[2];
  GtkWidget *box;
  GtkWidget *scroll_window;
  GtkWidget *treew;
  GtkWidget *fixed;
  GtkWidget *label;
  gint i;

  /* Test1: Create a regular case of construction*/
  box = gtk_vbox_new(FALSE,0);
  scroll_window = gtk_scrolled_window_new(NULL, NULL);

  treew = create_view_and_model();

  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  
  for(i=0; i<2; i++)
    button[i] = gtk_button_new_with_label("Junk button");

  gtk_box_pack_start_defaults(GTK_BOX(box), button[0]);

  fixed = hildon_scroll_area_new(scroll_window, treew);

  /* Check the return value is a proper gtk_fixed object */
  if (!GTK_FIXED(fixed))
    {
      gtk_widget_destroy (GTK_WIDGET (scroll_window));
      gtk_widget_destroy (GTK_WIDGET (box));
      gtk_widget_destroy (GTK_WIDGET (treew));
      fail("hildon-scroll-area: Creation failed.");
    }

  gtk_box_pack_start_defaults(GTK_BOX(box), fixed);
  gtk_box_pack_start_defaults(GTK_BOX(box), button[1]);

  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll_window), box);

  gtk_widget_destroy (GTK_WIDGET (scroll_window));

  /* Test2: Create a scroll area with a label, it is not a proper case but it is 
     returning a proper value */
  scroll_window = gtk_scrolled_window_new(NULL, NULL);

  label = gtk_label_new("This is a label widget example");
  fixed = hildon_scroll_area_new(scroll_window, label);

  /* Check the return value is a fixed value when we try to create the scroll area with a label */
  if (!GTK_FIXED(fixed)) 
    {
      gtk_widget_destroy (GTK_WIDGET (label));
      gtk_widget_destroy (GTK_WIDGET (scroll_window));
      fail("hildon-scroll-area: Creation with a label did not return a proper fixed object");
    }
  gtk_widget_destroy (GTK_WIDGET (label));
  gtk_widget_destroy (GTK_WIDGET (scroll_window));
}
END_TEST

/**
 * Purpose: Check the construction of the scroll-area
 *          invalid values.
 * Cases considered:
 *    - Create with NULL widgets
 *    - Create with actual invalid widget instead of the scrolled-window
 *    - Create with actual invalid widget instead of the treeview
 */
START_TEST (test_create_scroll_area_invalid)
{
  GtkWidget *fixed;
  GtkWidget *label;
  GtkWidget *treew;
  GtkWidget *scroll_window;

  fixed = hildon_scroll_area_new(NULL, NULL);

  /* Test1: Create with NULL widgets */
  fail_if(fixed != NULL,
          "hildon-scroll-area: Invalid creation did not return a NULL value.");

  treew = create_view_and_model();
  label = gtk_label_new("This is an invalid example widget");
  fixed = hildon_scroll_area_new(label, treew);

  /* Test2: Create with actual invalid widget instead of the scrolled-window */
  if (GTK_FIXED(fixed)) 
    {    
      gtk_widget_destroy (GTK_WIDGET (label));
      gtk_widget_destroy (GTK_WIDGET (treew));
      fail ("hildon-scroll-area: Invalid creation did not return a NULL value when we set an invalid value in the first parameter.");
    }
  
  gtk_widget_destroy (GTK_WIDGET (label));
  gtk_widget_destroy (GTK_WIDGET (treew));

  scroll_window = gtk_scrolled_window_new(NULL, NULL);
  fixed = hildon_scroll_area_new(scroll_window, label);

  /* Test3: Create with actual invalid widget instead of the treeview */
  if (!GTK_FIXED(fixed))
    {      
      gtk_widget_destroy (GTK_WIDGET (scroll_window));
      fail ("hildon-scroll-area: Invalid creation returned a NULL value when we set an invalid value in the second parameter.");
    }
  
  gtk_widget_destroy (GTK_WIDGET (scroll_window));
  
}
END_TEST


/* ---------- Suite creation ---------- */

Suite *create_hildon_scroll_area_suite()
{
  /* Create the suite */
  Suite *s = suite_create("HildonScrollArea");

  /* Create test cases */
  TCase *tc1 = tcase_create("create_scroll_area");

  /* Create test case for scroll-area and add it to the suite */
  tcase_add_checked_fixture(tc1, fx_setup_default_scroll_area, NULL);
  tcase_add_test(tc1, test_create_scroll_area_regular);
  tcase_add_test(tc1, test_create_scroll_area_invalid);
  suite_add_tcase(s, tc1);

  /* Return created suite */
  return s;
}