~unity-team/unity-scope-click/devel

« back to all changes in this revision

Viewing changes to scope/tests/test_apps_query.cpp

  • Committer: Tarmac
  • Author(s): Pawel Stolowski
  • Date: 2014-07-16 23:45:02 UTC
  • mfrom: (310.4.10 use-local-departments)
  • Revision ID: tarmac-20140716234502-yuv89fhikjzmsfrp
Make use of departments db if available to provide departments for installed apps.

Approved by Alejandro J. Cura, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include <gmock/gmock.h>
35
35
 
36
36
#include <clickapps/apps-query.h>
 
37
#include <click/departments-db.h>
37
38
 
38
39
#include <unity/scopes/SearchReply.h>
39
40
#include <unity/scopes/SearchMetadata.h>
56
57
    }
57
58
};
58
59
 
 
60
class MockClickInterface : public click::Interface
 
61
{
 
62
public:
 
63
    MockClickInterface() = default;
 
64
    MOCK_METHOD3(find_installed_apps, std::vector<click::Application>(const std::string&, const std::unordered_set<std::string>&, bool));
 
65
};
 
66
 
 
67
class MockAppsQuery : public click::apps::Query
 
68
{
 
69
private:
 
70
    std::shared_ptr<MockClickInterface> click_iface;
 
71
 
 
72
public:
 
73
    MockAppsQuery(unity::scopes::CannedQuery const& query, std::shared_ptr<click::DepartmentsDb> depts_db, scopes::SearchMetadata const& metadata,
 
74
            const std::shared_ptr<MockClickInterface>& click_iface)
 
75
        : click::apps::Query(query, depts_db, metadata),
 
76
          click_iface(click_iface)
 
77
    {
 
78
    }
 
79
 
 
80
    click::Interface& clickInterfaceInstance() override
 
81
    {
 
82
        return *click_iface;
 
83
    }
 
84
};
 
85
 
59
86
MATCHER_P(HasApplicationTitle, n, "") { return arg["title"].get_string() == n; }
60
87
 
61
88
TEST_F(ResultPusherTest, testPushTopAndLocalResults)
99
126
    const unity::scopes::CannedQuery query("foo.scope", "FooBar", "");
100
127
    const unity::scopes::CannedQuery query2("foo.scope", "Metallica", "");
101
128
 
102
 
    click::apps::Query q(query, metadata);
103
 
    click::apps::Query q2(query2, metadata);
 
129
    click::apps::Query q(query, nullptr, metadata);
 
130
    click::apps::Query q2(query2, nullptr, metadata);
104
131
 
105
132
    scopes::testing::MockSearchReply mock_reply;
106
133
    scopes::SearchReplyProxy reply(&mock_reply, [](unity::scopes::SearchReply*){});
124
151
    q2.add_fake_store_app(reply2);
125
152
}
126
153
 
 
154
// this matcher expects a list of department ids in depts:
 
155
// first on the list is the root, followed by children ids.
 
156
// the arg of the matcher is unity::scopes::Department ptr.
 
157
MATCHER_P(MatchesDepartments, depts, "") {
 
158
    auto it = depts.begin();
 
159
    if (arg->id() != *it)
 
160
        return false;
 
161
    auto const subdeps = arg->subdepartments();
 
162
    if (subdeps.size() != depts.size() - 1)
 
163
        return false;
 
164
    for (auto const& sub: subdeps)
 
165
    {
 
166
        if (sub->id() != *(++it))
 
167
            return false;
 
168
    }
 
169
    return true;
 
170
}
 
171
 
 
172
TEST(Query, Departments)
 
