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