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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/memory/scoped_vector.h" | 7 #include "base/memory/scoped_vector.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/ui/app_list/search/chrome_search_result.h" | 11 #include "chrome/browser/ui/app_list/search/chrome_search_result.h" |
12 #include "chrome/browser/ui/app_list/search/history_types.h" | 12 #include "chrome/browser/ui/app_list/search/history_types.h" |
13 #include "chrome/browser/ui/app_list/search/mixer.h" | 13 #include "chrome/browser/ui/app_list/search/mixer.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "ui/app_list/app_list_model.h" | 15 #include "ui/app_list/app_list_model.h" |
16 #include "ui/app_list/search_provider.h" | 16 #include "ui/app_list/search_provider.h" |
17 | 17 |
18 namespace app_list { | 18 namespace app_list { |
19 namespace test { | 19 namespace test { |
20 | 20 |
21 class TestSearchResult : public ChromeSearchResult { | 21 class TestSearchResult : public ChromeSearchResult { |
22 public: | 22 public: |
23 TestSearchResult(const std::string& id, double relevance) { | 23 TestSearchResult(const std::string& id, double relevance) |
| 24 : instance_id_(instantiation_count++) { |
24 set_id(id); | 25 set_id(id); |
25 set_title(base::UTF8ToUTF16(id)); | 26 set_title(base::UTF8ToUTF16(id)); |
26 set_relevance(relevance); | 27 set_relevance(relevance); |
27 } | 28 } |
28 virtual ~TestSearchResult() {} | 29 virtual ~TestSearchResult() {} |
29 | 30 |
30 private: | |
31 // ChromeSearchResult overides: | 31 // ChromeSearchResult overides: |
32 virtual void Open(int event_flags) OVERRIDE {} | 32 virtual void Open(int event_flags) OVERRIDE {} |
33 virtual void InvokeAction(int action_index, int event_flags) OVERRIDE {} | 33 virtual void InvokeAction(int action_index, int event_flags) OVERRIDE {} |
34 virtual scoped_ptr<ChromeSearchResult> Duplicate() OVERRIDE { | 34 virtual scoped_ptr<ChromeSearchResult> Duplicate() OVERRIDE { |
35 return scoped_ptr<ChromeSearchResult>( | 35 return scoped_ptr<ChromeSearchResult>( |
36 new TestSearchResult(id(), relevance())).Pass(); | 36 new TestSearchResult(id(), relevance())).Pass(); |
37 } | 37 } |
38 virtual ChromeSearchResultType GetType() OVERRIDE { | 38 virtual ChromeSearchResultType GetType() OVERRIDE { |
39 return SEARCH_RESULT_TYPE_BOUNDARY; | 39 return SEARCH_RESULT_TYPE_BOUNDARY; |
40 } | 40 } |
41 | 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 |
42 DISALLOW_COPY_AND_ASSIGN(TestSearchResult); | 54 DISALLOW_COPY_AND_ASSIGN(TestSearchResult); |
43 }; | 55 }; |
| 56 int TestSearchResult::instantiation_count = 0; |
44 | 57 |
45 class TestSearchProvider : public SearchProvider { | 58 class TestSearchProvider : public SearchProvider { |
46 public: | 59 public: |
47 explicit TestSearchProvider(const std::string& prefix) | 60 explicit TestSearchProvider(const std::string& prefix) |
48 : prefix_(prefix), | 61 : prefix_(prefix), |
49 count_(0) {} | 62 count_(0) {} |
50 virtual ~TestSearchProvider() {} | 63 virtual ~TestSearchProvider() {} |
51 | 64 |
52 // SearchProvider overrides: | 65 // SearchProvider overrides: |
53 virtual void Start(const base::string16& query) OVERRIDE { | 66 virtual void Start(const base::string16& query) OVERRIDE { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // This gives "dup0". | 188 // This gives "dup0". |
176 webstore_provider()->set_prefix(dup); | 189 webstore_provider()->set_prefix(dup); |
177 webstore_provider()->set_count(1); | 190 webstore_provider()->set_count(1); |
178 | 191 |
179 RunQuery(); | 192 RunQuery(); |
180 | 193 |
181 // Only three results with unique id are kept. | 194 // Only three results with unique id are kept. |
182 EXPECT_EQ("dup0,dup1,dup2", GetResults()); | 195 EXPECT_EQ("dup0,dup1,dup2", GetResults()); |
183 } | 196 } |
184 | 197 |
| 198 TEST_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 |
| 205 AppListModel::SearchResults ui_results; |
| 206 |
| 207 // Publish the first three results to |ui_results|. |
| 208 Mixer::SortedResults new_results; |
| 209 new_results.push_back(Mixer::SortData(result1.get(), 1.0f)); |
| 210 new_results.push_back(Mixer::SortData(result2.get(), 1.0f)); |
| 211 new_results.push_back(Mixer::SortData(result3.get(), 1.0f)); |
| 212 |
| 213 Mixer::Publish(new_results, &ui_results); |
| 214 EXPECT_EQ(3u, ui_results.item_count()); |
| 215 // The objects in |ui_results| should be new copies because the input results |
| 216 // are owned and |ui_results| needs to own its results as well. |
| 217 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[0].result), |
| 218 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); |
| 219 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[1].result), |
| 220 TestSearchResult::GetInstanceId(ui_results.GetItemAt(1))); |
| 221 EXPECT_NE(TestSearchResult::GetInstanceId(new_results[2].result), |
| 222 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); |
| 223 |
| 224 // Save the current |ui_results| instance ids for comparison later. |
| 225 std::vector<int> old_ui_result_ids; |
| 226 for (size_t i = 0; i < ui_results.item_count(); ++i) { |
| 227 old_ui_result_ids.push_back( |
| 228 TestSearchResult::GetInstanceId(ui_results.GetItemAt(i))); |
| 229 } |
| 230 |
| 231 // Change the first result to a totally new object (with a new ID). |
| 232 new_results[0] = Mixer::SortData(result4.get(), 1.0f); |
| 233 |
| 234 // Change the second result's title, but keep the same id. (The result will |
| 235 // keep the id "app2" but change its title to "New App 2 Title".) |
| 236 const base::string16 kNewAppTitle = base::UTF8ToUTF16("New App 2 Title"); |
| 237 new_results[1].result->set_title(kNewAppTitle); |
| 238 |
| 239 // Change the third result's object address (it points to an object with the |
| 240 // same data). |
| 241 new_results[2] = Mixer::SortData(result3_copy.get(), 1.0f); |
| 242 |
| 243 Mixer::Publish(new_results, &ui_results); |
| 244 EXPECT_EQ(3u, ui_results.item_count()); |
| 245 |
| 246 // The first result will be a new object, as the ID has changed. |
| 247 EXPECT_NE(old_ui_result_ids[0], |
| 248 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0))); |
| 249 |
| 250 // The second result will still use the original object, but have a different |
| 251 // title, since the ID did not change. |
| 252 EXPECT_EQ(old_ui_result_ids[1], |
| 253 TestSearchResult::GetInstanceId(ui_results.GetItemAt(1))); |
| 254 EXPECT_EQ(kNewAppTitle, ui_results.GetItemAt(1)->title()); |
| 255 |
| 256 // The third result will use the original object as the ID did not change. |
| 257 EXPECT_EQ(old_ui_result_ids[2], |
| 258 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2))); |
| 259 } |
| 260 |
185 } // namespace test | 261 } // namespace test |
186 } // namespace app_list | 262 } // namespace app_list |
OLD | NEW |