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
|
// Copyright (c) 2011 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.
//
// NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit)
// rather than as part of test_shell_tests because they rely on being able
// to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses
// TYPE_UI, which URLRequest doesn't allow.
//
#include "webkit/fileapi/file_system_operation.h"
#include "base/message_loop.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_temp_dir.h"
#include "base/message_loop.h"
#include "googleurl/src/gurl.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_job.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/blob/blob_data.h"
#include "webkit/blob/blob_storage_controller.h"
#include "webkit/blob/blob_url_request_job.h"
#include "webkit/fileapi/file_system_callback_dispatcher.h"
#include "webkit/fileapi/file_system_file_util.h"
namespace fileapi {
namespace {
class MockDispatcher;
} // namespace (anonymous)
class FileSystemOperationWriteTest : public testing::Test {
public:
FileSystemOperationWriteTest()
: loop_(MessageLoop::TYPE_IO),
status_(base::PLATFORM_FILE_OK),
bytes_written_(0),
complete_(false) {}
FileSystemOperation* operation();
void set_failure_status(base::PlatformFileError status) {
EXPECT_FALSE(complete_);
EXPECT_EQ(status_, base::PLATFORM_FILE_OK);
EXPECT_NE(status, base::PLATFORM_FILE_OK);
complete_ = true;
status_ = status;
}
base::PlatformFileError status() const { return status_; }
void add_bytes_written(int64 bytes, bool complete) {
bytes_written_ += bytes;
EXPECT_FALSE(complete_);
complete_ = complete;
}
int64 bytes_written() const { return bytes_written_; }
bool complete() const { return complete_; }
virtual void SetUp();
virtual void TearDown();
protected:
GURL URLForRelativePath(const std::string& path) const {
// Only the path will actually get used.
return GURL("file://").Resolve(file_.value()).Resolve(path);
}
GURL URLForPath(const FilePath& path) const {
// Only the path will actually get used.
return GURL("file://").Resolve(path.value());
}
MessageLoop loop_;
ScopedTempDir dir_;
FilePath file_;
// For post-operation status.
base::PlatformFileError status_;
int64 bytes_written_;
bool complete_;
DISALLOW_COPY_AND_ASSIGN(FileSystemOperationWriteTest);
};
namespace {
class TestURLRequestContext : public net::URLRequestContext {
public:
webkit_blob::BlobStorageController* blob_storage_controller() {
return &blob_storage_controller_;
}
private:
webkit_blob::BlobStorageController blob_storage_controller_;
};
static net::URLRequestJob* BlobURLRequestJobFactory(net::URLRequest* request,
const std::string& scheme) {
webkit_blob::BlobStorageController* blob_storage_controller =
static_cast<TestURLRequestContext*>(request->context())->
blob_storage_controller();
return new webkit_blob::BlobURLRequestJob(
request,
blob_storage_controller->GetBlobDataFromUrl(request->url()),
base::MessageLoopProxy::CreateForCurrentThread());
}
class MockDispatcher : public FileSystemCallbackDispatcher {
public:
MockDispatcher(FileSystemOperationWriteTest* test) : test_(test) { }
virtual void DidFail(base::PlatformFileError status) {
test_->set_failure_status(status);
MessageLoop::current()->Quit();
}
virtual void DidSucceed() {
ADD_FAILURE();
}
virtual void DidReadMetadata(
const base::PlatformFileInfo& info,
const FilePath& platform_path) {
ADD_FAILURE();
}
virtual void DidReadDirectory(
const std::vector<base::FileUtilProxy::Entry>& entries,
bool /* has_more */) {
ADD_FAILURE();
}
virtual void DidOpenFileSystem(const std::string&, const GURL&) {
ADD_FAILURE();
}
virtual void DidWrite(int64 bytes, bool complete) {
test_->add_bytes_written(bytes, complete);
MessageLoop::current()->Quit();
}
private:
FileSystemOperationWriteTest* test_;
};
} // namespace (anonymous)
void FileSystemOperationWriteTest::SetUp() {
ASSERT_TRUE(dir_.CreateUniqueTempDir());
file_util::CreateTemporaryFileInDir(dir_.path(), &file_);
net::URLRequest::RegisterProtocolFactory("blob", &BlobURLRequestJobFactory);
}
void FileSystemOperationWriteTest::TearDown() {
net::URLRequest::RegisterProtocolFactory("blob", NULL);
}
FileSystemOperation* FileSystemOperationWriteTest::operation() {
FileSystemOperation* operation = new FileSystemOperation(
new MockDispatcher(this),
base::MessageLoopProxy::CreateForCurrentThread(),
NULL,
FileSystemFileUtil::GetInstance());
operation->file_system_operation_context()->set_src_type(
kFileSystemTypeTemporary);
operation->file_system_operation_context()->set_dest_type(
kFileSystemTypeTemporary);
return operation;
}
TEST_F(FileSystemOperationWriteTest, TestWriteSuccess) {
GURL blob_url("blob:success");
scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData());
blob_data->AppendData("Hello, world!\n");
scoped_refptr<TestURLRequestContext> url_request_context(
new TestURLRequestContext());
url_request_context->blob_storage_controller()->
RegisterBlobUrl(blob_url, blob_data);
operation()->Write(url_request_context, URLForPath(file_), blob_url, 0);
MessageLoop::current()->Run();
url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url);
EXPECT_EQ(14, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, status());
EXPECT_TRUE(complete());
}
TEST_F(FileSystemOperationWriteTest, TestWriteZero) {
GURL blob_url("blob:zero");
scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData());
blob_data->AppendData("");
scoped_refptr<TestURLRequestContext> url_request_context(
new TestURLRequestContext());
url_request_context->blob_storage_controller()->
RegisterBlobUrl(blob_url, blob_data);
operation()->Write(url_request_context, URLForPath(file_), blob_url, 0);
MessageLoop::current()->Run();
url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url);
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, status());
EXPECT_TRUE(complete());
}
TEST_F(FileSystemOperationWriteTest, TestWriteInvalidBlobUrl) {
scoped_refptr<TestURLRequestContext> url_request_context(
new TestURLRequestContext());
operation()->Write(url_request_context, URLForPath(file_),
GURL("blob:invalid"), 0);
MessageLoop::current()->Run();
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, status());
EXPECT_TRUE(complete());
}
TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) {
GURL blob_url("blob:writeinvalidfile");
scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData());
blob_data->AppendData("It\'ll not be written.");
scoped_refptr<TestURLRequestContext> url_request_context(
new TestURLRequestContext());
url_request_context->blob_storage_controller()->
RegisterBlobUrl(blob_url, blob_data);
operation()->Write(url_request_context,
URLForRelativePath("nonexist"), blob_url, 0);
MessageLoop::current()->Run();
url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url);
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
EXPECT_TRUE(complete());
}
TEST_F(FileSystemOperationWriteTest, TestWriteDir) {
GURL blob_url("blob:writedir");
scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData());
blob_data->AppendData("It\'ll not be written, too.");
scoped_refptr<TestURLRequestContext> url_request_context(
new TestURLRequestContext());
url_request_context->blob_storage_controller()->
RegisterBlobUrl(blob_url, blob_data);
operation()->Write(url_request_context, URLForPath(dir_.path()), blob_url, 0);
MessageLoop::current()->Run();
url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url);
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status());
EXPECT_TRUE(complete());
}
// TODO(ericu,dmikurube): Add tests for Cancel.
} // namespace fileapi
|