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
|
// 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 "base/bind.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "chrome/browser/chromeos/gdata/gdata_wapi_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "net/test/test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gdata {
namespace {
class GDataTest : public InProcessBrowserTest {
public:
GDataTest()
: InProcessBrowserTest(),
gdata_test_server_(net::TestServer::TYPE_GDATA,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
}
virtual void SetUpOnMainThread() OVERRIDE {
ASSERT_TRUE(gdata_test_server_.Start());
service_.reset(new gdata::GDataWapiService);
service_->Initialize(browser()->profile());
service_->auth_service_for_testing()->set_access_token_for_testing(
net::TestServer::kGDataAuthToken);
}
virtual void CleanUpOnMainThread() {
service_.reset();
}
protected:
FilePath GetTestCachedFilePath(const FilePath& file_name) {
return browser()->profile()->GetPath().Append(file_name);
}
net::TestServer gdata_test_server_;
scoped_ptr<gdata::GDataWapiService> service_;
};
// The test callback for GDataWapiService::DownloadFile().
void TestDownloadCallback(gdata::GDataErrorCode* result,
std::string* contents,
gdata::GDataErrorCode error,
const GURL& content_url,
const FilePath& temp_file) {
*result = error;
file_util::ReadFileToString(temp_file, contents);
file_util::Delete(temp_file, false);
MessageLoop::current()->Quit();
}
// The test callback for GDataWapiService::GetDocuments().
void TestGetDocumentsCallback(gdata::GDataErrorCode* result_code,
base::Value** result_data,
gdata::GDataErrorCode error,
scoped_ptr<base::Value> feed_data) {
*result_code = error;
*result_data = feed_data.release();
MessageLoop::current()->Quit();
}
} // namespace
IN_PROC_BROWSER_TEST_F(GDataTest, Download) {
gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR;
std::string contents;
service_->DownloadFile(
FilePath("/dummy/gdata/testfile.txt"),
GetTestCachedFilePath(FilePath("cached_testfile.txt")),
gdata_test_server_.GetURL("files/chromeos/gdata/testfile.txt"),
base::Bind(&TestDownloadCallback, &result, &contents),
gdata::GetContentCallback());
content::RunMessageLoop();
EXPECT_EQ(gdata::HTTP_SUCCESS, result);
FilePath expected_filepath = gdata_test_server_.document_root().Append(
FilePath(FILE_PATH_LITERAL("chromeos/gdata/testfile.txt")));
std::string expected_contents;
file_util::ReadFileToString(expected_filepath, &expected_contents);
EXPECT_EQ(expected_contents, contents);
}
IN_PROC_BROWSER_TEST_F(GDataTest, NonExistingDownload) {
gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR;
std::string dummy_contents;
service_->DownloadFile(
FilePath("/dummy/gdata/no-such-file.txt"),
GetTestCachedFilePath(FilePath("cache_no-such-file.txt")),
gdata_test_server_.GetURL("files/chromeos/gdata/no-such-file.txt"),
base::Bind(&TestDownloadCallback, &result, &dummy_contents),
gdata::GetContentCallback());
content::RunMessageLoop();
EXPECT_EQ(gdata::HTTP_NOT_FOUND, result);
// Do not verify the not found message.
}
IN_PROC_BROWSER_TEST_F(GDataTest, GetDocuments) {
gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR;
base::Value* result_data = NULL;
service_->GetDocuments(
gdata_test_server_.GetURL("files/chromeos/gdata/root_feed.json"),
0, // start_changestamp
std::string(), // search string
std::string(), // directory resource ID
base::Bind(&TestGetDocumentsCallback, &result, &result_data));
content::RunMessageLoop();
EXPECT_EQ(gdata::HTTP_SUCCESS, result);
ASSERT_TRUE(result_data);
FilePath expected_filepath = gdata_test_server_.document_root().Append(
FilePath(FILE_PATH_LITERAL("chromeos/gdata/root_feed.json")));
std::string expected_contents;
file_util::ReadFileToString(expected_filepath, &expected_contents);
scoped_ptr<base::Value> expected_data(
base::JSONReader::Read(expected_contents));
EXPECT_TRUE(base::Value::Equals(expected_data.get(), result_data));
delete result_data;
}
IN_PROC_BROWSER_TEST_F(GDataTest, GetDocumentsFailure) {
// testfile.txt exists but the response is not JSON, so it should
// emit a parse error instead.
gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR;
base::Value* result_data = NULL;
service_->GetDocuments(
gdata_test_server_.GetURL("files/chromeos/gdata/testfile.txt"),
0, // start_changestamp
std::string(), // search string
std::string(), // directory resource ID
base::Bind(&TestGetDocumentsCallback, &result, &result_data));
content::RunMessageLoop();
EXPECT_EQ(gdata::GDATA_PARSE_ERROR, result);
EXPECT_FALSE(result_data);
}
} // namespace gdata
|