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

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: use testing macros for friendliness 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
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 set_id(id); 24 set_id(id);
25 set_title(base::UTF8ToUTF16(id)); 25 set_title(base::UTF8ToUTF16(id));
26 set_relevance(relevance); 26 set_relevance(relevance);
27 } 27 }
28 virtual ~TestSearchResult() {} 28 virtual ~TestSearchResult() {}
29 29
30 private:
31 // ChromeSearchResult overides: 30 // ChromeSearchResult overides:
32 virtual void Open(int event_flags) OVERRIDE {} 31 virtual void Open(int event_flags) OVERRIDE {}
33 virtual void InvokeAction(int action_index, int event_flags) OVERRIDE {} 32 virtual void InvokeAction(int action_index, int event_flags) OVERRIDE {}
34 virtual scoped_ptr<ChromeSearchResult> Duplicate() OVERRIDE { 33 virtual scoped_ptr<ChromeSearchResult> Duplicate() OVERRIDE {
35 return scoped_ptr<ChromeSearchResult>( 34 return scoped_ptr<ChromeSearchResult>(
36 new TestSearchResult(id(), relevance())).Pass(); 35 new TestSearchResult(id(), relevance())).Pass();
37 } 36 }
38 virtual ChromeSearchResultType GetType() OVERRIDE { 37 virtual ChromeSearchResultType GetType() OVERRIDE {
39 return SEARCH_RESULT_TYPE_BOUNDARY; 38 return SEARCH_RESULT_TYPE_BOUNDARY;
40 } 39 }
41 40
41 private:
42 DISALLOW_COPY_AND_ASSIGN(TestSearchResult); 42 DISALLOW_COPY_AND_ASSIGN(TestSearchResult);
43 }; 43 };
44 44
45 class TestSearchProvider : public SearchProvider { 45 class TestSearchProvider : public SearchProvider {
46 public: 46 public:
47 explicit TestSearchProvider(const std::string& prefix) 47 explicit TestSearchProvider(const std::string& prefix)
48 : prefix_(prefix), 48 : prefix_(prefix),
49 count_(0) {} 49 count_(0) {}
50 virtual ~TestSearchProvider() {} 50 virtual ~TestSearchProvider() {}
51 51
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 // This gives "dup0". 175 // This gives "dup0".
176 webstore_provider()->set_prefix(dup); 176 webstore_provider()->set_prefix(dup);
177 webstore_provider()->set_count(1); 177 webstore_provider()->set_count(1);
178 178
179 RunQuery(); 179 RunQuery();
180 180
181 // Only three results with unique id are kept. 181 // Only three results with unique id are kept.
182 EXPECT_EQ("dup0,dup1,dup2", GetResults()); 182 EXPECT_EQ("dup0,dup1,dup2", GetResults());
183 } 183 }
184 184
185 TEST_F(MixerTest, Publish) {
186 scoped_ptr<ChromeSearchResult> result1(new TestSearchResult("app1", 0));
187 scoped_ptr<ChromeSearchResult> result2(new TestSearchResult("app2", 0));
188 scoped_ptr<ChromeSearchResult> result3(new TestSearchResult("app3", 0));
189 scoped_ptr<ChromeSearchResult> result3_copy = result3->Duplicate();
190 scoped_ptr<ChromeSearchResult> result4(new TestSearchResult("app4", 0));
191
192 AppListModel::SearchResults ui_results;
193
194 // Publish the first three results to |ui_results|.
195 Mixer::SortedResults new_results;
196 new_results.push_back(Mixer::SortData(result1.get(), 1.0f));
197 new_results.push_back(Mixer::SortData(result2.get(), 1.0f));
198 new_results.push_back(Mixer::SortData(result3.get(), 1.0f));
199
200 mixer()->Publish(new_results, &ui_results);
201 EXPECT_EQ(3u, ui_results.item_count());
202 // The objects in |ui_results| should be new copies because the input results
203 // are owned and |ui_results| needs to own its results as well.
204 EXPECT_NE(new_results[0].result, ui_results.GetItemAt(0));
205 EXPECT_NE(new_results[1].result, ui_results.GetItemAt(1));
206 EXPECT_NE(new_results[2].result, ui_results.GetItemAt(2));
207
208 // Save the current |ui_results| for comparison later.
209 std::vector<const SearchResult*> old_ui_results;
210 for (size_t i = 0; i < ui_results.item_count(); ++i)
211 old_ui_results.push_back(ui_results.GetItemAt(i));
212
213 // Change the first result to a totally new object (with a new ID).
214 new_results[0] = Mixer::SortData(result4.get(), 1.0f);
215
216 // Change the second result's title, but keep the same id. (The result will
217 // keep the id "app2" but change its title to "New App 2 Title".)
218 const base::string16 kNewAppTitle = base::UTF8ToUTF16("New App 2 Title");
219 new_results[1].result->set_title(kNewAppTitle);
220
221 // Change the third result's object address (it points to an object with the
222 // same data).
223 new_results[2] = Mixer::SortData(result3_copy.get(), 1.0f);
224
225 mixer()->Publish(new_results, &ui_results);
226 EXPECT_EQ(3u, ui_results.item_count());
227
228 // The first result will be a new object, as the ID has changed.
229 EXPECT_NE(old_ui_results[0], ui_results.GetItemAt(0));
230
231 // The second result will still use the original object, but have a different
232 // title, since the ID did not change.
233 EXPECT_EQ(old_ui_results[1], ui_results.GetItemAt(1));
234 EXPECT_EQ(kNewAppTitle, ui_results.GetItemAt(1)->title());
235
236 // The third result will use the original object as the ID did not change.
237 EXPECT_EQ(old_ui_results[2], ui_results.GetItemAt(2));
238 }
239
185 } // namespace test 240 } // namespace test
186 } // namespace app_list 241 } // namespace app_list
OLDNEW
« chrome/browser/ui/app_list/search/mixer.h ('K') | « 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