aboutsummaryrefslogtreecommitdiff
path: root/src/gmenuharness/MenuMatcher.cpp
blob: 7fd257d2ee06f67cf7fcd6495b9a35373e66d885 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser 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 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 program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Pete Woods <pete.woods@canonical.com>
 */

#include <lomiri/gmenuharness/MenuMatcher.h>
#include <lomiri/gmenuharness/MatchUtils.h>

#include <iostream>

#include <gio/gio.h>

using namespace std;

namespace lomiri
{

namespace gmenuharness
{

namespace
{

static void gdbus_connection_deleter(GDBusConnection* connection)
{
//    if (!g_dbus_connection_is_closed(connection))
//    {
//        g_dbus_connection_close_sync(connection, nullptr, nullptr);
//    }
    g_clear_object(&connection);
}
}

struct MenuMatcher::Parameters::Priv
{
    string m_busName;

    vector<pair<string, string>> m_actions;

    string m_menuObjectPath;
};

MenuMatcher::Parameters::Parameters(const string& busName,
                                    const vector<pair<string, string>>& actions,
                                    const string& menuObjectPath) :
        p(new Priv)
{
    p->m_busName = busName;
    p->m_actions = actions;
    p->m_menuObjectPath = menuObjectPath;
}

MenuMatcher::Parameters::~Parameters()
{
}

MenuMatcher::Parameters::Parameters(const Parameters& other) :
        p(new Priv)
{
    *this = other;
}

MenuMatcher::Parameters::Parameters(Parameters&& other)
{
    *this = move(other);
}

MenuMatcher::Parameters& MenuMatcher::Parameters::operator=(const Parameters& other)
{
    p->m_busName = other.p->m_busName;
    p->m_actions = other.p->m_actions;
    p->m_menuObjectPath = other.p->m_menuObjectPath;
    return *this;
}

MenuMatcher::Parameters& MenuMatcher::Parameters::operator=(Parameters&& other)
{
    p = move(other.p);
    return *this;
}

struct MenuMatcher::Priv
{
    Priv(const Parameters& parameters) :
        m_parameters(parameters)
    {
    }

    Parameters m_parameters;

    vector<MenuItemMatcher> m_items;

    shared_ptr<GDBusConnection> m_system;

    shared_ptr<GDBusConnection> m_session;

    shared_ptr<GMenuModel>  m_menu;

    map<string, shared_ptr<GActionGroup>> m_actions;
};

MenuMatcher::MenuMatcher(const Parameters& parameters) :
        p(new Priv(parameters))
{
    p->m_system.reset(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, nullptr),
                      &gdbus_connection_deleter);
    g_dbus_connection_set_exit_on_close(p->m_system.get(), false);

    p->m_session.reset(g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr),
                       &gdbus_connection_deleter);
    g_dbus_connection_set_exit_on_close(p->m_session.get(), false);

    p->m_menu.reset(
            G_MENU_MODEL(
                    g_dbus_menu_model_get(
                            p->m_session.get(),
                            p->m_parameters.p->m_busName.c_str(),
                            p->m_parameters.p->m_menuObjectPath.c_str())),
            &g_object_deleter);

    for (const auto& action : p->m_parameters.p->m_actions)
    {
        shared_ptr<GActionGroup> actionGroup(
                G_ACTION_GROUP(
                        g_dbus_action_group_get(
                                p->m_session.get(),
                                p->m_parameters.p->m_busName.c_str(),
                                action.second.c_str())),
                &g_object_deleter);
        p->m_actions[action.first] = actionGroup;
    }
}

MenuMatcher::~MenuMatcher()
{
}

MenuMatcher& MenuMatcher::item(const MenuItemMatcher& item)
{
    p->m_items.emplace_back(item);
    return *this;
}

void MenuMatcher::match(MatchResult& matchResult) const
{
    vector<unsigned int> location;

    while (true)
    {
        MatchResult childMatchResult(matchResult.createChild());

        int menuSize = g_menu_model_get_n_items(p->m_menu.get());
        if (p->m_items.size() > (unsigned int) menuSize)
        {
            childMatchResult.failure(
                    location,
                    "Row count mismatch, expected " + to_string(p->m_items.size())
                            + " but found " + to_string(menuSize));
        }
        else
        {
            for (size_t i = 0; i < p->m_items.size(); ++i)
            {
                const auto& matcher = p->m_items.at(i);
                matcher.match(childMatchResult, location, p->m_menu, p->m_actions, i);
            }
        }

        if (childMatchResult.success())
        {
            matchResult.merge(childMatchResult);
            break;
        }
        else
        {
            if (matchResult.hasTimedOut())
            {
                matchResult.merge(childMatchResult);
                break;
            }
            menuWaitForItems(p->m_menu);
        }
    }
}

MatchResult MenuMatcher::match() const
{
    MatchResult matchResult;
    match(matchResult);
    return matchResult;
}

}   // namespace gmenuharness

}   // namespace lomiri