~kamstrup/libunity/faves-enumerator

« back to all changes in this revision

Viewing changes to test/vala/test-lens.vala

  • Committer: Tarmac
  • Author(s): Michal Hruby
  • Date: 2011-12-01 11:27:41 UTC
  • mfrom: (87.3.13 libunity)
  • Revision ID: tarmac-20111201112741-9gw2rrydvgqs8kg5
Simplify the way we use DBus method calls, returns and signals.. Fixes: . Reviewed by Mikkel Kamstrup Erlandsen.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by Michal Hruby <michal.hruby@canonical.com>
 
17
 *
 
18
 */
 
19
 
 
20
public class Main
 
21
{
 
22
  static bool remote_scope_test = false;
 
23
  public static int main (string[] args)
 
24
  {
 
25
    if ("--with-remote-scope" in args) remote_scope_test = true;
 
26
 
 
27
    if (remote_scope_test)
 
28
    {
 
29
      Environment.set_variable ("LIBUNITY_LENS_DIRECTORY",
 
30
                                Config.TESTDIR + "/data", true);
 
31
    }
 
32
    else
 
33
    {
 
34
      Environment.set_variable ("LIBUNITY_LENS_DIRECTORY",
 
35
                                Config.TESTDIR, true);
 
36
    }
 
37
 
 
38
    Test.init (ref args);
 
39
 
 
40
    Test.add_data_func ("/Unit/Lens/Export", test_lens_export);
 
41
    if (remote_scope_test)
 
42
    {
 
43
      Test.add_data_func ("/Integration/RemoteScope/Initialize", test_remote_scope_init);
 
44
    }
 
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);
 
50
 
 
51
    Test.run ();
 
52
 
 
53
    return 0;
 
54
  }
 
55
 
 
56
  static Unity.Lens exported_lens;
 
57
  static bool name_owned = false;
 
58
 
 
59
  public static void test_lens_export ()
 
60
  {
 
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 (); });
 
65
 
 
66
    var lens = new Unity.Lens ("/com/canonical/Unity/Lens/Test",
 
67
                               "unity_lens_test");
 
68
    lens.search_in_global = false;
 
69
    lens.search_hint = "Search hint";
 
70
    lens.export ();
 
71
 
 
72
    exported_lens = lens;
 
73
  }
 
74
 
 
75
  static Unity.Scope local_scope;
 
76
 
 
77
  public static void test_local_scope_init ()
 
78
  {
 
79
    assert (exported_lens != null);
 
80
 
 
81
    var scope = new Unity.Scope ("/com/canonical/Unity/LocalScope/Test");
 
82
 
 
83
    local_scope = scope;
 
84
    exported_lens.add_local_scope (scope);
 
85
  }
 
86
 
 
87
  public static void test_remote_scope_init ()
 
88
  {
 
89
    assert (exported_lens != null);
 
90
 
 
91
    bool scope_up = false;
 
92
 
 
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; });
 
100
 
 
101
    Timeout.add (2000, () => { ml.quit (); return false; });
 
102
    ml.run ();
 
103
 
 
104
    assert (scope_up == true);
 
105
 
 
106
    flush_bus ();
 
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; });
 
111
    ml.run ();
 
112
 
 
113
    Bus.unwatch_name (watch_id);
 
114
    // should be still up
 
115
    assert (scope_up == true);
 
116
  }
 
117
 
 
118
  private static void call_lens_search (string search_string, TestDataFunc? cb = null)
 
119
  {
 
120
    var bus = Bus.get_sync (BusType.SESSION);
 
121
 
 
122
    var vb = new VariantBuilder (new VariantType ("(sa{sv})"));
 
123
    vb.add ("s", search_string);
 
124
    vb.open (new VariantType ("a{sv}"));
 
125
    vb.close ();
 
126
 
 
127
    bus.call.begin ("com.canonical.Unity.Lens.Test",
 
128
                    "/com/canonical/Unity/Lens/Test",
 
129
                    "com.canonical.Unity.Lens",
 
130
                    "Search",
 
131
                    vb.end (),
 
132
                    null,
 
133
                    0,
 
134
                    -1,
 
135
                    null,
 
136
                    (obj, res) =>
 
137
    {
 
138
      try
 
139
      {
 
140
        var reply = bus.call.end (res);
 
141
        if (cb != null) cb ();
 
142
      }
 
143
      catch (Error e)
 
144
      {
 
145
        warning ("%s", e.message);
 
146
      }
 
147
    });
 
148
  }
 
149
 
 
150
  public static void test_lens_search ()
 
151
  {
 
152
    var ml = new MainLoop ();
 
153
    // make sure we got response from own_name, so we can send ourselves
 
154
    // a dbus method call
 
155
    Idle.add (() =>
 
156
    {
 
157
      if (name_owned) ml.quit ();
 
158
      return !name_owned;
 
159
    });
 
160
 
 
161
    ml.run ();
 
162
 
 
163
    call_lens_search ("foo");
 
164
  }
 
