~townsend/libertine-scope/rename-desktop-apps

« back to all changes in this revision

Viewing changes to tests/test_query.cpp

  • Committer: Larry Price
  • Date: 2016-04-18 16:45:42 UTC
  • mfrom: (31.2.1 libertine-scope-search)
  • Revision ID: larry.price@canonical.com-20160418164542-0p6d36bb12to8nbm
Filter app launchers based on a user-input regular expression.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU General Public License, version 3, as published by the
 
6
 * Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include "libertine-scope/query.h"
 
18
#include "tests/fake_libertine.h"
 
19
#include <unity/scopes/SearchMetadata.h>
 
20
#include <unity/scopes/CannedQuery.h>
 
21
#include <unity/scopes/SearchReplyProxyFwd.h>
 
22
#include <unity/scopes/testing/MockSearchReply.h>
 
23
#include <unity/scopes/Category.h>
 
24
#include <vector>
 
25
#include <memory>
 
26
#include <gtest/gtest.h>
 
27
#include <gmock/gmock.h>
 
28
 
 
29
namespace
 
30
{
 
31
const std::string LIBERTINE_OUTPUT_WITH_APPS = R"(
 
32
  {
 
33
    "name": "fake-container",
 
34
    "app_launchers": [{
 
35
      "name": "LibreOffice",
 
36
      "no_display": false,
 
37
      "desktop_file_name": "/link/to/libreoffice.desktop"
 
38
    }, {
 
39
      "name": "Linux",
 
40
      "no_display": true,
 
41
      "desktop_file_name": "/link/to/linux.desktop"
 
42
    }, {
 
43
      "name": "Library",
 
44
      "no_display": false,
 
45
      "desktop_file_name": "/link/to/library.desktop"
 
46
    }]
 
47
  }
 
48
)";
 
49
 
 
50
 
 
51
class FakeCategory : public unity::scopes::Category
 
52
{
 
53
public:
 
54
    FakeCategory(std::string const& id, std::string const& title,
 
55
                 std::string const& icon, unity::scopes::CategoryRenderer const& renderer) :
 
56
       unity::scopes::Category(id, title, icon, renderer)
 
57
    {
 
58
    }
 
59
 
 
60
};
 
61
 
 
62
 
 
63
MATCHER_P3(ResultPropertiesMatch, title, art, uri, "")
 
64
{
 
65
  return arg.contains("title") && arg.contains("art") && arg.contains("uri") &&
 
66
      arg["title"] == unity::scopes::Variant(title) &&
 
67
      arg["art"] == unity::scopes::Variant(art) &&
 
68
      arg["uri"] == unity::scopes::Variant(uri);
 
69
}
 
70
 
 
71
 
 
72
class TestQueryFixture : public ::testing::Test
 
73
{
 
74
public:
 
75
  TestQueryFixture()
 
76
    : metadata("en_US", "phone")
 
77
    , canned_query("libertine-scope")
 
78
    , reply()
 
79
    , proxy(&reply, [](unity::scopes::SearchReply*) {})
 
80
    , category(std::make_shared<FakeCategory>("fake", "fake-container", "Application", unity::scopes::CategoryRenderer()))
 
81
  {
 
82
  }
 
83
 
 
84
  void expect_registry()
 
85
  {
 
86
    EXPECT_CALL(reply, register_category("fake", "fake-container", "Application", testing::_)).WillOnce(testing::Return(category));
 
87
  }
 
88
 
 
89
  void expect_push(std::string title, std::string art, std::string appId, bool success = true)
 
90
  {
 
91
    EXPECT_CALL(reply, push(testing::Matcher<unity::scopes::CategorisedResult const&>(ResultPropertiesMatch(title, art, appId)))).WillOnce(testing::Return(success));
 
92
  }
 
93
 
 
94
  unity::scopes::SearchMetadata metadata;
 
95
  unity::scopes::CannedQuery canned_query;
 
96
  testing::NiceMock<unity::scopes::testing::MockSearchReply> reply;
 
97
  unity::scopes::SearchReplyProxy proxy;
 
98
  std::shared_ptr<FakeCategory> category;
 
99
};
 
100
 
 
101
 
 
102
TEST_F(TestQueryFixture, returnsAllDisplayableAppsWithoutFilters)
 
103
{
 
104
  expect_registry();
 
105
  expect_push("LibreOffice", "", "appid://fake/libreoffice/0.0");
 
106
  expect_push("Library", "", "appid://fake/library/0.0");
 
107
 
 
108
  Query query(canned_query, metadata, []() {
 
109
    return FakeLibertine::make_fake(LIBERTINE_OUTPUT_WITH_APPS);
 
110
  });
 
111
  query.run(proxy);
 
112
}
 
113
 
 
114
 
 
115
TEST_F(TestQueryFixture, returnsRegularExpressionFilteredListOfApps)
 
116
{
 
117
  expect_registry();
 
118
  expect_push("LibreOffice", "", "appid://fake/libreoffice/0.0");
 
119
  canned_query.set_query_string("li.*office");
 
120
 
 
121
  Query query(canned_query, metadata, []() {
 
122
    return FakeLibertine::make_fake(LIBERTINE_OUTPUT_WITH_APPS);
 
123
  });
 
124
  query.run(proxy);
 
125
}
 
126
 
 
127
 
 
128
TEST_F(TestQueryFixture, haltsFurtherPushesAfterFailedPush)
 
129
{
 
130
  expect_registry();
 
131
  expect_push("LibreOffice", "", "appid://fake/libreoffice/0.0", false);
 
132
 
 
133
  Query query(canned_query, metadata, []() {
 
134
    return FakeLibertine::make_fake(LIBERTINE_OUTPUT_WITH_APPS);
 
135
  });
 
136
  query.run(proxy);
 
137
}
 
138
 
 
139
} // anonymous namespace