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

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: 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 typedef std::vector<Mixer::SortData> SortedResults;
20
19 // Maximum number of results to show. 21 // Maximum number of results to show.
20 const size_t kMaxResults = 6; 22 const size_t kMaxResults = 6;
21 const size_t kMaxMainGroupResults = 4; 23 const size_t kMaxMainGroupResults = 4;
22 const size_t kMaxWebstoreResults = 2; 24 const size_t kMaxWebstoreResults = 2;
23 const size_t kMaxPeopleResults = 2; 25 const size_t kMaxPeopleResults = 2;
24 26
25 // A value to indicate no max number of results limit. 27 // A value to indicate no max number of results limit.
26 const size_t kNoMaxResultsLimit = 0; 28 const size_t kNoMaxResultsLimit = 0;
27 29
28 // Used for sorting and mixing results.
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
39 bool operator<(const SortData& other) const {
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|. 30 // Removes duplicates from |results|.
50 void RemoveDuplicates(SortedResults* results) { 31 void RemoveDuplicates(SortedResults* results) {
51 SortedResults final; 32 SortedResults final;
52 final.reserve(results->size()); 33 final.reserve(results->size());
53 34
54 std::set<std::string> id_set; 35 std::set<std::string> id_set;
55 for (SortedResults::iterator it = results->begin(); 36 for (SortedResults::iterator it = results->begin();
56 it != results->end(); 37 it != results->end();
57 ++it) { 38 ++it) {
58 const std::string& id = it->result->id(); 39 const std::string& id = it->result->id();
59 if (id_set.find(id) != id_set.end()) 40 if (id_set.find(id) != id_set.end())
60 continue; 41 continue;
61 42
62 id_set.insert(id); 43 id_set.insert(id);
63 final.push_back(*it); 44 final.push_back(*it);
64 } 45 }
65 46
66 results->swap(final); 47 results->swap(final);
67 } 48 }
68 49
69 // Publishes the given |results| to |ui_results|. Reuse existing ones to avoid
70 // flickering.
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 }
94
95 } // namespace 50 } // namespace
96 51
52 Mixer::SortData::SortData() : result(NULL), score(0.0) {
53 }
54
55 Mixer::SortData::SortData(ChromeSearchResult* result, double score)
56 : result(result), score(score) {
57 }
58
59 bool Mixer::SortData::operator<(const SortData& other) const {
60 // This data precedes (less than) |other| if it has higher score.
61 return score > other.score;
62 }
63
97 // Used to group relevant providers together fox mixing their results. 64 // Used to group relevant providers together fox mixing their results.
98 class Mixer::Group { 65 class Mixer::Group {
99 public: 66 public:
100 Group(size_t max_results, double boost) 67 Group(size_t max_results, double boost)
101 : max_results_(max_results), 68 : max_results_(max_results),
102 boost_(boost) { 69 boost_(boost) {
103 } 70 }
104 ~Group() {} 71 ~Group() {}
105 72
106 void AddProvider(SearchProvider* provider) { 73 void AddProvider(SearchProvider* provider) {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 groups_[OMNIBOX_GROUP]->results().begin() + remaining_slots); 186 groups_[OMNIBOX_GROUP]->results().begin() + remaining_slots);
220 187
221 std::sort(results.begin(), results.end()); 188 std::sort(results.begin(), results.end());
222 RemoveDuplicates(&results); 189 RemoveDuplicates(&results);
223 if (results.size() > kMaxResults) 190 if (results.size() > kMaxResults)
224 results.resize(kMaxResults); 191 results.resize(kMaxResults);
225 192
226 Publish(results, ui_results_); 193 Publish(results, ui_results_);
227 } 194 }
228 195
196 void Mixer::Publish(const std::vector<SortData>& results,
197 AppListModel::SearchResults* ui_results) {
198 for (size_t i = 0; i < results.size(); ++i) {
199 ChromeSearchResult* result = results[i].result;
200
201 ChromeSearchResult* ui_result =
202 i < ui_results->item_count()
203 ? static_cast<ChromeSearchResult*>(ui_results->GetItemAt(i))
204 : NULL;
205 if (ui_result && ui_result->id() == result->id()) {
206 ui_result->set_title(result->title());
207 ui_result->set_title_tags(result->title_tags());
208 ui_result->set_details(result->details());
209 ui_result->set_details_tags(result->details_tags());
210 ui_results->NotifyItemsChanged(i, 1);
211 } else {
212 if (ui_result)
213 ui_results->DeleteAt(i);
214 ui_results->AddAt(i, result->Duplicate().release());
215 }
216 }
217
218 while (ui_results->item_count() > results.size())
219 ui_results->DeleteAt(ui_results->item_count() - 1);
220 }
221
229 void Mixer::FetchResults(const KnownResults& known_results) { 222 void Mixer::FetchResults(const KnownResults& known_results) {
230 for (Groups::iterator group_it = groups_.begin(); 223 for (Groups::iterator group_it = groups_.begin();
231 group_it != groups_.end(); 224 group_it != groups_.end();
232 ++group_it) { 225 ++group_it) {
233 (*group_it)->FetchResults(known_results); 226 (*group_it)->FetchResults(known_results);
234 } 227 }
235 } 228 }
236 229
237 } // namespace app_list 230 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698