~ubuntu-branches/ubuntu/vivid/url-dispatcher/vivid-proposed

« back to all changes in this revision

Viewing changes to tests/service-test.cc

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Ted Gould
  • Date: 2014-08-27 02:10:21 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20140827021021-fzeyvmg3kcqk5yxr
Tags: 0.1+14.10.20140827-0ubuntu1
[ Ted Gould ]
* Split update jobs into independent jobs
* Support for unity8-dash URLs even though it's not an application
  (LP: #1361349)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <liburl-dispatcher/url-dispatcher.h>
22
22
#include <libdbustest/dbus-test.h>
23
23
 
 
24
#include "url-db.h"
 
25
 
24
26
class ServiceTest : public ::testing::Test
25
27
{
26
28
        protected:
27
29
                DbusTestService * service = NULL;
28
30
                DbusTestDbusMock * mock = NULL;
 
31
                DbusTestDbusMock * dashmock = NULL;
29
32
                DbusTestDbusMockObject * obj = NULL;
30
33
                DbusTestDbusMockObject * jobobj = NULL;
31
34
                DbusTestProcess * dispatcher = NULL;
35
38
                        g_setenv("UBUNTU_APP_LAUNCH_USE_SESSION", "1", TRUE);
36
39
                        g_setenv("URL_DISPATCHER_DISABLE_RECOVERABLE_ERROR", "1", TRUE);
37
40
 
 
41
                        SetUpDb();
 
42
 
38
43
                        service = dbus_test_service_new(NULL);
39
44
 
40
45
                        dispatcher = dbus_test_process_new(URL_DISPATCHER_SERVICE);
41
46
                        dbus_test_task_set_name(DBUS_TEST_TASK(dispatcher), "Dispatcher");
42
47
                        dbus_test_service_add_task(service, DBUS_TEST_TASK(dispatcher));
43
48
 
 
49
                        /* Upstart Mock */
44
50
                        mock = dbus_test_dbus_mock_new("com.ubuntu.Upstart");
45
51
                        obj = dbus_test_dbus_mock_get_object(mock, "/com/ubuntu/Upstart", "com.ubuntu.Upstart0_6", NULL);
46
52
 
60
66
                                "ret = dbus.ObjectPath('/instance')", /* python */
61
67
                                NULL); /* error */
62
68
 
 
69
                        dbus_test_task_set_name(DBUS_TEST_TASK(mock), "Upstart");
63
70
                        dbus_test_service_add_task(service, DBUS_TEST_TASK(mock));
 
71
 
 
72
                        /* Dash Mock */
 
73
                        dashmock = dbus_test_dbus_mock_new("com.canonical.UnityDash");
 
74
 
 
75
                        DbusTestDbusMockObject * fdoobj = dbus_test_dbus_mock_get_object(dashmock, "/unity8_2ddash", "org.freedesktop.Application", NULL);
 
76
                        dbus_test_dbus_mock_object_add_method(dashmock, fdoobj,
 
77
                                                                                                  "Open",
 
78
                                                                                                  G_VARIANT_TYPE("(asa{sv})"),
 
79
                                                                                                  NULL, /* return */
 
80
                                                                                                  "", /* python */
 
81
                                                                                                  NULL); /* error */
 
82
                        dbus_test_task_set_name(DBUS_TEST_TASK(dashmock), "UnityDash");
 
83
                        dbus_test_service_add_task(service, DBUS_TEST_TASK(dashmock));
 
84
 
 
85
                        /* Start your engines! */
64
86
                        dbus_test_service_start_tasks(service);
65
87
 
66
88
                        bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
76
98
 
77
99
                        g_clear_object(&dispatcher);
78
100
                        g_clear_object(&mock);
 
101
                        g_clear_object(&dashmock);
79
102
                        g_clear_object(&service);
80
103
 
81
104
                        g_object_unref(bus);
87
110
                                        g_main_iteration(TRUE);
88
111
                                cleartry++;
89
112
                        }
 
113
 
 
114
                        TearDownDb();
90
115
                        return;
91
116
                }
 
