Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: chrome/browser/google_apis/test_util.cc

Issue 12093107: Extract testing utilities from gdata_wapi_operations_unittest to test_util. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/google_apis/test_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/google_apis/test_util.h" 5 #include "chrome/browser/google_apis/test_util.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_file_value_serializer.h" 8 #include "base/json/json_file_value_serializer.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/threading/sequenced_worker_pool.h" 14 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/google_apis/drive_api_parser.h" 15 #include "chrome/browser/google_apis/drive_api_parser.h"
16 #include "chrome/browser/google_apis/gdata_wapi_parser.h" 16 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
17 #include "chrome/browser/google_apis/test_server/http_request.h"
17 #include "chrome/browser/google_apis/test_server/http_response.h" 18 #include "chrome/browser/google_apis/test_server/http_response.h"
18 #include "chrome/browser/google_apis/test_server/http_server.h"
19 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
20 #include "googleurl/src/gurl.h" 20 #include "googleurl/src/gurl.h"
21 21
22 namespace google_apis { 22 namespace google_apis {
23 namespace test_util { 23 namespace test_util {
24 24
25 // This class is used to monitor if any task is posted to a message loop. 25 // This class is used to monitor if any task is posted to a message loop.
26 class TaskObserver : public MessageLoop::TaskObserver { 26 class TaskObserver : public MessageLoop::TaskObserver {
27 public: 27 public:
28 TaskObserver() : posted_(false) {} 28 TaskObserver() : posted_(false) {}
29 virtual ~TaskObserver() {} 29 virtual ~TaskObserver() {}
30 30
31 // MessageLoop::TaskObserver overrides. 31 // MessageLoop::TaskObserver overrides.
32 virtual void WillProcessTask(base::TimeTicks time_posted) {} 32 virtual void WillProcessTask(base::TimeTicks time_posted) {}
33 virtual void DidProcessTask(base::TimeTicks time_posted) { 33 virtual void DidProcessTask(base::TimeTicks time_posted) {
34 posted_ = true; 34 posted_ = true;
35 } 35 }
36 36
37 // Returns true if any task was posted. 37 // Returns true if any task was posted.
38 bool posted() const { return posted_; } 38 bool posted() const { return posted_; }
39 39
40 private: 40 private:
41 bool posted_; 41 bool posted_;
42 DISALLOW_COPY_AND_ASSIGN(TaskObserver); 42 DISALLOW_COPY_AND_ASSIGN(TaskObserver);
43 }; 43 };
44 44
45 bool RemovePrefix(const std::string& input,
46 const std::string& prefix,
47 std::string* output) {
48 if (!StartsWithASCII(input, prefix, true /* case sensitive */))
49 return false;
50
51 *output = input.substr(prefix.size());
52 return true;
53 }
54
45 FilePath GetTestFilePath(const std::string& relative_path) { 55 FilePath GetTestFilePath(const std::string& relative_path) {
46 FilePath path; 56 FilePath path;
47 if (!PathService::Get(base::DIR_SOURCE_ROOT, &path)) 57 if (!PathService::Get(base::DIR_SOURCE_ROOT, &path))
48 return FilePath(); 58 return FilePath();
49 path = path.AppendASCII("chrome") 59 path = path.AppendASCII("chrome")
50 .AppendASCII("test") 60 .AppendASCII("test")
51 .AppendASCII("data") 61 .AppendASCII("data")
52 .AppendASCII("chromeos") 62 .AppendASCII("chromeos")
53 .Append(FilePath::FromUTF8Unsafe(relative_path)); 63 .Append(FilePath::FromUTF8Unsafe(relative_path));
54 return path; 64 return path;
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 http_response->set_content(content); 185 http_response->set_content(content);
176 http_response->set_content_type(content_type); 186 http_response->set_content_type(content_type);
177 return http_response.Pass(); 187 return http_response.Pass();
178 } 188 }
179 189
180 void DoNothingForReAuthenticateCallback( 190 void DoNothingForReAuthenticateCallback(
181 AuthenticatedOperationInterface* /* operation */) { 191 AuthenticatedOperationInterface* /* operation */) {
182 NOTREACHED(); 192 NOTREACHED();
183 } 193 }
184 194
195 scoped_ptr<test_server::HttpResponse> HandleDownloadRequest(
196 const GURL& base_url,
197 test_server::HttpRequest* out_request,
198 const test_server::HttpRequest& request) {
199 *out_request = request;
200
201 GURL absolute_url = base_url.Resolve(request.relative_url);
202 std::string remaining_path;
203 if (!RemovePrefix(absolute_url.path(), "/files/", &remaining_path))
204 return scoped_ptr<test_server::HttpResponse>();
205 return CreateHttpResponseFromFile(GetTestFilePath(remaining_path));
206 }
207
185 bool VerifyJsonData(const FilePath& expected_json_file_path, 208 bool VerifyJsonData(const FilePath& expected_json_file_path,
186 const base::Value* json_data) { 209 const base::Value* json_data) {
187 if (!json_data) { 210 if (!json_data) {
188 LOG(ERROR) << "json_data is NULL"; 211 LOG(ERROR) << "json_data is NULL";
189 return false; 212 return false;
190 } 213 }
191 214
192 std::string expected_content; 215 std::string expected_content;
193 if (!file_util::ReadFileToString( 216 if (!file_util::ReadFileToString(
194 expected_json_file_path, &expected_content)) { 217 expected_json_file_path, &expected_content)) {
195 LOG(ERROR) << "Failed to read file: " << expected_json_file_path.value(); 218 LOG(ERROR) << "Failed to read file: " << expected_json_file_path.value();
196 return false; 219 return false;
197 } 220 }
198 221
199 scoped_ptr<base::Value> expected_json_data( 222 scoped_ptr<base::Value> expected_json_data(
200 base::JSONReader::Read(expected_content)); 223 base::JSONReader::Read(expected_content));
201 if (!base::Value::Equals(expected_json_data.get(), json_data)) { 224 if (!base::Value::Equals(expected_json_data.get(), json_data)) {
202 LOG(ERROR) 225 LOG(ERROR)
203 << "The value of json_data is different from the file's content."; 226 << "The value of json_data is different from the file's content.";
204 return false; 227 return false;
205 } 228 }
206 229
207 return true; 230 return true;
208 } 231 }
209 232
210 } // namespace test_util 233 } // namespace test_util
211 } // namespace google_apis 234 } // namespace google_apis
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/test_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698