~midori/midori/trunk

725 by Christian Dywan
Add an extension test
1
/*
2
 Copyright (C) 2009 Christian Dywan <christian@twotoasts.de>
3
4
 This library is free software; you can redistribute it and/or
5
 modify it under the terms of the GNU Lesser General Public
6
 License as published by the Free Software Foundation; either
7
 version 2.1 of the License, or (at your option) any later version.
8
9
 See the file COPYING for the full license text.
10
*/
11
12
#include "midori.h"
5612 by Christian Dywan
Use midori_paths_get_lib_path in extension test
13
#include <glib/gstdio.h>
725 by Christian Dywan
Add an extension test
14
15
const gpointer magic = (gpointer)0xdeadbeef;
16
17
static void
18
extension_activate_cb (MidoriExtension* extension,
19
                       MidoriApp*       app)
20
{
21
    g_object_set_data (G_OBJECT (extension), "activated", magic);
22
}
23
24
static void
25
extension_deactivate_cb (MidoriExtension* extension)
26
{
27
    g_object_set_data (G_OBJECT (extension), "deactivated", magic);
28
}
29
30
static void
31
extension_create (void)
32
{
912 by Christian Dywan
Use a valid app when testing extension activation
33
    MidoriApp* app;
725 by Christian Dywan
Add an extension test
34
    MidoriExtension* extension;
35
5593 by Christian Dywan
Reflect custom app name in instance name
36
    app = midori_app_new (NULL);
725 by Christian Dywan
Add an extension test
37
    extension = g_object_new (MIDORI_TYPE_EXTENSION, NULL);
38
    g_assert (!midori_extension_is_prepared (extension));
39
    g_object_set (extension, "name", "TestExtension",
40
                             "version", "1.0", NULL);
41
    g_assert (!midori_extension_is_prepared (extension));
42
    g_object_set (extension, "description", "Nothing but a test.",
43
                             "authors", "John Doe", NULL);
44
    /* FIXME: We should require to connect to "activate"
45
    g_assert (!midori_extension_is_prepared (extension)); */
46
    g_signal_connect (extension, "activate",
47
                      G_CALLBACK (extension_activate_cb), NULL);
48
    g_assert (midori_extension_is_prepared (extension));
49
    g_assert (!midori_extension_is_active (extension));
912 by Christian Dywan
Use a valid app when testing extension activation
50
    g_signal_emit_by_name (extension, "activate", app);
725 by Christian Dywan
Add an extension test
51
    g_assert (midori_extension_is_active (extension));
912 by Christian Dywan
Use a valid app when testing extension activation
52
    g_assert (midori_extension_get_app (extension) == app);
725 by Christian Dywan
Add an extension test
53
    g_assert (g_object_get_data (G_OBJECT (extension), "activated") == magic);
54
    g_signal_connect (extension, "deactivate",
55
                      G_CALLBACK (extension_deactivate_cb), NULL);
56
    midori_extension_deactivate (extension);
57
    g_assert (!midori_extension_is_active (extension));
58
    g_assert (g_object_get_data (G_OBJECT (extension), "deactivated") == magic);
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
59
    g_object_unref (app);
60
    g_object_unref (extension);
725 by Christian Dywan
Add an extension test
61
}
62
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
63
static MidoriExtension*
64
extension_mock_object (void)
725 by Christian Dywan
Add an extension test
65
{
66
    MidoriExtension* extension;
67
68
    extension = g_object_new (MIDORI_TYPE_EXTENSION,
69
                              "name", "TestExtension",
70
                              "version", "1.0",
71
                              "description", "Nothing but a test.",
72
                              "authors", "John Doe",
73
                              NULL);
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
74
    return extension;
75
}
76
77
static void
1047 by Christian Dywan
Unit test using a string_list setting
78
katze_assert_cmpstrv (gchar** values1,
79
                      gsize   length1,
80
                      gchar** values2,
81
                      gsize   length2)
