| 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/contacts/gdata_contacts_service.h" | 5 #include "chrome/browser/chromeos/contacts/gdata_contacts_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "chrome/browser/chromeos/contacts/contact.pb.h" | 13 #include "chrome/browser/chromeos/contacts/contact.pb.h" |
| 14 #include "chrome/browser/chromeos/contacts/contact_test_util.h" | 14 #include "chrome/browser/chromeos/contacts/contact_test_util.h" |
| 15 #include "chrome/browser/google_apis/auth_service.h" | 15 #include "chrome/browser/google_apis/auth_service.h" |
| 16 #include "chrome/browser/google_apis/test_util.h" | 16 #include "chrome/browser/google_apis/test_util.h" |
| 17 #include "chrome/browser/google_apis/time_util.h" | 17 #include "chrome/browser/google_apis/time_util.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
| 24 #include "content/public/test/test_utils.h" | 24 #include "content/public/test/test_utils.h" |
| 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 25 #include "net/test/embedded_test_server/http_request.h" | 26 #include "net/test/embedded_test_server/http_request.h" |
| 26 #include "net/test/embedded_test_server/http_response.h" | 27 #include "net/test/embedded_test_server/http_response.h" |
| 27 #include "net/test/embedded_test_server/http_server.h" | |
| 28 #include "net/url_request/url_request_test_util.h" | 28 #include "net/url_request/url_request_test_util.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "ui/gfx/size.h" | 30 #include "ui/gfx/size.h" |
| 31 | 31 |
| 32 using content::BrowserThread; | 32 using content::BrowserThread; |
| 33 | 33 |
| 34 namespace contacts { | 34 namespace contacts { |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 const char kTestGDataAuthToken[] = "testtoken"; | 37 const char kTestGDataAuthToken[] = "testtoken"; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 } | 78 } |
| 79 | 79 |
| 80 virtual void SetUp() OVERRIDE { | 80 virtual void SetUp() OVERRIDE { |
| 81 io_thread_.StartIOThread(); | 81 io_thread_.StartIOThread(); |
| 82 profile_.reset(new TestingProfile); | 82 profile_.reset(new TestingProfile); |
| 83 request_context_getter_ = new net::TestURLRequestContextGetter( | 83 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 84 content::BrowserThread::GetMessageLoopProxyForThread( | 84 content::BrowserThread::GetMessageLoopProxyForThread( |
| 85 content::BrowserThread::IO)); | 85 content::BrowserThread::IO)); |
| 86 | 86 |
| 87 test_server_.reset( | 87 test_server_.reset( |
| 88 new google_apis::test_server::HttpServer( | 88 new net::test_server::EmbeddedTestServer( |
| 89 content::BrowserThread::GetMessageLoopProxyForThread( | 89 content::BrowserThread::GetMessageLoopProxyForThread( |
| 90 content::BrowserThread::IO))); | 90 content::BrowserThread::IO))); |
| 91 ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady()); | 91 ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady()); |
| 92 test_server_->RegisterRequestHandler( | 92 test_server_->RegisterRequestHandler( |
| 93 base::Bind(&GDataContactsServiceTest::HandleDownloadRequest, | 93 base::Bind(&GDataContactsServiceTest::HandleDownloadRequest, |
| 94 base::Unretained(this))); | 94 base::Unretained(this))); |
| 95 service_.reset(new GDataContactsService( | 95 service_.reset(new GDataContactsService( |
| 96 request_context_getter_, | 96 request_context_getter_, |
| 97 profile_.get())); | 97 profile_.get())); |
| 98 service_->Initialize(); | 98 service_->Initialize(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 base::Unretained(this)), | 130 base::Unretained(this)), |
| 131 base::Bind(&GDataContactsServiceTest::OnFailure, | 131 base::Bind(&GDataContactsServiceTest::OnFailure, |
| 132 base::Unretained(this)), | 132 base::Unretained(this)), |
| 133 min_update_time); | 133 min_update_time); |
| 134 content::RunMessageLoop(); | 134 content::RunMessageLoop(); |
| 135 contacts->swap(downloaded_contacts_); | 135 contacts->swap(downloaded_contacts_); |
| 136 return download_was_successful_; | 136 return download_was_successful_; |
| 137 } | 137 } |
| 138 | 138 |
| 139 scoped_ptr<GDataContactsService> service_; | 139 scoped_ptr<GDataContactsService> service_; |
| 140 scoped_ptr<google_apis::test_server::HttpServer> test_server_; | 140 scoped_ptr<net::test_server::EmbeddedTestServer> test_server_; |
| 141 | 141 |
| 142 private: | 142 private: |
| 143 // Rewrites |original_url|, a photo URL from a contacts feed, to instead point | 143 // Rewrites |original_url|, a photo URL from a contacts feed, to instead point |
| 144 // at a file on |test_server_|. | 144 // at a file on |test_server_|. |
| 145 std::string RewritePhotoUrl(const std::string& original_url) { | 145 std::string RewritePhotoUrl(const std::string& original_url) { |
| 146 return test_server_->GetURL(GURL(original_url).path()).spec(); | 146 return test_server_->GetURL(GURL(original_url).path()).spec(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 // Handles success for Download(). | 149 // Handles success for Download(). |
| 150 void OnSuccess(scoped_ptr<ScopedVector<contacts::Contact> > contacts) { | 150 void OnSuccess(scoped_ptr<ScopedVector<contacts::Contact> > contacts) { |
| 151 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 151 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 152 download_was_successful_ = true; | 152 download_was_successful_ = true; |
| 153 downloaded_contacts_.swap(contacts); | 153 downloaded_contacts_.swap(contacts); |
| 154 MessageLoop::current()->Quit(); | 154 MessageLoop::current()->Quit(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Handles failure for Download(). | 157 // Handles failure for Download(). |
| 158 void OnFailure() { | 158 void OnFailure() { |
| 159 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 159 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 160 download_was_successful_ = false; | 160 download_was_successful_ = false; |
| 161 downloaded_contacts_.reset(new ScopedVector<contacts::Contact>()); | 161 downloaded_contacts_.reset(new ScopedVector<contacts::Contact>()); |
| 162 MessageLoop::current()->Quit(); | 162 MessageLoop::current()->Quit(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 // Handles a request for downloading a file. Reads a requested file and | 165 // Handles a request for downloading a file. Reads a requested file and |
| 166 // returns the content. | 166 // returns the content. |
| 167 scoped_ptr<google_apis::test_server::HttpResponse> HandleDownloadRequest( | 167 scoped_ptr<net::test_server::HttpResponse> HandleDownloadRequest( |
| 168 const google_apis::test_server::HttpRequest& request) { | 168 const net::test_server::HttpRequest& request) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 170 // Requested url must not contain a query string. | 170 // Requested url must not contain a query string. |
| 171 scoped_ptr<google_apis::test_server::HttpResponse> result = | 171 scoped_ptr<net::test_server::HttpResponse> result = |
| 172 google_apis::test_util::CreateHttpResponseFromFile( | 172 google_apis::test_util::CreateHttpResponseFromFile( |
| 173 google_apis::test_util::GetTestFilePath( | 173 google_apis::test_util::GetTestFilePath( |
| 174 std::string("chromeos/gdata/contacts") + request.relative_url)); | 174 std::string("chromeos/gdata/contacts") + request.relative_url)); |
| 175 return result.Pass(); | 175 return result.Pass(); |
| 176 } | 176 } |
| 177 | 177 |
| 178 MessageLoopForUI message_loop_; | 178 MessageLoopForUI message_loop_; |
| 179 content::TestBrowserThread ui_thread_; | 179 content::TestBrowserThread ui_thread_; |
| 180 content::TestBrowserThread io_thread_; | 180 content::TestBrowserThread io_thread_; |
| 181 scoped_ptr<TestingProfile> profile_; | 181 scoped_ptr<TestingProfile> profile_; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 scoped_ptr<contacts::Contact> contact2(new contacts::Contact); | 337 scoped_ptr<contacts::Contact> contact2(new contacts::Contact); |
| 338 InitContact("http://example.com/2", "2012-06-21T16:20:13.208Z", | 338 InitContact("http://example.com/2", "2012-06-21T16:20:13.208Z", |
| 339 false, "Bob Smith", "Bob", "", "Smith", "", "", | 339 false, "Bob Smith", "Bob", "", "Smith", "", "", |
| 340 contact2.get()); | 340 contact2.get()); |
| 341 EXPECT_EQ(contacts::test::VarContactsToString( | 341 EXPECT_EQ(contacts::test::VarContactsToString( |
| 342 2, contact1.get(), contact2.get()), | 342 2, contact1.get(), contact2.get()), |
| 343 contacts::test::ContactsToString(*contacts)); | 343 contacts::test::ContactsToString(*contacts)); |
| 344 } | 344 } |
| 345 | 345 |
| 346 } // namespace contacts | 346 } // namespace contacts |
| OLD | NEW |