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.
5 #include "chrome/browser/google_apis/base_operations.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/operation_registry.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_server/http_request.h"
16 #include "chrome/browser/google_apis/test_server/http_response.h"
17 #include "chrome/browser/google_apis/test_server/http_server.h"
18 #include "chrome/browser/google_apis/test_util.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/test/test_browser_thread.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace google_apis
{
28 const char kTestAuthToken
[] = "testtoken";
29 const char kTestUserAgent
[] = "test-user-agent";
33 class BaseOperationsServerTest
: public testing::Test
{
35 BaseOperationsServerTest()
36 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
37 file_thread_(content::BrowserThread::FILE),
38 io_thread_(content::BrowserThread::IO
) {
41 virtual void SetUp() OVERRIDE
{
43 io_thread_
.StartIOThread();
44 profile_
.reset(new TestingProfile
);
46 request_context_getter_
= new net::TestURLRequestContextGetter(
47 content::BrowserThread::GetMessageLoopProxyForThread(
48 content::BrowserThread::IO
));
50 ASSERT_TRUE(test_server_
.InitializeAndWaitUntilReady());
51 test_server_
.RegisterRequestHandler(
52 base::Bind(&test_util::HandleDownloadRequest
,
53 test_server_
.base_url(),
54 base::Unretained(&http_request_
)));
57 virtual void TearDown() OVERRIDE
{
58 test_server_
.ShutdownAndWaitUntilComplete();
59 request_context_getter_
= NULL
;
62 // Returns a temporary file path suitable for storing the cache file.
63 base::FilePath
GetTestCachedFilePath(const base::FilePath
& file_name
) {
64 return profile_
->GetPath().Append(file_name
);
67 MessageLoopForUI message_loop_
;
68 content::TestBrowserThread ui_thread_
;
69 content::TestBrowserThread file_thread_
;
70 content::TestBrowserThread io_thread_
;
71 test_server::HttpServer test_server_
;
72 scoped_ptr
<TestingProfile
> profile_
;
73 OperationRegistry operation_registry_
;
74 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
76 // The incoming HTTP request is saved so tests can verify the request
77 // parameters like HTTP method (ex. some operations should use DELETE
79 test_server::HttpRequest http_request_
;
82 TEST_F(BaseOperationsServerTest
, DownloadFileOperation_ValidFile
) {
83 GDataErrorCode result_code
= GDATA_OTHER_ERROR
;
84 base::FilePath temp_file
;
85 DownloadFileOperation
* operation
= new DownloadFileOperation(
87 request_context_getter_
.get(),
88 CreateComposedCallback(
89 base::Bind(&test_util::RunAndQuit
),
90 test_util::CreateCopyResultCallback(&result_code
, &temp_file
)),
93 test_server_
.GetURL("/files/chromeos/gdata/testfile.txt"),
94 base::FilePath::FromUTF8Unsafe("/dummy/gdata/testfile.txt"),
95 GetTestCachedFilePath(
96 base::FilePath::FromUTF8Unsafe("cached_testfile.txt")));
97 operation
->Start(kTestAuthToken
, kTestUserAgent
,
98 base::Bind(&test_util::DoNothingForReAuthenticateCallback
));
99 MessageLoop::current()->Run();
101 std::string contents
;
102 file_util::ReadFileToString(temp_file
, &contents
);
103 file_util::Delete(temp_file
, false);
105 EXPECT_EQ(HTTP_SUCCESS
, result_code
);
106 EXPECT_EQ(test_server::METHOD_GET
, http_request_
.method
);
107 EXPECT_EQ("/files/chromeos/gdata/testfile.txt", http_request_
.relative_url
);
109 const base::FilePath expected_path
=
110 test_util::GetTestFilePath("chromeos/gdata/testfile.txt");
111 std::string expected_contents
;
112 file_util::ReadFileToString(expected_path
, &expected_contents
);
113 EXPECT_EQ(expected_contents
, contents
);
116 // http://crbug.com/169588
117 TEST_F(BaseOperationsServerTest
,
118 DISABLED_DownloadFileOperation_NonExistentFile
) {
119 GDataErrorCode result_code
= GDATA_OTHER_ERROR
;
120 base::FilePath temp_file
;
121 DownloadFileOperation
* operation
= new DownloadFileOperation(
122 &operation_registry_
,
123 request_context_getter_
.get(),
124 CreateComposedCallback(
125 base::Bind(&test_util::RunAndQuit
),
126 test_util::CreateCopyResultCallback(&result_code
, &temp_file
)),
127 GetContentCallback(),
129 test_server_
.GetURL("/files/chromeos/gdata/no-such-file.txt"),
130 base::FilePath::FromUTF8Unsafe("/dummy/gdata/no-such-file.txt"),
131 GetTestCachedFilePath(
132 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt")));
133 operation
->Start(kTestAuthToken
, kTestUserAgent
,
134 base::Bind(&test_util::DoNothingForReAuthenticateCallback
));
135 MessageLoop::current()->Run();
137 std::string contents
;
138 file_util::ReadFileToString(temp_file
, &contents
);
139 file_util::Delete(temp_file
, false);
141 EXPECT_EQ(HTTP_NOT_FOUND
, result_code
);
142 EXPECT_EQ(test_server::METHOD_GET
, http_request_
.method
);
143 EXPECT_EQ("/files/chromeos/gdata/no-such-file.txt",
144 http_request_
.relative_url
);
145 // Do not verify the not found message.
148 } // namespace google_apis