Fix build break
[chromium-blink-merge.git] / chrome / browser / google_apis / test_util.cc
blob27d395767bd5e6f8b2131308b5c218271e2861f5
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
3 // found in the LICENSE file.
5 #include "chrome/browser/google_apis/test_util.h"
7 #include "base/file_util.h"
8 #include "base/json/json_file_value_serializer.h"
9 #include "base/json/json_reader.h"
10 #include "base/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/pending_task.h"
13 #include "base/string_util.h"
14 #include "base/stringprintf.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_split.h"
17 #include "base/threading/sequenced_worker_pool.h"
18 #include "chrome/browser/google_apis/drive_api_parser.h"
19 #include "chrome/browser/google_apis/gdata_wapi_operations.h"
20 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
21 #include "chrome/browser/google_apis/test_server/http_request.h"
22 #include "chrome/browser/google_apis/test_server/http_response.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "googleurl/src/gurl.h"
26 namespace google_apis {
27 namespace test_util {
29 // This class is used to monitor if any task is posted to a message loop.
30 class TaskObserver : public MessageLoop::TaskObserver {
31 public:
32 TaskObserver() : posted_(false) {}
33 virtual ~TaskObserver() {}
35 // MessageLoop::TaskObserver overrides.
36 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
38 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
39 posted_ = true;
42 // Returns true if any task was posted.
43 bool posted() const { return posted_; }
45 private:
46 bool posted_;
47 DISALLOW_COPY_AND_ASSIGN(TaskObserver);
50 bool RemovePrefix(const std::string& input,
51 const std::string& prefix,
52 std::string* output) {
53 if (!StartsWithASCII(input, prefix, true /* case sensitive */))
54 return false;
56 *output = input.substr(prefix.size());
57 return true;
60 base::FilePath GetTestFilePath(const std::string& relative_path) {
61 base::FilePath path;
62 if (!PathService::Get(base::DIR_SOURCE_ROOT, &path))
63 return base::FilePath();
64 path = path.AppendASCII("chrome")
65 .AppendASCII("test")
66 .AppendASCII("data")
67 .Append(base::FilePath::FromUTF8Unsafe(relative_path));
68 return path;
71 GURL GetBaseUrlForTesting(int port) {
72 return GURL(base::StringPrintf("http://127.0.0.1:%d/", port));
75 void RunBlockingPoolTask() {
76 while (true) {
77 content::BrowserThread::GetBlockingPool()->FlushForTesting();
79 TaskObserver task_observer;
80 MessageLoop::current()->AddTaskObserver(&task_observer);
81 MessageLoop::current()->RunUntilIdle();
82 MessageLoop::current()->RemoveTaskObserver(&task_observer);
83 if (!task_observer.posted())
84 break;
88 void RunAndQuit(const base::Closure& closure) {
89 closure.Run();
90 MessageLoop::current()->Quit();
93 scoped_ptr<base::Value> LoadJSONFile(const std::string& relative_path) {
94 base::FilePath path = GetTestFilePath(relative_path);
96 std::string error;
97 JSONFileValueSerializer serializer(path);
98 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error));
99 LOG_IF(WARNING, !value.get()) << "Failed to parse " << path.value()
100 << ": " << error;
101 return value.Pass();
104 // Returns a HttpResponse created from the given file path.
105 scoped_ptr<test_server::HttpResponse> CreateHttpResponseFromFile(
106 const base::FilePath& file_path) {
107 std::string content;
108 if (!file_util::ReadFileToString(file_path, &content))
109 return scoped_ptr<test_server::HttpResponse>();
111 std::string content_type = "text/plain";
112 if (EndsWith(file_path.AsUTF8Unsafe(), ".json", true /* case sensitive */)) {
113 content_type = "application/json";
114 } else if (EndsWith(file_path.AsUTF8Unsafe(), ".xml", true)) {
115 content_type = "application/atom+xml";
118 scoped_ptr<test_server::HttpResponse> http_response(
119 new test_server::HttpResponse);
120 http_response->set_code(test_server::SUCCESS);
121 http_response->set_content(content);
122 http_response->set_content_type(content_type);
123 return http_response.Pass();
126 void DoNothingForReAuthenticateCallback(
127 AuthenticatedOperationInterface* /* operation */) {
128 NOTREACHED();
131 scoped_ptr<test_server::HttpResponse> HandleDownloadRequest(
132 const GURL& base_url,
133 test_server::HttpRequest* out_request,
134 const test_server::HttpRequest& request) {
135 *out_request = request;
137 GURL absolute_url = base_url.Resolve(request.relative_url);
138 std::string remaining_path;
139 if (!RemovePrefix(absolute_url.path(), "/files/", &remaining_path))
140 return scoped_ptr<test_server::HttpResponse>();
141 return CreateHttpResponseFromFile(GetTestFilePath(remaining_path));
144 bool VerifyJsonData(const base::FilePath& expected_json_file_path,
145 const base::Value* json_data) {
146 if (!json_data) {
147 LOG(ERROR) << "json_data is NULL";
148 return false;
151 std::string expected_content;
152 if (!file_util::ReadFileToString(
153 expected_json_file_path, &expected_content)) {
154 LOG(ERROR) << "Failed to read file: " << expected_json_file_path.value();
155 return false;
158 scoped_ptr<base::Value> expected_json_data(
159 base::JSONReader::Read(expected_content));
160 if (!base::Value::Equals(expected_json_data.get(), json_data)) {
161 LOG(ERROR)
162 << "The value of json_data is different from the file's content.";
163 return false;
166 return true;
169 bool ParseContentRangeHeader(const std::string& value,
170 int64* start_position,
171 int64* end_position,
172 int64* length) {
173 DCHECK(start_position);
174 DCHECK(end_position);
175 DCHECK(length);
177 std::string remaining;
178 if (!RemovePrefix(value, "bytes ", &remaining))
179 return false;
181 std::vector<std::string> parts;
182 base::SplitString(remaining, '/', &parts);
183 if (parts.size() != 2U)
184 return false;
186 const std::string range = parts[0];
187 if (!base::StringToInt64(parts[1], length))
188 return false;
190 parts.clear();
191 base::SplitString(range, '-', &parts);
192 if (parts.size() != 2U)
193 return false;
195 return (base::StringToInt64(parts[0], start_position) &&
196 base::StringToInt64(parts[1], end_position));
199 void AppendProgressCallbackResult(std::vector<ProgressInfo>* progress_values,
200 int64 progress,
201 int64 total) {
202 progress_values->push_back(ProgressInfo(progress, total));
205 } // namespace test_util
206 } // namespace google_apis