82
{
83
    gsize i;
84
85
    g_assert ((values1 && values2) || (!values1 && !values2));
86
    /* g_assert_cmpint (length1, ==, length2); */
87
88
    for (i = 0; i < length1; i++)
89
        g_assert_cmpstr (values1[i], ==, values2[i]);
90
}
91
92
static void
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
93
extension_settings (void)
94
{
912 by Christian Dywan
Use a valid app when testing extension activation
95
    MidoriApp* app;
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
96
    MidoriExtension* extension;
97
    gboolean nihilist;
98
    gint age;
99
    const gchar* lastname;
1047 by Christian Dywan
Unit test using a string_list setting
100
    gchar* pet_names[] = {"Tiger", "Bonzo", "Streuner", NULL};
101
    gchar** names;
102
    gsize names_n;
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
103
5593 by Christian Dywan
Reflect custom app name in instance name
104
    app = midori_app_new (NULL);
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
105
    extension = extension_mock_object ();
106
    midori_extension_install_boolean (extension, "nihilist", TRUE);
107
    nihilist = midori_extension_get_boolean (extension, "nihilist");
108
    g_assert (!nihilist);
109
    g_signal_connect (extension, "activate",
110
                      G_CALLBACK (extension_activate_cb), NULL);
912 by Christian Dywan
Use a valid app when testing extension activation
111
    g_signal_emit_by_name (extension, "activate", app);
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
112
    nihilist = midori_extension_get_boolean (extension, "nihilist");
113
    g_assert (nihilist);
114
    midori_extension_set_boolean (extension, "nihilist", FALSE);
115
    nihilist = midori_extension_get_boolean (extension, "nihilist");
116
    g_assert (!nihilist);
117
    midori_extension_deactivate (extension);
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
118
    g_object_unref (extension);
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
119
120
    extension = extension_mock_object ();
121
    midori_extension_install_integer (extension, "age", 88);
122
    age = midori_extension_get_integer (extension, "age");
123
    g_assert_cmpint (age, ==, 0);
124
    g_signal_connect (extension, "activate",
125
                      G_CALLBACK (extension_activate_cb), NULL);
912 by Christian Dywan
Use a valid app when testing extension activation
126
    g_signal_emit_by_name (extension, "activate", app);
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
127
    age = midori_extension_get_integer (extension, "age");
128
    g_assert_cmpint (age, ==, 88);
129
    midori_extension_set_integer (extension, "age", 66);
130
    age = midori_extension_get_integer (extension, "age");
131
    g_assert_cmpint (age, ==, 66);
132
    midori_extension_deactivate (extension);
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
133
    g_object_unref (extension);
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
134
135
    extension = extension_mock_object ();
725 by Christian Dywan
Add an extension test
136
    midori_extension_install_string (extension, "lastname", "Thomas Mann");
137
    lastname = midori_extension_get_string (extension, "lastname");
138
    g_assert_cmpstr (lastname, ==, NULL);
139
    g_signal_connect (extension, "activate",
140
                      G_CALLBACK (extension_activate_cb), NULL);
912 by Christian Dywan
Use a valid app when testing extension activation
141
    g_signal_emit_by_name (extension, "activate", app);
725 by Christian Dywan
Add an extension test
142
    lastname = midori_extension_get_string (extension, "lastname");
143
    g_assert_cmpstr (lastname, ==, "Thomas Mann");
144
    midori_extension_set_string (extension, "lastname", "Theodor Fontane");
145
    lastname = midori_extension_get_string (extension, "lastname");
146
    g_assert_cmpstr (lastname, ==, "Theodor Fontane");
769 by Christian Dywan
Add boolean and integer settings to MidoriExtension, with tests
147
    midori_extension_deactivate (extension);
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
148
    g_object_unref (extension);
1047 by Christian Dywan
Unit test using a string_list setting
149
150
    extension = extension_mock_object ();
151
    midori_extension_install_string_list (extension, "pets", pet_names, 3);
152
    names = midori_extension_get_string_list (extension, "pets", &names_n);
153
    katze_assert_cmpstrv (NULL, 0, names, names_n);
154
    g_strfreev (names);
155
    g_signal_emit_by_name (extension, "activate", app);
156
    names = midori_extension_get_string_list (extension, "pets", &names_n);
157
    katze_assert_cmpstrv (pet_names, 3, names, names_n);
158
    g_strfreev (names);
159
    /* names = g_strsplit ("Carla,Fluffy,Goro,Kitty", ",", 0);
160
    midori_extension_set_string_list (extension, "pets", names, G_MAXSIZE);
161
    g_strfreev (names);
162
    names = midori_extension_get_string_list (extension, "pets", &names_n);
163
    g_assert_cmpint (names_n, ==, 4); */
164
    names = g_strsplit ("Carla,Fluffy,Goro,Kitty", ",", 0);
165
    midori_extension_set_string_list (extension, "pets", names, 2);
166
    g_strfreev (names);
167
    names = midori_extension_get_string_list (extension, "pets", &names_n);
6448.1.17 by Christian Dywan
Free names in extension_settings in extensions test
168
    g_strfreev (names);
1047 by Christian Dywan
Unit test using a string_list setting
169
    g_assert_cmpint (names_n, ==, 2);
170
    midori_extension_deactivate (extension);
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
171
    g_object_unref (extension);
172
    g_object_unref (app);
725 by Christian Dywan
Add an extension test
173
}
174
1403 by Christian Dywan
Implement support for extension_test() to unit test extensions
175
static void
176
extension_activate (gconstpointer data)
177
{
5593 by Christian Dywan
Reflect custom app name in instance name
178
    MidoriApp* app = midori_app_new (NULL);
4172 by Christian Dywan
Fix test issues due to unset settings, version, session
179
    g_object_set (app, "settings", midori_web_settings_new (), NULL);
5357 by Christian Dywan
Move extension loading/ activating out of main for testing
180
    midori_extension_activate (G_OBJECT (data), NULL, TRUE, app);
5374 by Christian Dywan
Introduce Completion API with search and history classes
181
    /* TODO: MidoriCompletion */
1403 by Christian Dywan
Implement support for extension_test() to unit test extensions
182
    g_object_unref (app);
183
}
184
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
185
static GObject*
186
extension_load (const gchar* extension_path, gboolean register_tests)
1403 by Christian Dywan
Implement support for extension_test() to unit test extensions
187
{
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
188
    g_assert (g_file_test (extension_path, G_FILE_TEST_EXISTS) == 1);
189
    gchar* extension_dir = g_path_get_dirname (extension_path);
190
    gchar* filename = g_path_get_basename (extension_path);
191
192
    GObject* extension = midori_extension_load_from_file (extension_dir, filename, FALSE, TRUE);
193
    
194
    if (register_tests)
195
    {
5987 by Christian Dywan
Always set extension "filename" and test paths for multi extensions
196
        if (KATZE_IS_ARRAY (extension))
197
        {
198
            MidoriExtension* extension_item;
199
            KATZE_ARRAY_FOREACH_ITEM (extension_item, KATZE_ARRAY (extension))
200
                if (MIDORI_IS_EXTENSION (extension_item))
201
                {
202
                    gchar* key = katze_object_get_object (extension_item, "key");
203
                    gchar* path = g_strdup_printf ("/extensions/%s/%s", filename, key);
204
                    g_test_add_data_func (path, extension_item, extension_activate);
205
                    g_free (path);
206
                    g_free (key);
207
                }
208
        }
209
        else if (extension != NULL)
1403 by Christian Dywan
Implement support for extension_test() to unit test extensions
210
        {
5357 by Christian Dywan
Move extension loading/ activating out of main for testing
211
            gchar* path = g_strconcat ("/extensions/", filename, NULL);
212
            g_test_add_data_func (path, extension, extension_activate);
213
            g_free (path);
1403 by Christian Dywan
Implement support for extension_test() to unit test extensions
214
        }
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
215
    }
216
217
    g_free (extension_dir);
218
    g_free (filename);
219
    return extension;
1403 by Christian Dywan
Implement support for extension_test() to unit test extensions
220
}
221
5589 by Christian Dywan
Set 10 seconds timeout on app_normal and tab_special tests
222
static void
223
extension_config (void)
224
{
225
    gchar* filename = midori_paths_get_extension_config_dir ("adblock");
7085.1.2 by tuxator at o2
Use g_file_test check instead as it works for both platforms
226
    g_assert (g_file_test (filename, G_FILE_TEST_EXISTS) == 1);
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
227
    g_free (filename);
5589 by Christian Dywan
Set 10 seconds timeout on app_normal and tab_special tests
228
}
229
725 by Christian Dywan
Add an extension test
230
int
231
main (int    argc,
232
      char** argv)
