~ubuntu-branches/ubuntu/trusty/unity-scope-home/trusty

« back to all changes in this revision

Viewing changes to tests/unit/test-utils.vala

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Pawel Stolowski, Ubuntu daily release
  • Date: 2013-10-03 05:03:35 UTC
  • mfrom: (1.1.19)
  • Revision ID: package-import@ubuntu.com-20131003050335-lifuqlmbhs2senjl
Tags: 6.8.2+13.10.20131003-0ubuntu1
[ Pawel Stolowski ]
* Disable filter updates if phone factor is not 'desktop'. (LP:
  #1229201)
* Renamed 'Search plugins' filter option of Applications to 'Dash
  plugins'. (LP: #1231556)

[ Ubuntu daily release ]
* Automatic snapshot from revision 175

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011-2013 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
using Unity.Protocol;
 
21
 
 
22
public static bool run_with_timeout (MainLoop ml, uint timeout_ms = 5000)
 
23
{
 
24
  bool timeout_reached = false;
 
25
  var t_id = Timeout.add (timeout_ms, () =>
 
26
  {
 
27
    timeout_reached = true;
 
28
    debug ("Timeout reached");
 
29
    ml.quit ();
 
30
    return false;
 
31
  });
 
32
  
 
33
  ml.run ();
 
34
  
 
35
  if (!timeout_reached) Source.remove (t_id);
 
36
  
 
37
  return !timeout_reached;
 
38
}
 
39
 
 
40
/* A bit of magic to get proper-ish fixture support */
 
41
public interface Fixture : Object
 
42
{
 
43
  class DelegateWrapper
 
44
  {
 
45
    TestDataFunc func;
 
46
    public DelegateWrapper (owned TestDataFunc f) { func = (owned) f; }
 
47
  }
 
48
  
 
49
  public virtual void setup () {}
 
50
  public virtual void teardown () {}
 
51
  
 
52
  [CCode (has_target = false)]
 
53
  public delegate void Callback<T> (T ptr);
 
54
  
 
55
  private static List<DelegateWrapper> _tests;
 
56
  
 
57
  public static unowned TestDataFunc create<F> (Callback<void*> cb)
 
58
  requires (typeof (F).is_a (typeof (Fixture)))
 
59
  {
 
60
    TestDataFunc functor = () =>
 
61
    {
 
62
      var type = typeof (F);
 
63
      var instance = Object.new (type) as Fixture;
 
64
      instance.setup ();
 
65
      cb (instance);
 
66
      instance.teardown ();
 
67
    };
 
68
    unowned TestDataFunc copy = functor;
 
69
    _tests.append (new DelegateWrapper ((owned) functor));
 
70
    return copy;
 
71
  }
 
72
  public static unowned TestDataFunc create_static<F> (Callback<F> cb)
 
73
  {
 
74
    return create<F> ((Callback<void*>) cb);
 
75
  }
 
76
}
 
77
 
 
78
// this will auto-disconnect signals when it goes out of scope
 
79
public class SignalWrapper
 
80
{
 
81
  unowned Object obj;
 
82
  ulong sig_id;
 
83
  
 
84
  public SignalWrapper (Object o, ulong signal_id)
 
85
  {
 
86
    obj = o;
 
87
    sig_id = signal_id;
 
88
  }
 
89
 
 
90
  ~SignalWrapper ()
 
91
  {
 
92
    SignalHandler.disconnect (obj, sig_id);
 
93
  }
 
94
}
 
95
 
 
96
public static ScopeProxy? acquire_test_proxy (string name, string path)
 
97
{
 
98
  var ml = new MainLoop ();
 
99
  ScopeProxy? proxy = null;
 
100
  ScopeProxy.new_from_dbus.begin (name, path, null, (obj, res) =>
 
101
  {
 
102
    try
 
103
    {
 
104
      proxy = ScopeProxy.new_from_dbus.end (res);
 
105
    }
 
106
    catch (Error e) {}
 
107
    ml.quit ();
 
108
  });
 
109
  assert (run_with_timeout (ml));
 
110
  return proxy;
 
111
}
 
112
 
 
113
public static void wait_for_synchronization (Dee.Model model)
 
114
{
 
115
  var shared_model = model as Dee.SharedModel;
 
116
  if (shared_model == null) return;
 
117
  
 
118
  if (shared_model.is_synchronized ()) return;
 
119
  SignalWrapper[] signals = {};
 
120
  var ml = new MainLoop ();
 
121
  
 
122
  signals += new SignalWrapper (shared_model, 
 
123
                                shared_model.notify["synchronized"].connect (() =>
 
124
                                {
 
125
                                  ml.quit ();
 
126
                                }));
 
127
  
 
128
  run_with_timeout (ml);
 
129
}
 
130
 
 
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)
 
136
{
 
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,
 
145
                            null,
 
146
                            (obj, res) =>
 
147
                            {
 
148
                              try
 
149
                              {
 
150
                                channel_id = proxy.open_channel.end (res, out real_model);
 
151
                                if (wait_for_sync)
 
152
                                {
 
153
                                  wait_for_synchronization (real_model);
 
154
                                }
 
155
                                ml.quit ();
 
156
                              }
 
157
                              catch (Error err)
 
158
                              {
 
159
                                ml.quit ();
 
160
                              }
 
161
                            });
 
162
  
 
163
  assert (run_with_timeout (ml));
 
164
  assert (channel_id != null);
 
165
  model = real_model as Dee.SerializableModel;
 
166
  return channel_id;
 
167
}
 
168
 
 
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)
 
173
{
 
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),
 
178
                      null,
 
179
                      (obj, res) =>
 
180
                      {
 
181
                        try
 
182
                        {
 
183
                          reply_dict = proxy.search.end (res);
 
184
                        }
 
185
                        catch (Error err) {}
 
186
                        ml.quit ();
 
187
                      });
 
188
  
 
189
  bool got_search_signal = false;
 
190
  
 
191
  assert (run_with_timeout (ml, 10000));
 
192
  assert (reply_dict != null);
 
193
  return reply_dict;
 
194
}