Implement MoveFileLocal (with creating a snapshot).
[chromium-blink-merge.git] / ui / app_list / search / mixer_unittest.cc
blob7c56159dd1afa8d46a8aa454f8ef4832077c17fb
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <set>
6 #include <string>
8 #include "base/memory/scoped_vector.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/app_list/app_list_model.h"
14 #include "ui/app_list/search/history_types.h"
15 #include "ui/app_list/search/mixer.h"
16 #include "ui/app_list/search_provider.h"
17 #include "ui/app_list/search_result.h"
19 namespace app_list {
20 namespace test {
22 class TestSearchResult : public SearchResult {
23 public:
24 TestSearchResult(const std::string& id, double relevance)
25 : instance_id_(instantiation_count++) {
26 set_id(id);
27 set_title(base::UTF8ToUTF16(id));
28 set_relevance(relevance);
30 ~TestSearchResult() override {}
32 using SearchResult::set_voice_result;
34 // SearchResult overrides:
35 void Open(int event_flags) override {}
36 void InvokeAction(int action_index, int event_flags) override {}
37 scoped_ptr<SearchResult> Duplicate() const override {
38 return make_scoped_ptr(new TestSearchResult(id(), relevance()));
41 // For reference equality testing. (Addresses cannot be used to test reference
42 // equality because it is possible that an object will be allocated at the
43 // same address as a previously deleted one.)
44 static int GetInstanceId(SearchResult* result) {
45 return static_cast<const TestSearchResult*>(result)->instance_id_;
48 private:
49 static int instantiation_count;
51 int instance_id_;
53 DISALLOW_COPY_AND_ASSIGN(TestSearchResult);
55 int TestSearchResult::instantiation_count = 0;
57 class TestSearchProvider : public SearchProvider {
58 public:
59 explicit TestSearchProvider(const std::string& prefix)
60 : prefix_(prefix), count_(0), bad_relevance_range_(false) {}
61 ~TestSearchProvider() override {}
63 // SearchProvider overrides:
64 void Start(bool is_voice_query, const base::string16& query) override {
65 ClearResults();
66 for (size_t i = 0; i < count_; ++i) {
67 const std::string id =
68 base::StringPrintf("%s%d", prefix_.c_str(), static_cast<int>(i));
69 double relevance = 1.0 - i / 10.0;
70 // If bad_relevance_range_, change the relevances to give results outside
71 // of the canonical [0.0, 1.0] range.
72 if (bad_relevance_range_)
73 relevance = 10.0 - i * 10;
74 TestSearchResult* result = new TestSearchResult(id, relevance);
75 if (voice_result_indices.find(i) != voice_result_indices.end())
76 result->set_voice_result(true);
77 Add(scoped_ptr<SearchResult>(result).Pass());
80 void Stop() override {}
82 void set_prefix(const std::string& prefix) { prefix_ = prefix; }
83 void set_count(size_t count) { count_ = count; }
84 void set_as_voice_result(size_t index) { voice_result_indices.insert(index); }
85 void set_bad_relevance_range() { bad_relevance_range_ = true; }
87 private:
88 std::string prefix_;
89 size_t count_;
90 bool bad_relevance_range_;
91 // Indices of results that will have the |voice_result| flag set.
92 std::set<size_t> voice_result_indices;
94 DISALLOW_COPY_AND_ASSIGN(TestSearchProvider);
97 class MixerTest : public testing::Test {
98 public:
99 MixerTest() : is_voice_query_(false) {}
100 ~MixerTest() override {}
102 // testing::Test overrides:
103 void SetUp() override {
104 results_.reset(new AppListModel::SearchResults);
106 providers_.push_back(new TestSearchProvider("app"));
107 providers_.push_back(new TestSearchProvider("omnibox"));
108 providers_.push_back(new TestSearchProvider("webstore"));
109 providers_.push_back(new TestSearchProvider("people"));
111 is_voice_query_ = false;
113 mixer_.reset(new Mixer(results_.get()));
114 mixer_->Init();
115 mixer_->AddProviderToGroup(Mixer::MAIN_GROUP, providers_[0]);
116 mixer_->AddProviderToGroup(Mixer::OMNIBOX_GROUP, providers_[1]);
117 mixer_->AddProviderToGroup(Mixer::WEBSTORE_GROUP, providers_[2]);
118 mixer_->AddProviderToGroup(Mixer::PEOPLE_GROUP, providers_[3]);
121 void RunQuery() {
122 const base::string16 query;
124 for (size_t i = 0; i < providers_.size(); ++i) {
125 providers_[i]->Start(is_voice_query_, query);
126 providers_[i]->Stop();
129 mixer_->MixAndPublish(is_voice_query_, known_results_);
132 std::string GetResults() const {
133 std::string result;
134 for (size_t i = 0; i < results_->item_count(); ++i) {
135 if (!result.empty())
136 result += ',';
138 result += base::UTF16ToUTF8(results_->GetItemAt(i)->title());
141 return result;
144 Mixer* mixer() { return mixer_.get(); }
145 TestSearchProvider* app_provider() { return providers_[0]; }
146 TestSearchProvider* omnibox_provider() { return providers_[1]; }
147 TestSearchProvider* webstore_provider() { return providers_[2]; }
148 TestSearchProvider* people_provider() { return providers_[3]; }
150 // Sets whether test runs should be treated as a voice query.
151 void set_is_voice_query(bool is_voice_query) {
152 is_voice_query_ = is_voice_query;
155 void AddKnownResult(const std::string& id, KnownResultType type) {
156 known_results_[id] = type;
159 private:
160 scoped_ptr<Mixer> mixer_;
161 scoped_ptr<AppListModel::SearchResults> results_;
162 KnownResults known_results_;
164 bool is_voice_query_;
166 ScopedVector<TestSearchProvider> providers_;
168 DISALLOW_COPY_AND_ASSIGN(MixerTest);
171 TEST_F(MixerTest, Basic) {
172 struct TestCase {
173 const size_t app_results;
174 const size_t omnibox_results;
175 const size_t webstore_results;
176 const size_t people_results;
177 const char* expected;
178 } kTestCases[] = {
179 {0, 0, 0, 0, ""},
180 {10, 0, 0, 0, "app0,app1,app2,app3"},
181 {0, 0, 10, 0, "webstore0,webstore1"},
182 {0, 0, 0, 10, "people0,people1"},
183 {4, 6, 0, 0, "app0,app1,app2,app3,omnibox0,omnibox1"},
184 {4, 6, 2, 0, "app0,app1,app2,app3,omnibox0,webstore0"},
185 {4, 6, 0, 2, "app0,app1,app2,app3,omnibox0,people0"},
186 {10, 10, 10, 0, "app0,app1,app2,app3,omnibox0,webstore0"},
187 {0, 10, 0, 0, "omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,omnibox5"},
188 {0, 10, 1, 0, "omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,webstore0"},
189 {0, 10, 2, 0, "omnibox0,omnibox1,omnibox2,omnibox3,webstore0,webstore1"},
190 {1, 10, 0, 0, "app0,omnibox0,omnibox1,omnibox2,omnibox3,omnibox4"},
191 {2, 10, 0, 0, "app0,app1,omnibox0,omnibox1,omnibox2,omnibox3"},
192 {2, 10, 1, 0, "app0,app1,omnibox0,omnibox1,omnibox2,webstore0"},
193 {2, 10, 2, 0, "app0,app1,omnibox0,omnibox1,webstore0,webstore1"},
194 {2, 0, 2, 0, "app0,app1,webstore0,webstore1"},
195 {10, 0, 10, 10, "app0,app1,app2,app3,webstore0,webstore1"},
196 {10, 10, 10, 10, "app0,app1,app2,app3,omnibox0,webstore0"},
197 {0, 0, 0, 0, ""},
200 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
201 app_provider()->set_count(kTestCases[i].app_results);
202 omnibox_provider()->set_count(kTestCases[i].omnibox_results);
203 webstore_provider()->set_count(kTestCases[i].webstore_results);
204 people_provider()->set_count(kTestCases[i].people_results);
205 RunQuery();
207 EXPECT_EQ(kTestCases[i].expected, GetResults()) << "Case " << i;
211 TEST_F(MixerTest, RemoveDuplicates) {
212 const std::string dup = "dup";
214 // This gives "dup0,dup1,dup2".
215 app_provider()->set_prefix(dup);
216 app_provider()->set_count(3);
218 // This gives "dup0,dup1".
219 omnibox_provider()->set_prefix(dup);
220 omnibox_provider()->set_count(2);
222 // This gives "dup0".
223 webstore_provider()->set_prefix(dup);
224 webstore_provider()->set_count(1);
226 RunQuery();
228 // Only three results with unique id are kept.
229 EXPECT_EQ("dup0,dup1,dup2", GetResults());
232 // Tests that "known results" have priority over others.
233 TEST_F(MixerTest, KnownResultsPriority) {
234 // This gives omnibox 0 -- 5.
235 omnibox_provider()->set_count(6);
237 // omnibox 1 -- 4 are "known results".
238 AddKnownResult("omnibox1", PREFIX_SECONDARY);
239 AddKnownResult("omnibox2", PERFECT_SECONDARY);
240 AddKnownResult("omnibox3", PREFIX_PRIMARY);
241 AddKnownResult("omnibox4", PERFECT_PRIMARY);
243 RunQuery();
245 // omnibox 1 -- 4 should be prioritised over the others. They should be
246 // ordered 4, 3, 2, 1 (in order of match quality).
247 EXPECT_EQ("omnibox4,omnibox3,omnibox2,omnibox1,omnibox0,omnibox5",
248 GetResults());
251 TEST_F(MixerTest, VoiceQuery) {
252 omnibox_provider()->set_count(3);
253 RunQuery();
254 EXPECT_EQ("omnibox0,omnibox1,omnibox2", GetResults());
256 // Set "omnibox1" as a voice result. Do not expect any changes (as this is not
257 // a voice query).
258 omnibox_provider()->set_as_voice_result(1);
259 RunQuery();
260 EXPECT_EQ("omnibox0,omnibox1,omnibox2", GetResults());
262 // Perform a voice query. Expect voice result first.
263 set_is_voice_query(true);
264 RunQuery();
265 EXPECT_EQ("omnibox1,omnibox0,omnibox2", GetResults());
267 // All voice results should appear before non-voice results.
268 omnibox_provider()->set_as_voice_result(2);
269 RunQuery();
270 EXPECT_EQ("omnibox1,omnibox2,omnibox0", GetResults());
273 TEST_F(MixerTest, BadRelevanceRange) {
274 // This gives relevance scores: (10.0, 0.0). Even though providers are
275 // supposed to give scores within the range [0.0, 1.0], we cannot rely on
276 // providers to do this, since they retrieve results from disparate and
277 // unreliable sources (like the Google+ API).
278 people_provider()->set_bad_relevance_range();
279 people_provider()->set_count(2);
281 // Give a massive boost to the second result.
282 AddKnownResult("people1", PERFECT_PRIMARY);
284 RunQuery();
286 // If the results are correctly clamped to the range [0.0, 1.0], the boost to
287 // "people1" will push it over the first result. If not, the massive base
288 // score of "people0" will erroneously keep it on top.
289 EXPECT_EQ("people1,people0", GetResults());
292 TEST_F(MixerTest, Publish) {
293 scoped_ptr<SearchResult> result1(new TestSearchResult("app1", 0));
294 scoped_ptr<SearchResult> result2(new TestSearchResult("app2", 0));
295 scoped_ptr<SearchResult> result3(new TestSearchResult("app3", 0));
296 scoped_ptr<SearchResult> result3_copy = result3->Duplicate();
297 scoped_ptr<SearchResult> result4(new TestSearchResult("app4", 0));
298 scoped_ptr<SearchResult> result5(new TestSearchResult("app5", 0));
300 AppListModel::SearchResults ui_results;
302 // Publish the first three results to |ui_results|.
303 Mixer::SortedResults new_results;
304 new_results.push_back(Mixer::SortData(result1.get(), 1.0f));
305 new_results.push_back(Mixer::SortData(result2.get(), 1.0f));
306 new_results.push_back(Mixer::SortData(result3.get(), 1.0f));
308 Mixer::Publish(new_results, &ui_results);
309 EXPECT_EQ(3u, ui_results.item_count());
310 // The objects in |ui_results| should be new copies because the input results
311 // are owned and |ui_results| needs to own its results as well.
312 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[0].result),
313 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0)));
314 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[1].result),
315 TestSearchResult::GetInstanceId(ui_results.GetItemAt(1)));
316 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[2].result),
317 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2)));
319 // Save the current |ui_results| instance ids for comparison later.
320 std::vector<int> old_ui_result_ids;
321 for (size_t i = 0; i < ui_results.item_count(); ++i) {
322 old_ui_result_ids.push_back(
323 TestSearchResult::GetInstanceId(ui_results.GetItemAt(i)));
326 // Change the first result to a totally new object (with a new ID).
327 new_results[0] = Mixer::SortData(result4.get(), 1.0f);
329 // Change the second result's title, but keep the same id. (The result will
330 // keep the id "app2" but change its title to "New App 2 Title".)
331 const base::string16 kNewAppTitle = base::UTF8ToUTF16("New App 2 Title");
332 new_results[1].result->set_title(kNewAppTitle);
334 // Change the third result's object address (it points to an object with the
335 // same data).
336 new_results[2] = Mixer::SortData(result3_copy.get(), 1.0f);
338 Mixer::Publish(new_results, &ui_results);
339 EXPECT_EQ(3u, ui_results.item_count());
341 // The first result will be a new object, as the ID has changed.
342 EXPECT_NE(old_ui_result_ids[0],
343 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0)));
345 // The second result will still use the original object, but have a different
346 // title, since the ID did not change.
347 EXPECT_EQ(old_ui_result_ids[1],
348 TestSearchResult::GetInstanceId(ui_results.GetItemAt(1)));
349 EXPECT_EQ(kNewAppTitle, ui_results.GetItemAt(1)->title());
351 // The third result will use the original object as the ID did not change.
352 EXPECT_EQ(old_ui_result_ids[2],
353 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2)));
355 // Save the current |ui_results| order which should is app4, app2, app3.
356 old_ui_result_ids.clear();
357 for (size_t i = 0; i < ui_results.item_count(); ++i) {
358 old_ui_result_ids.push_back(
359 TestSearchResult::GetInstanceId(ui_results.GetItemAt(i)));
362 // Reorder the existing results and add a new one in the second place.
363 new_results[0] = Mixer::SortData(result2.get(), 1.0f);
364 new_results[1] = Mixer::SortData(result5.get(), 1.0f);
365 new_results[2] = Mixer::SortData(result3.get(), 1.0f);
366 new_results.push_back(Mixer::SortData(result4.get(), 1.0f));
368 Mixer::Publish(new_results, &ui_results);
369 EXPECT_EQ(4u, ui_results.item_count());
371 // The reordered results should use the original objects.
372 EXPECT_EQ(old_ui_result_ids[0],
373 TestSearchResult::GetInstanceId(ui_results.GetItemAt(3)));
374 EXPECT_EQ(old_ui_result_ids[1],
375 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0)));
376 EXPECT_EQ(old_ui_result_ids[2],
377 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2)));
380 } // namespace test
381 } // namespace app_list