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

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

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