~ted/ubuntu-app-launch/snap-icon-unbreak

« back to all changes in this revision

Viewing changes to tests/second-exec-test.cc

Updated to failure-is-an-option

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2013 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3, as published
6
 
 * by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authors:
17
 
 *     Ted Gould <ted.gould@canonical.com>
18
 
 */
19
 
 
20
 
#include <gtest/gtest.h>
21
 
#include <gio/gio.h>
22
 
 
23
 
extern "C" {
24
 
#include "../second-exec-core.h"
25
 
#include "upstart-app-launch.h"
26
 
#include "upstart-app-launch-mock.h"
27
 
}
28
 
 
29
 
class SecondExecTest : public ::testing::Test
30
 
{
31
 
        private:
32
 
                GTestDBus * testbus = NULL;
33
 
 
34
 
        protected:
35
 
                std::string last_focus_appid;
36
 
                std::string last_resume_appid;
37
 
                guint resume_timeout = 0;
38
 
 
39
 
        private:
40
 
                static void focus_cb (const gchar * appid, gpointer user_data) {
41
 
                        SecondExecTest * _this = static_cast<SecondExecTest *>(user_data);
42
 
                        _this->last_focus_appid = appid;
43
 
                }
44
 
 
45
 
                static void resume_cb (const gchar * appid, gpointer user_data) {
46
 
                        SecondExecTest * _this = static_cast<SecondExecTest *>(user_data);
47
 
                        _this->last_resume_appid = appid;
48
 
 
49
 
                        if (_this->resume_timeout > 0) {
50
 
                                _this->pause(_this->resume_timeout);
51
 
                        }
52
 
                }
53
 
 
54
 
        protected:
55
 
                virtual void SetUp() {
56
 
                        testbus = g_test_dbus_new(G_TEST_DBUS_NONE);
57
 
                        g_test_dbus_up(testbus);
58
 
 
59
 
                        upstart_app_launch_observer_add_app_focus(focus_cb, this);
60
 
                        upstart_app_launch_observer_add_app_resume(resume_cb, this);
61
 
                }
62
 
                virtual void TearDown() {
63
 
                        upstart_app_launch_observer_delete_app_focus(focus_cb, this);
64
 
                        upstart_app_launch_observer_delete_app_resume(resume_cb, this);
65
 
 
66
 
                        g_test_dbus_down(testbus);
67
 
                        g_object_unref(testbus);
68
 
                }
69
 
 
70
 
                static gboolean pause_helper (gpointer pmainloop) {
71
 
                        g_main_loop_quit((GMainLoop *)pmainloop);
72
 
                        return G_SOURCE_REMOVE;
73
 
                }
74
 
 
75
 
                void pause (guint time) {
76
 
                        if (time > 0) {
77
 
                                GMainLoop * mainloop = g_main_loop_new(NULL, FALSE);
78
 
                                guint timer = g_timeout_add(time, pause_helper, mainloop);
79
 
 
80
 
                                g_main_loop_run(mainloop);
81
 
 
82
 
                                g_source_remove(timer);
83
 
                                g_main_loop_unref(mainloop);
84
 
                        }
85
 
 
86
 
                        while (g_main_pending()) {
87
 
                                g_main_iteration(TRUE);
88
 
                        }
89
 
                }
90
 
};
91
 
 
92
 
TEST_F(SecondExecTest, AppIdTest)
93
 
{
94
 
        ASSERT_TRUE(second_exec("foo", NULL));
95
 
        pause(50); /* Ensure all the events come through */
96
 
        ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
97
 
        ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
98
 
}
99
 
 
100
 
GDBusMessage *
101
 
filter_func_good (GDBusConnection * conn, GDBusMessage * message, gboolean incomming, gpointer user_data)
102
 
{
103
 
        if (!incomming) {
104
 
                return message;
105
 
        }
106
 
 
107
 
        if (g_strcmp0(g_dbus_message_get_path(message), (gchar *)user_data) == 0) {
108
 
                GDBusMessage * reply = g_dbus_message_new_method_reply(message);
109
 
                g_dbus_connection_send_message(conn, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
110
 
                g_object_unref(message);
111
 
                return NULL;
112
 
        }
113
 
 
114
 
        return message;
115
 
}
116
 
 
117
 
TEST_F(SecondExecTest, UrlSendTest)
118
 
{
119
 
        upstart_app_launch_mock_set_primary_pid(getpid());
120
 
 
121
 
        GDBusConnection * session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
122
 
        guint filter = g_dbus_connection_add_filter(session,
123
 
                filter_func_good,
124
 
                (gpointer)"/foo",
125
 
                NULL);
126
 
 
127
 
        ASSERT_TRUE(second_exec("foo", "http://www.test.com"));
128
 
        pause(100); /* Ensure all the events come through */
129
 
 
130
 
        ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
131
 
        ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
132
 
 
133
 
        g_dbus_connection_remove_filter(session, filter);
134
 
        g_object_unref(session);
135
 
}
136
 
 
137
 
TEST_F(SecondExecTest, UrlSendNoObjectTest)
138
 
{
139
 
        upstart_app_launch_mock_set_primary_pid(getpid());
140
 
 
141
 
        ASSERT_TRUE(second_exec("foo", "http://www.test.com"));
142
 
        pause(100); /* Ensure all the events come through */
143
 
 
144
 
        ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
145
 
        ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
146
 
}
147
 
 
148
 
TEST_F(SecondExecTest, UnityTimeoutTest)
149
 
{
150
 
        this->resume_timeout = 100;
151
 
 
152
 
        ASSERT_TRUE(second_exec("foo", NULL));
153
 
        pause(100); /* Ensure all the events come through */
154
 
        ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
155
 
        ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
156
 
}
157
 
 
158
 
TEST_F(SecondExecTest, UnityTimeoutUriTest)
159
 
{
160
 
        this->resume_timeout = 200;
161
 
 
162
 
        ASSERT_TRUE(second_exec("foo", "http://www.test.com"));
163
 
        pause(100); /* Ensure all the events come through */
164
 
        ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
165
 
        ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
166
 
}
167
 
 
168
 
GDBusMessage *
169
 
filter_respawn (GDBusConnection * conn, GDBusMessage * message, gboolean incomming, gpointer user_data)
170
 
{
171
 
        if (g_strcmp0(g_dbus_message_get_member(message), "UnityResumeResponse") == 0) {
172
 
                g_object_unref(message);
173
 
                return NULL;
174
 
        }
175
 
 
176
 
        return message;
177
 
}
178
 
 
179
 
TEST_F(SecondExecTest, UnityLostTest)
180
 
{
181
 
        upstart_app_launch_mock_set_primary_pid(getpid());
182
 
 
183
 
        GDBusConnection * session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
184
 
        guint filter = g_dbus_connection_add_filter(session,
185
 
                filter_respawn,
186
 
                NULL,
187
 
                NULL);
188
 
 
189
 
        guint start = g_get_monotonic_time();
190
 
 
191
 
        ASSERT_TRUE(second_exec("foo", "http://www.test.com"));
192
 
 
193
 
        guint end = g_get_monotonic_time();
194
 
 
195
 
        ASSERT_LT(end - start, 600 * 1000);
196
 
 
197
 
        pause(100); /* Ensure all the events come through */
198
 
        ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
199
 
        ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
200
 
 
201
 
        g_dbus_connection_remove_filter(session, filter);
202
 
        g_object_unref(session);
203
 
}
204
 
 
205