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.
8 #include "base/memory/scoped_vector.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/mock_entropy_provider.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/app_list/app_list_model.h"
16 #include "ui/app_list/search/history_types.h"
17 #include "ui/app_list/search/mixer.h"
18 #include "ui/app_list/search_provider.h"
19 #include "ui/app_list/search_result.h"
24 // Maximum number of results to show in each mixer group.
25 const size_t kMaxAppsGroupResults
= 4;
26 // Ignored unless AppListMixer field trial is "Blended".
27 const size_t kMaxOmniboxResults
= 4;
28 const size_t kMaxWebstoreResults
= 2;
29 const size_t kMaxPeopleResults
= 2;
31 class TestSearchResult
: public SearchResult
{
33 TestSearchResult(const std::string
& id
, double relevance
)
34 : instance_id_(instantiation_count
++) {
36 set_title(base::UTF8ToUTF16(id
));
37 set_relevance(relevance
);
39 ~TestSearchResult() override
{}
41 using SearchResult::set_voice_result
;
43 // SearchResult overrides:
44 void Open(int event_flags
) override
{}
45 void InvokeAction(int action_index
, int event_flags
) override
{}
46 scoped_ptr
<SearchResult
> Duplicate() const override
{
47 return make_scoped_ptr(new TestSearchResult(id(), relevance()));
50 // For reference equality testing. (Addresses cannot be used to test reference
51 // equality because it is possible that an object will be allocated at the
52 // same address as a previously deleted one.)
53 static int GetInstanceId(SearchResult
* result
) {
54 return static_cast<const TestSearchResult
*>(result
)->instance_id_
;
58 static int instantiation_count
;
62 DISALLOW_COPY_AND_ASSIGN(TestSearchResult
);
64 int TestSearchResult::instantiation_count
= 0;
66 class TestSearchProvider
: public SearchProvider
{
68 explicit TestSearchProvider(const std::string
& prefix
)
71 bad_relevance_range_(false),
72 display_type_(SearchResult::DISPLAY_LIST
) {}
73 ~TestSearchProvider() override
{}
75 // SearchProvider overrides:
76 void Start(bool is_voice_query
, const base::string16
& query
) override
{
78 for (size_t i
= 0; i
< count_
; ++i
) {
79 const std::string id
=
80 base::StringPrintf("%s%d", prefix_
.c_str(), static_cast<int>(i
));
81 double relevance
= 1.0 - i
/ 10.0;
82 // If bad_relevance_range_, change the relevances to give results outside
83 // of the canonical [0.0, 1.0] range.
84 if (bad_relevance_range_
)
85 relevance
= 10.0 - i
* 10;
86 TestSearchResult
* result
= new TestSearchResult(id
, relevance
);
87 result
->set_display_type(display_type_
);
88 if (voice_result_indices
.find(i
) != voice_result_indices
.end())
89 result
->set_voice_result(true);
90 Add(scoped_ptr
<SearchResult
>(result
).Pass());
93 void Stop() override
{}
95 void set_prefix(const std::string
& prefix
) { prefix_
= prefix
; }
96 void set_display_type(SearchResult::DisplayType display_type
) {
97 display_type_
= display_type
;
99 void set_count(size_t count
) { count_
= count
; }
100 void set_as_voice_result(size_t index
) { voice_result_indices
.insert(index
); }
101 void set_bad_relevance_range() { bad_relevance_range_
= true; }
106 bool bad_relevance_range_
;
107 SearchResult::DisplayType display_type_
;
108 // Indices of results that will have the |voice_result| flag set.
109 std::set
<size_t> voice_result_indices
;
111 DISALLOW_COPY_AND_ASSIGN(TestSearchProvider
);
114 // Test is parameterized with bool. True enables the "Blended" field trial.
115 class MixerTest
: public testing::Test
,
116 public testing::WithParamInterface
<bool> {
119 : is_voice_query_(false),
120 field_trial_list_(new base::MockEntropyProvider()) {}
121 ~MixerTest() override
{}
123 // testing::Test overrides:
124 void SetUp() override
{
125 // If the parameter is true, enable the field trial.
126 const char* field_trial_name
= GetParam() ? "Blended" : "default";
127 base::FieldTrialList::CreateFieldTrial("AppListMixer", field_trial_name
);
129 results_
.reset(new AppListModel::SearchResults
);
131 providers_
.push_back(new TestSearchProvider("app"));
132 providers_
.push_back(new TestSearchProvider("omnibox"));
133 providers_
.push_back(new TestSearchProvider("webstore"));
134 providers_
.push_back(new TestSearchProvider("people"));
136 is_voice_query_
= false;
138 mixer_
.reset(new Mixer(results_
.get()));
140 size_t apps_group_id
= mixer_
->AddGroup(kMaxAppsGroupResults
, 3.0, 1.0);
141 size_t omnibox_group_id
=
142 mixer_
->AddOmniboxGroup(kMaxOmniboxResults
, 2.0, 1.0);
143 size_t webstore_group_id
= mixer_
->AddGroup(kMaxWebstoreResults
, 1.0, 0.5);
144 size_t people_group_id
= mixer_
->AddGroup(kMaxPeopleResults
, 0.0, 1.0);
146 mixer_
->AddProviderToGroup(apps_group_id
, providers_
[0]);
147 mixer_
->AddProviderToGroup(omnibox_group_id
, providers_
[1]);
148 mixer_
->AddProviderToGroup(webstore_group_id
, providers_
[2]);
149 mixer_
->AddProviderToGroup(people_group_id
, providers_
[3]);
153 const base::string16 query
;
155 for (size_t i
= 0; i
< providers_
.size(); ++i
) {
156 providers_
[i
]->Start(is_voice_query_
, query
);
157 providers_
[i
]->Stop();
160 mixer_
->MixAndPublish(is_voice_query_
, known_results_
);
163 std::string
GetResults() const {
165 for (size_t i
= 0; i
< results_
->item_count(); ++i
) {
169 result
+= base::UTF16ToUTF8(results_
->GetItemAt(i
)->title());
175 Mixer
* mixer() { return mixer_
.get(); }
176 TestSearchProvider
* app_provider() { return providers_
[0]; }
177 TestSearchProvider
* omnibox_provider() { return providers_
[1]; }
178 TestSearchProvider
* webstore_provider() { return providers_
[2]; }
179 TestSearchProvider
* people_provider() { return providers_
[3]; }
181 // Sets whether test runs should be treated as a voice query.
182 void set_is_voice_query(bool is_voice_query
) {
183 is_voice_query_
= is_voice_query
;
186 void AddKnownResult(const std::string
& id
, KnownResultType type
) {
187 known_results_
[id
] = type
;
191 scoped_ptr
<Mixer
> mixer_
;
192 scoped_ptr
<AppListModel::SearchResults
> results_
;
193 KnownResults known_results_
;
195 bool is_voice_query_
;
197 ScopedVector
<TestSearchProvider
> providers_
;
199 base::FieldTrialList field_trial_list_
;
201 DISALLOW_COPY_AND_ASSIGN(MixerTest
);
204 TEST_P(MixerTest
, Basic
) {
205 // Note: Some cases in |expected_blended| have vastly more results than
206 // others, due to the "at least 6" mechanism. If it gets at least 6 results
207 // from all providers, it stops at 6. If not, it fetches potentially many more
208 // results from all providers. Not ideal, but currently by design.
210 const size_t app_results
;
211 const size_t omnibox_results
;
212 const size_t webstore_results
;
213 const size_t people_results
;
214 const char* expected_default
; // Expected results with trial off.
215 const char* expected_blended
; // Expected results with trial on.
217 {0, 0, 0, 0, "", ""},
222 "app0,app1,app2,app3",
223 "app0,app1,app2,app3,app4,app5,app6,app7,app8,app9"},
228 "webstore0,webstore1",
229 "webstore0,webstore1,webstore2,webstore3,webstore4,webstore5,webstore6,"
230 "webstore7,webstore8,webstore9"},
236 "people0,people1,people2,people3,people4,people5,people6,people7,"
242 "app0,app1,app2,app3,omnibox0,omnibox1",
243 "app0,omnibox0,app1,omnibox1,app2,omnibox2,app3,omnibox3"},
248 "app0,app1,app2,app3,omnibox0,webstore0",
249 "app0,omnibox0,app1,omnibox1,app2,omnibox2,app3,omnibox3,webstore0,"
255 "app0,app1,app2,app3,omnibox0,people0",
256 "app0,omnibox0,people0,app1,omnibox1,people1,app2,omnibox2,app3,"
262 "app0,app1,app2,app3,omnibox0,webstore0",
263 "app0,omnibox0,app1,omnibox1,app2,omnibox2,app3,omnibox3,webstore0,"
269 "omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,omnibox5",
270 "omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,omnibox5,omnibox6,"
271 "omnibox7,omnibox8,omnibox9"},
276 "omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,webstore0",
277 "omnibox0,omnibox1,omnibox2,omnibox3,webstore0,omnibox4,omnibox5,"
278 "omnibox6,omnibox7,omnibox8,omnibox9"},
283 "omnibox0,omnibox1,omnibox2,omnibox3,webstore0,webstore1",
284 "omnibox0,omnibox1,omnibox2,omnibox3,webstore0,webstore1"},
289 "app0,omnibox0,omnibox1,omnibox2,omnibox3,omnibox4",
290 "app0,omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,omnibox5,omnibox6,"
291 "omnibox7,omnibox8,omnibox9"},
296 "app0,app1,omnibox0,omnibox1,omnibox2,omnibox3",
297 "app0,omnibox0,app1,omnibox1,omnibox2,omnibox3"},
302 "app0,app1,omnibox0,omnibox1,omnibox2,webstore0",
303 "app0,omnibox0,app1,omnibox1,omnibox2,omnibox3,webstore0"},
308 "app0,app1,omnibox0,omnibox1,webstore0,webstore1",
309 "app0,omnibox0,app1,omnibox1,omnibox2,omnibox3,webstore0,webstore1"},
314 "app0,app1,webstore0,webstore1",
315 "app0,app1,webstore0,webstore1"},
320 "app0,app1,app2,app3,webstore0,webstore1",
321 "app0,people0,app1,people1,app2,app3,webstore0,webstore1"},
326 "app0,app1,app2,app3,omnibox0,webstore0",
327 "app0,omnibox0,people0,app1,omnibox1,people1,app2,omnibox2,app3,"
328 "omnibox3,webstore0,webstore1"},
329 {0, 0, 0, 0, "", ""},
332 for (size_t i
= 0; i
< arraysize(kTestCases
); ++i
) {
333 app_provider()->set_count(kTestCases
[i
].app_results
);
334 omnibox_provider()->set_count(kTestCases
[i
].omnibox_results
);
335 webstore_provider()->set_count(kTestCases
[i
].webstore_results
);
336 people_provider()->set_count(kTestCases
[i
].people_results
);
339 const char* expected
= GetParam() ? kTestCases
[i
].expected_blended
340 : kTestCases
[i
].expected_default
;
341 EXPECT_EQ(expected
, GetResults()) << "Case " << i
;
345 TEST_P(MixerTest
, RemoveDuplicates
) {
346 const std::string dup
= "dup";
348 // This gives "dup0,dup1,dup2".
349 app_provider()->set_prefix(dup
);
350 app_provider()->set_count(3);
352 // This gives "dup0,dup1".
353 omnibox_provider()->set_prefix(dup
);
354 omnibox_provider()->set_count(2);
356 // This gives "dup0".
357 webstore_provider()->set_prefix(dup
);
358 webstore_provider()->set_count(1);
362 // Only three results with unique id are kept.
363 EXPECT_EQ("dup0,dup1,dup2", GetResults());
366 // Tests that "known results" have priority over others.
367 TEST_P(MixerTest
, KnownResultsPriority
) {
368 // This gives omnibox 0 -- 5.
369 omnibox_provider()->set_count(6);
371 // omnibox 1 -- 4 are "known results".
372 AddKnownResult("omnibox1", PREFIX_SECONDARY
);
373 AddKnownResult("omnibox2", PERFECT_SECONDARY
);
374 AddKnownResult("omnibox3", PREFIX_PRIMARY
);
375 AddKnownResult("omnibox4", PERFECT_PRIMARY
);
379 // omnibox 1 -- 4 should be prioritised over the others. They should be
380 // ordered 4, 3, 2, 1 (in order of match quality).
381 EXPECT_EQ("omnibox4,omnibox3,omnibox2,omnibox1,omnibox0,omnibox5",
385 // Tests that "known results" are not considered for recommendation results.
386 TEST_P(MixerTest
, KnownResultsIgnoredForRecommendations
) {
387 // This gives omnibox 0 -- 5.
388 omnibox_provider()->set_count(6);
389 omnibox_provider()->set_display_type(SearchResult::DISPLAY_RECOMMENDATION
);
391 // omnibox 1 -- 4 are "known results".
392 AddKnownResult("omnibox1", PREFIX_SECONDARY
);
393 AddKnownResult("omnibox2", PERFECT_SECONDARY
);
394 AddKnownResult("omnibox3", PREFIX_PRIMARY
);
395 AddKnownResult("omnibox4", PERFECT_PRIMARY
);
399 // omnibox 1 -- 4 should be unaffected despite being known results.
400 EXPECT_EQ("omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,omnibox5",
404 TEST_P(MixerTest
, VoiceQuery
) {
405 omnibox_provider()->set_count(3);
407 EXPECT_EQ("omnibox0,omnibox1,omnibox2", GetResults());
409 // Set "omnibox1" as a voice result. Do not expect any changes (as this is not
411 omnibox_provider()->set_as_voice_result(1);
413 EXPECT_EQ("omnibox0,omnibox1,omnibox2", GetResults());
415 // Perform a voice query. Expect voice result first.
416 set_is_voice_query(true);
418 EXPECT_EQ("omnibox1,omnibox0,omnibox2", GetResults());
420 // All voice results should appear before non-voice results.
421 omnibox_provider()->set_as_voice_result(2);
423 EXPECT_EQ("omnibox1,omnibox2,omnibox0", GetResults());
426 TEST_P(MixerTest
, BadRelevanceRange
) {
427 // This gives relevance scores: (10.0, 0.0). Even though providers are
428 // supposed to give scores within the range [0.0, 1.0], we cannot rely on
429 // providers to do this, since they retrieve results from disparate and
430 // unreliable sources (like the Google+ API).
431 people_provider()->set_bad_relevance_range();
432 people_provider()->set_count(2);
434 // Give a massive boost to the second result.
435 AddKnownResult("people1", PERFECT_PRIMARY
);
439 // If the results are correctly clamped to the range [0.0, 1.0], the boost to
440 // "people1" will push it over the first result. If not, the massive base
441 // score of "people0" will erroneously keep it on top.
442 EXPECT_EQ("people1,people0", GetResults());
445 TEST_P(MixerTest
, Publish
) {
446 scoped_ptr
<SearchResult
> result1(new TestSearchResult("app1", 0));
447 scoped_ptr
<SearchResult
> result2(new TestSearchResult("app2", 0));
448 scoped_ptr
<SearchResult
> result3(new TestSearchResult("app3", 0));
449 scoped_ptr
<SearchResult
> result3_copy
= result3
->Duplicate();
450 scoped_ptr
<SearchResult
> result4(new TestSearchResult("app4", 0));
451 scoped_ptr
<SearchResult
> result5(new TestSearchResult("app5", 0));
453 AppListModel::SearchResults ui_results
;
455 // Publish the first three results to |ui_results|.
456 Mixer::SortedResults new_results
;
457 new_results
.push_back(Mixer::SortData(result1
.get(), 1.0f
));
458 new_results
.push_back(Mixer::SortData(result2
.get(), 1.0f
));
459 new_results
.push_back(Mixer::SortData(result3
.get(), 1.0f
));
461 Mixer::Publish(new_results
, &ui_results
);
462 EXPECT_EQ(3u, ui_results
.item_count());
463 // The objects in |ui_results| should be new copies because the input results
464 // are owned and |ui_results| needs to own its results as well.
465 EXPECT_NE(TestSearchResult::GetInstanceId(new_results
[0].result
),
466 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(0)));
467 EXPECT_NE(TestSearchResult::GetInstanceId(new_results
[1].result
),
468 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(1)));
469 EXPECT_NE(TestSearchResult::GetInstanceId(new_results
[2].result
),
470 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(2)));
472 // Save the current |ui_results| instance ids for comparison later.
473 std::vector
<int> old_ui_result_ids
;
474 for (size_t i
= 0; i
< ui_results
.item_count(); ++i
) {
475 old_ui_result_ids
.push_back(
476 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(i
)));
479 // Change the first result to a totally new object (with a new ID).
480 new_results
[0] = Mixer::SortData(result4
.get(), 1.0f
);
482 // Change the second result's title, but keep the same id. (The result will
483 // keep the id "app2" but change its title to "New App 2 Title".)
484 const base::string16 kNewAppTitle
= base::UTF8ToUTF16("New App 2 Title");
485 new_results
[1].result
->set_title(kNewAppTitle
);
487 // Change the third result's object address (it points to an object with the
489 new_results
[2] = Mixer::SortData(result3_copy
.get(), 1.0f
);
491 Mixer::Publish(new_results
, &ui_results
);
492 EXPECT_EQ(3u, ui_results
.item_count());
494 // The first result will be a new object, as the ID has changed.
495 EXPECT_NE(old_ui_result_ids
[0],
496 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(0)));
498 // The second result will still use the original object, but have a different
499 // title, since the ID did not change.
500 EXPECT_EQ(old_ui_result_ids
[1],
501 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(1)));
502 EXPECT_EQ(kNewAppTitle
, ui_results
.GetItemAt(1)->title());
504 // The third result will use the original object as the ID did not change.
505 EXPECT_EQ(old_ui_result_ids
[2],
506 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(2)));
508 // Save the current |ui_results| order which should is app4, app2, app3.
509 old_ui_result_ids
.clear();
510 for (size_t i
= 0; i
< ui_results
.item_count(); ++i
) {
511 old_ui_result_ids
.push_back(
512 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(i
)));
515 // Reorder the existing results and add a new one in the second place.
516 new_results
[0] = Mixer::SortData(result2
.get(), 1.0f
);
517 new_results
[1] = Mixer::SortData(result5
.get(), 1.0f
);
518 new_results
[2] = Mixer::SortData(result3
.get(), 1.0f
);
519 new_results
.push_back(Mixer::SortData(result4
.get(), 1.0f
));
521 Mixer::Publish(new_results
, &ui_results
);
522 EXPECT_EQ(4u, ui_results
.item_count());
524 // The reordered results should use the original objects.
525 EXPECT_EQ(old_ui_result_ids
[0],
526 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(3)));
527 EXPECT_EQ(old_ui_result_ids
[1],
528 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(0)));
529 EXPECT_EQ(old_ui_result_ids
[2],
530 TestSearchResult::GetInstanceId(ui_results
.GetItemAt(2)));
533 INSTANTIATE_TEST_CASE_P(MixerTestInstance
, MixerTest
, testing::Bool());
536 } // namespace app_list