2
* Copyright (C) 2011 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by Michal Hruby <michal.hruby@canonical.com>
22
static bool remote_scope_test = false;
23
public static int main (string[] args)
25
if ("--with-remote-scope" in args) remote_scope_test = true;
27
if (remote_scope_test)
29
Environment.set_variable ("LIBUNITY_LENS_DIRECTORY",
30
Config.TESTDIR + "/data", true);
34
Environment.set_variable ("LIBUNITY_LENS_DIRECTORY",
35
Config.TESTDIR, true);
40
Test.add_data_func ("/Unit/Lens/Export", test_lens_export);
41
if (remote_scope_test)
43
Test.add_data_func ("/Integration/RemoteScope/Initialize", test_remote_scope_init);
45
Test.add_data_func ("/Unit/Lens/Search", test_lens_search);
46
Test.add_data_func ("/Unit/LocalScope/Initialize", test_local_scope_init);
47
Test.add_data_func ("/Unit/LocalScope/SearchChanged", test_local_scope_search);
48
Test.add_data_func ("/Unit/Lens/ReturnAfterScopeFinish", test_lens_return_after_scope_finish);
49
Test.add_data_func ("/Unit/Lens/TwoSearches", test_lens_two_searches);
56
static Unity.Lens exported_lens;
57
static bool name_owned = false;
59
public static void test_lens_export ()
61
// register us a name on the bus
62
Bus.own_name (BusType.SESSION, "com.canonical.Unity.Lens.Test", 0,
63
() => {}, () => { name_owned = true; },
64
() => { debug ("Name lost"); assert_not_reached (); });
66
var lens = new Unity.Lens ("/com/canonical/Unity/Lens/Test",
68
lens.search_in_global = false;
69
lens.search_hint = "Search hint";
75
static Unity.Scope local_scope;
77
public static void test_local_scope_init ()
79
assert (exported_lens != null);
81
var scope = new Unity.Scope ("/com/canonical/Unity/LocalScope/Test");
84
exported_lens.add_local_scope (scope);
87
public static void test_remote_scope_init ()
89
assert (exported_lens != null);
91
bool scope_up = false;
93
// the remote scope doesn't have a dbus service file installed, so we
94
// expect that something (dbus-test-runner) started it already
95
var ml = new MainLoop ();
96
uint watch_id = Bus.watch_name (BusType.SESSION,
97
"com.canonical.Unity.Scope0.Test", 0,
98
() => { scope_up = true; ml.quit (); },
99
() => { scope_up = false; });
101
Timeout.add (2000, () => { ml.quit (); return false; });
104
assert (scope_up == true);
107
// we need to wait a bit more to connect to the proxy
108
// FIXME: find a better way to do this
109
ml = new MainLoop ();
110
Timeout.add (500, () => { ml.quit (); return false; });
113
Bus.unwatch_name (watch_id);
114
// should be still up
115
assert (scope_up == true);
118
private static void call_lens_search (string search_string, TestDataFunc? cb = null)
120
var bus = Bus.get_sync (BusType.SESSION);
122
var vb = new VariantBuilder (new VariantType ("(sa{sv})"));
123
vb.add ("s", search_string);
124
vb.open (new VariantType ("a{sv}"));
127
bus.call.begin ("com.canonical.Unity.Lens.Test",
128
"/com/canonical/Unity/Lens/Test",
129
"com.canonical.Unity.Lens",
140
var reply = bus.call.end (res);
141
if (cb != null) cb ();
145
warning ("%s", e.message);
150
public static void test_lens_search ()
152
var ml = new MainLoop ();
153
// make sure we got response from own_name, so we can send ourselves
154
// a dbus method call
157
if (name_owned) ml.quit ();
163
call_lens_search ("foo");
166
public static void test_local_scope_search ()
168
assert (local_scope != null);
170
var ml = new MainLoop ();
171
bool got_search_changed = false;
172
ulong sig_id = local_scope.search_changed.connect ((lens_search) =>
174
assert (lens_search.search_string == "foo");
175
got_search_changed = true;
176
lens_search.finished ();
180
// wait for the signal or timeout
181
Timeout.add (1000, () => { ml.quit (); return false; });
184
assert (got_search_changed == true);
185
SignalHandler.disconnect (local_scope, sig_id);
188
private static void flush_bus ()
190
var bus = Bus.get_sync (BusType.SESSION);
193
var ml = new MainLoop ();
194
Idle.add (() => { ml.quit (); return false; });
196
// this should flush the dbus method calls
199
public static void test_lens_return_after_scope_finish ()
201
assert (local_scope != null);
203
var ml = new MainLoop ();
204
bool got_search_changed = false;
205
bool finish_called = false;
207
TestDataFunc cb = () =>
211
ulong sig_id = local_scope.search_changed.connect ((lens_search) =>
213
got_search_changed = true;
214
Timeout.add (750, () =>
216
finish_called = true;
217
lens_search.finished ();
222
// we want to make sure the Search DBus call doesn't return before we
223
// call finished on the LensSearch instance
224
call_lens_search ("qoo", cb);
225
Timeout.add (5000, () => { ml.quit (); return false; });
228
assert (got_search_changed == true);
229
assert (finish_called == true);
231
SignalHandler.disconnect (local_scope, sig_id);
234
public static void test_lens_two_searches ()
236
assert (local_scope != null);
238
var ml = new MainLoop ();
239
Cancellable? canc1 = null;
240
Cancellable? canc2 = null;
241
bool got_search_changed = false;
242
uint finish_calls = 0;
244
ulong sig_id = local_scope.search_changed.connect ((lens_search, search_type, cancellable) =>
246
got_search_changed = true;
247
switch (lens_search.search_string)
249
case "foo1": canc1 = cancellable; break;
250
case "foo2": canc2 = cancellable; break;
251
default: assert_not_reached ();
254
var timer_id = Timeout.add (1000, () =>
257
lens_search.finished ();
259
if (finish_calls == 2) ml.quit ();
267
var reply_ml = new MainLoop ();
269
TestDataFunc foo1_finished_cb = () =>
272
if (++replies == 2) reply_ml.quit ();
274
TestDataFunc foo2_finished_cb = () =>
277
if (++replies == 2) reply_ml.quit ();
280
// we dont want to wait indefinitely
281
var bad_timer = Timeout.add (2000, () => { assert_not_reached (); });
282
call_lens_search ("foo1", foo1_finished_cb);
285
ml = new MainLoop ();
286
call_lens_search ("foo2", foo2_finished_cb);
289
Source.remove (bad_timer);
291
assert (canc1 != null);
292
assert (canc2 != null);
294
assert (canc1.is_cancelled () == true);
295
assert (canc2.is_cancelled () == false);
299
// the timers are still running and we need to wait for the replies
302
// make sure the first search finished earlier that the second
303
assert (order == "12");
305
SignalHandler.disconnect (local_scope, sig_id);