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

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

Issue 372843003: Add unit test for Mixer::Publish. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comment, fix an address comparison that was missed 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 "chrome/browser/ui/app_list/search/mixer.h" 5 #include "chrome/browser/ui/app_list/search/mixer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "chrome/browser/ui/app_list/search/chrome_search_result.h" 12 #include "chrome/browser/ui/app_list/search/chrome_search_result.h"
13 #include "ui/app_list/search_provider.h" 13 #include "ui/app_list/search_provider.h"
14 14
15 namespace app_list { 15 namespace app_list {
16 16
17 namespace { 17 namespace {
18 18
19 // Maximum number of results to show. 19 // Maximum number of results to show.
20 const size_t kMaxResults = 6; 20 const size_t kMaxResults = 6;
21 const size_t kMaxMainGroupResults = 4; 21 const size_t kMaxMainGroupResults = 4;
22 const size_t kMaxWebstoreResults = 2; 22 const size_t kMaxWebstoreResults = 2;
23 const size_t kMaxPeopleResults = 2; 23 const size_t kMaxPeopleResults = 2;
24 24
25 // A value to indicate no max number of results limit. 25 // A value to indicate no max number of results limit.
26 const size_t kNoMaxResultsLimit = 0; 26 const size_t kNoMaxResultsLimit = 0;
27 27
28 // Used for sorting and mixing results. 28 } // namespace
29 struct SortData {
30 SortData()
31 : result(NULL),
32 score(0.0) {
33 }
34 SortData(ChromeSearchResult* result, double score)
35 : result(result),
36 score(score) {
37 }
38 29
39 bool operator<(const SortData& other) const { 30 Mixer::SortData::SortData() : result(NULL), score(0.0) {
40 // This data precedes (less than) |other| if it has higher score.
41 return score > other.score;
42 }
43
44 ChromeSearchResult* result; // Not owned.
45 double score;
46 };
47 typedef std::vector<SortData> SortedResults;
48
49 // Removes duplicates from |results|.
50 void RemoveDuplicates(SortedResults* results) {
51 SortedResults final;
52 final.reserve(results->size());
53
54 std::set<std::string> id_set;
55 for (SortedResults::iterator it = results->begin();
56 it != results->end();
57 ++it) {
58 const std::string& id = it->result->id();
59 if (id_set.find(id) != id_set.end())
60 continue;
61
62 id_set.insert(id);
63 final.push_back(*it);
64 }
65
66 results->swap(final);
67 } 31 }
68 32
69 // Publishes the given |results| to |ui_results|. Reuse existing ones to avoid 33 Mixer::SortData::SortData(ChromeSearchResult* result, double score)
70 // flickering. 34 : result(result), score(score) {
71 void Publish(const SortedResults& results,
72 AppListModel::SearchResults* ui_results) {
73 for (size_t i = 0; i < results.size(); ++i) {
74 ChromeSearchResult* result = results[i].result;
75
76 ChromeSearchResult* ui_result = i < ui_results->item_count() ?
77 static_cast<ChromeSearchResult*>(ui_results->GetItemAt(i)) : NULL;
78 if (ui_result && ui_result->id() == result->id()) {
79 ui_result->set_title(result->title());
80 ui_result->set_title_tags(result->title_tags());
81 ui_result->set_details(result->details());
82 ui_result->set_details_tags(result->details_tags());
83 ui_results->NotifyItemsChanged(i, 1);
84 } else {
85 if (ui_result)
86 ui_results->DeleteAt(i);
87 ui_results->AddAt(i, result->Duplicate().release());
88 }
89 }
90
91 while (ui_results->item_count() > results.size())
92 ui_results->DeleteAt(ui_results->item_count() - 1);
93 } 35 }
94 36
95 } // namespace 37 bool Mixer::SortData::operator<(const SortData& other) const {
38 // This data precedes (less than) |other| if it has higher score.
39 return score > other.score;
40 }
96 41
97 // Used to group relevant providers together fox mixing their results. 42 // Used to group relevant providers together fox mixing their results.
98 class Mixer::Group { 43 class Mixer::Group {
99 public: 44 public:
100 Group(size_t max_results, double boost) 45 Group(size_t max_results, double boost)
101 : max_results_(max_results), 46 : max_results_(max_results),
102 boost_(boost) { 47 boost_(boost) {
103 } 48 }
104 ~Group() {} 49 ~Group() {}
105 50
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 groups_[OMNIBOX_GROUP]->results().begin() + remaining_slots); 164 groups_[OMNIBOX_GROUP]->results().begin() + remaining_slots);
220 165
221 std::sort(results.begin(), results.end()); 166 std::sort(results.begin(), results.end());
222 RemoveDuplicates(&results); 167 RemoveDuplicates(&results);
223 if (results.size() > kMaxResults) 168 if (results.size() > kMaxResults)
224 results.resize(kMaxResults); 169 results.resize(kMaxResults);
225 170
226 Publish(results, ui_results_); 171 Publish(results, ui_results_);
227 } 172 }
228 173
174 void Mixer::Publish(const SortedResults& results,
175 AppListModel::SearchResults* ui_results) {
176 for (size_t i = 0; i < results.size(); ++i) {
177 ChromeSearchResult* result = results[i].result;
178
179 ChromeSearchResult* ui_result =
180 i < ui_results->item_count()
181 ? static_cast<ChromeSearchResult*>(ui_results->GetItemAt(i))
182 : NULL;
183 if (ui_result && ui_result->id() == result->id()) {
184 ui_result->set_title(result->title());
185 ui_result->set_title_tags(result->title_tags());
186 ui_result->set_details(result->details());
187 ui_result->set_details_tags(result->details_tags());
188 ui_results->NotifyItemsChanged(i, 1);
189 } else {
190 if (ui_result)
191 ui_results->DeleteAt(i);
192 ui_results->AddAt(i, result->Duplicate().release());
193 }
194 }
195
196 while (ui_results->item_count() > results.size())
197 ui_results->DeleteAt(ui_results->item_count() - 1);
198 }
199
200 void Mixer::RemoveDuplicates(SortedResults* results) {
201 SortedResults final;
202 final.reserve(results->size());
203
204 std::set<std::string> id_set;
205 for (SortedResults::iterator it = results->begin(); it != results->end();
206 ++it) {
207 const std::string& id = it->result->id();
208 if (id_set.find(id) != id_set.end())
209 continue;
210
211 id_set.insert(id);
212 final.push_back(*it);
213 }
214
215 results->swap(final);
216 }
217
229 void Mixer::FetchResults(const KnownResults& known_results) { 218 void Mixer::FetchResults(const KnownResults& known_results) {
230 for (Groups::iterator group_it = groups_.begin(); 219 for (Groups::iterator group_it = groups_.begin();
231 group_it != groups_.end(); 220 group_it != groups_.end();
232 ++group_it) { 221 ++group_it) {
233 (*group_it)->FetchResults(known_results); 222 (*group_it)->FetchResults(known_results);
234 } 223 }
235 } 224 }
236 225
237 } // namespace app_list 226 } // namespace app_list
OLDNEW
« no previous file with comments | « chrome/browser/ui/app_list/search/mixer.h ('k') | chrome/browser/ui/app_list/search/mixer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698