165
 
 
166
  public static void test_local_scope_search ()
 
167
  {
 
168
    assert (local_scope != null);
 
169
 
 
170
    var ml = new MainLoop ();
 
171
    bool got_search_changed = false;
 
172
    ulong sig_id = local_scope.search_changed.connect ((lens_search) =>
 
173
    {
 
174
      assert (lens_search.search_string == "foo");
 
175
      got_search_changed = true;
 
176
      lens_search.finished ();
 
177
      ml.quit ();
 
178
    });
 
179
 
 
180
    // wait for the signal or timeout
 
181
    Timeout.add (1000, () => { ml.quit (); return false; });
 
182
    ml.run ();
 
183
 
 
184
    assert (got_search_changed == true);
 
185
    SignalHandler.disconnect (local_scope, sig_id);
 
186
  }
 
187
 
 
188
  private static void flush_bus ()
 
189
  {
 
190
    var bus = Bus.get_sync (BusType.SESSION);
 
191
    bus.flush_sync ();
 
192
 
 
193
    var ml = new MainLoop ();
 
194
    Idle.add (() => { ml.quit (); return false; });
 
195
    ml.run ();
 
196
    // this should flush the dbus method calls
 
197
  }
 
198
 
 
199
  public static void test_lens_return_after_scope_finish ()
 
200
  {
 
201
    assert (local_scope != null);
 
202
 
 
203
    var ml = new MainLoop ();
 
204
    bool got_search_changed = false;
 
205
    bool finish_called = false;
 
206
 
 
207
    TestDataFunc cb = () =>
 
208
    {
 
209
      ml.quit ();
 
210
    };
 
211
    ulong sig_id = local_scope.search_changed.connect ((lens_search) =>
 
212
    {
 
213
      got_search_changed = true;
 
214
      Timeout.add (750, () =>
 
215
      {
 
216
        finish_called = true;
 
217
        lens_search.finished ();
 
218
        return false;
 
219
      });
 
220
    });
 
221
 
 
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; });
 
226
    ml.run ();
 
227
 
 
228
    assert (got_search_changed == true);
 
229
    assert (finish_called == true);
 
230
 
 
231
    SignalHandler.disconnect (local_scope, sig_id);
 
232
  }
 
233
 
 
234
  public static void test_lens_two_searches ()
 
235
  {
 
236
    assert (local_scope != null);
 
237
 
 
238
    var ml = new MainLoop ();
 
239
    Cancellable? canc1 = null;
 
240
    Cancellable? canc2 = null;
 
241
    bool got_search_changed = false;
 
242
    uint finish_calls = 0;
 
243
 
 
244
    ulong sig_id = local_scope.search_changed.connect ((lens_search, search_type, cancellable) =>
 
245
    {
 
246
      got_search_changed = true;
 
247
      switch (lens_search.search_string)
 
248
      {
 
249
        case "foo1": canc1 = cancellable; break;
 
250
        case "foo2": canc2 = cancellable; break;
 
251
        default: assert_not_reached ();
 
252
      }
 
253
 
 
254
      var timer_id = Timeout.add (1000, () =>
 
255
      {
 
256
        finish_calls++;
 
257
        lens_search.finished ();
 
258
 
 
259
        if (finish_calls == 2) ml.quit ();
 
260
        return false;
 
261
      });
 
262
 
 
263
      ml.quit ();
 
264
    });
 
265
 
 
266
    string order = "";
 
267
    var reply_ml = new MainLoop ();
 
268
    int replies = 0;
 
269
    TestDataFunc foo1_finished_cb = () =>
 
270
    {
 
271
      order += "1";
 
272
      if (++replies == 2) reply_ml.quit ();
 
273
    };
 
274
    TestDataFunc foo2_finished_cb = () =>
 
275
    {
 
276
      order += "2";
 
277
      if (++replies == 2) reply_ml.quit ();
 
278
    };
 
279
 
 
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);
 
283
    ml.run ();
 
284
    flush_bus ();
 
285
    ml = new MainLoop ();
 
286
    call_lens_search ("foo2", foo2_finished_cb);
 
287
    ml.run ();
 
288
 
 
289
    Source.remove (bad_timer);
 
290
 
 
291
    assert (canc1 != null);
 
292
    assert (canc2 != null);
 
293
 
 
294
    assert (canc1.is_cancelled () == true);
 
295
    assert (canc2.is_cancelled () == false);
 
296
 
 
297
    flush_bus ();
 
298
 
 
299
    // the timers are still running and we need to wait for the replies
 
300
    reply_ml.run ();
 
301
 
 
302
    // make sure the first search finished earlier that the second
 
303
    assert (order == "12");
 
304
 
 
305
    SignalHandler.disconnect (local_scope, sig_id);
 
306
  }
 
307
}