117
 
 
118
                void SetUpDb () {
 
119
                        const gchar * cachedir = CMAKE_BINARY_DIR "/service-test-cache";
 
120
                        ASSERT_EQ(0, g_mkdir_with_parents(cachedir, 0700));
 
121
 
 
122
                        g_setenv("XDG_CACHE_HOME", cachedir, TRUE);
 
123
 
 
124
                        sqlite3 * db = url_db_create_database();
 
125
 
 
126
                        GTimeVal time = {0};
 
127
                        time.tv_sec = 5;
 
128
                        url_db_set_file_motification_time(db, "/unity8-dash.url-dispatcher", &time);
 
129
                        url_db_insert_url(db, "/unity8-dash.url-dispatcher", "scope", NULL);
 
130
                        sqlite3_close(db);
 
131
                }
 
132
 
 
133
                void TearDownDb () {
 
134
                        g_spawn_command_line_sync("rm -rf " CMAKE_BINARY_DIR "/service-test-cache", NULL, NULL, NULL, NULL);
 
135
                }
 
136
                
 
137
                static gboolean quit_loop (gpointer ploop) {
 
138
                        g_main_loop_quit((GMainLoop *)ploop);
 
139
                        return FALSE;
 
140
                }
 
141
 
 
142
                void pause (int time) {
 
143
                        GMainLoop * loop = g_main_loop_new(NULL, FALSE);
 
144
                        guint timer = g_timeout_add(time, quit_loop, loop);
 
145
                        g_main_loop_run(loop);
 
146
                        g_main_loop_unref(loop);
 
147
                }
92
148
};
93
149
 
94
150
static void
190
246
        g_strfreev(errorappids);
191
247
}
192
248
 
 
249
void
 
250
focus_signal_cb (GDBusConnection *connection, const gchar *sender_name, const gchar *object_path, const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data)
 
251
{
 
252
        guint * focus_count = (guint *)user_data;
 
253
        *focus_count = *focus_count + 1;
 
254
        g_debug("Focus signaled: %d", *focus_count);
 
255
}
 
256
 
 
257
TEST_F(ServiceTest, Unity8DashTest) {
 
258
        guint focus_count = 0;
 
259
        GDBusConnection * bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
 
260
 
 
261
        guint focus_signal = g_dbus_connection_signal_subscribe(bus,
 
262
                                                                NULL, /* sender */
 
263
                                                                "com.canonical.UbuntuAppLaunch",
 
264
                                                                "UnityFocusRequest",
 
265
                                                                "/",
 
266
                                                                "unity8-dash", /* arg0 */
 
267
                                                                G_DBUS_SIGNAL_FLAGS_NONE,
 
268
                                                                focus_signal_cb,
 
269
                                                                &focus_count,
 
270
                                                                NULL); /* destroy func */
 
271
 
 
272
 
 
273
        DbusTestDbusMockObject * fdoobj = dbus_test_dbus_mock_get_object(dashmock, "/unity8_2ddash", "org.freedesktop.Application", NULL);
 
274
        GMainLoop * main = g_main_loop_new(NULL, FALSE);
 
275
 
 
276
        /* Send an invalid URL */
 
277
        url_dispatch_send("scope://foo-bar", simple_cb, main);
 
278
 
 
279
        /* Give it some time to send and reply */
 
280
        g_main_loop_run(main);
 
281
        g_main_loop_unref(main);
 
282
 
 
283
        guint callslen = 0;
 
284
        const DbusTestDbusMockCall * calls = dbus_test_dbus_mock_object_get_method_calls(mock, jobobj, "Start", &callslen, NULL);
 
285
 
 
286
        EXPECT_EQ(0, callslen);
 
287
 
 
288
        callslen = 0;
 
289
        calls = dbus_test_dbus_mock_object_get_method_calls(dashmock, fdoobj, "Open", &callslen, NULL);
 
290
 
 
291
        EXPECT_EQ(1, callslen);
 
292
        EXPECT_TRUE(g_variant_equal(calls[0].params, g_variant_new_parsed("(['scope://foo-bar'], @a{sv} {})")));
 
293
 
 
294
        EXPECT_EQ(1, focus_count);
 
295
 
 
296
        g_dbus_connection_signal_unsubscribe(bus, focus_signal);
 
297
        g_clear_object(&bus);
 
298
}