2
* Copyright 2013 Canonical Ltd.
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.
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.
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/>.
17
* Ted Gould <ted.gould@canonical.com>
20
#include <gtest/gtest.h>
24
#include "../second-exec-core.h"
25
#include "upstart-app-launch.h"
26
#include "upstart-app-launch-mock.h"
29
class SecondExecTest : public ::testing::Test
32
GTestDBus * testbus = NULL;
35
std::string last_focus_appid;
36
std::string last_resume_appid;
37
guint resume_timeout = 0;
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;
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;
49
if (_this->resume_timeout > 0) {
50
_this->pause(_this->resume_timeout);
55
virtual void SetUp() {
56
testbus = g_test_dbus_new(G_TEST_DBUS_NONE);
57
g_test_dbus_up(testbus);
59
upstart_app_launch_observer_add_app_focus(focus_cb, this);
60
upstart_app_launch_observer_add_app_resume(resume_cb, this);
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);
66
g_test_dbus_down(testbus);
67
g_object_unref(testbus);
70
static gboolean pause_helper (gpointer pmainloop) {
71
g_main_loop_quit((GMainLoop *)pmainloop);
72
return G_SOURCE_REMOVE;
75
void pause (guint time) {
77
GMainLoop * mainloop = g_main_loop_new(NULL, FALSE);
78
guint timer = g_timeout_add(time, pause_helper, mainloop);
80
g_main_loop_run(mainloop);
82
g_source_remove(timer);
83
g_main_loop_unref(mainloop);
86
while (g_main_pending()) {
87
g_main_iteration(TRUE);
92
TEST_F(SecondExecTest, AppIdTest)
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");
101
filter_func_good (GDBusConnection * conn, GDBusMessage * message, gboolean incomming, gpointer user_data)
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);
117
TEST_F(SecondExecTest, UrlSendTest)
119
upstart_app_launch_mock_set_primary_pid(getpid());
121
GDBusConnection * session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
122
guint filter = g_dbus_connection_add_filter(session,
127
ASSERT_TRUE(second_exec("foo", "http://www.test.com"));
128
pause(100); /* Ensure all the events come through */
130
ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
131
ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
133
g_dbus_connection_remove_filter(session, filter);
134
g_object_unref(session);
137
TEST_F(SecondExecTest, UrlSendNoObjectTest)
139
upstart_app_launch_mock_set_primary_pid(getpid());
141
ASSERT_TRUE(second_exec("foo", "http://www.test.com"));
142
pause(100); /* Ensure all the events come through */
144
ASSERT_STREQ(this->last_focus_appid.c_str(), "foo");
145
ASSERT_STREQ(this->last_resume_appid.c_str(), "foo");
148
TEST_F(SecondExecTest, UnityTimeoutTest)
150
this->resume_timeout = 100;
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");
158
TEST_F(SecondExecTest, UnityTimeoutUriTest)
160
this->resume_timeout = 200;
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");
169
filter_respawn (GDBusConnection * conn, GDBusMessage * message, gboolean incomming, gpointer user_data)
171
if (g_strcmp0(g_dbus_message_get_member(message), "UnityResumeResponse") == 0) {
172
g_object_unref(message);
179
TEST_F(SecondExecTest, UnityLostTest)
181
upstart_app_launch_mock_set_primary_pid(getpid());
183
GDBusConnection * session = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
184
guint filter = g_dbus_connection_add_filter(session,
189
guint start = g_get_monotonic_time();
191
ASSERT_TRUE(second_exec("foo", "http://www.test.com"));
193
guint end = g_get_monotonic_time();
195
ASSERT_LT(end - start, 600 * 1000);
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");
201
g_dbus_connection_remove_filter(session, filter);
202
g_object_unref(session);