OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/contacts/google_contact_source.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/time.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "chrome/browser/chromeos/contacts/contact.h" |
| 13 #include "chrome/browser/chromeos/contacts/contact_source_observer.h" |
| 14 #include "chrome/browser/chromeos/contacts/contact_test_lib.h" |
| 15 #include "chrome/browser/chromeos/contacts/fake_contact_database.h" |
| 16 #include "chrome/browser/chromeos/gdata/fake_gdata_contacts_service.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 using content::BrowserThread; |
| 22 |
| 23 namespace contacts { |
| 24 namespace test { |
| 25 |
| 26 // ContactSourceObserver implementation that just counts the number of times |
| 27 // that it's been told that a source has been updated. |
| 28 class TestContactSourceObserver : public ContactSourceObserver { |
| 29 public: |
| 30 TestContactSourceObserver() : num_updates_(0) {} |
| 31 ~TestContactSourceObserver() {} |
| 32 |
| 33 int num_updates() const { return num_updates_; } |
| 34 void reset_stats() { num_updates_ = 0; } |
| 35 |
| 36 // ContactSourceObserver overrides: |
| 37 void OnContactsUpdated(ContactSource* source) OVERRIDE { |
| 38 DCHECK(source); |
| 39 num_updates_++; |
| 40 } |
| 41 |
| 42 private: |
| 43 // Number of times that OnContactsUpdated() has been called. |
| 44 int num_updates_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(TestContactSourceObserver); |
| 47 }; |
| 48 |
| 49 class GoogleContactSourceTest : public testing::Test { |
| 50 public: |
| 51 GoogleContactSourceTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} |
| 52 virtual ~GoogleContactSourceTest() {} |
| 53 |
| 54 protected: |
| 55 // testing::Test implementation. |
| 56 virtual void SetUp() OVERRIDE { |
| 57 testing::Test::SetUp(); |
| 58 profile_.reset(new TestingProfile); |
| 59 |
| 60 source_.reset(new GoogleContactSource(profile_.get())); |
| 61 source_->AddObserver(&observer_); |
| 62 |
| 63 test_api_.reset(new GoogleContactSource::TestAPI(source_.get())); |
| 64 |
| 65 db_ = new FakeContactDatabase; |
| 66 test_api_->SetDatabase(db_); |
| 67 |
| 68 gdata_service_ = new gdata::FakeGDataContactsService; |
| 69 test_api_->SetGDataService(gdata_service_); |
| 70 } |
| 71 |
| 72 TestContactSourceObserver* observer() { return &observer_; } |
| 73 GoogleContactSource* source() { return source_.get(); } |
| 74 GoogleContactSource::TestAPI* test_api() { return test_api_.get(); } |
| 75 FakeContactDatabase* db() { return db_; } |
| 76 gdata::FakeGDataContactsService* gdata_service() { return gdata_service_; } |
| 77 |
| 78 private: |
| 79 MessageLoopForUI message_loop_; |
| 80 content::TestBrowserThread ui_thread_; |
| 81 |
| 82 TestContactSourceObserver observer_; |
| 83 scoped_ptr<TestingProfile> profile_; |
| 84 scoped_ptr<GoogleContactSource> source_; |
| 85 scoped_ptr<GoogleContactSource::TestAPI> test_api_; |
| 86 |
| 87 FakeContactDatabase* db_; // not owned |
| 88 gdata::FakeGDataContactsService* gdata_service_; // not owned |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(GoogleContactSourceTest); |
| 91 }; |
| 92 |
| 93 TEST_F(GoogleContactSourceTest, LoadFromDatabase) { |
| 94 // Store two contacts in the database. |
| 95 scoped_ptr<Contact> contact1(new Contact); |
| 96 InitContact("provider1", "1", false, contact1.get()); |
| 97 scoped_ptr<Contact> contact2(new Contact); |
| 98 InitContact("provider2", "2", false, contact2.get()); |
| 99 ContactPointers db_contacts; |
| 100 db_contacts.push_back(contact1.get()); |
| 101 db_contacts.push_back(contact2.get()); |
| 102 db()->SetContacts(db_contacts); |
| 103 |
| 104 // Tell the GData service to report failure, initialize the source, and check |
| 105 // that the contacts from the database are loaded. |
| 106 gdata_service()->set_download_should_succeed(false); |
| 107 source()->Init(); |
| 108 ContactPointers loaded_contacts; |
| 109 source()->AppendContacts(&loaded_contacts); |
| 110 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| 111 ContactsToString(loaded_contacts)); |
| 112 EXPECT_TRUE(test_api()->update_scheduled()); |
| 113 EXPECT_EQ(1, observer()->num_updates()); |
| 114 } |
| 115 |
| 116 TEST_F(GoogleContactSourceTest, LoadFromGData) { |
| 117 // Store two contacts in the GData service. |
| 118 scoped_ptr<Contact> contact1(new Contact); |
| 119 InitContact("provider1", "1", false, contact1.get()); |
| 120 scoped_ptr<Contact> contact2(new Contact); |
| 121 InitContact("provider2", "2", false, contact2.get()); |
| 122 ContactPointers gdata_contacts; |
| 123 gdata_contacts.push_back(contact1.get()); |
| 124 gdata_contacts.push_back(contact2.get()); |
| 125 gdata_service()->SetContacts(gdata_contacts, base::Time()); |
| 126 |
| 127 // Initialize the source and check that the contacts are loaded from GData. |
| 128 source()->Init(); |
| 129 ContactPointers loaded_contacts; |
| 130 source()->AppendContacts(&loaded_contacts); |
| 131 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| 132 ContactsToString(loaded_contacts)); |
| 133 EXPECT_TRUE(test_api()->update_scheduled()); |
| 134 EXPECT_EQ(1, observer()->num_updates()); |
| 135 |
| 136 // The contacts should've been saved to the database, too. |
| 137 EXPECT_EQ(VarContactsToString(2, contact1.get(), contact2.get()), |
| 138 ContactsToString(db()->contacts())); |
| 139 } |
| 140 |
| 141 TEST_F(GoogleContactSourceTest, UpdateFromGData) { |
| 142 scoped_ptr<Contact> contact1(new Contact); |
| 143 InitContact("provider1", "1", false, contact1.get()); |
| 144 scoped_ptr<Contact> contact2(new Contact); |
| 145 InitContact("provider2", "2", false, contact2.get()); |
| 146 scoped_ptr<Contact> contact3(new Contact); |
| 147 InitContact("provider3", "3", false, contact3.get()); |
| 148 |
| 149 // Store the first two contacts in the database. |
| 150 ContactPointers db_contacts; |
| 151 db_contacts.push_back(contact1.get()); |
| 152 db_contacts.push_back(contact2.get()); |
| 153 db()->SetContacts(db_contacts); |
| 154 |
| 155 // Store all three in the GData service. We expect the update request to ask |
| 156 // for all contacts updated one millisecond after the newest contact in the |
| 157 // database. |
| 158 ContactPointers gdata_contacts; |
| 159 gdata_contacts.push_back(contact1.get()); |
| 160 gdata_contacts.push_back(contact2.get()); |
| 161 gdata_contacts.push_back(contact3.get()); |
| 162 gdata_service()->SetContacts( |
| 163 gdata_contacts, |
| 164 contact2->update_time + base::TimeDelta::FromMilliseconds(1)); |
| 165 |
| 166 // Check that the source ends up with all three contacts. |
| 167 source()->Init(); |
| 168 ContactPointers loaded_contacts; |
| 169 source()->AppendContacts(&loaded_contacts); |
| 170 EXPECT_EQ(VarContactsToString( |
| 171 3, contact1.get(), contact2.get(), contact3.get()), |
| 172 ContactsToString(loaded_contacts)); |
| 173 EXPECT_EQ(2, observer()->num_updates()); |
| 174 |
| 175 // All three contacts should've been saved to the database. |
| 176 EXPECT_EQ(VarContactsToString( |
| 177 3, contact1.get(), contact2.get(), contact3.get()), |
| 178 ContactsToString(db()->contacts())); |
| 179 EXPECT_TRUE(test_api()->update_scheduled()); |
| 180 } |
| 181 |
| 182 TEST_F(GoogleContactSourceTest, FetchUpdatedContacts) { |
| 183 scoped_ptr<Contact> contact1(new Contact); |
| 184 InitContact("provider1", "1", false, contact1.get()); |
| 185 scoped_ptr<Contact> contact2(new Contact); |
| 186 InitContact("provider2", "2", false, contact2.get()); |
| 187 scoped_ptr<Contact> contact3(new Contact); |
| 188 InitContact("provider3", "3", false, contact3.get()); |
| 189 |
| 190 // Tell the GData service to return all three contacts in response to a full |
| 191 // update. |
| 192 ContactPointers gdata_contacts; |
| 193 gdata_contacts.push_back(contact1.get()); |
| 194 gdata_contacts.push_back(contact2.get()); |
| 195 gdata_contacts.push_back(contact3.get()); |
| 196 gdata_service()->SetContacts(gdata_contacts, base::Time()); |
| 197 |
| 198 source()->Init(); |
| 199 ContactPointers loaded_contacts; |
| 200 source()->AppendContacts(&loaded_contacts); |
| 201 EXPECT_EQ(ContactsToString(gdata_contacts), |
| 202 ContactsToString(loaded_contacts)); |
| 203 EXPECT_EQ(ContactsToString(gdata_contacts), |
| 204 ContactsToString(db()->contacts())); |
| 205 EXPECT_TRUE(test_api()->update_scheduled()); |
| 206 EXPECT_EQ(1, observer()->num_updates()); |
| 207 observer()->reset_stats(); |
| 208 |
| 209 // Update the third contact. |
| 210 contact3->full_name = "new full name"; |
| 211 base::Time old_contact3_update_time = contact3->update_time; |
| 212 contact3->update_time += base::TimeDelta::FromSeconds(10); |
| 213 gdata_service()->SetContacts( |
| 214 gdata_contacts, |
| 215 old_contact3_update_time + base::TimeDelta::FromMilliseconds(1)); |
| 216 |
| 217 // Check that the updated contact is loaded (i.e. the source passed the |
| 218 // correct minimum update time to the service). |
| 219 test_api()->DoUpdate(); |
| 220 loaded_contacts.clear(); |
| 221 source()->AppendContacts(&loaded_contacts); |
| 222 EXPECT_EQ(ContactsToString(gdata_contacts), |
| 223 ContactsToString(loaded_contacts)); |
| 224 EXPECT_EQ(ContactsToString(gdata_contacts), |
| 225 ContactsToString(db()->contacts())); |
| 226 EXPECT_TRUE(test_api()->update_scheduled()); |
| 227 EXPECT_EQ(1, observer()->num_updates()); |
| 228 observer()->reset_stats(); |
| 229 |
| 230 // The next update should be based on the third contact's new update time. |
| 231 contact3->full_name = "yet another full name"; |
| 232 gdata_service()->SetContacts( |
| 233 gdata_contacts, |
| 234 contact3->update_time + base::TimeDelta::FromMilliseconds(1)); |
| 235 test_api()->DoUpdate(); |
| 236 loaded_contacts.clear(); |
| 237 source()->AppendContacts(&loaded_contacts); |
| 238 EXPECT_EQ(ContactsToString(gdata_contacts), |
| 239 ContactsToString(loaded_contacts)); |
| 240 EXPECT_EQ(ContactsToString(gdata_contacts), |
| 241 ContactsToString(db()->contacts())); |
| 242 EXPECT_TRUE(test_api()->update_scheduled()); |
| 243 EXPECT_EQ(1, observer()->num_updates()); |
| 244 } |
| 245 |
| 246 } // namespace test |
| 247 } // namespace contacts |
OLD | NEW |