173
{
 
174
    const std::vector<click::Application> installed_apps = {{"app1", "App1", 0.0f, "icon", "url", "descr", "scrshot"}};
 
175
    const scopes::SearchMetadata metadata("en_EN", "phone");
 
176
    auto clickif = std::make_shared<MockClickInterface>();
 
177
 
 
178
    const scopes::CategoryRenderer renderer("{}");
 
179
    auto ptrCat = std::make_shared<FakeCategory>("id", "", "", renderer);
 
180
    auto depts_db = std::make_shared<MockDepartmentsDb>(":memory:");
 
181
 
 
182
    const std::list<std::string> expected_locales {"en_EN", "en_US", ""};
 
183
 
 
184
    // query for root of the departments tree
 
185
    {
 
186
        const unity::scopes::CannedQuery query("foo.scope", "", "");
 
187
 
 
188
        MockAppsQuery q(query, depts_db, metadata, clickif);
 
189
 
 
190
        scopes::testing::MockSearchReply mock_reply;
 
191
        scopes::SearchReplyProxy reply(&mock_reply, [](unity::scopes::SearchReply*){});
 
192
 
 
193
        std::list<std::string> expected_departments({{"", "games", "video"}});
 
194
 
 
195
        EXPECT_CALL(*clickif, find_installed_apps(_, _, _)).WillOnce(Return(installed_apps));
 
196
        EXPECT_CALL(mock_reply, register_category("predefined", _, _, _)).WillOnce(Return(ptrCat));
 
197
        EXPECT_CALL(mock_reply, register_category("local", _, _, _)).WillOnce(Return(ptrCat));
 
198
        EXPECT_CALL(mock_reply, register_category("store", _, _, _)).WillOnce(Return(ptrCat));
 
199
        EXPECT_CALL(mock_reply, register_departments(MatchesDepartments(expected_departments)));
 
200
 
 
201
        EXPECT_CALL(mock_reply, push(Matcher<unity::scopes::CategorisedResult const&>(_))).Times(2).WillRepeatedly(Return(true));
 
202
 
 
203
        EXPECT_CALL(*depts_db, get_department_name("games", expected_locales)).WillOnce(Return("Games"));
 
204
        EXPECT_CALL(*depts_db, get_department_name("video", expected_locales)).WillOnce(Return("Video"));
 
205
        EXPECT_CALL(*depts_db, get_children_departments("")).WillOnce(Return(
 
206
                    std::list<click::DepartmentsDb::DepartmentInfo>({
 
207
                        {"games", false},
 
208
                        {"video", true}
 
209
                    }))
 
210
                );
 
211
 
 
212
        q.run(reply);
 
213
    }
 
214
 
 
215
    // query for a leaf department
 
216
    {
 
217
        const unity::scopes::CannedQuery query("foo.scope", "", "games");
 
218
 
 
219
        MockAppsQuery q(query, depts_db, metadata, clickif);
 
220
 
 
221
        scopes::testing::MockSearchReply mock_reply;
 
222
        scopes::SearchReplyProxy reply(&mock_reply, [](unity::scopes::SearchReply*){});
 
223
 
 
224
        std::list<std::string> expected_departments({"", "games"});
 
225
 
 
226
        EXPECT_CALL(*clickif, find_installed_apps(_, _, _)).WillOnce(Return(installed_apps));
 
227
        EXPECT_CALL(mock_reply, register_category("local", _, _, _)).WillOnce(Return(ptrCat));
 
228
        EXPECT_CALL(mock_reply, register_category("store", _, _, _)).WillOnce(Return(ptrCat));
 
229
        EXPECT_CALL(mock_reply, register_departments(MatchesDepartments(expected_departments)));
 
230
 
 
231
        EXPECT_CALL(mock_reply, push(Matcher<unity::scopes::CategorisedResult const&>(_))).Times(2).WillRepeatedly(Return(true));
 
232
 
 
233
        EXPECT_CALL(*depts_db, get_parent_department_id("games")).WillOnce(Return(""));
 
234
        EXPECT_CALL(*depts_db, get_department_name("games", expected_locales)).WillOnce(Return("Games"));
 
235
        EXPECT_CALL(*depts_db, get_children_departments("games")).WillOnce(Return(
 
236
                    std::list<click::DepartmentsDb::DepartmentInfo>({})
 
237
                ));
 
238
 
 
239
        q.run(reply);
 
240
    }
 
241
}
 
242