mixer_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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. 4 5#include <string> 6 7#include "base/memory/scoped_vector.h" 8#include "base/strings/string16.h" 9#include "base/strings/stringprintf.h" 10#include "base/strings/utf_string_conversions.h" 11#include "chrome/browser/ui/app_list/search/chrome_search_result.h" 12#include "chrome/browser/ui/app_list/search/history_types.h" 13#include "chrome/browser/ui/app_list/search/mixer.h" 14#include "testing/gtest/include/gtest/gtest.h" 15#include "ui/app_list/app_list_model.h" 16#include "ui/app_list/search_provider.h" 17 18namespace app_list { 19namespace test { 20 21class TestSearchResult : public ChromeSearchResult { 22 public: 23 TestSearchResult(const std::string& id, double relevance) 24 : instance_id_(instantiation_count++) { 25 set_id(id); 26 set_title(base::UTF8ToUTF16(id)); 27 set_relevance(relevance); 28 } 29 virtual ~TestSearchResult() {} 30 31 // ChromeSearchResult overides: 32 virtual void Open(int event_flags) OVERRIDE {} 33 virtual void InvokeAction(int action_index, int event_flags) OVERRIDE {} 34 virtual scoped_ptr<ChromeSearchResult> Duplicate() OVERRIDE { 35 return scoped_ptr<ChromeSearchResult>( 36 new TestSearchResult(id(), relevance())).Pass(); 37 } 38 virtual ChromeSearchResultType GetType() OVERRIDE { 39 return SEARCH_RESULT_TYPE_BOUNDARY; 40 } 41 42 // For reference equality testing. (Addresses cannot be used to test reference 43 // equality because it is possible that an object will be allocated at the 44 // same address as a previously deleted one.) 45 static int GetInstanceId(SearchResult* result) { 46 return static_cast<const TestSearchResult*>(result)->instance_id_; 47 } 48 49 private: 50 static int instantiation_count; 51 52 int instance_id_; 53 54 DISALLOW_COPY_AND_ASSIGN(TestSearchResult); 55}; 56int TestSearchResult::instantiation_count = 0; 57 58class TestSearchProvider : public SearchProvider { 59 public: 60 explicit TestSearchProvider(const std::string& prefix) 61 : prefix_(prefix), 62 count_(0) {} 63 virtual ~TestSearchProvider() {} 64 65 // SearchProvider overrides: 66 virtual void Start(const base::string16& query) OVERRIDE { 67 ClearResults(); 68 for (size_t i = 0; i < count_; ++i) { 69 const std::string id = 70 base::StringPrintf("%s%d", prefix_.c_str(), static_cast<int>(i)); 71 const double relevance = 1.0 - i / 10.0; 72 Add(scoped_ptr<SearchResult>(new TestSearchResult(id, relevance)).Pass()); 73 } 74 } 75 virtual void Stop() OVERRIDE {} 76 77 void set_prefix(const std::string& prefix) { prefix_ = prefix; } 78 void set_count(size_t count) { count_ = count; } 79 80 private: 81 std::string prefix_; 82 size_t count_; 83 84 DISALLOW_COPY_AND_ASSIGN(TestSearchProvider); 85}; 86 87class MixerTest : public testing::Test { 88 public: 89 MixerTest() {} 90 virtual ~MixerTest() {} 91 92 // testing::Test overrides: 93 virtual void SetUp() OVERRIDE { 94 results_.reset(new AppListModel::SearchResults); 95 96 providers_.push_back(new TestSearchProvider("app")); 97 providers_.push_back(new TestSearchProvider("omnibox")); 98 providers_.push_back(new TestSearchProvider("webstore")); 99 providers_.push_back(new TestSearchProvider("people")); 100 101 mixer_.reset(new Mixer(results_.get())); 102 mixer_->Init(); 103 mixer_->AddProviderToGroup(Mixer::MAIN_GROUP, providers_[0]); 104 mixer_->AddProviderToGroup(Mixer::OMNIBOX_GROUP, providers_[1]); 105 mixer_->AddProviderToGroup(Mixer::WEBSTORE_GROUP, providers_[2]); 106 mixer_->AddProviderToGroup(Mixer::PEOPLE_GROUP, providers_[3]); 107 } 108 109 void RunQuery() { 110 const base::string16 query; 111 112 for (size_t i = 0; i < providers_.size(); ++i) { 113 providers_[i]->Start(query); 114 providers_[i]->Stop(); 115 } 116 117 mixer_->MixAndPublish(KnownResults()); 118 } 119 120 std::string GetResults() const { 121 std::string result; 122 for (size_t i = 0; i < results_->item_count(); ++i) { 123 if (!result.empty()) 124 result += ','; 125 126 result += base::UTF16ToUTF8(results_->GetItemAt(i)->title()); 127 } 128 129 return result; 130 } 131 132 Mixer* mixer() { return mixer_.get(); } 133 TestSearchProvider* app_provider() { return providers_[0]; } 134 TestSearchProvider* omnibox_provider() { return providers_[1]; } 135 TestSearchProvider* webstore_provider() { return providers_[2]; } 136 137 private: 138 scoped_ptr<Mixer> mixer_; 139 scoped_ptr<AppListModel::SearchResults> results_; 140 141 ScopedVector<TestSearchProvider> providers_; 142 143 DISALLOW_COPY_AND_ASSIGN(MixerTest); 144}; 145 146TEST_F(MixerTest, Basic) { 147 struct TestCase { 148 const size_t app_results; 149 const size_t omnibox_results; 150 const size_t webstore_results; 151 const char* expected; 152 } kTestCases[] = { 153 {0, 0, 0, ""}, 154 {4, 6, 2, "app0,app1,app2,app3,omnibox0,webstore0"}, 155 {10, 10, 10, "app0,app1,app2,app3,omnibox0,webstore0"}, 156 {0, 10, 0, "omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,omnibox5"}, 157 {0, 10, 1, "omnibox0,omnibox1,omnibox2,omnibox3,omnibox4,webstore0"}, 158 {0, 10, 2, "omnibox0,omnibox1,omnibox2,omnibox3,webstore0,webstore1"}, 159 {1, 10, 0, "app0,omnibox0,omnibox1,omnibox2,omnibox3,omnibox4"}, 160 {2, 10, 0, "app0,app1,omnibox0,omnibox1,omnibox2,omnibox3"}, 161 {2, 10, 1, "app0,app1,omnibox0,omnibox1,omnibox2,webstore0"}, 162 {2, 10, 2, "app0,app1,omnibox0,omnibox1,webstore0,webstore1"}, 163 {2, 0, 2, "app0,app1,webstore0,webstore1"}, 164 {0, 0, 0, ""}, 165 }; 166 167 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { 168 app_provider()->set_count(kTestCases[i].app_results); 169 omnibox_provider()->set_count(kTestCases[i].omnibox_results); 170 webstore_provider()->set_count(kTestCases[i].webstore_results); 171 RunQuery(); 172 173 EXPECT_EQ(kTestCases[i].expected, GetResults()) << "Case " << i; 174 } 175} 176 177TEST_F(MixerTest, RemoveDuplicates) { 178 const std::string dup = "dup"; 179 180 // This gives "dup0,dup1,dup2". 181 app_provider()->set_prefix(dup); 182 app_provider()->set_count(3); 183 184 // This gives "dup0,dup1". 185 omnibox_provider()->set_prefix(dup); 186 omnibox_provider()->set_count(2); 187 188 // This gives "dup0". 189 webstore_provider()->set_prefix(dup); 190 webstore_provider()->set_count(1); 191 192 RunQuery(); 193 194 // Only three results with unique id are kept. 195 EXPECT_EQ("dup0,dup1,dup2", GetResults()); 196} 197 198TEST_F(MixerTest, Publish) { 199 scoped_ptr<ChromeSearchResult> result1(new TestSearchResult("app1", 0)); 200 scoped_ptr<ChromeSearchResult> result2(new TestSearchResult("app2", 0)); 201 scoped_ptr<ChromeSearchResult> result3(new TestSearchResult("app3", 0)); 202 scoped_ptr<ChromeSearchResult> result3_copy = result3->Duplicate(); 203 scoped_ptr<ChromeSearchResult> result4(new TestSearchResult("app4", 0)); 204 scoped_ptr<ChromeSearchResult> result5(new TestSearchResult("app5", 0)); 205 206 AppListModel::SearchResults ui_results; 207 208 // Publish the first three results to |ui_results|. 209 Mixer::SortedResults new_results; 210 new_results.push_back(Mixer::SortData(result1.get(), 1.0f)); 211 new_results.push_back(Mixer::SortData(result2.get(), 1.0f)); 212 new_results.push_back(Mixer::SortData(result3.get(), 1.0f)); 213 214 Mixer::Publish(new_results, &ui_results); 215 EXPECT_EQ(3u, ui_results.item_count()); 216 // The objects in |ui_results| should be new copies because the input results 217 // are owned and |ui_results| needs to own its results as well. 218 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[0].result), 219 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); 220 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[1].result), 221 TestSearchResult::GetInstanceId(ui_results.GetItemAt(1))); 222 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[2].result), 223 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); 224 225 // Save the current |ui_results| instance ids for comparison later. 226 std::vector<int> old_ui_result_ids; 227 for (size_t i = 0; i < ui_results.item_count(); ++i) { 228 old_ui_result_ids.push_back( 229 TestSearchResult::GetInstanceId(ui_results.GetItemAt(i))); 230 } 231 232 // Change the first result to a totally new object (with a new ID). 233 new_results[0] = Mixer::SortData(result4.get(), 1.0f); 234 235 // Change the second result's title, but keep the same id. (The result will 236 // keep the id "app2" but change its title to "New App 2 Title".) 237 const base::string16 kNewAppTitle = base::UTF8ToUTF16("New App 2 Title"); 238 new_results[1].result->set_title(kNewAppTitle); 239 240 // Change the third result's object address (it points to an object with the 241 // same data). 242 new_results[2] = Mixer::SortData(result3_copy.get(), 1.0f); 243 244 Mixer::Publish(new_results, &ui_results); 245 EXPECT_EQ(3u, ui_results.item_count()); 246 247 // The first result will be a new object, as the ID has changed. 248 EXPECT_NE(old_ui_result_ids[0], 249 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); 250 251 // The second result will still use the original object, but have a different 252 // title, since the ID did not change. 253 EXPECT_EQ(old_ui_result_ids[1], 254 TestSearchResult::GetInstanceId(ui_results.GetItemAt(1))); 255 EXPECT_EQ(kNewAppTitle, ui_results.GetItemAt(1)->title()); 256 257 // The third result will use the original object as the ID did not change. 258 EXPECT_EQ(old_ui_result_ids[2], 259 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); 260 261 // Save the current |ui_results| order which should is app4, app2, app3. 262 old_ui_result_ids.clear(); 263 for (size_t i = 0; i < ui_results.item_count(); ++i) { 264 old_ui_result_ids.push_back( 265 TestSearchResult::GetInstanceId(ui_results.GetItemAt(i))); 266 } 267 268 // Reorder the existing results and add a new one in the second place. 269 new_results[0] = Mixer::SortData(result2.get(), 1.0f); 270 new_results[1] = Mixer::SortData(result5.get(), 1.0f); 271 new_results[2] = Mixer::SortData(result3.get(), 1.0f); 272 new_results.push_back(Mixer::SortData(result4.get(), 1.0f)); 273 274 Mixer::Publish(new_results, &ui_results); 275 EXPECT_EQ(4u, ui_results.item_count()); 276 277 // The reordered results should use the original objects. 278 EXPECT_EQ(old_ui_result_ids[0], 279 TestSearchResult::GetInstanceId(ui_results.GetItemAt(3))); 280 EXPECT_EQ(old_ui_result_ids[1], 281 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); 282 EXPECT_EQ(old_ui_result_ids[2], 283 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); 284} 285 286} // namespace test 287} // namespace app_list 288