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 "base/bind.h" |
| 6 #include "base/file_path.h" |
| 7 #include "base/message_loop_proxy.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/google_apis/drive_api_operations.h" |
| 10 #include "chrome/browser/google_apis/drive_api_url_generator.h" |
| 11 #include "chrome/browser/google_apis/operation_registry.h" |
| 12 #include "chrome/browser/google_apis/test_server/http_server.h" |
| 13 #include "chrome/browser/google_apis/test_util.h" |
| 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace google_apis { |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char kTestDriveApiAuthToken[] = "testtoken"; |
| 23 const char kTestUserAgent[] = "test-user-agent"; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 class DriveApiOperationsTest : public testing::Test { |
| 28 public: |
| 29 DriveApiOperationsTest() |
| 30 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 31 file_thread_(content::BrowserThread::FILE), |
| 32 io_thread_(content::BrowserThread::IO) { |
| 33 } |
| 34 |
| 35 virtual void SetUp() OVERRIDE { |
| 36 file_thread_.Start(); |
| 37 io_thread_.StartIOThread(); |
| 38 |
| 39 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 40 content::BrowserThread::GetMessageLoopProxyForThread( |
| 41 content::BrowserThread::IO)); |
| 42 |
| 43 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
| 44 test_server_.RegisterRequestHandler( |
| 45 base::Bind(&DriveApiOperationsTest::HandleDataFileRequest, |
| 46 base::Unretained(this))); |
| 47 |
| 48 url_generator_.reset(new DriveApiUrlGenerator( |
| 49 test_util::GetBaseUrlForTesting(test_server_.port()))); |
| 50 } |
| 51 |
| 52 virtual void TearDown() OVERRIDE { |
| 53 test_server_.ShutdownAndWaitUntilComplete(); |
| 54 request_context_getter_ = NULL; |
| 55 expected_data_file_path_.clear(); |
| 56 } |
| 57 |
| 58 MessageLoopForUI message_loop_; |
| 59 content::TestBrowserThread ui_thread_; |
| 60 content::TestBrowserThread file_thread_; |
| 61 content::TestBrowserThread io_thread_; |
| 62 test_server::HttpServer test_server_; |
| 63 OperationRegistry operation_registry_; |
| 64 scoped_ptr<DriveApiUrlGenerator> url_generator_; |
| 65 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 66 |
| 67 // This is a path to the file which contains expected response from |
| 68 // the server. See also HandleDataFileRequest below. |
| 69 FilePath expected_data_file_path_; |
| 70 |
| 71 // The incoming HTTP request is saved so tests can verify the request |
| 72 // parameters like HTTP method (ex. some operations should use DELETE |
| 73 // instead of GET). |
| 74 test_server::HttpRequest http_request_; |
| 75 |
| 76 private: |
| 77 // Reads the data file of |expected_data_file_path_| and returns its content |
| 78 // for the request. |
| 79 // To use this method, it is necessary to set |expected_data_file_path_| |
| 80 // to the appropriate file path before sending the request to the server. |
| 81 scoped_ptr<test_server::HttpResponse> HandleDataFileRequest( |
| 82 const test_server::HttpRequest& request) { |
| 83 http_request_ = request; |
| 84 |
| 85 if (expected_data_file_path_.empty()) { |
| 86 // The file is not specified. Delegate the processing to the next |
| 87 // handler. |
| 88 return scoped_ptr<test_server::HttpResponse>(); |
| 89 } |
| 90 |
| 91 // Return the response from the data file. |
| 92 return test_util::CreateHttpResponseFromFile(expected_data_file_path_); |
| 93 } |
| 94 }; |
| 95 |
| 96 TEST_F(DriveApiOperationsTest, GetAboutOperation_ValidFeed) { |
| 97 // Set an expected data file containing valid result. |
| 98 expected_data_file_path_ = test_util::GetTestFilePath("drive/about.json"); |
| 99 |
| 100 GDataErrorCode error = GDATA_OTHER_ERROR; |
| 101 scoped_ptr<base::Value> feed_data; |
| 102 |
| 103 GetAboutOperation* operation = new GetAboutOperation( |
| 104 &operation_registry_, |
| 105 request_context_getter_.get(), |
| 106 *url_generator_, |
| 107 base::Bind(&test_util::CopyResultsFromGetDataCallbackAndQuit, |
| 108 &error, &feed_data)); |
| 109 operation->Start(kTestDriveApiAuthToken, kTestUserAgent, |
| 110 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); |
| 111 MessageLoop::current()->Run(); |
| 112 |
| 113 EXPECT_EQ(HTTP_SUCCESS, error); |
| 114 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); |
| 115 EXPECT_EQ("/drive/v2/about", http_request_.relative_url); |
| 116 EXPECT_TRUE(test_util::VerifyJsonData( |
| 117 test_util::GetTestFilePath("drive/about.json"), feed_data.get())); |
| 118 } |
| 119 |
| 120 TEST_F(DriveApiOperationsTest, GetAboutOperation_InvalidFeed) { |
| 121 // Set an expected data file containing invalid result. |
| 122 expected_data_file_path_ = test_util::GetTestFilePath("gdata/testfile.txt"); |
| 123 |
| 124 GDataErrorCode error = GDATA_OTHER_ERROR; |
| 125 scoped_ptr<base::Value> feed_data; |
| 126 |
| 127 GetAboutOperation* operation = new GetAboutOperation( |
| 128 &operation_registry_, |
| 129 request_context_getter_.get(), |
| 130 *url_generator_, |
| 131 base::Bind(&test_util::CopyResultsFromGetDataCallbackAndQuit, |
| 132 &error, &feed_data)); |
| 133 operation->Start(kTestDriveApiAuthToken, kTestUserAgent, |
| 134 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); |
| 135 MessageLoop::current()->Run(); |
| 136 |
| 137 // "parse error" should be returned, and the feed should be NULL. |
| 138 EXPECT_EQ(GDATA_PARSE_ERROR, error); |
| 139 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); |
| 140 EXPECT_EQ("/drive/v2/about", http_request_.relative_url); |
| 141 EXPECT_FALSE(feed_data.get()); |
| 142 } |
| 143 |
| 144 } // namespace google_apis |
OLD | NEW |