~ps-jenkins/indicator-transfer/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to tests/test-exporter.cpp

  • Committer: Charles Kerr
  • Date: 2014-06-17 01:36:16 UTC
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: charles.kerr@canonical.com-20140617013616-7fcd22wh3hbgaovh
code drop

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2014 Canonical Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 *
16
 
 * Authors:
17
 
 *   Charles Kerr <charles.kerr@canonical.com>
18
 
 */
19
 
 
20
 
#include "actions-mock.h"
21
 
#include "glib-fixture.h"
22
 
 
23
 
#include <transfer/dbus-shared.h>
24
 
#include <transfer/exporter.h>
25
 
 
26
 
#include <set>
27
 
#include <string>
28
 
 
29
 
using namespace unity::indicator::transfer;
30
 
 
31
 
class ExporterFixture: public GlibFixture
32
 
{
33
 
private:
34
 
 
35
 
    typedef GlibFixture super;
36
 
 
37
 
protected:
38
 
 
39
 
    GTestDBus* bus = nullptr;
40
 
 
41
 
    void SetUp()
42
 
    {
43
 
        super::SetUp();
44
 
 
45
 
        // bring up the test bus
46
 
        bus = g_test_dbus_new(G_TEST_DBUS_NONE);
47
 
        g_test_dbus_up(bus);
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);
51
 
    }
52
 
 
53
 
    void TearDown()
54
 
    {
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);
62
 
        g_clear_object(&bus);
63
 
 
64
 
        super::TearDown();
65
 
    }
66
 
};
67
 
 
68
 
TEST_F(ExporterFixture, HelloWorld)
69
 
{
70
 
    // confirms that the Test DBus SetUp() and TearDown() works
71
 
}
72
 
 
73
 
TEST_F(ExporterFixture, Publish)
74
 
{
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}};
79
 
 
80
 
    // create the menus
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)));
86
 
 
87
 
    // export 'em
88
 
    Exporter exporter;
89
 
    exporter.publish(gactions, menus);
90
 
    wait_msec();
91
 
 
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));
95
 
 
96
 
    // wait for the exported ActionGroup to be populated
97
 
    if (g_strv_length(names_strv) == 0)
98
 
    {
99
 
        g_strfreev(names_strv);
100
 
        wait_for_signal(exported, "action-added");
101
 
        names_strv = g_action_group_list_actions(G_ACTION_GROUP(exported));
102
 
    }
103
 
 
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]);
108
 
 
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"));
117
 
 
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](){
122
 
        name_lost = true;
123
 
        g_main_loop_quit(loop);
124
 
    });
125
 
    g_dbus_connection_close_sync(connection, nullptr, nullptr);
126
 
    g_main_loop_run(loop);
127
 
    EXPECT_TRUE(name_lost);
128
 
 
129
 
    // cleanup
130
 
    g_strfreev(names_strv);
131
 
    g_clear_object(&exported);
132
 
    g_clear_object(&connection);
133
 
}