summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_apis/test_util.cc
blob: a3309db859b57f8a0f595723c21f26b1b37a828d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) 2012 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 "chrome/browser/google_apis/test_util.h"

#include "base/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_reader.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/pending_task.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/google_apis/drive_api_parser.h"
#include "chrome/browser/google_apis/gdata_wapi_operations.h"
#include "chrome/browser/google_apis/gdata_wapi_parser.h"
#include "chrome/browser/google_apis/test_server/http_request.h"
#include "chrome/browser/google_apis/test_server/http_response.h"
#include "content/public/browser/browser_thread.h"
#include "googleurl/src/gurl.h"

namespace google_apis {
namespace test_util {

// This class is used to monitor if any task is posted to a message loop.
class TaskObserver : public MessageLoop::TaskObserver {
 public:
  TaskObserver() : posted_(false) {}
  virtual ~TaskObserver() {}

  // MessageLoop::TaskObserver overrides.
  virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
  }
  virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
    posted_ = true;
  }

  // Returns true if any task was posted.
  bool posted() const { return posted_; }

 private:
  bool posted_;
  DISALLOW_COPY_AND_ASSIGN(TaskObserver);
};

bool RemovePrefix(const std::string& input,
                  const std::string& prefix,
                  std::string* output) {
  if (!StartsWithASCII(input, prefix, true /* case sensitive */))
    return false;

  *output = input.substr(prefix.size());
  return true;
}

base::FilePath GetTestFilePath(const std::string& relative_path) {
  base::FilePath path;
  if (!PathService::Get(base::DIR_SOURCE_ROOT, &path))
    return base::FilePath();
  path = path.AppendASCII("chrome")
             .AppendASCII("test")
             .AppendASCII("data")
             .AppendASCII("chromeos")
             .Append(base::FilePath::FromUTF8Unsafe(relative_path));
  return path;
}

GURL GetBaseUrlForTesting(int port) {
  return GURL(base::StringPrintf("http://127.0.0.1:%d/", port));
}

void RunBlockingPoolTask() {
  while (true) {
    content::BrowserThread::GetBlockingPool()->FlushForTesting();

    TaskObserver task_observer;
    MessageLoop::current()->AddTaskObserver(&task_observer);
    MessageLoop::current()->RunUntilIdle();
    MessageLoop::current()->RemoveTaskObserver(&task_observer);
    if (!task_observer.posted())
      break;
  }
}

scoped_ptr<base::Value> LoadJSONFile(const std::string& relative_path) {
  base::FilePath path = GetTestFilePath(relative_path);

  std::string error;
  JSONFileValueSerializer serializer(path);
  scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error));
  LOG_IF(WARNING, !value.get()) << "Failed to parse " << path.value()
                                << ": " << error;
  return value.Pass();
}

void CopyResultsFromEntryActionCallback(GDataErrorCode* error_out,
                                        GDataErrorCode error_in) {
  *error_out = error_in;
}

void CopyResultFromEntryActionCallbackAndQuit(GDataErrorCode* error_out,
                                              GDataErrorCode error_in) {
  *error_out = error_in;
  MessageLoop::current()->Quit();
}

void CopyResultsFromGetDataCallback(GDataErrorCode* error_out,
                                    scoped_ptr<base::Value>* value_out,
                                    GDataErrorCode error_in,
                                    scoped_ptr<base::Value> value_in) {
  value_out->swap(value_in);
  *error_out = error_in;
}

void CopyResultsFromGetDataCallbackAndQuit(GDataErrorCode* error_out,
                                           scoped_ptr<base::Value>* value_out,
                                           GDataErrorCode error_in,
                                           scoped_ptr<base::Value> value_in) {
  *error_out = error_in;
  *value_out = value_in.Pass();
  MessageLoop::current()->Quit();
}

void CopyResultsFromGetResourceEntryCallback(
    GDataErrorCode* error_out,
    scoped_ptr<ResourceEntry>* resource_entry_out,
    GDataErrorCode error_in,
    scoped_ptr<ResourceEntry> resource_entry_in) {
  resource_entry_out->swap(resource_entry_in);
  *error_out = error_in;
}

void CopyResultsFromGetResourceListCallback(
    GDataErrorCode* error_out,
    scoped_ptr<ResourceList>* resource_list_out,
    GDataErrorCode error_in,
    scoped_ptr<ResourceList> resource_list_in) {
  resource_list_out->swap(resource_list_in);
  *error_out = error_in;
}

void CopyResultsFromGetAccountMetadataCallback(
    GDataErrorCode* error_out,
    scoped_ptr<AccountMetadataFeed>* account_metadata_out,
    GDataErrorCode error_in,
    scoped_ptr<AccountMetadataFeed> account_metadata_in) {
  account_metadata_out->swap(account_metadata_in);
  *error_out = error_in;
}

