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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_contacts_service_browsertest.cc

Issue 10818017: contacts: Add GDataContactsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: s/struct/class/ in forward declaration (caught by clang) Created 8 years, 4 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
(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.pb.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 "content/public/browser/browser_thread.h"
21 #include "content/public/test/test_utils.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->set_provider_id(provider_id);
54 base::Time update_time;
55 CHECK(util::GetTimeFromString(rfc_3339_update_time, &update_time))
56 << "Unable to parse time \"" << rfc_3339_update_time << "\"";
57 contact->set_update_time(update_time.ToInternalValue());
58 contact->set_deleted(deleted);
59 contact->set_full_name(full_name);
60 contact->set_given_name(given_name);
61 contact->set_additional_name(additional_name);
62 contact->set_family_name(family_name);
63 contact->set_name_prefix(name_prefix);
64 contact->set_name_suffix(name_suffix);
65 }
66
67 class GDataContactsServiceTest : public InProcessBrowserTest {
68 public:
69 GDataContactsServiceTest()
70 : InProcessBrowserTest(),
71 test_server_(net::TestServer::TYPE_GDATA,
72 net::TestServer::kLocalhost,
73 FilePath(kTestDataPath)),
74 download_was_successful_(false) {
75 }
76
77 virtual void SetUpOnMainThread() OVERRIDE {
78 ASSERT_TRUE(test_server_.Start());
79 service_.reset(new GDataContactsService(browser()->profile()));
80 service_->Initialize();
81 service_->auth_service_for_testing()->set_access_token_for_testing(
82 net::TestServer::kGDataAuthToken);
83 service_->set_rewrite_photo_url_callback_for_testing(
84 base::Bind(&GDataContactsServiceTest::RewritePhotoUrl,
85 base::Unretained(this)));
86 }
87
88 virtual void CleanUpOnMainThread() {
89 service_.reset();
90 }
91
92 protected:
93 GDataContactsService* service() { return service_.get(); }
94
95 // Downloads contacts from |feed_filename| (within the chromeos/gdata/contacts
96 // test data directory). |min_update_time| is appended to the URL and the
97 // resulting contacts are swapped into |contacts|. Returns false if the
98 // download failed.
99 bool Download(const std::string& feed_filename,
100 const base::Time& min_update_time,
101 scoped_ptr<ScopedVector<contacts::Contact> >* contacts) {
102 DCHECK(contacts);
103 service_->set_feed_url_for_testing(
104 test_server_.GetURL(kFeedBaseUrl + feed_filename));
105 service_->DownloadContacts(
106 base::Bind(&GDataContactsServiceTest::OnSuccess,
107 base::Unretained(this)),
108 base::Bind(&GDataContactsServiceTest::OnFailure,
109 base::Unretained(this)),
110 min_update_time);
111 content::RunMessageLoop();
112 contacts->swap(downloaded_contacts_);
113 return download_was_successful_;
114 }
115
116 private:
117 // Rewrites |original_url|, a photo URL from a contacts feed, to instead point
118 // at a file on |test_server_|.
119 std::string RewritePhotoUrl(const std::string& original_url) {
120 return test_server_.GetURL(kFeedBaseUrl + GURL(original_url).path()).spec();
121 }
122
123 // Handles success for Download().
124 void OnSuccess(scoped_ptr<ScopedVector<contacts::Contact> > contacts) {
125 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 download_was_successful_ = true;
127 downloaded_contacts_.swap(contacts);
128 MessageLoop::current()->Quit();
129 }
130
131 // Handles failure for Download().
132 void OnFailure() {
133 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134 download_was_successful_ = false;
135 downloaded_contacts_.reset(new ScopedVector<contacts::Contact>());
136 MessageLoop::current()->Quit();
137 }
138
139 net::TestServer test_server_;
140 scoped_ptr<GDataContactsService> service_;
141
142 // Was the last download successful? Used to pass the result back from
143 // OnSuccess() and OnFailure() to Download().
144 bool download_was_successful_;
145
146 // Used to pass downloaded contacts back to Download().
147 scoped_ptr<ScopedVector<contacts::Contact> > downloaded_contacts_;
148 };
149
150 } // namespace
151
152 // Test that we report failure for feeds that are broken in various ways.
153 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, BrokenFeeds) {
154 scoped_ptr<ScopedVector<contacts::Contact> > contacts;
155 EXPECT_FALSE(Download("some_bogus_file", base::Time(), &contacts));
156 EXPECT_FALSE(Download("empty.txt", base::Time(), &contacts));
157 EXPECT_FALSE(Download("not_json.txt", base::Time(), &contacts));
158 EXPECT_FALSE(Download("not_dictionary.json", base::Time(), &contacts));
159 EXPECT_FALSE(Download("no_feed.json", base::Time(), &contacts));
160 EXPECT_FALSE(Download("no_category.json", base::Time(), &contacts));
161 EXPECT_FALSE(Download("wrong_category.json", base::Time(), &contacts));
162 EXPECT_FALSE(Download("feed_photo_404.json", base::Time(), &contacts));
163 }
164
165 // Check that we're able to download an empty feed and a normal-looking feed
166 // with two regular contacts and one deleted one.
167 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, Download) {
168 scoped_ptr<ScopedVector<contacts::Contact> > contacts;
169 EXPECT_TRUE(Download("no_entries.json", base::Time(), &contacts));
170 EXPECT_TRUE(contacts->empty());
171
172 EXPECT_TRUE(Download("feed.json", base::Time(), &contacts));
173
174 // All of these expected values are hardcoded in the feed.
175 scoped_ptr<contacts::Contact> contact1(new contacts::Contact);
176 InitContact("http://example.com/1",
177 "2012-06-04T15:53:36.023Z",
178 false, "Joe Contact", "Joe", "", "Contact", "", "",
179 contact1.get());
180 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact1.get());
181 contacts::test::AddEmailAddress(
182 "joe.contact@gmail.com",
183 contacts::Contact_AddressType_Relation_OTHER, "", true, contact1.get());
184 contacts::test::AddPostalAddress(
185 "345 Spear St\nSan Francisco CA 94105",
186 contacts::Contact_AddressType_Relation_HOME, "", false, contact1.get());
187
188 scoped_ptr<contacts::Contact> contact2(new contacts::Contact);
189 InitContact("http://example.com/2",
190 "2012-06-21T16:20:13.208Z",
191 false, "Dr. Jane Liz Doe Sr.", "Jane", "Liz", "Doe", "Dr.", "Sr.",
192 contact2.get());
193 contacts::test::AddEmailAddress(
194 "jane.doe@gmail.com",
195 contacts::Contact_AddressType_Relation_HOME, "", true, contact2.get());
196 contacts::test::AddEmailAddress(
197 "me@privacy.net",
198 contacts::Contact_AddressType_Relation_WORK, "", false, contact2.get());
199 contacts::test::AddEmailAddress(
200 "foo@example.org",
201 contacts::Contact_AddressType_Relation_OTHER, "Fake", false,
202 contact2.get());
203 contacts::test::AddPhoneNumber(
204 "123-456-7890",
205 contacts::Contact_AddressType_Relation_MOBILE, "", false,
206 contact2.get());
207 contacts::test::AddPhoneNumber(
208 "234-567-8901",
209 contacts::Contact_AddressType_Relation_OTHER, "grandcentral", false,
210 contact2.get());
211 contacts::test::AddPostalAddress(
212 "100 Elm St\nSan Francisco, CA 94110",
213 contacts::Contact_AddressType_Relation_HOME, "", false, contact2.get());
214 contacts::test::AddInstantMessagingAddress(
215 "foo@example.org",
216 contacts::Contact_InstantMessagingAddress_Protocol_GOOGLE_TALK,
217 contacts::Contact_AddressType_Relation_OTHER, "", false,
218 contact2.get());
219 contacts::test::AddInstantMessagingAddress(
220 "12345678",
221 contacts::Contact_InstantMessagingAddress_Protocol_ICQ,
222 contacts::Contact_AddressType_Relation_OTHER, "", false,
223 contact2.get());
224
225 scoped_ptr<contacts::Contact> contact3(new contacts::Contact);
226 InitContact("http://example.com/3",
227 "2012-07-23T23:07:06.133Z",
228 true, "", "", "", "", "", "",
229 contact3.get());
230
231 EXPECT_EQ(contacts::test::VarContactsToString(
232 3, contact1.get(), contact2.get(), contact3.get()),
233 contacts::test::ContactsToString(*contacts));
234 }
235
236 // Download a feed containing more photos than we're able to download in
237 // parallel to check that we still end up with all the photos.
238 IN_PROC_BROWSER_TEST_F(GDataContactsServiceTest, ParallelPhotoDownload) {
239 // The feed used for this test contains 8 contacts.
240 const int kNumContacts = 8;
241 service()->set_max_simultaneous_photo_downloads_for_testing(2);
242 scoped_ptr<ScopedVector<contacts::Contact> > contacts;
243 EXPECT_TRUE(Download("feed_multiple_photos.json", base::Time(), &contacts));
244 ASSERT_EQ(static_cast<size_t>(kNumContacts), contacts->size());
245
246 ScopedVector<contacts::Contact> expected_contacts;
247 for (int i = 0; i < kNumContacts; ++i) {
248 contacts::Contact* contact = new contacts::Contact;
249 InitContact(base::StringPrintf("http://example.com/%d", i + 1),
250 "2012-06-04T15:53:36.023Z",
251 false, "", "", "", "", "", "", contact);
252 contacts::test::SetPhoto(gfx::Size(kPhotoSize, kPhotoSize), contact);
253 expected_contacts.push_back(contact);
254 }
255 EXPECT_EQ(contacts::test::ContactsToString(expected_contacts),
256 contacts::test::ContactsToString(*contacts));
257 }
258
259 } // namespace gdata
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/gdata_contacts_service.cc ('k') | chrome/browser/chromeos/gdata/gdata_operations.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698