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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
// 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 <vector>
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.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 "net/url_request/url_request_job_factory_impl.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/blob/mock_blob_url_request_context.h"
#include "webkit/fileapi/file_system_context.h"
#include "webkit/fileapi/file_system_file_util.h"
#include "webkit/fileapi/file_system_util.h"
#include "webkit/fileapi/local_file_system_operation.h"
#include "webkit/fileapi/local_file_system_test_helper.h"
#include "webkit/fileapi/local_file_util.h"
#include "webkit/fileapi/mock_file_change_observer.h"
#include "webkit/quota/quota_manager.h"
using quota::QuotaManager;
using webkit_blob::MockBlobURLRequestContext;
using webkit_blob::ScopedTextBlob;
namespace fileapi {
namespace {
void AssertStatusEq(base::PlatformFileError expected,
base::PlatformFileError actual) {
ASSERT_EQ(expected, actual);
}
class MockQuotaManager : public QuotaManager {
public:
MockQuotaManager(const base::FilePath& base_dir, int64 quota)
: QuotaManager(false /* is_incognito */, base_dir,
base::MessageLoopProxy::current(),
base::MessageLoopProxy::current(),
NULL /* special_storage_policy */),
usage_(0),
quota_(quota) {}
virtual void GetUsageAndQuota(
const GURL& origin, quota::StorageType type,
const GetUsageAndQuotaCallback& callback) OVERRIDE {
callback.Run(quota::kQuotaStatusOk, usage_, quota_);
}
void set_usage(int64 usage) { usage_ = usage; }
void set_quota(int64 quota) { quota_ = quota; }
protected:
virtual ~MockQuotaManager() {}
private:
int64 usage_;
int64 quota_;
};
} // namespace
class LocalFileSystemOperationWriteTest
: public testing::Test,
public base::SupportsWeakPtr<LocalFileSystemOperationWriteTest> {
public:
LocalFileSystemOperationWriteTest()
: test_helper_(GURL("http://example.com"), kFileSystemTypeTest),
loop_(MessageLoop::TYPE_IO),
status_(base::PLATFORM_FILE_OK),
cancel_status_(base::PLATFORM_FILE_ERROR_FAILED),
bytes_written_(0),
complete_(false),
url_request_context_(test_helper_.file_system_context()) {
change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
}
LocalFileSystemOperation* operation();
base::PlatformFileError status() const { return status_; }
base::PlatformFileError cancel_status() const { return cancel_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:
const ChangeObserverList& change_observers() const {
return change_observers_;
}
MockFileChangeObserver* change_observer() {
return &change_observer_;
}
FileSystemURL URLForPath(const base::FilePath& path) const {
return test_helper_.CreateURL(path);
}
// Callback function for recording test results.
FileSystemOperation::WriteCallback RecordWriteCallback() {
return base::Bind(&LocalFileSystemOperationWriteTest::DidWrite,
AsWeakPtr());
}
FileSystemOperation::StatusCallback RecordCancelCallback() {
return base::Bind(&LocalFileSystemOperationWriteTest::DidCancel,
AsWeakPtr());
}
void DidWrite(base::PlatformFileError status, int64 bytes, bool complete) {
if (status == base::PLATFORM_FILE_OK) {
add_bytes_written(bytes, complete);
if (complete)
MessageLoop::current()->Quit();
} else {
EXPECT_FALSE(complete_);
EXPECT_EQ(status_, base::PLATFORM_FILE_OK);
complete_ = true;
status_ = status;
if (MessageLoop::current()->is_running())
MessageLoop::current()->Quit();
}
}
void DidCancel(base::PlatformFileError status) {
cancel_status_ = status;
}
FileSystemFileUtil* file_util() {
return test_helper_.file_util();
}
scoped_refptr<MockQuotaManager> quota_manager_;
LocalFileSystemTestOriginHelper test_helper_;
MessageLoop loop_;
base::ScopedTempDir dir_;
base::FilePath virtual_path_;
// For post-operation status.
base::PlatformFileError status_;
base::PlatformFileError cancel_status_;
int64 bytes_written_;
bool complete_;
MockBlobURLRequestContext url_request_context_;
private:
MockFileChangeObserver change_observer_;
ChangeObserverList change_observers_;
DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperationWriteTest);
};
void LocalFileSystemOperationWriteTest::SetUp() {
ASSERT_TRUE(dir_.CreateUniqueTempDir());
base::FilePath base_dir = dir_.path().AppendASCII("filesystem");
quota_manager_ = new MockQuotaManager(base_dir, 1024);
test_helper_.SetUp(base_dir,
false /* unlimited quota */,
quota_manager_->proxy());
virtual_path_ = base::FilePath(FILE_PATH_LITERAL("temporary file"));
operation()->CreateFile(
URLForPath(virtual_path_), true /* exclusive */,
base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK));
}
void LocalFileSystemOperationWriteTest::TearDown() {
quota_manager_ = NULL;
test_helper_.TearDown();
}
LocalFileSystemOperation* LocalFileSystemOperationWriteTest::operation() {
LocalFileSystemOperation* operation = test_helper_.NewOperation();
operation->operation_context()->set_change_observers(change_observers());
return operation;
}
TEST_F(LocalFileSystemOperationWriteTest, TestWriteSuccess) {
const GURL blob_url("blob:success");
ScopedTextBlob blob(url_request_context_, blob_url, "Hello, world!\n");
operation()->Write(&url_request_context_, URLForPath(virtual_path_), blob_url,
0, RecordWriteCallback());
MessageLoop::current()->Run();
EXPECT_EQ(14, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, status());
EXPECT_TRUE(complete());
EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
}
TEST_F(LocalFileSystemOperationWriteTest, TestWriteZero) {
GURL blob_url("blob:zero");
scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData());
url_request_context_.blob_storage_controller()->AddFinishedBlob(
blob_url, blob_data);
operation()->Write(&url_request_context_, URLForPath(virtual_path_),
blob_url, 0, RecordWriteCallback());
MessageLoop::current()->Run();
url_request_context_.blob_storage_controller()->RemoveBlob(blob_url);
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, status());
EXPECT_TRUE(complete());
EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
}
TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidBlobUrl) {
operation()->Write(&url_request_context_, URLForPath(virtual_path_),
GURL("blob:invalid"), 0, RecordWriteCallback());
MessageLoop::current()->Run();
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_FAILED, status());
EXPECT_TRUE(complete());
EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count());
}
TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidFile) {
GURL blob_url("blob:writeinvalidfile");
ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written.");
operation()->Write(&url_request_context_,
URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist"))),
blob_url, 0, RecordWriteCallback());
MessageLoop::current()->Run();
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
EXPECT_TRUE(complete());
EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
}
TEST_F(LocalFileSystemOperationWriteTest, TestWriteDir) {
base::FilePath virtual_dir_path(FILE_PATH_LITERAL("d"));
operation()->CreateDirectory(
URLForPath(virtual_dir_path),
true /* exclusive */, false /* recursive */,
base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK));
GURL blob_url("blob:writedir");
ScopedTextBlob blob(url_request_context_, blob_url,
"It\'ll not be written, too.");
operation()->Write(&url_request_context_, URLForPath(virtual_dir_path),
blob_url, 0, RecordWriteCallback());
MessageLoop::current()->Run();
EXPECT_EQ(0, bytes_written());
// TODO(kinuko): This error code is platform- or fileutil- dependent
// right now. Make it return PLATFORM_FILE_ERROR_NOT_A_FILE in every case.
EXPECT_TRUE(status() == base::PLATFORM_FILE_ERROR_NOT_A_FILE ||
status() == base::PLATFORM_FILE_ERROR_ACCESS_DENIED ||
status() == base::PLATFORM_FILE_ERROR_FAILED);
EXPECT_TRUE(complete());
EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
}
TEST_F(LocalFileSystemOperationWriteTest, TestWriteFailureByQuota) {
GURL blob_url("blob:success");
ScopedTextBlob blob(url_request_context_, blob_url, "Hello, world!\n");
quota_manager_->set_quota(10);
operation()->Write(&url_request_context_, URLForPath(virtual_path_), blob_url,
0, RecordWriteCallback());
MessageLoop::current()->Run();
EXPECT_EQ(10, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status());
EXPECT_TRUE(complete());
EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
}
TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelSuccessfulWrite) {
GURL blob_url("blob:success");
ScopedTextBlob blob(url_request_context_, blob_url, "Hello, world!\n");
FileSystemOperation* write_operation = operation();
write_operation->Write(&url_request_context_, URLForPath(virtual_path_),
blob_url, 0, RecordWriteCallback());
write_operation->Cancel(RecordCancelCallback());
// We use RunAllPendings() instead of Run() here, because we won't dispatch
// callbacks after Cancel() is issued (so no chance to Quit) nor do we need
// to run another write cycle.
MessageLoop::current()->RunUntilIdle();
// Issued Cancel() before receiving any response from Write(),
// so nothing should have happen.
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status());
EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status());
EXPECT_TRUE(complete());
EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count());
}
TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelFailingWrite) {
GURL blob_url("blob:writeinvalidfile");
ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written.");
FileSystemOperation* write_operation = operation();
write_operation->Write(&url_request_context_,
URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist"))),
blob_url, 0, RecordWriteCallback());
write_operation->Cancel(RecordCancelCallback());
// We use RunAllPendings() instead of Run() here, because we won't dispatch
// callbacks after Cancel() is issued (so no chance to Quit) nor do we need
// to run another write cycle.
MessageLoop::current()->RunUntilIdle();
// Issued Cancel() before receiving any response from Write(),
// so nothing should have happen.
EXPECT_EQ(0, bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status());
EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status());
EXPECT_TRUE(complete());
EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count());
}
// TODO(ericu,dmikurube,kinuko): Add more tests for cancel cases.
} // namespace fileapi
|