Release 2.1.94
[hildon] / tests / check-hildon-picker-button.c
1 /*
2  * This file is a part of hildon tests
3  *
4  * Copyright (C) 2009 Nokia Corporation, all rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <check.h>
26 #include <gtk/gtkmain.h>
27 #include "test_suites.h"
28 #include "check_utils.h"
29 #include <hildon/hildon.h>
30
31 static HildonButton *button = NULL;
32 static HildonTouchSelector *selector = NULL;
33 static GtkWindow *window = NULL;
34
35 static void
36 fx_setup ()
37 {
38     int argc = 0;
39
40     gtk_init (&argc, NULL);
41
42     window = GTK_WINDOW (hildon_window_new());
43
44     fail_if (!HILDON_IS_WINDOW(window),
45              "hildon-picker-button: Window creation failed.");
46
47     button = HILDON_BUTTON (hildon_picker_button_new (HILDON_SIZE_AUTO,
48                               HILDON_BUTTON_ARRANGEMENT_VERTICAL));
49     selector = HILDON_TOUCH_SELECTOR (hildon_touch_selector_new_text ());
50
51     hildon_touch_selector_append_text (selector, "Row one");
52     hildon_touch_selector_append_text (selector, "Row two");
53     hildon_touch_selector_append_text (selector, "Row three");
54     hildon_touch_selector_append_text (selector, "Row four");
55
56     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button), selector);
57
58     gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (button));
59     gtk_widget_show (GTK_WIDGET (button));
60
61     show_test_window (GTK_WIDGET (window));
62 }
63
64 static void
65 fx_teardown ()
66 {
67     gtk_widget_destroy (GTK_WIDGET (window));
68 }
69
70 /**
71    Purpose: test that programmatic changes in the selector update the
72    value displayed in the button.
73
74    Checks for:
75
76    - Initial value must be empty.
77    - Programmatically selecting an item in the selector should show it in the button.
78    - Updating the contents of a selected row should update the displayed value accordingly.
79
80 */
81 START_TEST (test_hildon_picker_button_value)
82 {
83     const gchar *value;
84     GtkTreeIter iter;
85     GtkTreeModel *model;
86     const gchar *new_value = "Something different";
87
88     /* Test 1: getting initial value. */
89     value = hildon_button_get_value (button);
90     fail_if (strcmp (value, "") != 0,
91              "hildon-picker-button: default value wrong: `%s'",
92              value);
93
94     /* Test 2: Selecting a particular row. */
95     hildon_touch_selector_set_active (selector, 0, 1);
96     value = hildon_button_get_value (button);
97     fail_if (strcmp (value, "Row two") != 0,
98              "hildon-picker-button: switched to second row in the selector, "
99              "but button displays `%s'.", value);
100
101     /* Test 3: Modifying a selected row. */
102     model = hildon_touch_selector_get_model (selector, 0);
103     hildon_touch_selector_get_selected (selector, 0, &iter);
104     gtk_list_store_set (GTK_LIST_STORE (model), &iter,
105                         0, new_value, -1);
106     value = hildon_button_get_value (button);
107     fail_if (strcmp (value, new_value) != 0,
108              "hildon-picker-button: set the currently selected row to `%s' "
109              "but button displays `%s'.", new_value, value);
110
111 }
112 END_TEST
113
114 Suite *create_hildon_picker_button_suite (void)
115 {
116     Suite *s = suite_create ("HildonPickerButton");
117
118     TCase *tc1 = tcase_create ("hildon_picker_button");
119     tcase_add_checked_fixture (tc1, fx_setup, fx_teardown);
120     tcase_add_test (tc1, test_hildon_picker_button_value);
121     suite_add_tcase (s, tc1);
122
123     return s;
124 }