summaryrefslogtreecommitdiff
path: root/tests/lib/test-utils.vala
blob: a6b855c018671737fac8163169b80ac43dae03d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright (C) 2011 Collabora Ltd.
 * Copyright (C) 2012 Philip Withnall
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Travis Reitter <travis.reitter@collabora.com>
 *          Philip Withnall <philip@tecnocode.co.uk>
 *
 */

using Folks;
using GLib;

public class Folks.TestUtils
{
  /* Implemented in C */
  [CCode (cname = "haze_remove_directory",
          cheader_filename = "haze-remove-directory.h")]
  public extern static bool remove_directory_recursively (string path);

  /**
   * Apply an arbitrary fudge factor to the timeout, which might be
   * appropriate for any debuggers or other slow wrappers we're using.
   *
   * @param timeout A timeout in any units
   * @return A revised timeout in the same units
   */
  public static int multiply_timeout (int timeout)
    {
      int result = timeout;

      if (Environment.get_variable ("FOLKS_TEST_VALGRIND") != null)
        {
          result *= 10;
        }

      if (Environment.get_variable ("FOLKS_TEST_CALLGRIND") != null)
        {
          result *= 10;
        }

      return result;
    }

  /**
   * Run //loop// with a timeout. Something should call ``loop.quit()``
   * before the timeout is reached. If not, a fatal error occurs.
   *
   * @param loop A main loop.
   * @param timeout A timeout, initially in seconds, defaulting to long
   *    enough to do something "reasonably fast". It will be adjusted with
   *    multiply_timeout() before use.
   */
  public static void loop_run_with_timeout (MainLoop loop, int timeout = 5)
    {
      var source = new TimeoutSource.seconds (
          TestUtils.multiply_timeout (timeout));
      source.set_callback (() => { error ("Timed out"); });
      source.attach (loop.get_context ());

      loop.run ();

      source.destroy ();
    }

  /**
   * Run //loop// with a timeout. Something should call ``loop.quit()``
   * before the timeout is reached. If not, the timeout callback will do so.
   *
   * This function is a bad idea: we don't want things to run for an arbitrary
   * time. Use loop_run_with_timeout() instead.
   *
   * FIXME: Remove this when it has no more callers.
   *
   * @param loop A main loop.
   * @param timeout A timeout, initially in seconds, defaulting to long
   *    enough to do something "reasonably fast". It will be adjusted with
   *    multiply_timeout() before use.
   */
  [Deprecated (replacement = "loop_run_with_timeout")]
  public static void loop_run_with_non_fatal_timeout (MainLoop loop,
      int timeout = 3)
    {
      var source = new TimeoutSource.seconds (
          TestUtils.multiply_timeout (timeout));
      source.set_callback (() =>
        {
          loop.quit ();
          return false;
        });
      source.attach (loop.get_context ());

      loop.run ();

      source.destroy ();
    }

  /**
   * Compare the content of two {@link LoadableIcon}s for equality.
   *
   * This is in contrast to {@link Icon.equal}, which returns ``false`` for
   * identical icons stored in different subclasses or in different storage
   * locations.
   *
   * This function is particularly useful for tests which compare an avatar set
   * on an {@link AvatarDetails}'s with that object's final avatar, since it may
   * be stored in a location in the avatar cache.
   *
   * @param a the first icon
   * @param b the second icon
   * @param size the size at which to compare the icons
   * @return ``true`` if the instances are equal, ``false`` otherwise
   */
  public static async bool loadable_icons_content_equal (LoadableIcon a,
      LoadableIcon b,
      int icon_size)
    {
      bool retval = false;

      if (a == b)
        return true;

      if (a != null && b != null)
        {
          try
            {
              string a_type;
              var a_input = yield a.load_async (icon_size, null, out a_type);
              string b_type;
              var b_input = yield b.load_async (icon_size, null, out b_type);

              /* arbitrary value */
              size_t bufsize = 2048;
              var a_data = new uint8[bufsize];
              var b_data = new uint8[bufsize];

              /* assume equal until proven otherwise */
              retval = true;
              size_t a_read_size = -1;
              do
                {
                  a_read_size = yield a_input.read_async (a_data);
                  var b_read_size = yield b_input.read_async (b_data);

                  if (a_read_size != b_read_size ||
                      Memory.cmp (a_data, b_data, a_read_size) != 0)
                    {
                      retval = false;
                      break;
                    }
                }
              while (a_read_size > 0);

              yield a_input.close_async ();
              yield b_input.close_async ();
            }
          catch (GLib.Error e1)
            {
              retval = false;
              warning ("Failed to read loadable icon for comparison: %s",
                  e1.message);
            }
        }

      return retval;
    }

