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/file_util.h" | |
8 #include "base/json/json_reader.h" | |
9 #include "base/message_loop_proxy.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/google_apis/drive_api_operations.h" | |
12 #include "chrome/browser/google_apis/drive_api_url_generator.h" | |
13 #include "chrome/browser/google_apis/operation_registry.h" | |
14 #include "chrome/browser/google_apis/test_server/http_server.h" | |
15 #include "chrome/browser/google_apis/test_util.h" | |
16 #include "content/public/test/test_browser_thread.h" | |
17 #include "net/url_request/url_request_test_util.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace google_apis { | |
21 | |
22 namespace { | |
23 | |
24 const char kTestDriveApiAuthToken[] = "testtoken"; | |
25 const char kTestUserAgent[] = "test-user-agent"; | |
26 | |
27 // Does nothing for ReAuthenticateCallback(). This function should not be | |
28 // reached as there won't be any authentication failures in the test. | |
29 void DoNothingForReAuthenticateCallback( | |
30 AuthenticatedOperationInterface* /* operation */) { | |
31 NOTREACHED(); | |
32 } | |
33 | |
34 // Copies the result from the GetDataCallback, and quit the message loop. | |
35 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
| |
36 GDataErrorCode* out_error, | |
37 scoped_ptr<base::Value>* out_feed_data, | |
38 GDataErrorCode error, | |
39 scoped_ptr<base::Value> feed_data) { | |
40 *out_error = error; | |
41 *out_feed_data = feed_data.Pass(); | |
42 MessageLoop::current()->Quit(); | |
43 } | |
44 | |
45 // Verifies the given |json_data| equals to the contents in the file specified | |
46 // by |expected_json_file_path|. | |
47 testing::AssertionResult AssertJsonFile( | |
48 const char* expected_json_file_path_str, | |
49 const char* json_data_str, | |
50 const FilePath& expected_json_file_path, | |
51 const base::Value* json_data) { | |
52 if (!json_data) { | |
53 return testing::AssertionFailure() << json_data_str << " is NULL."; | |
54 } | |
55 | |
56 std::string expected_content; | |
57 if (!file_util::ReadFileToString( | |
58 expected_json_file_path, &expected_content)) { | |
59 return testing::AssertionFailure() | |
60 << "Failed to read file: " << expected_json_file_path_str | |
61 << " (" << expected_json_file_path.value() << ")"; | |
62 } | |
63 | |
64 scoped_ptr<base::Value> expected_json_data( | |
65 base::JSONReader::Read(expected_content)); | |
66 if (!base::Value::Equals(expected_json_data.get(), json_data)) { | |
67 return testing::AssertionFailure() | |
68 << "The contents in " << expected_json_file_path_str | |
69 << " (" << expected_json_file_path.value() << ") and " | |
70 << json_data_str << " are not equal."; | |
71 } | |
72 | |
73 return testing::AssertionSuccess(); | |
74 } | |
75 | |
76 // Checks expectation for the json value by comparing the specified file | |
77 // containing the expected json value. If the value is NULL or different from | |
78 // the content in the file, this expectation is failed. | |
79 #define EXPECT_JSON_FILE(file_path, json_value) \ | |
80 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
| |
81 | |
82 } // namespace | |
83 | |
84 class DriveApiOperationsTest : public testing::Test { | |
85 public: | |
86 DriveApiOperationsTest() | |
87 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
88 file_thread_(content::BrowserThread::FILE), | |
89 io_thread_(content::BrowserThread::IO) { | |
90 } | |
91 | |
92 virtual void SetUp() OVERRIDE { | |
93 file_thread_.Start(); | |
94 io_thread_.StartIOThread(); | |
95 | |
96 request_context_getter_ = new net::TestURLRequestContextGetter( | |
97 content::BrowserThread::GetMessageLoopProxyForThread( | |
98 content::BrowserThread::IO)); | |
99 | |
100 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | |
101 test_server_.RegisterRequestHandler( | |
102 base::Bind(&DriveApiOperationsTest::HandleDataFileRequest, | |
103 base::Unretained(this))); | |
104 | |
105 url_generator_.reset(new DriveApiUrlGenerator( | |
106 test_util::GetBaseUrlForTesting(test_server_.port()))); | |
107 } | |
108 | |
109 virtual void TearDown() OVERRIDE { | |
110 test_server_.ShutdownAndWaitUntilComplete(); | |
111 request_context_getter_ = NULL; | |
112 expected_data_file_path_.clear(); | |
113 } | |
114 | |
115 MessageLoopForUI message_loop_; | |
116 content::TestBrowserThread ui_thread_; | |
117 content::TestBrowserThread file_thread_; | |
118 content::TestBrowserThread io_thread_; | |
119 test_server::HttpServer test_server_; | |
120 OperationRegistry operation_registry_; | |
121 scoped_ptr<DriveApiUrlGenerator> url_generator_; | |
122 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | |
123 | |
124 // This is a path to the file which contains expected response from | |
125 // the server. See also HandleDataFileRequest below. | |
126 FilePath expected_data_file_path_; | |
127 | |
128 // The incoming HTTP request is saved so tests can verify the request | |
129 // parameters like HTTP method (ex. some operations should use DELETE | |
130 // instead of GET). | |
131 test_server::HttpRequest http_request_; | |
132 | |
133 private: | |
134 // Reads the data file of |expected_data_file_path_| and returns its content | |
135 // for the request. | |
136 // To use this method, it is necessary to set |expected_data_file_path_| | |
137 // to the appropriate file path before sending the request to the server. | |
138 scoped_ptr<test_server::HttpResponse> HandleDataFileRequest( | |
139 const test_server::HttpRequest& request) { | |
140 http_request_ = request; | |
141 | |
142 if (expected_data_file_path_.empty()) { | |
143 // The file is not specified. Delegate the processing to the next | |
144 // handler. | |
145 return scoped_ptr<test_server::HttpResponse>(); | |
146 } | |
147 | |
148 // Return the response from the data file. | |
149 return test_util::CreateHttpResponseFromFile(expected_data_file_path_); | |
150 } | |
151 }; | |
152 | |
153 TEST_F(DriveApiOperationsTest, GetAboutOperation_ValidFeed) { | |
154 // Set an expected data file containing valid result. | |
155 expected_data_file_path_ = test_util::GetTestFilePath("drive/about.json"); | |
156 | |
157 GDataErrorCode error = GDATA_OTHER_ERROR; | |
158 scoped_ptr<base::Value> feed_data; | |
159 | |
160 GetAboutOperation* operation = new GetAboutOperation( | |
161 &operation_registry_, | |
162 request_context_getter_.get(), | |
163 *url_generator_, | |
164 base::Bind(&CopyResultFromGetDataCallbackAndQuit, &error, &feed_data)); | |
165 operation->Start(kTestDriveApiAuthToken, kTestUserAgent, | |
166 base::Bind(&DoNothingForReAuthenticateCallback)); | |
167 MessageLoop::current()->Run(); | |
168 | |
169 EXPECT_EQ(HTTP_SUCCESS, error); | |
170 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); | |
171 EXPECT_EQ("/drive/v2/about", http_request_.relative_url); | |
172 EXPECT_JSON_FILE( | |
173 test_util::GetTestFilePath("drive/about.json"), feed_data.get()); | |
174 } | |
175 | |
176 TEST_F(DriveApiOperationsTest, GetAboutOperation_InvalidFeed) { | |
177 // Set an expected data file containing invalid result. | |
178 expected_data_file_path_ = test_util::GetTestFilePath("gdata/testfile.txt"); | |
179 | |
180 GDataErrorCode error = GDATA_OTHER_ERROR; | |
181 scoped_ptr<base::Value> feed_data; | |
182 | |
183 GetAboutOperation* operation = new GetAboutOperation( | |
184 &operation_registry_, | |
185 request_context_getter_.get(), | |
186 *url_generator_, | |
187 base::Bind(&CopyResultFromGetDataCallbackAndQuit, &error, &feed_data)); | |
188 operation->Start(kTestDriveApiAuthToken, kTestUserAgent, | |
189 base::Bind(&DoNothingForReAuthenticateCallback)); | |
190 MessageLoop::current()->Run(); | |
191 | |
192 // "parse error" should be returned, and the feed should be NULL. | |
193 EXPECT_EQ(GDATA_PARSE_ERROR, error); | |
194 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); | |
195 EXPECT_EQ("/drive/v2/about", http_request_.relative_url); | |
196 EXPECT_FALSE(feed_data.get()); | |
197 } | |
198 | |
199 } // namespace google_apis | |
OLD | NEW |