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