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
|
/*
* Copyright 2014-2016 Canonical Ltd.
*
* 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/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*/
#pragma once
#include "glib-fixture.h"
#include <libdbustest/dbus-test.h>
/***
****
***/
class LibdbusmockFixture: public GlibFixture
{
private:
typedef GlibFixture super;
protected:
GDBusConnection * system_bus {};
GDBusConnection * session_bus {};
DbusTestService * service {};
void SetUp() override
{
super::SetUp();
service = dbus_test_service_new(nullptr);
}
void startDbusMock()
{
// start 'em up.
// make the system bus work off the mock bus too, since that's
// where the upower and screen are on the system bus...
dbus_test_service_start_tasks(service);
g_setenv("DBUS_SYSTEM_BUS_ADDRESS", g_getenv("DBUS_SESSION_BUS_ADDRESS"), TRUE);
session_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr);
ASSERT_NE(nullptr, session_bus);
g_dbus_connection_set_exit_on_close(session_bus, false);
g_object_add_weak_pointer(G_OBJECT(session_bus), (gpointer *)&session_bus);
system_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, nullptr);
ASSERT_NE(nullptr, system_bus);
g_dbus_connection_set_exit_on_close(system_bus, FALSE);
g_object_add_weak_pointer(G_OBJECT(system_bus), (gpointer *)&system_bus);
}
void TearDown() override
{
g_clear_object(&service);
g_object_unref(session_bus);
g_object_unref(system_bus);
// wait a little while for the scaffolding to shut down,
// but don't block on it forever...
wait_for([this](){return system_bus==nullptr && session_bus==nullptr;}, 5000);
super::TearDown();
}
bool wait_for_method_call(DbusTestDbusMock* mock,
DbusTestDbusMockObject* obj,
const gchar* method,
GVariant* params=nullptr,
guint timeout_msec=100)
{
if (params != nullptr)
g_variant_ref_sink(params);
auto test_function = [mock, obj, method, params]() {
GError* error {};
const auto called = dbus_test_dbus_mock_object_check_method_call(mock,
obj,
method,
params,
&error);
if (error != nullptr) {
g_critical("Error looking for method call '%s': %s", method, error->message);
g_clear_error(&error);
}
return called;
};
const auto ret = wait_for(test_function, timeout_msec);
g_clear_pointer(¶ms, g_variant_unref);
return ret;
}
void EXPECT_METHOD_CALLED_EVENTUALLY(DbusTestDbusMock* mock,
DbusTestDbusMockObject* obj,
const gchar* method,
GVariant* params=nullptr,
guint timeout_msec=1000)
{
EXPECT_TRUE(wait_for_method_call(mock, obj, method, params, timeout_msec)) << "method: " << method;
}
void EXPECT_METHOD_NOT_CALLED_EVENTUALLY(DbusTestDbusMock* mock,
DbusTestDbusMockObject* obj,
const gchar* method,
GVariant* params=nullptr,
guint timeout_msec=1000)
{
EXPECT_FALSE(wait_for_method_call(mock, obj, method, params, timeout_msec)) << "method: " << method;
}
void ASSERT_METHOD_CALLED_EVENTUALLY(DbusTestDbusMock* mock,
DbusTestDbusMockObject* obj,
const gchar* method,
GVariant* params=nullptr,
guint timeout_msec=1000)
{
ASSERT_TRUE(wait_for_method_call(mock, obj, method, params, timeout_msec)) << "method: " << method;
}
void ASSERT_METHOD_NOT_CALLED_EVENTUALLY(DbusTestDbusMock* mock,
DbusTestDbusMockObject* obj,
const gchar* method,
GVariant* params=nullptr,
guint timeout_msec=1000)
{
ASSERT_FALSE(wait_for_method_call(mock, obj, method, params, timeout_msec)) << "method: " << method;
}
};
|