Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: chrome/browser/ui/app_list/search/mixer_unittest.cc

Issue 372843003: Add unit test for Mixer::Publish. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add instance ids to TestSearchResult Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/app_list/search/mixer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 static int GetInstanceId(SearchResult* result) {
Matt Giuca 2014/07/09 01:53:22 // For reference equality testing. (Addresses cann
calamity 2014/07/09 02:52:51 Done.
43 return static_cast<const TestSearchResult*>(result)->instance_id_;
44 }
45
46 private:
47 static int instantiation_count;
48
49 int instance_id_;
50
42 DISALLOW_COPY_AND_ASSIGN(TestSearchResult); 51 DISALLOW_COPY_AND_ASSIGN(TestSearchResult);
43 }; 52 };
53 int TestSearchResult::instantiation_count = 0;
44 54
45 class TestSearchProvider : public SearchProvider { 55 class TestSearchProvider : public SearchProvider {
46 public: 56 public:
47 explicit TestSearchProvider(const std::string& prefix) 57 explicit TestSearchProvider(const std::string& prefix)
48 : prefix_(prefix), 58 : prefix_(prefix),
49 count_(0) {} 59 count_(0) {}
50 virtual ~TestSearchProvider() {} 60 virtual ~TestSearchProvider() {}
51 61
52 // SearchProvider overrides: 62 // SearchProvider overrides:
53 virtual void Start(const base::string16& query) OVERRIDE { 63 virtual void Start(const base::string16& query) OVERRIDE {
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 // This gives "dup0". 185 // This gives "dup0".
176 webstore_provider()->set_prefix(dup); 186 webstore_provider()->set_prefix(dup);
177 webstore_provider()->set_count(1); 187 webstore_provider()->set_count(1);
178 188
179 RunQuery(); 189 RunQuery();
180 190
181 // Only three results with unique id are kept. 191 // Only three results with unique id are kept.
182 EXPECT_EQ("dup0,dup1,dup2", GetResults()); 192 EXPECT_EQ("dup0,dup1,dup2", GetResults());
183 } 193 }
184 194
195 TEST_F(MixerTest, Publish) {
196 scoped_ptr<ChromeSearchResult> result1(new TestSearchResult("app1", 0));
197 scoped_ptr<ChromeSearchResult> result2(new TestSearchResult("app2", 0));
198 scoped_ptr<ChromeSearchResult> result3(new TestSearchResult("app3", 0));
199 scoped_ptr<ChromeSearchResult> result3_copy = result3->Duplicate();
200 scoped_ptr<ChromeSearchResult> result4(new TestSearchResult("app4", 0));
201
202 AppListModel::SearchResults ui_results;
203
204 // Publish the first three results to |ui_results|.
205 Mixer::SortedResults new_results;
206 new_results.push_back(Mixer::SortData(result1.get(), 1.0f));
207 new_results.push_back(Mixer::SortData(result2.get(), 1.0f));
208 new_results.push_back(Mixer::SortData(result3.get(), 1.0f));
209
210 Mixer::Publish(new_results, &ui_results);
211 EXPECT_EQ(3u, ui_results.item_count());
212 // The objects in |ui_results| should be new copies because the input results
213 // are owned and |ui_results| needs to own its results as well.
214 EXPECT_NE(new_results[0].result, ui_results.GetItemAt(0));
215 EXPECT_NE(new_results[1].result, ui_results.GetItemAt(1));
216 EXPECT_NE(new_results[2].result, ui_results.GetItemAt(2));
217
218 // Save the current |ui_results| instance ids for comparison later.
219 std::vector<int> old_ui_result_ids;
220 for (size_t i = 0; i < ui_results.item_count(); ++i) {
221 old_ui_result_ids.push_back(
222 TestSearchResult::GetInstanceId(ui_results.GetItemAt(i)));
223 }
224
225 // Change the first result to a totally new object (with a new ID).
226 new_results[0] = Mixer::SortData(result4.get(), 1.0f);
227
228 // Change the second result's title, but keep the same id. (The result will
229 // keep the id "app2" but change its title to "New App 2 Title".)
230 const base::string16 kNewAppTitle = base::UTF8ToUTF16("New App 2 Title");
231 new_results[1].result->set_title(kNewAppTitle);
232
233 // Change the third result's object address (it points to an object with the
234 // same data).
235 new_results[2] = Mixer::SortData(result3_copy.get(), 1.0f);
236
237 Mixer::Publish(new_results, &ui_results);
238 EXPECT_EQ(3u, ui_results.item_count());
239
240 // The first result will be a new object, as the ID has changed.
241 EXPECT_NE(old_ui_result_ids[0],
242 TestSearchResult::GetInstanceId(ui_results.GetItemAt(0)));
243
244 // The second result will still use the original object, but have a different
245 // title, since the ID did not change.
246 EXPECT_EQ(old_ui_result_ids[1],
247 TestSearchResult::GetInstanceId(ui_results.GetItemAt(1)));
248 EXPECT_EQ(kNewAppTitle, ui_results.GetItemAt(1)->title());
249
250 // The third result will use the original object as the ID did not change.
251 EXPECT_EQ(old_ui_result_ids[2],
252 TestSearchResult::GetInstanceId(ui_results.GetItemAt(2)));
253 }
254
185 } // namespace test 255 } // namespace test
186 } // namespace app_list 256 } // namespace app_list
OLDNEW
« no previous file with comments | « chrome/browser/ui/app_list/search/mixer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698