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_operations.h" | 5 #include "chrome/browser/google_apis/base_operations.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/operation_registry.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/operation_runner.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" |
(...skipping 19 matching lines...) Expand all Loading... |
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(), |
| 53 request_context_getter_, |
| 54 std::vector<std::string>(), |
| 55 kTestUserAgent)); |
| 56 operation_runner_->auth_service()->set_access_token_for_testing( |
| 57 kTestAuthToken); |
| 58 |
52 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 59 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
53 test_server_.RegisterRequestHandler( | 60 test_server_.RegisterRequestHandler( |
54 base::Bind(&test_util::HandleDownloadRequest, | 61 base::Bind(&test_util::HandleDownloadRequest, |
55 test_server_.base_url(), | 62 test_server_.base_url(), |
56 base::Unretained(&http_request_))); | 63 base::Unretained(&http_request_))); |
57 } | 64 } |
58 | 65 |
59 virtual void TearDown() OVERRIDE { | 66 virtual void TearDown() OVERRIDE { |
60 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 67 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
61 request_context_getter_ = NULL; | 68 request_context_getter_ = NULL; |
62 } | 69 } |
63 | 70 |
64 // Returns a temporary file path suitable for storing the cache file. | 71 // Returns a temporary file path suitable for storing the cache file. |
65 base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) { | 72 base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) { |
66 return profile_->GetPath().Append(file_name); | 73 return profile_->GetPath().Append(file_name); |
67 } | 74 } |
68 | 75 |
69 MessageLoopForUI message_loop_; | 76 MessageLoopForUI message_loop_; |
70 content::TestBrowserThread ui_thread_; | 77 content::TestBrowserThread ui_thread_; |
71 content::TestBrowserThread file_thread_; | 78 content::TestBrowserThread file_thread_; |
72 content::TestBrowserThread io_thread_; | 79 content::TestBrowserThread io_thread_; |
73 net::test_server::EmbeddedTestServer test_server_; | 80 net::test_server::EmbeddedTestServer test_server_; |
74 scoped_ptr<TestingProfile> profile_; | 81 scoped_ptr<TestingProfile> profile_; |
75 OperationRegistry operation_registry_; | 82 scoped_ptr<OperationRunner> operation_runner_; |
76 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 83 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
77 | 84 |
78 // 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 |
79 // parameters like HTTP method (ex. some operations should use DELETE | 86 // parameters like HTTP method (ex. some operations should use DELETE |
80 // instead of GET). | 87 // instead of GET). |
81 net::test_server::HttpRequest http_request_; | 88 net::test_server::HttpRequest http_request_; |
82 }; | 89 }; |
83 | 90 |
84 TEST_F(BaseOperationsServerTest, DownloadFileOperation_ValidFile) { | 91 TEST_F(BaseOperationsServerTest, DownloadFileOperation_ValidFile) { |
85 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 92 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
86 base::FilePath temp_file; | 93 base::FilePath temp_file; |
87 DownloadFileOperation* operation = new DownloadFileOperation( | 94 DownloadFileOperation* operation = new DownloadFileOperation( |
88 &operation_registry_, | 95 operation_runner_.get(), |
89 request_context_getter_.get(), | 96 request_context_getter_.get(), |
90 CreateComposedCallback( | 97 CreateComposedCallback( |
91 base::Bind(&test_util::RunAndQuit), | 98 base::Bind(&test_util::RunAndQuit), |
92 test_util::CreateCopyResultCallback(&result_code, &temp_file)), | 99 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
93 GetContentCallback(), | 100 GetContentCallback(), |
94 ProgressCallback(), | 101 ProgressCallback(), |
95 test_server_.GetURL("/files/chromeos/gdata/testfile.txt"), | 102 test_server_.GetURL("/files/chromeos/gdata/testfile.txt"), |
96 base::FilePath::FromUTF8Unsafe("/dummy/gdata/testfile.txt"), | 103 base::FilePath::FromUTF8Unsafe("/dummy/gdata/testfile.txt"), |
97 GetTestCachedFilePath( | 104 GetTestCachedFilePath( |
98 base::FilePath::FromUTF8Unsafe("cached_testfile.txt"))); | 105 base::FilePath::FromUTF8Unsafe("cached_testfile.txt"))); |
99 operation->Start(kTestAuthToken, kTestUserAgent, | 106 operation_runner_->StartOperationWithRetry(operation); |
100 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); | |
101 MessageLoop::current()->Run(); | 107 MessageLoop::current()->Run(); |
102 | 108 |
103 std::string contents; | 109 std::string contents; |
104 file_util::ReadFileToString(temp_file, &contents); | 110 file_util::ReadFileToString(temp_file, &contents); |
105 file_util::Delete(temp_file, false); | 111 file_util::Delete(temp_file, false); |
106 | 112 |
107 EXPECT_EQ(HTTP_SUCCESS, result_code); | 113 EXPECT_EQ(HTTP_SUCCESS, result_code); |
108 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 114 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
109 EXPECT_EQ("/files/chromeos/gdata/testfile.txt", http_request_.relative_url); | 115 EXPECT_EQ("/files/chromeos/gdata/testfile.txt", http_request_.relative_url); |
110 | 116 |
111 const base::FilePath expected_path = | 117 const base::FilePath expected_path = |
112 test_util::GetTestFilePath("chromeos/gdata/testfile.txt"); | 118 test_util::GetTestFilePath("chromeos/gdata/testfile.txt"); |
113 std::string expected_contents; | 119 std::string expected_contents; |
114 file_util::ReadFileToString(expected_path, &expected_contents); | 120 file_util::ReadFileToString(expected_path, &expected_contents); |
115 EXPECT_EQ(expected_contents, contents); | 121 EXPECT_EQ(expected_contents, contents); |
116 } | 122 } |
117 | 123 |
118 // http://crbug.com/169588 | 124 // http://crbug.com/169588 |
119 TEST_F(BaseOperationsServerTest, | 125 TEST_F(BaseOperationsServerTest, |
120 DISABLED_DownloadFileOperation_NonExistentFile) { | 126 DISABLED_DownloadFileOperation_NonExistentFile) { |
121 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 127 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
122 base::FilePath temp_file; | 128 base::FilePath temp_file; |
123 DownloadFileOperation* operation = new DownloadFileOperation( | 129 DownloadFileOperation* operation = new DownloadFileOperation( |
124 &operation_registry_, | 130 operation_runner_.get(), |
125 request_context_getter_.get(), | 131 request_context_getter_.get(), |
126 CreateComposedCallback( | 132 CreateComposedCallback( |
127 base::Bind(&test_util::RunAndQuit), | 133 base::Bind(&test_util::RunAndQuit), |
128 test_util::CreateCopyResultCallback(&result_code, &temp_file)), | 134 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
129 GetContentCallback(), | 135 GetContentCallback(), |
130 ProgressCallback(), | 136 ProgressCallback(), |
131 test_server_.GetURL("/files/chromeos/gdata/no-such-file.txt"), | 137 test_server_.GetURL("/files/chromeos/gdata/no-such-file.txt"), |
132 base::FilePath::FromUTF8Unsafe("/dummy/gdata/no-such-file.txt"), | 138 base::FilePath::FromUTF8Unsafe("/dummy/gdata/no-such-file.txt"), |
133 GetTestCachedFilePath( | 139 GetTestCachedFilePath( |
134 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt"))); | 140 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt"))); |
135 operation->Start(kTestAuthToken, kTestUserAgent, | 141 operation_runner_->StartOperationWithRetry(operation); |
136 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); | |
137 MessageLoop::current()->Run(); | 142 MessageLoop::current()->Run(); |
138 | 143 |
139 std::string contents; | 144 std::string contents; |
140 file_util::ReadFileToString(temp_file, &contents); | 145 file_util::ReadFileToString(temp_file, &contents); |
141 file_util::Delete(temp_file, false); | 146 file_util::Delete(temp_file, false); |
142 | 147 |
143 EXPECT_EQ(HTTP_NOT_FOUND, result_code); | 148 EXPECT_EQ(HTTP_NOT_FOUND, result_code); |
144 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 149 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
145 EXPECT_EQ("/files/chromeos/gdata/no-such-file.txt", | 150 EXPECT_EQ("/files/chromeos/gdata/no-such-file.txt", |
146 http_request_.relative_url); | 151 http_request_.relative_url); |
147 // Do not verify the not found message. | 152 // Do not verify the not found message. |
148 } | 153 } |
149 | 154 |
150 } // namespace google_apis | 155 } // namespace google_apis |
OLD | NEW |