2
* Copyright 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include "actions-mock.h"
21
#include "glib-fixture.h"
23
#include <transfer/dbus-shared.h>
24
#include <transfer/exporter.h>
29
using namespace unity::indicator::transfer;
31
class ExporterFixture: public GlibFixture
35
typedef GlibFixture super;
39
GTestDBus* bus = nullptr;
45
// bring up the test bus
46
bus = g_test_dbus_new(G_TEST_DBUS_NONE);
48
const auto address = g_test_dbus_get_bus_address(bus);
49
g_setenv("DBUS_SYSTEM_BUS_ADDRESS", address, true);
50
g_setenv("DBUS_SESSION_BUS_ADDRESS", address, true);
55
GError * error = nullptr;
56
GDBusConnection* connection = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error);
57
if(!g_dbus_connection_is_closed(connection))
58
g_dbus_connection_close_sync(connection, nullptr, &error);
59
g_assert_no_error(error);
60
g_clear_object(&connection);
61
g_test_dbus_down(bus);
68
TEST_F(ExporterFixture, HelloWorld)
70
// confirms that the Test DBus SetUp() and TearDown() works
73
TEST_F(ExporterFixture, Publish)
75
// create the MockActions and GActions bridge
76
std::shared_ptr<MockActions> mock_actions{new MockActions};
77
std::shared_ptr<Actions> actions {std::dynamic_pointer_cast<Actions>(mock_actions)};
78
std::shared_ptr<GActions> gactions{new GActions{actions}};
81
std::shared_ptr<Transfers> transfers {new Transfers{}};
82
std::vector<std::shared_ptr<Menu>> menus;
83
MenuFactory menu_factory (transfers, gactions);
84
for(int i=0; i<Menu::NUM_PROFILES; i++)
85
menus.push_back(menu_factory.buildMenu(Menu::Profile(i)));
89
exporter.publish(gactions, menus);
92
auto connection = g_bus_get_sync (G_BUS_TYPE_SESSION, nullptr, nullptr);
93
auto exported = g_dbus_action_group_get (connection, BUS_NAME, BUS_PATH);
94
auto names_strv = g_action_group_list_actions(G_ACTION_GROUP(exported));
96
// wait for the exported ActionGroup to be populated
97
if (g_strv_length(names_strv) == 0)
99
g_strfreev(names_strv);
100
wait_for_signal(exported, "action-added");
101
names_strv = g_action_group_list_actions(G_ACTION_GROUP(exported));
104
// convert it to a std::set for easy prodding
105
std::set<std::string> names;
106
for(int i=0; names_strv && names_strv[i]; i++)
107
names.insert(names_strv[i]);
109
// confirm the actions that we expect
110
EXPECT_EQ(1, names.count("activate-transfer"));
111
EXPECT_EQ(1, names.count("cancel-transfer"));
112
EXPECT_EQ(1, names.count("pause-transfer"));
113
EXPECT_EQ(1, names.count("resume-transfer"));
114
EXPECT_EQ(1, names.count("pause-all"));
115
EXPECT_EQ(1, names.count("resume-all"));
116
EXPECT_EQ(1, names.count("clear-all"));
118
// try closing the connection prematurely
119
// to test Exporter's name-lost signal
120
bool name_lost {false};
121
exporter.name_lost.connect([this,&name_lost](){
123
g_main_loop_quit(loop);
125
g_dbus_connection_close_sync(connection, nullptr, nullptr);
126
g_main_loop_run(loop);
127
EXPECT_TRUE(name_lost);
130
g_strfreev(names_strv);
131
g_clear_object(&exported);
132
g_clear_object(&connection);