  /**
   * Prepare a backend and wait for it to reach quiescence.
   *
   * This will prepare the given {@link Backend} then yield until it reaches
   * quiescence. No timeout is used, so if the backend never reaches quiescence,
   * this function will never return; callers must add their own timeout to
   * avoid this if necessary.
   *
   * When this returns, the backend is guaranteed to be quiescent.
   *
   * @param backend the backend to prepare
   */
  public static async void backend_prepare_and_wait_for_quiescence (
      Backend backend) throws GLib.Error
    {
      var has_yielded = false;
      var signal_id = backend.notify["is-quiescent"].connect ((obj, pspec) =>
        {
          if (has_yielded == true)
            {
              TestUtils.backend_prepare_and_wait_for_quiescence.callback ();
            }
        });

      try
        {
          yield backend.prepare ();

          if (backend.is_quiescent == false)
            {
              has_yielded = true;
              yield;
            }
        }
      finally
        {
          backend.disconnect (signal_id);
          assert (backend.is_quiescent == true);
        }
    }

  /**
   * Prepare an aggregator and wait for it to reach quiescence.
   *
   * This will prepare the given {@link IndividualAggregator} then yield until
   * it reaches quiescence. No timeout is used, so if the aggregator never
   * reaches quiescence, this function will never return; callers must add their
   * own timeout to avoid this if necessary.
   *
   * When this returns, the aggregator is guaranteed to be quiescent.
   *
   * @param aggregator the aggregator to prepare
   */
  public static async void aggregator_prepare_and_wait_for_quiescence (
      IndividualAggregator aggregator) throws GLib.Error
    {
      var has_yielded = false;
      var signal_id = aggregator.notify["is-quiescent"].connect ((obj, pspec) =>
        {
          if (has_yielded == true)
            {
              TestUtils.aggregator_prepare_and_wait_for_quiescence.callback ();
            }
        });

      try
        {
          yield aggregator.prepare ();

          if (aggregator.is_quiescent == false)
            {
              has_yielded = true;
              yield;
            }
        }
      finally
        {
          aggregator.disconnect (signal_id);
          assert (aggregator.is_quiescent == true);
        }
    }

  /**
   * Run a helper executable.
   *
   * @param argv Arguments for the executable. The first is the path of
   *  the executable itself, relative to ${builddir}/tests.
   * @param capture_stdout If non-null, the executable's standard output is
   *  placed here. If null, the executable's standard output goes to the
   *  same place as the test's standard output.
   */
  public static void run_test_helper_sync (string[] argv,
      out string capture_stdout = null) throws GLib.Error
    {
      string execdir;

      if (Environment.get_variable ("FOLKS_TESTS_INSTALLED") != null)
        {
          execdir = BuildConf.PKGLIBEXECDIR + "/tests";
        }
      else
        {
          execdir = BuildConf.ABS_TOP_BUILDDIR + "/tests";
        }

      var argv_ = argv[0:argv.length];
      argv_[0] = execdir + "/" + argv_[0];

      int exit_status = -1;
      Process.spawn_sync (null /* cwd */,
          argv_,
          null /* envp */,
          0 /* flags */,
          null /* child setup */,
          out capture_stdout,
          null /* do not capture stderr */,
          out exit_status);

      Process.check_exit_status (exit_status);
    }

  /**
   * Return the path to a test file that is distributed in the source tarball
   * and, if installed, is installed into ${pkgdatadir}/tests.
   *
   * @param filename A filename relative to ${top_srcdir}/tests
   *  or ${pkgdatadir}/tests (or equivalently, ${datadir}/folks/tests).
   */
  public static string get_source_test_data (string filename)
    {
      if (Environment.get_variable ("FOLKS_TESTS_INSTALLED") != null)
        {
          return BuildConf.PACKAGE_DATADIR + "/tests/" + filename;
        }
      else
        {
          return BuildConf.ABS_TOP_SRCDIR + "/tests/" + filename;
        }
    }

  /**
   * Return the path to a test file that is built by "make"
   * and, if installed, is installed into ${pkgdatadir}/tests.
   *
   * @param filename A filename relative to ${top_builddir}/tests
   *  or ${pkgdatadir}/tests (or equivalently, ${datadir}/folks/tests).
   */
  public static string get_built_test_data (string filename)
    {
      if (Environment.get_variable ("FOLKS_TESTS_INSTALLED") != null)
        {
          return BuildConf.PACKAGE_DATADIR + "/tests/" + filename;
        }
      else
        {
          return BuildConf.ABS_TOP_BUILDDIR + "/tests/" + filename;
        }
    }
}