| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/google_apis/base_requests.h" | 5 #include "chrome/browser/google_apis/base_requests.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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/google_apis/auth_service.h" | 12 #include "chrome/browser/google_apis/auth_service.h" |
| 13 #include "chrome/browser/google_apis/operation_runner.h" | 13 #include "chrome/browser/google_apis/request_sender.h" |
| 14 #include "chrome/browser/google_apis/task_util.h" | 14 #include "chrome/browser/google_apis/task_util.h" |
| 15 #include "chrome/browser/google_apis/test_util.h" | 15 #include "chrome/browser/google_apis/test_util.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server.h" | 18 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 19 #include "net/test/embedded_test_server/http_request.h" | 19 #include "net/test/embedded_test_server/http_request.h" |
| 20 #include "net/test/embedded_test_server/http_response.h" | 20 #include "net/test/embedded_test_server/http_response.h" |
| 21 #include "net/url_request/url_request_test_util.h" | 21 #include "net/url_request/url_request_test_util.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 namespace google_apis { | 24 namespace google_apis { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const char kTestAuthToken[] = "testtoken"; | 28 const char kTestAuthToken[] = "testtoken"; |
| 29 const char kTestUserAgent[] = "test-user-agent"; | 29 const char kTestUserAgent[] = "test-user-agent"; |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 class BaseOperationsServerTest : public testing::Test { | 33 class BaseOperationsServerTest : public testing::Test { |
| 34 protected: | 34 protected: |
| 35 BaseOperationsServerTest() | 35 BaseOperationsServerTest() |
| 36 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 36 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 37 file_thread_(content::BrowserThread::FILE), | 37 file_thread_(content::BrowserThread::FILE), |
| 38 io_thread_(content::BrowserThread::IO), | 38 io_thread_(content::BrowserThread::IO), |
| 39 test_server_(content::BrowserThread::GetMessageLoopProxyForThread( | 39 test_server_(content::BrowserThread::GetMessageLoopProxyForThread( |
| 40 content::BrowserThread::IO)) { | 40 content::BrowserThread::IO)) { |
| 41 } | 41 } |
| 42 | 42 |
| 43 virtual void SetUp() OVERRIDE { | 43 virtual void SetUp() OVERRIDE { |
| 44 file_thread_.Start(); | 44 file_thread_.Start(); |
| 45 io_thread_.StartIOThread(); | 45 io_thread_.StartIOThread(); |
| 46 profile_.reset(new TestingProfile); | 46 profile_.reset(new TestingProfile); |
| 47 | 47 |
| 48 request_context_getter_ = new net::TestURLRequestContextGetter( | 48 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 49 content::BrowserThread::GetMessageLoopProxyForThread( | 49 content::BrowserThread::GetMessageLoopProxyForThread( |
| 50 content::BrowserThread::IO)); | 50 content::BrowserThread::IO)); |
| 51 | 51 |
| 52 operation_runner_.reset(new OperationRunner(profile_.get(), | 52 operation_runner_.reset(new RequestSender(profile_.get(), |
| 53 request_context_getter_.get(), | 53 request_context_getter_.get(), |
| 54 std::vector<std::string>(), | 54 std::vector<std::string>(), |
| 55 kTestUserAgent)); | 55 kTestUserAgent)); |
| 56 operation_runner_->auth_service()->set_access_token_for_testing( | 56 operation_runner_->auth_service()->set_access_token_for_testing( |
| 57 kTestAuthToken); | 57 kTestAuthToken); |
| 58 | 58 |
| 59 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 59 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
| 60 test_server_.RegisterRequestHandler( | 60 test_server_.RegisterRequestHandler( |
| 61 base::Bind(&test_util::HandleDownloadFileRequest, | 61 base::Bind(&test_util::HandleDownloadFileRequest, |
| 62 test_server_.base_url(), | 62 test_server_.base_url(), |
| 63 base::Unretained(&http_request_))); | 63 base::Unretained(&http_request_))); |
| 64 } | 64 } |
| 65 | 65 |
| 66 virtual void TearDown() OVERRIDE { | 66 virtual void TearDown() OVERRIDE { |
| 67 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 67 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 68 request_context_getter_ = NULL; | 68 request_context_getter_ = NULL; |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Returns a temporary file path suitable for storing the cache file. | 71 // Returns a temporary file path suitable for storing the cache file. |
| 72 base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) { | 72 base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) { |
| 73 return profile_->GetPath().Append(file_name); | 73 return profile_->GetPath().Append(file_name); |
| 74 } | 74 } |
| 75 | 75 |
| 76 base::MessageLoopForUI message_loop_; | 76 base::MessageLoopForUI message_loop_; |
| 77 content::TestBrowserThread ui_thread_; | 77 content::TestBrowserThread ui_thread_; |
| 78 content::TestBrowserThread file_thread_; | 78 content::TestBrowserThread file_thread_; |
| 79 content::TestBrowserThread io_thread_; | 79 content::TestBrowserThread io_thread_; |
| 80 net::test_server::EmbeddedTestServer test_server_; | 80 net::test_server::EmbeddedTestServer test_server_; |
| 81 scoped_ptr<TestingProfile> profile_; | 81 scoped_ptr<TestingProfile> profile_; |
| 82 scoped_ptr<OperationRunner> operation_runner_; | 82 scoped_ptr<RequestSender> operation_runner_; |
| 83 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 83 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 84 | 84 |
| 85 // The incoming HTTP request is saved so tests can verify the request | 85 // The incoming HTTP request is saved so tests can verify the request |
| 86 // parameters like HTTP method (ex. some operations should use DELETE | 86 // parameters like HTTP method (ex. some operations should use DELETE |
| 87 // instead of GET). | 87 // instead of GET). |
| 88 net::test_server::HttpRequest http_request_; | 88 net::test_server::HttpRequest http_request_; |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 TEST_F(BaseOperationsServerTest, DownloadFileRequest_ValidFile) { | 91 TEST_F(BaseOperationsServerTest, DownloadFileRequest_ValidFile) { |
| 92 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 92 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
| 93 base::FilePath temp_file; | 93 base::FilePath temp_file; |
| 94 DownloadFileRequest* operation = new DownloadFileRequest( | 94 DownloadFileRequest* operation = new DownloadFileRequest( |
| 95 operation_runner_.get(), | 95 operation_runner_.get(), |
| 96 request_context_getter_.get(), | 96 request_context_getter_.get(), |
| 97 CreateComposedCallback( | 97 CreateComposedCallback( |
| 98 base::Bind(&test_util::RunAndQuit), | 98 base::Bind(&test_util::RunAndQuit), |
| 99 test_util::CreateCopyResultCallback(&result_code, &temp_file)), | 99 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
| 100 GetContentCallback(), | 100 GetContentCallback(), |
| 101 ProgressCallback(), | 101 ProgressCallback(), |
| 102 test_server_.GetURL("/files/chromeos/gdata/testfile.txt"), | 102 test_server_.GetURL("/files/chromeos/gdata/testfile.txt"), |
| 103 base::FilePath::FromUTF8Unsafe("/dummy/gdata/testfile.txt"), | 103 base::FilePath::FromUTF8Unsafe("/dummy/gdata/testfile.txt"), |
| 104 GetTestCachedFilePath( | 104 GetTestCachedFilePath( |
| 105 base::FilePath::FromUTF8Unsafe("cached_testfile.txt"))); | 105 base::FilePath::FromUTF8Unsafe("cached_testfile.txt"))); |
| 106 operation_runner_->StartOperationWithRetry(operation); | 106 operation_runner_->StartRequestWithRetry(operation); |
| 107 base::MessageLoop::current()->Run(); | 107 base::MessageLoop::current()->Run(); |
| 108 | 108 |
| 109 std::string contents; | 109 std::string contents; |
| 110 file_util::ReadFileToString(temp_file, &contents); | 110 file_util::ReadFileToString(temp_file, &contents); |
| 111 file_util::Delete(temp_file, false); | 111 file_util::Delete(temp_file, false); |
| 112 | 112 |
| 113 EXPECT_EQ(HTTP_SUCCESS, result_code); | 113 EXPECT_EQ(HTTP_SUCCESS, result_code); |
| 114 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 114 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
| 115 EXPECT_EQ("/files/chromeos/gdata/testfile.txt", http_request_.relative_url); | 115 EXPECT_EQ("/files/chromeos/gdata/testfile.txt", http_request_.relative_url); |
| 116 | 116 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 131 request_context_getter_.get(), | 131 request_context_getter_.get(), |
| 132 CreateComposedCallback( | 132 CreateComposedCallback( |
| 133 base::Bind(&test_util::RunAndQuit), | 133 base::Bind(&test_util::RunAndQuit), |
| 134 test_util::CreateCopyResultCallback(&result_code, &temp_file)), | 134 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
| 135 GetContentCallback(), | 135 GetContentCallback(), |
| 136 ProgressCallback(), | 136 ProgressCallback(), |
| 137 test_server_.GetURL("/files/chromeos/gdata/no-such-file.txt"), | 137 test_server_.GetURL("/files/chromeos/gdata/no-such-file.txt"), |
| 138 base::FilePath::FromUTF8Unsafe("/dummy/gdata/no-such-file.txt"), | 138 base::FilePath::FromUTF8Unsafe("/dummy/gdata/no-such-file.txt"), |
| 139 GetTestCachedFilePath( | 139 GetTestCachedFilePath( |
| 140 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt"))); | 140 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt"))); |
| 141 operation_runner_->StartOperationWithRetry(operation); | 141 operation_runner_->StartRequestWithRetry(operation); |
| 142 base::MessageLoop::current()->Run(); | 142 base::MessageLoop::current()->Run(); |
| 143 | 143 |
| 144 std::string contents; | 144 std::string contents; |
| 145 file_util::ReadFileToString(temp_file, &contents); | 145 file_util::ReadFileToString(temp_file, &contents); |
| 146 file_util::Delete(temp_file, false); | 146 file_util::Delete(temp_file, false); |
| 147 | 147 |
| 148 EXPECT_EQ(HTTP_NOT_FOUND, result_code); | 148 EXPECT_EQ(HTTP_NOT_FOUND, result_code); |
| 149 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 149 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
| 150 EXPECT_EQ("/files/chromeos/gdata/no-such-file.txt", | 150 EXPECT_EQ("/files/chromeos/gdata/no-such-file.txt", |
| 151 http_request_.relative_url); | 151 http_request_.relative_url); |
| 152 // Do not verify the not found message. | 152 // Do not verify the not found message. |
| 153 } | 153 } |
| 154 | 154 |
| 155 } // namespace google_apis | 155 } // namespace google_apis |
| OLD | NEW |