2
* Copyright (C) 2011-2013 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
public static bool run_with_timeout (MainLoop ml, uint timeout_ms = 5000)
24
bool timeout_reached = false;
25
var t_id = Timeout.add (timeout_ms, () =>
27
timeout_reached = true;
28
debug ("Timeout reached");
35
if (!timeout_reached) Source.remove (t_id);
37
return !timeout_reached;
40
/* A bit of magic to get proper-ish fixture support */
41
public interface Fixture : Object
46
public DelegateWrapper (owned TestDataFunc f) { func = (owned) f; }
49
public virtual void setup () {}
50
public virtual void teardown () {}
52
[CCode (has_target = false)]
53
public delegate void Callback<T> (T ptr);
55
private static List<DelegateWrapper> _tests;
57
public static unowned TestDataFunc create<F> (Callback<void*> cb)
58
requires (typeof (F).is_a (typeof (Fixture)))
60
TestDataFunc functor = () =>
62
var type = typeof (F);
63
var instance = Object.new (type) as Fixture;
68
unowned TestDataFunc copy = functor;
69
_tests.append (new DelegateWrapper ((owned) functor));
72
public static unowned TestDataFunc create_static<F> (Callback<F> cb)
74
return create<F> ((Callback<void*>) cb);
78
// this will auto-disconnect signals when it goes out of scope
79
public class SignalWrapper
84
public SignalWrapper (Object o, ulong signal_id)
92
SignalHandler.disconnect (obj, sig_id);
96
public static ScopeProxy? acquire_test_proxy (string name, string path)
98
var ml = new MainLoop ();
99
ScopeProxy? proxy = null;
100
ScopeProxy.new_from_dbus.begin (name, path, null, (obj, res) =>
104
proxy = ScopeProxy.new_from_dbus.end (res);
109
assert (run_with_timeout (ml));
113
public static void wait_for_synchronization (Dee.Model model)
115
var shared_model = model as Dee.SharedModel;
116
if (shared_model == null) return;
118
if (shared_model.is_synchronized ()) return;
119
SignalWrapper[] signals = {};
120
var ml = new MainLoop ();
122
signals += new SignalWrapper (shared_model,
123
shared_model.notify["synchronized"].connect (() =>
128
run_with_timeout (ml);
131
public static string open_channel (ScopeProxy proxy,
132
ChannelType channel_type,
133
out Dee.SerializableModel model,
134
bool wait_for_sync = false,
135
ChannelFlags flags = 0)
137
string? channel_id = null;
138
Dee.Model? real_model = null;
139
var ml = new MainLoop ();
140
/* Need to use PRIVATE channel, cause standard SharedModel won't
141
* synchronize properly when trying to connect to the model
142
* from the same process (/bus address) */
143
proxy.open_channel.begin (channel_type,
144
flags | ChannelFlags.PRIVATE,
150
channel_id = proxy.open_channel.end (res, out real_model);
153
wait_for_synchronization (real_model);
163
assert (run_with_timeout (ml));
164
assert (channel_id != null);
165
model = real_model as Dee.SerializableModel;
169
public static HashTable<string, Variant> perform_search (
170
ScopeProxy proxy, string channel_id, string query,
171
HashTable<string, Variant>? hints = null,
172
Dee.SerializableModel? model = null)
174
var ml = new MainLoop ();
175
HashTable<string, Variant>? reply_dict = null;
176
proxy.search.begin (channel_id, query,
177
hints ?? new HashTable<string, Variant> (null, null),
183
reply_dict = proxy.search.end (res);
189
bool got_search_signal = false;
191
assert (run_with_timeout (ml, 10000));
192
assert (reply_dict != null);