void CopyResultsFromGetAccountMetadataCallbackAndQuit(
    GDataErrorCode* error_out,
    scoped_ptr<AccountMetadataFeed>* account_metadata_out,
    GDataErrorCode error_in,
    scoped_ptr<AccountMetadataFeed> account_metadata_in) {
  CopyResultsFromGetAccountMetadataCallback(
      error_out, account_metadata_out, error_in, account_metadata_in.Pass());
  MessageLoop::current()->Quit();
}

void CopyResultsFromGetAboutResourceCallback(
    GDataErrorCode* error_out,
    scoped_ptr<AboutResource>* about_resource_out,
    GDataErrorCode error_in,
    scoped_ptr<AboutResource> about_resource_in) {
  *error_out = error_in;
  *about_resource_out = about_resource_in.Pass();
}

void CopyResultsFromGetAppListCallback(
    GDataErrorCode* error_out,
    scoped_ptr<AppList>* app_list_out,
    GDataErrorCode error_in,
    scoped_ptr<AppList> app_list_in) {
  *app_list_out = app_list_in.Pass();
  *error_out = error_in;
}

void CopyResultsFromDownloadActionCallback(
    GDataErrorCode* error_out,
    base::FilePath* temp_file_out,
    GDataErrorCode error_in,
    const base::FilePath& temp_file_in) {
  *error_out = error_in;
  *temp_file_out = temp_file_in;
}

void CopyResultsFromInitiateUploadCallback(
    GDataErrorCode* error_out,
    GURL* url_out,
    GDataErrorCode error_in,
    const GURL& url_in) {
  *error_out = error_in;
  *url_out = url_in;
}

void CopyResultsFromInitiateUploadCallbackAndQuit(
    GDataErrorCode* error_out,
    GURL* url_out,
    GDataErrorCode error_in,
    const GURL& url_in) {
  CopyResultsFromInitiateUploadCallback(error_out, url_out, error_in, url_in);
  MessageLoop::current()->Quit();
}

void CopyResultsFromUploadRangeCallback(
    UploadRangeResponse* response_out,
    scoped_ptr<ResourceEntry>* entry_out,
    const UploadRangeResponse& response_in,
    scoped_ptr<ResourceEntry> entry_in) {
  *response_out = response_in;
  *entry_out = entry_in.Pass();
}

// Returns a HttpResponse created from the given file path.
scoped_ptr<test_server::HttpResponse> CreateHttpResponseFromFile(
    const base::FilePath& file_path) {
  std::string content;
  if (!file_util::ReadFileToString(file_path, &content))
    return scoped_ptr<test_server::HttpResponse>();

  std::string content_type = "text/plain";
  if (EndsWith(file_path.AsUTF8Unsafe(), ".json", true /* case sensitive */)) {
    content_type = "application/json";
  } else if (EndsWith(file_path.AsUTF8Unsafe(), ".xml", true)) {
    content_type = "application/atom+xml";
  }

  scoped_ptr<test_server::HttpResponse> http_response(
      new test_server::HttpResponse);
  http_response->set_code(test_server::SUCCESS);
  http_response->set_content(content);
  http_response->set_content_type(content_type);
  return http_response.Pass();
}

void DoNothingForReAuthenticateCallback(
    AuthenticatedOperationInterface* /* operation */) {
  NOTREACHED();
}

scoped_ptr<test_server::HttpResponse> HandleDownloadRequest(
    const GURL& base_url,
    test_server::HttpRequest* out_request,
    const test_server::HttpRequest& request) {
  *out_request = request;

  GURL absolute_url = base_url.Resolve(request.relative_url);
  std::string remaining_path;
  if (!RemovePrefix(absolute_url.path(), "/files/", &remaining_path))
    return scoped_ptr<test_server::HttpResponse>();
  return CreateHttpResponseFromFile(GetTestFilePath(remaining_path));
}

bool VerifyJsonData(const base::FilePath& expected_json_file_path,
                    const base::Value* json_data) {
  if (!json_data) {
    LOG(ERROR) << "json_data is NULL";
    return false;
  }

  std::string expected_content;
  if (!file_util::ReadFileToString(
          expected_json_file_path, &expected_content)) {
    LOG(ERROR) << "Failed to read file: " << expected_json_file_path.value();
    return false;
  }

  scoped_ptr<base::Value> expected_json_data(
      base::JSONReader::Read(expected_content));
  if (!base::Value::Equals(expected_json_data.get(), json_data)) {
    LOG(ERROR)
        << "The value of json_data is different from the file's content.";
    return false;
  }

  return true;
}

}  // namespace test_util
}  // namespace google_apis