Chromium Code Reviews| Index: chrome/browser/autocomplete/contact_provider_chromeos_unittest.cc |
| diff --git a/chrome/browser/autocomplete/contact_provider_chromeos_unittest.cc b/chrome/browser/autocomplete/contact_provider_chromeos_unittest.cc |
| index 52700a23379a2d883907c159213a6bf45a898db1..fadb6185d9aeb9e66fe7664f79ac8e764edc2eb7 100644 |
| --- a/chrome/browser/autocomplete/contact_provider_chromeos_unittest.cc |
| +++ b/chrome/browser/autocomplete/contact_provider_chromeos_unittest.cc |
| @@ -4,12 +4,14 @@ |
| #include "chrome/browser/autocomplete/contact_provider_chromeos.h" |
| +#include <cmath> |
| #include <map> |
| #include <string> |
| #include <vector> |
| #include "base/memory/scoped_ptr.h" |
| #include "base/message_loop.h" |
| +#include "base/string_number_conversions.h" |
| #include "base/string16.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/autocomplete/autocomplete_input.h" |
| @@ -73,18 +75,23 @@ class ContactProviderTest : public testing::Test { |
| false); // minimal_changes |
| } |
| + // Returns the contact ID in |match|'s additional info, or an empty string if |
| + // no ID is present. |
| + std::string GetContactIdFromMatch(const AutocompleteMatch& match) { |
| + 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.
|
| + match.additional_info.find(ContactProvider::kMatchContactIdKey); |
| + return it != match.additional_info.end() ? it->second : std::string(); |
| + } |
| + |
| // Returns pointers to all of the Contact objects referenced in |
| // |contact_provider_|'s current results. |
| contacts::ContactPointers GetMatchedContacts() { |
| contacts::ContactPointers contacts; |
| const ACMatches& matches = contact_provider_->matches(); |
| for (size_t i = 0; i < matches.size(); ++i) { |
| - std::map<std::string, std::string>::const_iterator id_it = |
| - matches[i].additional_info.find(ContactProvider::kMatchContactIdKey); |
| - CHECK(id_it != matches[i].additional_info.end()); |
| - const contacts::Contact* contact = |
| - contact_manager_->GetContactById(profile_, id_it->second); |
| - CHECK(contact) << "Unable to find contact with ID " << id_it->second; |
| + const contacts::Contact* contact = contact_manager_->GetContactById( |
| + profile_, GetContactIdFromMatch(matches[i])); |
| + 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
|
| contacts.push_back(contact); |
| } |
| return contacts; |
| @@ -99,10 +106,9 @@ class ContactProviderTest : public testing::Test { |
| StringMap contact_id_classifications; |
| const ACMatches& matches = contact_provider_->matches(); |
| for (size_t i = 0; i < matches.size(); ++i) { |
| - std::map<std::string, std::string>::const_iterator id_it = |
| - matches[i].additional_info.find(ContactProvider::kMatchContactIdKey); |
| - CHECK(id_it != matches[i].additional_info.end()); |
| - contact_id_classifications[id_it->second] = |
| + std::string id = GetContactIdFromMatch(matches[i]); |
| + CHECK(!id.empty()) << "Unable to find contact " << i; |
| + contact_id_classifications[id] = |
| AutocompleteMatch::ClassificationsToString(matches[i].contents_class); |
| } |
| @@ -227,3 +233,44 @@ TEST_F(ContactProviderTest, Collation) { |
| contacts::test::ContactsToString(GetMatchedContacts())); |
| EXPECT_EQ("0,0,6,2", GetMatchClassifications()); |
| } |
| + |
| +TEST_F(ContactProviderTest, Relevance) { |
| + // Create more contacts than the maximum number of results that an |
| + // AutocompleteProvider should return. Give them all the same family name and |
| + // ascending affinities from 0.0 to 1.0. |
| + const size_t kNumContacts = AutocompleteProvider::kMaxMatches + 1; |
| + const std::string kFamilyName = "Jones"; |
| + |
| + ScopedVector<contacts::Contact> contacts; |
| + contacts::ContactPointers contact_pointers; |
| + for (size_t i = 0; i < kNumContacts; ++i) { |
| + contacts::Contact* contact = new contacts::Contact; |
| + std::string id_string = base::IntToString(static_cast<int>(i)); |
| + InitContact(id_string, id_string, kFamilyName, |
| + id_string + " " + kFamilyName, contact); |
| + contact->set_affinity(static_cast<float>(i) / kNumContacts); |
| + contacts.push_back(contact); |
| + contact_pointers.push_back(contact); |
| + } |
| + |
| + contact_manager_->SetContacts(contact_pointers); |
| + contact_manager_->NotifyObserversAboutUpdatedContacts(); |
| + |
| + // Do a search for the family name and check that the total number of results |
| + // is limited as expected and that the results are ordered by descending |
| + // affinity. |
| + StartQuery(kFamilyName); |
| + const ACMatches& matches = contact_provider_->matches(); |
| + ASSERT_EQ(AutocompleteProvider::kMaxMatches, matches.size()); |
| + for (size_t i = 0; i < matches.size(); ++i) { |
| + const contacts::Contact& exp_contact = |
| + *(contacts[kNumContacts - 1 - i]); |
| + std::string match_id = GetContactIdFromMatch(matches[i]); |
| + EXPECT_EQ(exp_contact.contact_id(), match_id) |
| + << "Expected contact ID " << exp_contact.contact_id() |
| + << " for match " << i << " but got " << match_id << " instead"; |
| + EXPECT_EQ(static_cast<int>(roundf(ContactProvider::kBaseRelevance + |
| + 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.
|
| + matches[i].relevance); |
| + } |
| +} |