Index: chrome/browser/google_apis/drive_api_operations_unittest.cc |
diff --git a/chrome/browser/google_apis/drive_api_operations_unittest.cc b/chrome/browser/google_apis/drive_api_operations_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..83794834880b3d34a7e3dedbb61f95285600a53f |
--- /dev/null |
+++ b/chrome/browser/google_apis/drive_api_operations_unittest.cc |
@@ -0,0 +1,199 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
+#include "base/json/json_reader.h" |
+#include "base/message_loop_proxy.h" |
+#include "base/values.h" |
+#include "chrome/browser/google_apis/drive_api_operations.h" |
+#include "chrome/browser/google_apis/drive_api_url_generator.h" |
+#include "chrome/browser/google_apis/operation_registry.h" |
+#include "chrome/browser/google_apis/test_server/http_server.h" |
+#include "chrome/browser/google_apis/test_util.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "net/url_request/url_request_test_util.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace google_apis { |
+ |
+namespace { |
+ |
+const char kTestDriveApiAuthToken[] = "testtoken"; |
+const char kTestUserAgent[] = "test-user-agent"; |
+ |
+// Does nothing for ReAuthenticateCallback(). This function should not be |
+// reached as there won't be any authentication failures in the test. |
+void DoNothingForReAuthenticateCallback( |
+ AuthenticatedOperationInterface* /* operation */) { |
+ NOTREACHED(); |
+} |
+ |
+// Copies the result from the GetDataCallback, and quit the message loop. |
+void CopyResultFromGetDataCallbackAndQuit( |
satorux1
2013/01/10 01:07:41
Please move this to test_util.cc so we can share t
hidehiko
2013/01/10 04:00:16
Done. Ditto for DoNothingForReAuthenticateCallback
|
+ GDataErrorCode* out_error, |
+ scoped_ptr<base::Value>* out_feed_data, |
+ GDataErrorCode error, |
+ scoped_ptr<base::Value> feed_data) { |
+ *out_error = error; |
+ *out_feed_data = feed_data.Pass(); |
+ MessageLoop::current()->Quit(); |
+} |
+ |
+// Verifies the given |json_data| equals to the contents in the file specified |
+// by |expected_json_file_path|. |
+testing::AssertionResult AssertJsonFile( |
+ const char* expected_json_file_path_str, |
+ const char* json_data_str, |
+ const FilePath& expected_json_file_path, |
+ const base::Value* json_data) { |
+ if (!json_data) { |
+ return testing::AssertionFailure() << json_data_str << " is NULL."; |
+ } |
+ |
+ std::string expected_content; |
+ if (!file_util::ReadFileToString( |
+ expected_json_file_path, &expected_content)) { |
+ return testing::AssertionFailure() |
+ << "Failed to read file: " << expected_json_file_path_str |
+ << " (" << expected_json_file_path.value() << ")"; |
+ } |
+ |
+ scoped_ptr<base::Value> expected_json_data( |
+ base::JSONReader::Read(expected_content)); |
+ if (!base::Value::Equals(expected_json_data.get(), json_data)) { |
+ return testing::AssertionFailure() |
+ << "The contents in " << expected_json_file_path_str |
+ << " (" << expected_json_file_path.value() << ") and " |
+ << json_data_str << " are not equal."; |
+ } |
+ |
+ return testing::AssertionSuccess(); |
+} |
+ |
+// Checks expectation for the json value by comparing the specified file |
+// containing the expected json value. If the value is NULL or different from |
+// the content in the file, this expectation is failed. |
+#define EXPECT_JSON_FILE(file_path, json_value) \ |
+ EXPECT_PRED_FORMAT2(AssertJsonFile, file_path, json_value) |
satorux1
2013/01/10 01:07:41
Hmm, I think this looks a bit too tricky. I think
hidehiko
2013/01/10 04:00:16
Indeed, but the way looses the reason why the test
|
+ |
+} // namespace |
+ |
+class DriveApiOperationsTest : public testing::Test { |
+ public: |
+ DriveApiOperationsTest() |
+ : ui_thread_(content::BrowserThread::UI, &message_loop_), |
+ file_thread_(content::BrowserThread::FILE), |
+ io_thread_(content::BrowserThread::IO) { |
+ } |
+ |
+ virtual void SetUp() OVERRIDE { |
+ file_thread_.Start(); |
+ io_thread_.StartIOThread(); |
+ |
+ request_context_getter_ = new net::TestURLRequestContextGetter( |
+ content::BrowserThread::GetMessageLoopProxyForThread( |
+ content::BrowserThread::IO)); |
+ |
+ ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
+ test_server_.RegisterRequestHandler( |
+ base::Bind(&DriveApiOperationsTest::HandleDataFileRequest, |
+ base::Unretained(this))); |
+ |
+ url_generator_.reset(new DriveApiUrlGenerator( |
+ test_util::GetBaseUrlForTesting(test_server_.port()))); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ test_server_.ShutdownAndWaitUntilComplete(); |
+ request_context_getter_ = NULL; |
+ expected_data_file_path_.clear(); |
+ } |
+ |
+ MessageLoopForUI message_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ content::TestBrowserThread file_thread_; |
+ content::TestBrowserThread io_thread_; |
+ test_server::HttpServer test_server_; |
+ OperationRegistry operation_registry_; |
+ scoped_ptr<DriveApiUrlGenerator> url_generator_; |
+ scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
+ |
+ // This is a path to the file which contains expected response from |
+ // the server. See also HandleDataFileRequest below. |
+ FilePath expected_data_file_path_; |
+ |
+ // The incoming HTTP request is saved so tests can verify the request |
+ // parameters like HTTP method (ex. some operations should use DELETE |
+ // instead of GET). |
+ test_server::HttpRequest http_request_; |
+ |
+ private: |
+ // Reads the data file of |expected_data_file_path_| and returns its content |
+ // for the request. |
+ // To use this method, it is necessary to set |expected_data_file_path_| |
+ // to the appropriate file path before sending the request to the server. |
+ scoped_ptr<test_server::HttpResponse> HandleDataFileRequest( |
+ const test_server::HttpRequest& request) { |
+ http_request_ = request; |
+ |
+ if (expected_data_file_path_.empty()) { |
+ // The file is not specified. Delegate the processing to the next |
+ // handler. |
+ return scoped_ptr<test_server::HttpResponse>(); |
+ } |
+ |
+ // Return the response from the data file. |
+ return test_util::CreateHttpResponseFromFile(expected_data_file_path_); |
+ } |
+}; |
+ |
+TEST_F(DriveApiOperationsTest, GetAboutOperation_ValidFeed) { |
+ // Set an expected data file containing valid result. |
+ expected_data_file_path_ = test_util::GetTestFilePath("drive/about.json"); |
+ |
+ GDataErrorCode error = GDATA_OTHER_ERROR; |
+ scoped_ptr<base::Value> feed_data; |
+ |
+ GetAboutOperation* operation = new GetAboutOperation( |
+ &operation_registry_, |
+ request_context_getter_.get(), |
+ *url_generator_, |
+ base::Bind(&CopyResultFromGetDataCallbackAndQuit, &error, &feed_data)); |
+ operation->Start(kTestDriveApiAuthToken, kTestUserAgent, |
+ base::Bind(&DoNothingForReAuthenticateCallback)); |
+ MessageLoop::current()->Run(); |
+ |
+ EXPECT_EQ(HTTP_SUCCESS, error); |
+ EXPECT_EQ(test_server::METHOD_GET, http_request_.method); |
+ EXPECT_EQ("/drive/v2/about", http_request_.relative_url); |
+ EXPECT_JSON_FILE( |
+ test_util::GetTestFilePath("drive/about.json"), feed_data.get()); |
+} |
+ |
+TEST_F(DriveApiOperationsTest, GetAboutOperation_InvalidFeed) { |
+ // Set an expected data file containing invalid result. |
+ expected_data_file_path_ = test_util::GetTestFilePath("gdata/testfile.txt"); |
+ |
+ GDataErrorCode error = GDATA_OTHER_ERROR; |
+ scoped_ptr<base::Value> feed_data; |
+ |
+ GetAboutOperation* operation = new GetAboutOperation( |
+ &operation_registry_, |
+ request_context_getter_.get(), |
+ *url_generator_, |
+ base::Bind(&CopyResultFromGetDataCallbackAndQuit, &error, &feed_data)); |
+ operation->Start(kTestDriveApiAuthToken, kTestUserAgent, |
+ base::Bind(&DoNothingForReAuthenticateCallback)); |
+ MessageLoop::current()->Run(); |
+ |
+ // "parse error" should be returned, and the feed should be NULL. |
+ EXPECT_EQ(GDATA_PARSE_ERROR, error); |
+ EXPECT_EQ(test_server::METHOD_GET, http_request_.method); |
+ EXPECT_EQ("/drive/v2/about", http_request_.relative_url); |
+ EXPECT_FALSE(feed_data.get()); |
+} |
+ |
+} // namespace google_apis |