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/gdata/gdata_contacts_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/stringprintf.h" |
| 12 #include "base/time.h" |
| 13 #include "chrome/browser/chromeos/contacts/contact.h" |
| 14 #include "chrome/browser/chromeos/contacts/contact_test_util.h" |
| 15 #include "chrome/browser/chromeos/gdata/gdata_auth_service.h" |
| 16 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "net/test/test_server.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "ui/gfx/size.h" |
| 25 |
| 26 using content::BrowserThread; |
| 27 |
| 28 namespace gdata { |
| 29 namespace { |
| 30 |
| 31 // Path to the files that are served by the test server. |
| 32 const FilePath::CharType kTestDataPath[] = |
| 33 FILE_PATH_LITERAL("chrome/test/data"); |
| 34 |
| 35 // Base URL where feeds are located on the test server. |
| 36 const char kFeedBaseUrl[] = "files/chromeos/gdata/contacts/"; |
| 37 |
| 38 // Width and height of /photo.png on the test server. |
| 39 const int kPhotoSize = 48; |
| 40 |
| 41 // Initializes |contact| using the passed-in values. |
| 42 void InitContact(const std::string& provider_id, |
| 43 const std::string& rfc_3339_update_time, |
| 44 bool deleted, |
| 45 const std::string& full_name, |
| 46 const std::string& given_name, |
| 47 const std::string& additional_name, |
| 48 const std::string& family_name, |
| 49 const std::string& name_prefix, |
| 50 const std::string& name_suffix, |
| 51 contacts::Contact* contact) { |
| 52 DCHECK(contact); |
| 53 contact->provider_id = provider_id; |
| 54 CHECK(util::GetTimeFromString(rfc_3339_update_time, &(contact->update_time))) |
| 55 << "Unable to parse time \"" << rfc_3339_update_time << "\""; |
| 56 contact->deleted = deleted; |
| 57 contact->full_name = full_name; |
| 58 contact->given_name = given_name; |
| 59 contact->additional_name = additional_name; |
| 60 contact->family_name = family_name; |
| 61 contact->name_prefix = name_prefix; |
| 62 contact->name_suffix = name_suffix; |
| 63 } |
| 64 |
| 65 class GDataContactsServiceTest : public InProcessBrowserTest { |
| 66 public: |
| 67 GDataContactsServiceTest() |
| 68 : InProcessBrowserTest(), |
| 69 test_server_(net::TestServer::TYPE_GDATA, |
| 70 net::TestServer::kLocalhost, |
| 71 FilePath(kTestDataPath)), |
| 72 download_was_successful_(false) { |
| 73 } |
| 74 |
| 75 virtual void SetUpOnMainThread() OVERRIDE { |
| 76 ASSERT_TRUE(test_server_.Start()); |
| 77 service_.reset(new GDataContactsService(browser()->profile())); |
| 78 service_->Initialize(); |
| 79 service_->auth_service_for_testing()->set_oauth2_auth_token_for_testing( |
| 80 net::TestServer::kGDataAuthToken); |
| 81 service_->set_rewrite_photo_url_callback_for_testing( |
| 82 base::Bind(&GDataContactsServiceTest::RewritePhotoUrl, |
| 83 base::Unretained(this))); |
| 84 } |
| 85 |
| 86 virtual void CleanUpOnMainThread() { |
| 87 service_.reset(); |
| 88 } |
| 89 |
| 90 protected: |
| 91 GDataContactsService* service() { return service_.get(); } |
| 92 |
| 93 // Downloads contacts from |feed_filename| (within the chromeos/gdata/contacts |
| 94 // test data directory). |min_update_time| is appended to the URL and the |
| 95 // resulting contacts are swapped into |contacts|. Returns false if the |
| 96 // download failed. |
| 97 bool Download(const std::string& feed_filename, |
| 98 const base::Time& min_update_time, |
| 99 scoped_ptr<ScopedVector<contacts::Contact> >* contacts) { |
| 100 DCHECK(contacts); |
| 101 service_->set_feed_url_for_testing( |
| 102 test_server_.GetURL(kFeedBaseUrl + feed_filename)); |
| 103 service_->DownloadContacts( |
| 104 base::Bind(&GDataContactsServiceTest::OnSuccess, |
| 105 base::Unretained(this)), |
| 106 base::Bind(&GDataContactsServiceTest::OnFailure, |
| 107 base::Unretained(this)), |
| 108 min_update_time); |
| 109 ui_test_utils::RunMessageLoop(); |
| 110 contacts->swap(downloaded_contacts_); |
| 111 return download_was_successful_; |
| 112 } |
| 113 |
| 114 private: |
| 115 // Rewrites |original_url|, a photo URL from a contacts feed, to instead point |
| 116 // at a file on |test_server_|. |
| 117 std::string RewritePhotoUrl(const std::string& original_url) { |
| 118 return test_server_.GetURL(kFeedBaseUrl + GURL(original_url).path()).spec(); |
| 119 } |
| 120 |
| 121 // Handles success for Download(). |
| 122 void OnSuccess(scoped_ptr<ScopedVector<contacts::Contact> > contacts) { |
| 123 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 124 download_was_successful_ = true; |
| 125 downloaded_contacts_.swap(contacts); |
| 126 MessageLoop::current()->Quit(); |
| 127 } |
| 128 |
| 129 // Handles failure for Download(). |
| 130 void OnFailure() { |
| 131 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 132 download_was_successful_ = false; |
| 133 downloaded_contacts_.reset(new ScopedVector<contacts::Contact>()); |
| 134 MessageLoop::current()->Quit(); |
| 135 } |
| 136 |
| 137 net::TestServer test_server_; |
| 138 scoped_ptr<GDataContactsService> service_; |
| 139 |
| 140 // Was the last download successful? Used to pass the result back from |
| 141 // OnSuccess() and OnFailure() to Download(). |
| 142 bool download_was_successful_; |
| 143 |
| 144 // Used to pass downloaded contacts back to Download(). |
| 145 scoped_ptr<ScopedVector<contacts::Contact> > downloaded_contacts_; |
| 146 }; |
| 147 |
| 148 } // namespace |
| 149 |
| 150 // Test that we report failure for feeds that are broken in various ways. |
| 151 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, BrokenFeeds) { |
| 152 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 153 EXPECT_FALSE(Download("some_bogus_file", base::Time(), &contacts)); |
| 154 EXPECT_FALSE(Download("empty.txt", base::Time(), &contacts)); |
| 155 EXPECT_FALSE(Download("not_json.txt", base::Time(), &contacts)); |
| 156 EXPECT_FALSE(Download("not_dictionary.json", base::Time(), &contacts)); |
| 157 EXPECT_FALSE(Download("no_feed.json", base::Time(), &contacts)); |
| 158 EXPECT_FALSE(Download("no_category.json", base::Time(), &contacts)); |
| 159 EXPECT_FALSE(Download("wrong_category.json", base::Time(), &contacts)); |
| 160 EXPECT_FALSE(Download("feed_photo_404.json", base::Time(), &contacts)); |
| 161 } |
| 162 |
| 163 // Check that we're able to download an empty feed and a normal-looking feed |
| 164 // with two regular contacts and one deleted one. |
| 165 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, Download) { |
| 166 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 167 EXPECT_TRUE(Download("no_entries.json", base::Time(), &contacts)); |
| 168 EXPECT_TRUE(contacts->empty()); |
| 169 |
| 170 EXPECT_TRUE(Download("feed.json", base::Time(), &contacts)); |
| 171 |
| 172 // All of these expected values are hardcoded in the feed. |
| 173 scoped_ptr<contacts::Contact> contact1(new contacts::Contact); |
| 174 InitContact("http://example.com/1", |
| 175 "2012-06-04T15:53:36.023Z", |
| 176 false, "Joe Contact", "Joe", "", "Contact", "", "", |
| 177 contact1.get()); |
| 178 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact1.get()); |
| 179 contacts::test::AddEmailAddress( |
| 180 "joe.contact@gmail.com", |
| 181 contacts::Contact::AddressType::RELATION_OTHER, "", true, contact1.get()); |
| 182 contacts::test::AddPostalAddress( |
| 183 "345 Spear St\nSan Francisco CA 94105", |
| 184 contacts::Contact::AddressType::RELATION_HOME, "", false, contact1.get()); |
| 185 |
| 186 scoped_ptr<contacts::Contact> contact2(new contacts::Contact); |
| 187 InitContact("http://example.com/2", |
| 188 "2012-06-21T16:20:13.208Z", |
| 189 false, "Dr. Jane Liz Doe Sr.", "Jane", "Liz", "Doe", "Dr.", "Sr.", |
| 190 contact2.get()); |
| 191 contacts::test::AddEmailAddress( |
| 192 "jane.doe@gmail.com", |
| 193 contacts::Contact::AddressType::RELATION_HOME, "", true, contact2.get()); |
| 194 contacts::test::AddEmailAddress( |
| 195 "me@privacy.net", |
| 196 contacts::Contact::AddressType::RELATION_WORK, "", false, contact2.get()); |
| 197 contacts::test::AddEmailAddress( |
| 198 "foo@example.org", |
| 199 contacts::Contact::AddressType::RELATION_OTHER, "Fake", false, |
| 200 contact2.get()); |
| 201 contacts::test::AddPhoneNumber( |
| 202 "123-456-7890", |
| 203 contacts::Contact::AddressType::RELATION_MOBILE, "", false, |
| 204 contact2.get()); |
| 205 contacts::test::AddPhoneNumber( |
| 206 "234-567-8901", |
| 207 contacts::Contact::AddressType::RELATION_OTHER, "grandcentral", false, |
| 208 contact2.get()); |
| 209 contacts::test::AddPostalAddress( |
| 210 "100 Elm St\nSan Francisco, CA 94110", |
| 211 contacts::Contact::AddressType::RELATION_HOME, "", false, contact2.get()); |
| 212 contacts::test::AddInstantMessagingAddress( |
| 213 "foo@example.org", |
| 214 contacts::Contact::InstantMessagingAddress::PROTOCOL_GOOGLE_TALK, |
| 215 contacts::Contact::AddressType::RELATION_OTHER, "", false, |
| 216 contact2.get()); |
| 217 contacts::test::AddInstantMessagingAddress( |
| 218 "12345678", |
| 219 contacts::Contact::InstantMessagingAddress::PROTOCOL_ICQ, |
| 220 contacts::Contact::AddressType::RELATION_OTHER, "", false, |
| 221 contact2.get()); |
| 222 |
| 223 scoped_ptr<contacts::Contact> contact3(new contacts::Contact); |
| 224 InitContact("http://example.com/3", |
| 225 "2012-07-23T23:07:06.133Z", |
| 226 true, "", "", "", "", "", "", |
| 227 contact3.get()); |
| 228 |
| 229 EXPECT_EQ(contacts::test::VarContactsToString( |
| 230 3, contact1.get(), contact2.get(), contact3.get()), |
| 231 contacts::test::ContactsToString(*contacts)); |
| 232 } |
| 233 |
| 234 // Download a feed containing more photos than we're able to download in |
| 235 // parallel to check that we still end up with all the photos. |
| 236 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, ParallelPhotoDownload) { |
| 237 // The feed used for this test contains 8 contacts. |
| 238 const int kNumContacts = 8; |
| 239 service()->set_max_simultaneous_photo_downloads_for_testing(2); |
| 240 scoped_ptr<ScopedVector<contacts::Contact> > contacts; |
| 241 EXPECT_TRUE(Download("feed_multiple_photos.json", base::Time(), &contacts)); |
| 242 ASSERT_EQ(static_cast<size_t>(kNumContacts), contacts->size()); |
| 243 |
| 244 ScopedVector<contacts::Contact> expected_contacts; |
| 245 for (int i = 0; i < kNumContacts; ++i) { |
| 246 contacts::Contact* contact = new contacts::Contact; |
| 247 InitContact(base::StringPrintf("http://example.com/%d", i + 1), |
| 248 "2012-06-04T15:53:36.023Z", |
| 249 false, "", "", "", "", "", "", contact); |
| 250 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact); |
| 251 expected_contacts.push_back(contact); |
| 252 } |
| 253 EXPECT_EQ(contacts::test::ContactsToString(expected_contacts), |
| 254 contacts::test::ContactsToString(*contacts)); |
| 255 } |
| 256 |
| 257 } // namespace gdata |
OLD | NEW |