OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 int instance_id_; | 51 int instance_id_; |
52 | 52 |
53 DISALLOW_COPY_AND_ASSIGN(TestSearchResult); | 53 DISALLOW_COPY_AND_ASSIGN(TestSearchResult); |
54 }; | 54 }; |
55 int TestSearchResult::instantiation_count = 0; | 55 int TestSearchResult::instantiation_count = 0; |
56 | 56 |
57 class TestSearchProvider : public SearchProvider { | 57 class TestSearchProvider : public SearchProvider { |
58 public: | 58 public: |
59 explicit TestSearchProvider(const std::string& prefix) | 59 explicit TestSearchProvider(const std::string& prefix) |
60 : prefix_(prefix), count_(0) {} | 60 : prefix_(prefix), count_(0), bad_relevance_range_(false) {} |
61 ~TestSearchProvider() override {} | 61 ~TestSearchProvider() override {} |
62 | 62 |
63 // SearchProvider overrides: | 63 // SearchProvider overrides: |
64 void Start(bool is_voice_query, const base::string16& query) override { | 64 void Start(bool is_voice_query, const base::string16& query) override { |
65 ClearResults(); | 65 ClearResults(); |
66 for (size_t i = 0; i < count_; ++i) { | 66 for (size_t i = 0; i < count_; ++i) { |
67 const std::string id = | 67 const std::string id = |
68 base::StringPrintf("%s%d", prefix_.c_str(), static_cast<int>(i)); | 68 base::StringPrintf("%s%d", prefix_.c_str(), static_cast<int>(i)); |
69 const double relevance = 1.0 - i / 10.0; | 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; |
70 TestSearchResult* result = new TestSearchResult(id, relevance); | 74 TestSearchResult* result = new TestSearchResult(id, relevance); |
71 if (voice_result_indices.find(i) != voice_result_indices.end()) | 75 if (voice_result_indices.find(i) != voice_result_indices.end()) |
72 result->set_voice_result(true); | 76 result->set_voice_result(true); |
73 Add(scoped_ptr<SearchResult>(result).Pass()); | 77 Add(scoped_ptr<SearchResult>(result).Pass()); |
74 } | 78 } |
75 } | 79 } |
76 void Stop() override {} | 80 void Stop() override {} |
77 | 81 |
78 void set_prefix(const std::string& prefix) { prefix_ = prefix; } | 82 void set_prefix(const std::string& prefix) { prefix_ = prefix; } |
79 void set_count(size_t count) { count_ = count; } | 83 void set_count(size_t count) { count_ = count; } |
80 void set_as_voice_result(size_t index) { voice_result_indices.insert(index); } | 84 void set_as_voice_result(size_t index) { voice_result_indices.insert(index); } |
| 85 void set_bad_relevance_range() { bad_relevance_range_ = true; } |
81 | 86 |
82 private: | 87 private: |
83 std::string prefix_; | 88 std::string prefix_; |
84 size_t count_; | 89 size_t count_; |
| 90 bool bad_relevance_range_; |
85 // Indices of results that will have the |voice_result| flag set. | 91 // Indices of results that will have the |voice_result| flag set. |
86 std::set<size_t> voice_result_indices; | 92 std::set<size_t> voice_result_indices; |
87 | 93 |
88 DISALLOW_COPY_AND_ASSIGN(TestSearchProvider); | 94 DISALLOW_COPY_AND_ASSIGN(TestSearchProvider); |
89 }; | 95 }; |
90 | 96 |
91 class MixerTest : public testing::Test { | 97 class MixerTest : public testing::Test { |
92 public: | 98 public: |
93 MixerTest() : is_voice_query_(false) {} | 99 MixerTest() : is_voice_query_(false) {} |
94 ~MixerTest() override {} | 100 ~MixerTest() override {} |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 set_is_voice_query(true); | 263 set_is_voice_query(true); |
258 RunQuery(); | 264 RunQuery(); |
259 EXPECT_EQ("omnibox1,omnibox0,omnibox2", GetResults()); | 265 EXPECT_EQ("omnibox1,omnibox0,omnibox2", GetResults()); |
260 | 266 |
261 // All voice results should appear before non-voice results. | 267 // All voice results should appear before non-voice results. |
262 omnibox_provider()->set_as_voice_result(2); | 268 omnibox_provider()->set_as_voice_result(2); |
263 RunQuery(); | 269 RunQuery(); |
264 EXPECT_EQ("omnibox1,omnibox2,omnibox0", GetResults()); | 270 EXPECT_EQ("omnibox1,omnibox2,omnibox0", GetResults()); |
265 } | 271 } |
266 | 272 |
| 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); |
| 280 |
| 281 // Give a massive boost to the second result. |
| 282 AddKnownResult("people1", PERFECT_PRIMARY); |
| 283 |
| 284 RunQuery(); |
| 285 |
| 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()); |
| 290 } |
| 291 |
267 TEST_F(MixerTest, Publish) { | 292 TEST_F(MixerTest, Publish) { |
268 scoped_ptr<SearchResult> result1(new TestSearchResult("app1", 0)); | 293 scoped_ptr<SearchResult> result1(new TestSearchResult("app1", 0)); |
269 scoped_ptr<SearchResult> result2(new TestSearchResult("app2", 0)); | 294 scoped_ptr<SearchResult> result2(new TestSearchResult("app2", 0)); |
270 scoped_ptr<SearchResult> result3(new TestSearchResult("app3", 0)); | 295 scoped_ptr<SearchResult> result3(new TestSearchResult("app3", 0)); |
271 scoped_ptr<SearchResult> result3_copy = result3->Duplicate(); | 296 scoped_ptr<SearchResult> result3_copy = result3->Duplicate(); |
272 scoped_ptr<SearchResult> result4(new TestSearchResult("app4", 0)); | 297 scoped_ptr<SearchResult> result4(new TestSearchResult("app4", 0)); |
273 scoped_ptr<SearchResult> result5(new TestSearchResult("app5", 0)); | 298 scoped_ptr<SearchResult> result5(new TestSearchResult("app5", 0)); |
274 | 299 |
275 AppListModel::SearchResults ui_results; | 300 AppListModel::SearchResults ui_results; |
276 | 301 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 EXPECT_EQ(old_ui_result_ids[0], | 372 EXPECT_EQ(old_ui_result_ids[0], |
348 TestSearchResult::GetInstanceId(ui_results.GetItemAt(3))); | 373 TestSearchResult::GetInstanceId(ui_results.GetItemAt(3))); |
349 EXPECT_EQ(old_ui_result_ids[1], | 374 EXPECT_EQ(old_ui_result_ids[1], |
350 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); | 375 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); |
351 EXPECT_EQ(old_ui_result_ids[2], | 376 EXPECT_EQ(old_ui_result_ids[2], |
352 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); | 377 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); |
353 } | 378 } |
354 | 379 |
355 } // namespace test | 380 } // namespace test |
356 } // namespace app_list | 381 } // namespace app_list |
OLD | NEW |