| OLD | NEW |
| 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/chromeos/gdata/gdata_contacts_service.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_contacts_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 namespace gdata { | 28 namespace gdata { |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // Path to the files that are served by the test server. | 31 // Path to the files that are served by the test server. |
| 32 const FilePath::CharType kTestDataPath[] = | 32 const FilePath::CharType kTestDataPath[] = |
| 33 FILE_PATH_LITERAL("chrome/test/data"); | 33 FILE_PATH_LITERAL("chrome/test/data"); |
| 34 | 34 |
| 35 // Base URL where feeds are located on the test server. | 35 // Base URL where feeds are located on the test server. |
| 36 const char kFeedBaseUrl[] = "files/chromeos/gdata/contacts/"; | 36 const char kFeedBaseUrl[] = "files/chromeos/gdata/contacts/"; |
| 37 | 37 |
| 38 // Filename of JSON feed containing contact groups. |
| 39 const char kGroupsFeedFilename[] = "groups.json"; |
| 40 |
| 38 // Width and height of /photo.png on the test server. | 41 // Width and height of /photo.png on the test server. |
| 39 const int kPhotoSize = 48; | 42 const int kPhotoSize = 48; |
| 40 | 43 |
| 41 // Initializes |contact| using the passed-in values. | 44 // Initializes |contact| using the passed-in values. |
| 42 void InitContact(const std::string& provider_id, | 45 void InitContact(const std::string& provider_id, |
| 43 const std::string& rfc_3339_update_time, | 46 const std::string& rfc_3339_update_time, |
| 44 bool deleted, | 47 bool deleted, |
| 45 const std::string& full_name, | 48 const std::string& full_name, |
| 46 const std::string& given_name, | 49 const std::string& given_name, |
| 47 const std::string& additional_name, | 50 const std::string& additional_name, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 76 | 79 |
| 77 virtual void SetUpOnMainThread() OVERRIDE { | 80 virtual void SetUpOnMainThread() OVERRIDE { |
| 78 ASSERT_TRUE(test_server_.Start()); | 81 ASSERT_TRUE(test_server_.Start()); |
| 79 service_.reset(new GDataContactsService(browser()->profile())); | 82 service_.reset(new GDataContactsService(browser()->profile())); |
| 80 service_->Initialize(); | 83 service_->Initialize(); |
| 81 service_->auth_service_for_testing()->set_access_token_for_testing( | 84 service_->auth_service_for_testing()->set_access_token_for_testing( |
| 82 net::TestServer::kGDataAuthToken); | 85 net::TestServer::kGDataAuthToken); |
| 83 service_->set_rewrite_photo_url_callback_for_testing( | 86 service_->set_rewrite_photo_url_callback_for_testing( |
| 84 base::Bind(&GDataContactsServiceTest::RewritePhotoUrl, | 87 base::Bind(&GDataContactsServiceTest::RewritePhotoUrl, |
| 85 base::Unretained(this))); | 88 base::Unretained(this))); |
| 89 service_->set_groups_feed_url_for_testing( |
| 90 test_server_.GetURL(std::string(kFeedBaseUrl) + kGroupsFeedFilename)); |
| 86 service_->set_photo_download_timer_interval_for_testing( | 91 service_->set_photo_download_timer_interval_for_testing( |
| 87 base::TimeDelta::FromMilliseconds(10)); | 92 base::TimeDelta::FromMilliseconds(10)); |
| 88 } | 93 } |
| 89 | 94 |
| 90 virtual void CleanUpOnMainThread() { | 95 virtual void CleanUpOnMainThread() { |
| 91 service_.reset(); | 96 service_.reset(); |
| 92 } | 97 } |
| 93 | 98 |
| 94 protected: | 99 protected: |
| 95 GDataContactsService* service() { return service_.get(); } | |
| 96 | |
| 97 // Downloads contacts from |feed_filename| (within the chromeos/gdata/contacts | 100 // Downloads contacts from |feed_filename| (within the chromeos/gdata/contacts |
| 98 // test data directory). |min_update_time| is appended to the URL and the | 101 // test data directory). |min_update_time| is appended to the URL and the |
| 99 // resulting contacts are swapped into |contacts|. Returns false if the | 102 // resulting contacts are swapped into |contacts|. Returns false if the |
| 100 // download failed. | 103 // download failed. |
| 101 bool Download(const std::string& feed_filename, | 104 bool Download(const std::string& feed_filename, |
| 102 const base::Time& min_update_time, | 105 const base::Time& min_update_time, |
| 103 scoped_ptr<ScopedVector<contacts::Contact> >* contacts) { | 106 scoped_ptr<ScopedVector<contacts::Contact> >* contacts) { |
| 104 DCHECK(contacts); | 107 DCHECK(contacts); |
| 105 service_->set_feed_url_for_testing( | 108 service_->set_contacts_feed_url_for_testing( |
| 106 test_server_.GetURL(kFeedBaseUrl + feed_filename)); | 109 test_server_.GetURL(kFeedBaseUrl + feed_filename)); |
| 107 service_->DownloadContacts( | 110 service_->DownloadContacts( |
| 108 base::Bind(&GDataContactsServiceTest::OnSuccess, | 111 base::Bind(&GDataContactsServiceTest::OnSuccess, |
| 109 base::Unretained(this)), | 112 base::Unretained(this)), |
| 110 base::Bind(&GDataContactsServiceTest::OnFailure, | 113 base::Bind(&GDataContactsServiceTest::OnFailure, |
| 111 base::Unretained(this)), | 114 base::Unretained(this)), |
| 112 min_update_time); | 115 min_update_time); |
| 113 content::RunMessageLoop(); | 116 content::RunMessageLoop(); |
| 114 contacts->swap(downloaded_contacts_); | 117 contacts->swap(downloaded_contacts_); |
| 115 return download_was_successful_; | 118 return download_was_successful_; |
| 116 } | 119 } |
| 117 | 120 |
| 121 net::TestServer test_server_; |
| 122 scoped_ptr<GDataContactsService> service_; |
| 123 |
| 118 private: | 124 private: |
| 119 // Rewrites |original_url|, a photo URL from a contacts feed, to instead point | 125 // Rewrites |original_url|, a photo URL from a contacts feed, to instead point |
| 120 // at a file on |test_server_|. | 126 // at a file on |test_server_|. |
| 121 std::string RewritePhotoUrl(const std::string& original_url) { | 127 std::string RewritePhotoUrl(const std::string& original_url) { |
| 122 return test_server_.GetURL(kFeedBaseUrl + GURL(original_url).path()).spec(); | 128 return test_server_.GetURL(kFeedBaseUrl + GURL(original_url).path()).spec(); |
| 123 } | 129 } |
| 124 | 130 |
| 125 // Handles success for Download(). | 131 // Handles success for Download(). |
| 126 void OnSuccess(scoped_ptr<ScopedVector<contacts::Contact> > contacts) { | 132 void OnSuccess(scoped_ptr<ScopedVector<contacts::Contact> > contacts) { |
| 127 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 133 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 128 download_was_successful_ = true; | 134 download_was_successful_ = true; |
| 129 downloaded_contacts_.swap(contacts); | 135 downloaded_contacts_.swap(contacts); |
| 130 MessageLoop::current()->Quit(); | 136 MessageLoop::current()->Quit(); |
| 131 } | 137 } |
| 132 | 138 |
| 133 // Handles failure for Download(). | 139 // Handles failure for Download(). |
| 134 void OnFailure() { | 140 void OnFailure() { |
| 135 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 141 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 download_was_successful_ = false; | 142 download_was_successful_ = false; |
| 137 downloaded_contacts_.reset(new ScopedVector<contacts::Contact>()); | 143 downloaded_contacts_.reset(new ScopedVector<contacts::Contact>()); |
| 138 MessageLoop::current()->Quit(); | 144 MessageLoop::current()->Quit(); |
| 139 } | 145 } |
| 140 | 146 |
| 141 net::TestServer test_server_; | |
| 142 scoped_ptr<GDataContactsService> service_; | |
| 143 | |
| 144 // Was the last download successful? Used to pass the result back from | 147 // Was the last download successful? Used to pass the result back from |
| 145 // OnSuccess() and OnFailure() to Download(). | 148 // OnSuccess() and OnFailure() to Download(). |
| 146 bool download_was_successful_; | 149 bool download_was_successful_; |
| 147 | 150 |
| 148 // Used to pass downloaded contacts back to Download(). | 151 // Used to pass downloaded contacts back to Download(). |
| 149 scoped_ptr<ScopedVector<contacts::Contact> > downloaded_contacts_; | 152 scoped_ptr<ScopedVector<contacts::Contact> > downloaded_contacts_; |
| 150 }; | 153 }; |
| 151 | 154 |
| 152 } // namespace | 155 } // namespace |
| 153 | 156 |
| 154 // Test that we report failure for feeds that are broken in various ways. | 157 // Test that we report failure for feeds that are broken in various ways. |
| 155 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, BrokenFeeds) { | 158 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, BrokenFeeds) { |
| 156 scoped_ptr<ScopedVector<contacts::Contact> > contacts; | 159 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 157 EXPECT_FALSE(Download("some_bogus_file", base::Time(), &contacts)); | 160 EXPECT_FALSE(Download("some_bogus_file", base::Time(), &contacts)); |
| 158 EXPECT_FALSE(Download("empty.txt", base::Time(), &contacts)); | 161 EXPECT_FALSE(Download("empty.txt", base::Time(), &contacts)); |
| 159 EXPECT_FALSE(Download("not_json.txt", base::Time(), &contacts)); | 162 EXPECT_FALSE(Download("not_json.txt", base::Time(), &contacts)); |
| 160 EXPECT_FALSE(Download("not_dictionary.json", base::Time(), &contacts)); | 163 EXPECT_FALSE(Download("not_dictionary.json", base::Time(), &contacts)); |
| 161 EXPECT_FALSE(Download("no_feed.json", base::Time(), &contacts)); | 164 EXPECT_FALSE(Download("no_feed.json", base::Time(), &contacts)); |
| 162 EXPECT_FALSE(Download("no_category.json", base::Time(), &contacts)); | 165 EXPECT_FALSE(Download("no_category.json", base::Time(), &contacts)); |
| 163 EXPECT_FALSE(Download("wrong_category.json", base::Time(), &contacts)); | 166 EXPECT_FALSE(Download("wrong_category.json", base::Time(), &contacts)); |
| 164 | 167 |
| 165 // Missing photos should be allowed, though (as this can occur in production). | 168 // Missing photos should be allowed, though (as this can occur in production). |
| 166 EXPECT_TRUE(Download("feed_photo_404.json", base::Time(), &contacts)); | 169 EXPECT_TRUE(Download("feed_photo_404.json", base::Time(), &contacts)); |
| 167 ASSERT_EQ(static_cast<size_t>(1), contacts->size()); | 170 ASSERT_EQ(static_cast<size_t>(1), contacts->size()); |
| 168 EXPECT_FALSE((*contacts)[0]->has_raw_untrusted_photo()); | 171 EXPECT_FALSE((*contacts)[0]->has_raw_untrusted_photo()); |
| 169 } | 172 } |
| 170 | 173 |
| 174 // We should report failure when we're unable to download the contact group |
| 175 // feed. |
| 176 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, MissingGroupsFeed) { |
| 177 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 178 service_->set_groups_feed_url_for_testing( |
| 179 test_server_.GetURL(std::string(kFeedBaseUrl) + "404")); |
| 180 EXPECT_FALSE(Download("feed.json", base::Time(), &contacts)); |
| 181 EXPECT_TRUE(service_->cached_my_contacts_group_id_for_testing().empty()); |
| 182 } |
| 183 |
| 184 // We should also fail when the "My Contacts" group isn't listed in the group |
| 185 // feed. |
| 186 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, NoMyContactsGroup) { |
| 187 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 188 service_->set_groups_feed_url_for_testing( |
| 189 test_server_.GetURL(std::string(kFeedBaseUrl) + |
| 190 "groups_no_my_contacts.json")); |
| 191 EXPECT_FALSE(Download("feed.json", base::Time(), &contacts)); |
| 192 EXPECT_TRUE(service_->cached_my_contacts_group_id_for_testing().empty()); |
| 193 } |
| 194 |
| 171 // Check that we're able to download an empty feed and a normal-looking feed | 195 // Check that we're able to download an empty feed and a normal-looking feed |
| 172 // with two regular contacts and one deleted one. | 196 // with two regular contacts and one deleted one. |
| 173 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, Download) { | 197 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, Download) { |
| 174 scoped_ptr<ScopedVector<contacts::Contact> > contacts; | 198 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 175 EXPECT_TRUE(Download("no_entries.json", base::Time(), &contacts)); | 199 EXPECT_TRUE(Download("no_entries.json", base::Time(), &contacts)); |
| 176 EXPECT_TRUE(contacts->empty()); | 200 EXPECT_TRUE(contacts->empty()); |
| 177 | 201 |
| 178 EXPECT_TRUE(Download("feed.json", base::Time(), &contacts)); | 202 EXPECT_TRUE(Download("feed.json", base::Time(), &contacts)); |
| 179 | 203 |
| 204 // Check that we got the group ID for the "My Contacts" group that's hardcoded |
| 205 // in the groups feed. |
| 206 EXPECT_EQ( |
| 207 "http://www.google.com/m8/feeds/groups/test.user%40gmail.com/base/6", |
| 208 service_->cached_my_contacts_group_id_for_testing()); |
| 209 |
| 180 // All of these expected values are hardcoded in the feed. | 210 // All of these expected values are hardcoded in the feed. |
| 181 scoped_ptr<contacts::Contact> contact1(new contacts::Contact); | 211 scoped_ptr<contacts::Contact> contact1(new contacts::Contact); |
| 182 InitContact("http://example.com/1", | 212 InitContact("http://example.com/1", |
| 183 "2012-06-04T15:53:36.023Z", | 213 "2012-06-04T15:53:36.023Z", |
| 184 false, "Joe Contact", "Joe", "", "Contact", "", "", | 214 false, "Joe Contact", "Joe", "", "Contact", "", "", |
| 185 contact1.get()); | 215 contact1.get()); |
| 186 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact1.get()); | 216 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact1.get()); |
| 187 contacts::test::AddEmailAddress( | 217 contacts::test::AddEmailAddress( |
| 188 "joe.contact@gmail.com", | 218 "joe.contact@gmail.com", |
| 189 contacts::Contact_AddressType_Relation_OTHER, "", true, contact1.get()); | 219 contacts::Contact_AddressType_Relation_OTHER, "", true, contact1.get()); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 EXPECT_EQ(contacts::test::VarContactsToString( | 267 EXPECT_EQ(contacts::test::VarContactsToString( |
| 238 3, contact1.get(), contact2.get(), contact3.get()), | 268 3, contact1.get(), contact2.get(), contact3.get()), |
| 239 contacts::test::ContactsToString(*contacts)); | 269 contacts::test::ContactsToString(*contacts)); |
| 240 } | 270 } |
| 241 | 271 |
| 242 // Download a feed containing more photos than we're able to download in | 272 // Download a feed containing more photos than we're able to download in |
| 243 // parallel to check that we still end up with all the photos. | 273 // parallel to check that we still end up with all the photos. |
| 244 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, ParallelPhotoDownload) { | 274 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, ParallelPhotoDownload) { |
| 245 // The feed used for this test contains 8 contacts. | 275 // The feed used for this test contains 8 contacts. |
| 246 const int kNumContacts = 8; | 276 const int kNumContacts = 8; |
| 247 service()->set_max_photo_downloads_per_second_for_testing(6); | 277 service_->set_max_photo_downloads_per_second_for_testing(6); |
| 248 scoped_ptr<ScopedVector<contacts::Contact> > contacts; | 278 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 249 EXPECT_TRUE(Download("feed_multiple_photos.json", base::Time(), &contacts)); | 279 EXPECT_TRUE(Download("feed_multiple_photos.json", base::Time(), &contacts)); |
| 250 ASSERT_EQ(static_cast<size_t>(kNumContacts), contacts->size()); | 280 ASSERT_EQ(static_cast<size_t>(kNumContacts), contacts->size()); |
| 251 | 281 |
| 252 ScopedVector<contacts::Contact> expected_contacts; | 282 ScopedVector<contacts::Contact> expected_contacts; |
| 253 for (int i = 0; i < kNumContacts; ++i) { | 283 for (int i = 0; i < kNumContacts; ++i) { |
| 254 contacts::Contact* contact = new contacts::Contact; | 284 contacts::Contact* contact = new contacts::Contact; |
| 255 InitContact(base::StringPrintf("http://example.com/%d", i + 1), | 285 InitContact(base::StringPrintf("http://example.com/%d", i + 1), |
| 256 "2012-06-04T15:53:36.023Z", | 286 "2012-06-04T15:53:36.023Z", |
| 257 false, "", "", "", "", "", "", contact); | 287 false, "", "", "", "", "", "", contact); |
| 258 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact); | 288 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact); |
| 259 expected_contacts.push_back(contact); | 289 expected_contacts.push_back(contact); |
| 260 } | 290 } |
| 261 EXPECT_EQ(contacts::test::ContactsToString(expected_contacts), | 291 EXPECT_EQ(contacts::test::ContactsToString(expected_contacts), |
| 262 contacts::test::ContactsToString(*contacts)); | 292 contacts::test::ContactsToString(*contacts)); |
| 263 } | 293 } |
| 264 | 294 |
| 265 } // namespace gdata | 295 } // namespace gdata |
| OLD | NEW |