aboutsummaryrefslogtreecommitdiff
path: root/tests/geoclue-fixture.h
blob: 7e2901870bf00133fdde603609fda9a928976d5e (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
/*
 * Copyright 2013 Canonical Ltd.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "glib-fixture.h"

#include <libdbustest/dbus-test.h>

class GeoclueFixture : public GlibFixture
{
  private:

    typedef GlibFixture super;

    GDBusConnection * bus = nullptr;

  protected:

    DbusTestService * service = nullptr;
    DbusTestDbusMock * mock = nullptr;
    DbusTestDbusMockObject * obj_geo = nullptr;
    DbusTestDbusMockObject * obj_geo_m = nullptr;
    DbusTestDbusMockObject * obj_geo_mc = nullptr;
    DbusTestDbusMockObject * obj_geo_addr = nullptr;
    const std::string timezone_1 = "America/Denver";

    void SetUp ()
    {
      super::SetUp();

      GError * error = nullptr;
      const auto master_path = "/org/freedesktop/Geoclue/Master";
      const auto client_path = "/org/freedesktop/Geoclue/Master/client0";
      GString * gstr = g_string_new (nullptr);

      service = dbus_test_service_new (nullptr);
      mock = dbus_test_dbus_mock_new ("org.freedesktop.Geoclue.Master");

      auto interface = "org.freedesktop.Geoclue.Master";
      obj_geo_m = dbus_test_dbus_mock_get_object (mock, master_path, interface, nullptr);
      g_string_printf (gstr, "ret = '%s'", client_path);
      dbus_test_dbus_mock_object_add_method (mock, obj_geo_m, "Create", nullptr, G_VARIANT_TYPE_OBJECT_PATH, gstr->str, &error);

      interface = "org.freedesktop.Geoclue.MasterClient";
      obj_geo_mc = dbus_test_dbus_mock_get_object (mock, client_path, interface, nullptr);
      dbus_test_dbus_mock_object_add_method (mock, obj_geo_mc, "SetRequirements", G_VARIANT_TYPE("(iibi)"), nullptr, "", &error);
      dbus_test_dbus_mock_object_add_method (mock, obj_geo_mc, "AddressStart", nullptr, nullptr, "", &error);

      interface = "org.freedesktop.Geoclue";
      obj_geo = dbus_test_dbus_mock_get_object (mock, client_path, interface, nullptr);
      dbus_test_dbus_mock_object_add_method (mock, obj_geo, "AddReference", nullptr, nullptr, "", &error);
      g_string_printf (gstr, "ret = (1385238033, {'timezone': '%s'}, (3, 0.0, 0.0))", timezone_1.c_str());

      interface = "org.freedesktop.Geoclue.Address";
      obj_geo_addr = dbus_test_dbus_mock_get_object (mock, client_path, interface, nullptr);
      dbus_test_dbus_mock_object_add_method (mock, obj_geo_addr, "GetAddress", nullptr, G_VARIANT_TYPE("(ia{ss}(idd))"), gstr->str, &error);
                                             
      dbus_test_service_add_task(service, DBUS_TEST_TASK(mock));
      dbus_test_service_start_tasks(service);

      bus = g_bus_get_sync (G_BUS_TYPE_SESSION, nullptr, nullptr);
      g_dbus_connection_set_exit_on_close (bus, FALSE);
      g_object_add_weak_pointer (G_OBJECT(bus), (gpointer*)&bus);

      g_string_free (gstr, TRUE);
    }

    virtual void TearDown ()
    {
      g_clear_object (&mock);
      g_clear_object (&service);
      g_object_unref (bus);

      unsigned int cleartry = 0;
      while (bus != nullptr && cleartry < 10)
        {
          wait_msec (100);
          cleartry++;
        }

      // I've looked and can't find where this extra ref is coming from.
      // is there an unbalanced ref to the bus in the test harness?!
      while (bus != NULL)
        {
          g_object_unref (bus);
          wait_msec (1000);
        }

      super::TearDown ();
    }

private:

    struct EmitAddressChangedData
    {
        DbusTestDbusMock * mock = nullptr;
        DbusTestDbusMockObject * obj_geo_addr = nullptr;
        std::string timezone;
        EmitAddressChangedData(DbusTestDbusMock* mock_,
                               DbusTestDbusMockObject* obj_geo_addr_,
                               const std::string& timezone_): mock(mock_), obj_geo_addr(obj_geo_addr_), timezone(timezone_) {}
    };

    static gboolean emit_address_changed_idle (gpointer gdata)
    {
        auto data = static_cast<EmitAddressChangedData*>(gdata);
        auto fmt = g_strdup_printf ("(1385238033, {'timezone': '%s'}, (3, 0.0, 0.0))", data->timezone.c_str());

        GError * error = nullptr;
        dbus_test_dbus_mock_object_emit_signal(data->mock, data->obj_geo_addr,
                                               //"org.freedesktop.Geoclue.Address",
                                               "AddressChanged",
                                               G_VARIANT_TYPE("(ia{ss}(idd))"),
                                               g_variant_new_parsed (fmt),
                                               &error);
        if (error)
        {
            g_warning("%s: %s", G_STRFUNC, error->message);
            g_error_free (error);
        }

        g_free (fmt);
        delete data;
        return G_SOURCE_REMOVE;
    }

public:

    void setGeoclueTimezoneOnIdle (const std::string& newZone)
    {
        g_timeout_add (50, emit_address_changed_idle, new EmitAddressChangedData(mock, obj_geo_addr, newZone.c_str()));
    }

};