233
{
6411.5.9 by Christian Dywan
Introduce Midori.Test.init with a custom log handler
234
    midori_test_init (&argc, &argv);
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
235
    gchar* extension_path = NULL;
6646.1.2 by Christian Dywan
Execute extension tests separately from CMake
236
    GOptionEntry entries[] = {
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
237
        { "extension", 'e', 0, G_OPTION_ARG_STRING, &extension_path,
6646.1.2 by Christian Dywan
Execute extension tests separately from CMake
238
          "Execute cases defined in extension_init", "EXTENSION" },
239
        { NULL }
240
    };
241
    midori_app_setup (&argc, &argv, entries);
5365 by Christian Dywan
Leave writable folders unset in private/ app mode
242
    midori_paths_init (MIDORI_RUNTIME_MODE_NORMAL, NULL);
5995 by Christian Dywan
Fix unit tests building with WebKit2
243
    #ifndef HAVE_WEBKIT2
3267 by Christian Dywan
Add a dummy cookie jar for extension unit test
244
    soup_session_add_feature_by_type (webkit_get_default_session (),
245
        SOUP_TYPE_COOKIE_JAR);
5995 by Christian Dywan
Fix unit tests building with WebKit2
246
    #endif
725 by Christian Dywan
Add an extension test
247
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
248
    GObject* extension = NULL;
249
    GObject* extension2 = NULL;
250
251
    if (extension_path == NULL)
6646.1.2 by Christian Dywan
Execute extension tests separately from CMake
252
    {
253
        g_test_add_func ("/extensions/create", extension_create);
254
        g_test_add_func ("/extensions/settings", extension_settings);
255
        g_test_add_func ("/extensions/config", extension_config);
256
    }
257
    else
258
    {
259
        g_assert (g_module_supported ());
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
260
        extension = extension_load (extension_path, TRUE);
5357 by Christian Dywan
Move extension loading/ activating out of main for testing
261
        /* We require that extensions can be loaded repeatedly */
7093.1.1 by gue5t
Fix memory leaks and logic errors in extension loading and tests
262
        extension2 = extension_load (extension_path, FALSE);
263
    }
264
265
    int result = g_test_run ();
266
267
    if (extension)
268
    {
269
        midori_extension_deactivate (MIDORI_EXTENSION (extension));
270
        g_object_unref (extension);
271
    }
272
    /* The second copy loaded does not get activated, because no tests are
273
    registered for it */
274
    if (extension2)
275
        g_object_unref (extension2);
276
277
    return result;
725 by Christian Dywan
Add an extension test
278
}