summaryrefslogtreecommitdiff
path: root/tests/lib/test-case-helper.c
blob: 0011b37c566d3ddbb96ec1926851d6a2c0c1f488 (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
/* test-case-helper.c
 *
 * Copyright © 2013 Intel Corporation
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 *     Simon McVittie <simon.mcvittie@collabora.co.uk>
 */

#include "folks-test-internal.h"

#include <glib.h>
#include <glib-object.h>
#include <dbus/dbus.h>

typedef struct {
    gpointer self;
    FolksTestCaseTestMethod test;
} FolksTestCaseWeakMethod;

static void
folks_test_case_weak_method_setup (gpointer fixture G_GNUC_UNUSED,
    gconstpointer test_data)
{
  const FolksTestCaseWeakMethod *wm = test_data;

  g_return_if_fail (wm->self != NULL);
  g_return_if_fail (FOLKS_IS_TEST_CASE (wm->self));

  folks_test_case_set_up (wm->self);
}

static void
folks_test_case_weak_method_test (gpointer fixture G_GNUC_UNUSED,
    gconstpointer test_data)
{
  const FolksTestCaseWeakMethod *wm = test_data;

  g_return_if_fail (wm->self != NULL);
  g_return_if_fail (FOLKS_IS_TEST_CASE (wm->self));

  wm->test (wm->self);
}

static void
folks_test_case_weak_method_teardown (gpointer fixture G_GNUC_UNUSED,
    gconstpointer test_data)
{
  const FolksTestCaseWeakMethod *wm = test_data;

  g_return_if_fail (wm->self != NULL);
  g_return_if_fail (FOLKS_IS_TEST_CASE (wm->self));

  folks_test_case_tear_down (wm->self);
}

GTestCase *
folks_test_case_add_test_helper (FolksTestCase *self,
    const gchar *name,
    FolksTestCaseTestMethod test,
    void *test_target)
{
  FolksTestCaseWeakMethod *wm;

  g_return_if_fail (self == (FolksTestCase *) test_target);

  /* This will never be freed, so make sure not to hold references. */
  wm = g_new0 (FolksTestCaseWeakMethod, 1);
  wm->self = self;
  wm->test = test;
  g_object_add_weak_pointer (G_OBJECT (self), &wm->self);

  return g_test_create_case (name,
      0,
      wm,
      folks_test_case_weak_method_setup,
      folks_test_case_weak_method_test,
      folks_test_case_weak_method_teardown);
}

void
_folks_test_case_dbus_1_set_no_exit_on_disconnect (void)
{
  DBusConnection *conn = dbus_bus_get (DBUS_BUS_SESSION, NULL);

  if (conn != NULL)
    {
      dbus_connection_set_exit_on_disconnect (conn, FALSE);
      dbus_connection_unref (conn);
    }
}