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

Side by Side Diff: chrome/browser/autocomplete/contact_provider_chromeos_unittest.cc

Issue 10916304: autocomplete: Make ContactProvider rank contacts by affinity (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/autocomplete/contact_provider_chromeos.h" 5 #include "chrome/browser/autocomplete/contact_provider_chromeos.h"
6 6
7 #include <cmath>
7 #include <map> 8 #include <map>
8 #include <string> 9 #include <string>
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/string_number_conversions.h"
13 #include "base/string16.h" 15 #include "base/string16.h"
14 #include "base/utf_string_conversions.h" 16 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/autocomplete/autocomplete_input.h" 17 #include "chrome/browser/autocomplete/autocomplete_input.h"
16 #include "chrome/browser/autocomplete/autocomplete_match.h" 18 #include "chrome/browser/autocomplete/autocomplete_match.h"
17 #include "chrome/browser/autocomplete/autocomplete_provider.h" 19 #include "chrome/browser/autocomplete/autocomplete_provider.h"
18 #include "chrome/browser/chromeos/contacts/contact.pb.h" 20 #include "chrome/browser/chromeos/contacts/contact.pb.h"
19 #include "chrome/browser/chromeos/contacts/contact_manager_stub.h" 21 #include "chrome/browser/chromeos/contacts/contact_manager_stub.h"
20 #include "chrome/browser/chromeos/contacts/contact_test_util.h" 22 #include "chrome/browser/chromeos/contacts/contact_test_util.h"
21 #include "chrome/test/base/testing_browser_process.h" 23 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h" 24 #include "chrome/test/base/testing_profile.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 contact_provider_->Start( 68 contact_provider_->Start(
67 AutocompleteInput(UTF8ToUTF16(utf8_text), 69 AutocompleteInput(UTF8ToUTF16(utf8_text),
68 string16(), // desired_tld 70 string16(), // desired_tld
69 false, // prevent_inline_autocomplete 71 false, // prevent_inline_autocomplete
70 false, // prefer_keyword 72 false, // prefer_keyword
71 false, // allow_exact_keyword_match 73 false, // allow_exact_keyword_match
72 AutocompleteInput::ALL_MATCHES), 74 AutocompleteInput::ALL_MATCHES),
73 false); // minimal_changes 75 false); // minimal_changes
74 } 76 }
75 77
78 // Returns the contact ID in |match|'s additional info, or an empty string if
79 // no ID is present.
80 std::string GetContactIdFromMatch(const AutocompleteMatch& match) {
81 std::map<std::string, std::string>::const_iterator it =
Peter Kasting 2012/09/13 23:32:37 Nit: Use AutocompleteMatch::AdditionalInfo::const_
Daniel Erat 2012/09/14 15:31:50 Done.
82 match.additional_info.find(ContactProvider::kMatchContactIdKey);
83 return it != match.additional_info.end() ? it->second : std::string();
84 }
85
76 // Returns pointers to all of the Contact objects referenced in 86 // Returns pointers to all of the Contact objects referenced in
77 // |contact_provider_|'s current results. 87 // |contact_provider_|'s current results.
78 contacts::ContactPointers GetMatchedContacts() { 88 contacts::ContactPointers GetMatchedContacts() {
79 contacts::ContactPointers contacts; 89 contacts::ContactPointers contacts;
80 const ACMatches& matches = contact_provider_->matches(); 90 const ACMatches& matches = contact_provider_->matches();
81 for (size_t i = 0; i < matches.size(); ++i) { 91 for (size_t i = 0; i < matches.size(); ++i) {
82 std::map<std::string, std::string>::const_iterator id_it = 92 const contacts::Contact* contact = contact_manager_->GetContactById(
83 matches[i].additional_info.find(ContactProvider::kMatchContactIdKey); 93 profile_, GetContactIdFromMatch(matches[i]));
84 CHECK(id_it != matches[i].additional_info.end()); 94 CHECK(contact) << "Unable to find contact " << i;
Peter Kasting 2012/09/13 23:32:37 Nit: Avoid [D]CHECK in tests, use ASSERT (2 places
Daniel Erat 2012/09/14 15:31:50 I think that the testing macros won't work here.
Peter Kasting 2012/09/14 18:05:52 That's because this function returns non-void. If
85 const contacts::Contact* contact =
86 contact_manager_->GetContactById(profile_, id_it->second);
87 CHECK(contact) << "Unable to find contact with ID " << id_it->second;
88 contacts.push_back(contact); 95 contacts.push_back(contact);
89 } 96 }
90 return contacts; 97 return contacts;
91 } 98 }
92 99
93 // Returns a semicolon-separated string containing string representations (as 100 // Returns a semicolon-separated string containing string representations (as
94 // provided by AutocompleteMatch::ClassificationsToString()) of the 101 // provided by AutocompleteMatch::ClassificationsToString()) of the
95 // |contents_class| fields of all current matches. Results are sorted by 102 // |contents_class| fields of all current matches. Results are sorted by
96 // contact ID. 103 // contact ID.
97 std::string GetMatchClassifications() { 104 std::string GetMatchClassifications() {
98 typedef std::map<std::string, std::string> StringMap; 105 typedef std::map<std::string, std::string> StringMap;
99 StringMap contact_id_classifications; 106 StringMap contact_id_classifications;
100 const ACMatches& matches = contact_provider_->matches(); 107 const ACMatches& matches = contact_provider_->matches();
101 for (size_t i = 0; i < matches.size(); ++i) { 108 for (size_t i = 0; i < matches.size(); ++i) {
102 std::map<std::string, std::string>::const_iterator id_it = 109 std::string id = GetContactIdFromMatch(matches[i]);
103 matches[i].additional_info.find(ContactProvider::kMatchContactIdKey); 110 CHECK(!id.empty()) << "Unable to find contact " << i;
104 CHECK(id_it != matches[i].additional_info.end()); 111 contact_id_classifications[id] =
105 contact_id_classifications[id_it->second] =
106 AutocompleteMatch::ClassificationsToString(matches[i].contents_class); 112 AutocompleteMatch::ClassificationsToString(matches[i].contents_class);
107 } 113 }
108 114
109 std::string result; 115 std::string result;
110 for (StringMap::const_iterator it = contact_id_classifications.begin(); 116 for (StringMap::const_iterator it = contact_id_classifications.begin();
111 it != contact_id_classifications.end(); ++it) { 117 it != contact_id_classifications.end(); ++it) {
112 if (!result.empty()) 118 if (!result.empty())
113 result += ";"; 119 result += ";";
114 result += it->second; 120 result += it->second;
115 } 121 }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 contacts::test::VarContactsToString(1, contact.get()), 226 contacts::test::VarContactsToString(1, contact.get()),
221 contacts::test::ContactsToString(GetMatchedContacts())); 227 contacts::test::ContactsToString(GetMatchedContacts()));
222 EXPECT_EQ("0,2,5,0", GetMatchClassifications()); 228 EXPECT_EQ("0,2,5,0", GetMatchClassifications());
223 229
224 StartQuery("adelsvard"); 230 StartQuery("adelsvard");
225 EXPECT_EQ( 231 EXPECT_EQ(
226 contacts::test::VarContactsToString(1, contact.get()), 232 contacts::test::VarContactsToString(1, contact.get()),
227 contacts::test::ContactsToString(GetMatchedContacts())); 233 contacts::test::ContactsToString(GetMatchedContacts()));
228 EXPECT_EQ("0,0,6,2", GetMatchClassifications()); 234 EXPECT_EQ("0,0,6,2", GetMatchClassifications());
229 } 235 }
236
237 TEST_F(ContactProviderTest, Relevance) {
238 // Create more contacts than the maximum number of results that an
239 // AutocompleteProvider should return. Give them all the same family name and
240 // ascending affinities from 0.0 to 1.0.
241 const size_t kNumContacts = AutocompleteProvider::kMaxMatches + 1;
242 const std::string kFamilyName = "Jones";
243
244 ScopedVector<contacts::Contact> contacts;
245 contacts::ContactPointers contact_pointers;
246 for (size_t i = 0; i < kNumContacts; ++i) {
247 contacts::Contact* contact = new contacts::Contact;
248 std::string id_string = base::IntToString(static_cast<int>(i));
249 InitContact(id_string, id_string, kFamilyName,
250 id_string + " " + kFamilyName, contact);
251 contact->set_affinity(static_cast<float>(i) / kNumContacts);
252 contacts.push_back(contact);
253 contact_pointers.push_back(contact);
254 }
255
256 contact_manager_->SetContacts(contact_pointers);
257 contact_manager_->NotifyObserversAboutUpdatedContacts();
258
259 // Do a search for the family name and check that the total number of results
260 // is limited as expected and that the results are ordered by descending
261 // affinity.
262 StartQuery(kFamilyName);
263 const ACMatches& matches = contact_provider_->matches();
264 ASSERT_EQ(AutocompleteProvider::kMaxMatches, matches.size());
265 for (size_t i = 0; i < matches.size(); ++i) {
266 const contacts::Contact& exp_contact =
267 *(contacts[kNumContacts - 1 - i]);
268 std::string match_id = GetContactIdFromMatch(matches[i]);
269 EXPECT_EQ(exp_contact.contact_id(), match_id)
270 << "Expected contact ID " << exp_contact.contact_id()
271 << " for match " << i << " but got " << match_id << " instead";
272 EXPECT_EQ(static_cast<int>(roundf(ContactProvider::kBaseRelevance +
273 ContactProvider::kAffinityRelevanceBoost * exp_contact.affinity())),
Peter Kasting 2012/09/13 23:32:37 Nit: I try to get people to avoid checking the exa
Daniel Erat 2012/09/14 15:31:50 Done.
274 matches[i].relevance);
275 }
276 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698