summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/syncable
diff options
context:
space:
mode:
authorkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-16 05:16:23 +0000
committerkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-16 05:16:23 +0000
commit169adebb59774f88757a940621998440c590336b (patch)
tree7dda8ad06b08912556d30eac97058b2ad58e9d83 /webkit/fileapi/syncable
parentb1b800c69645b72c13df44a5b770d5b295b194fb (diff)
downloadchromium_src-169adebb59774f88757a940621998440c590336b.zip
chromium_src-169adebb59774f88757a940621998440c590336b.tar.gz
chromium_src-169adebb59774f88757a940621998440c590336b.tar.bz2
Implement CannedSyncableFileSystem.Write for write testing
Also factor out MockBlobURLRequestContext class so that we can easily test writing with blob urls. BUG=148897 TEST=LocalFileChangeTrackerTest.* TBR=jam@chromium.org Review URL: https://codereview.chromium.org/11146015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162078 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/fileapi/syncable')
-rw-r--r--webkit/fileapi/syncable/canned_syncable_file_system.cc124
-rw-r--r--webkit/fileapi/syncable/canned_syncable_file_system.h13
-rw-r--r--webkit/fileapi/syncable/local_file_change_tracker_unittest.cc50
-rw-r--r--webkit/fileapi/syncable/local_file_sync_context_unittest.cc3
4 files changed, 138 insertions, 52 deletions
diff --git a/webkit/fileapi/syncable/canned_syncable_file_system.cc b/webkit/fileapi/syncable/canned_syncable_file_system.cc
index b6fab40..ff7992b 100644
--- a/webkit/fileapi/syncable/canned_syncable_file_system.cc
+++ b/webkit/fileapi/syncable/canned_syncable_file_system.cc
@@ -28,28 +28,28 @@ namespace fileapi {
namespace {
-typedef CannedSyncableFileSystem::StatusCallback StatusCallback;
-
void Empty() {}
void Quit() { MessageLoop::current()->Quit(); }
+template <typename R>
void AssignAndQuit(base::TaskRunner* original_task_runner,
- PlatformFileError* result_out,
- PlatformFileError result) {
+ R* result_out, R result) {
DCHECK(result_out);
*result_out = result;
original_task_runner->PostTask(FROM_HERE, base::Bind(&Quit));
}
-PlatformFileError RunOnThread(
+
+template <typename R>
+R RunOnThread(
base::SingleThreadTaskRunner* task_runner,
const tracked_objects::Location& location,
- const base::Callback<void(const StatusCallback& callback)>& task) {
- PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED;
+ const base::Callback<void(const base::Callback<void(R)>& callback)>& task) {
+ R result;
task_runner->PostTask(
location,
- base::Bind(task, base::Bind(&AssignAndQuit,
+ base::Bind(task, base::Bind(&AssignAndQuit<R>,
base::MessageLoopProxy::current(),
&result)));
MessageLoop::current()->Run();
@@ -69,6 +69,26 @@ void VerifySameTaskRunner(
base::Bind(&EnsureRunningOn, make_scoped_refptr(runner2)));
}
+class WriteHelper {
+ public:
+ WriteHelper() : bytes_written_(0) {}
+ ~WriteHelper() {}
+
+ void DidWrite(const base::Callback<void(int64)>& completion_callback,
+ PlatformFileError error, int64 bytes, bool complete) {
+ if (error == base::PLATFORM_FILE_OK) {
+ bytes_written_ += bytes;
+ if (!complete)
+ return;
+ }
+ completion_callback.Run(error == base::PLATFORM_FILE_OK
+ ? bytes_written_ : static_cast<int64>(error));
+ }
+
+ private:
+ int64 bytes_written_;
+};
+
} // namespace
CannedSyncableFileSystem::CannedSyncableFileSystem(
@@ -154,60 +174,77 @@ SyncStatusCode CannedSyncableFileSystem::MaybeInitializeFileSystemContext(
PlatformFileError CannedSyncableFileSystem::CreateDirectory(
const FileSystemURL& url) {
- return RunOnThread(io_task_runner_,
- FROM_HERE,
- base::Bind(&CannedSyncableFileSystem::DoCreateDirectory,
- base::Unretained(this), url));
+ return RunOnThread<PlatformFileError>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&CannedSyncableFileSystem::DoCreateDirectory,
+ base::Unretained(this), url));
}
PlatformFileError CannedSyncableFileSystem::CreateFile(
const FileSystemURL& url) {
- return RunOnThread(io_task_runner_,
- FROM_HERE,
- base::Bind(&CannedSyncableFileSystem::DoCreateFile,
- base::Unretained(this), url));
+ return RunOnThread<PlatformFileError>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&CannedSyncableFileSystem::DoCreateFile,
+ base::Unretained(this), url));
}
PlatformFileError CannedSyncableFileSystem::Copy(
const FileSystemURL& src_url, const FileSystemURL& dest_url) {
- return RunOnThread(io_task_runner_,
- FROM_HERE,
- base::Bind(&CannedSyncableFileSystem::DoCopy,
- base::Unretained(this), src_url, dest_url));
+ return RunOnThread<PlatformFileError>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&CannedSyncableFileSystem::DoCopy,
+ base::Unretained(this), src_url, dest_url));
}
PlatformFileError CannedSyncableFileSystem::Move(
const FileSystemURL& src_url, const FileSystemURL& dest_url) {
- return RunOnThread(io_task_runner_,
- FROM_HERE,
- base::Bind(&CannedSyncableFileSystem::DoMove,
- base::Unretained(this), src_url, dest_url));
+ return RunOnThread<PlatformFileError>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&CannedSyncableFileSystem::DoMove,
+ base::Unretained(this), src_url, dest_url));
}
PlatformFileError CannedSyncableFileSystem::TruncateFile(
const FileSystemURL& url, int64 size) {
- return RunOnThread(io_task_runner_,
- FROM_HERE,
- base::Bind(&CannedSyncableFileSystem::DoTruncateFile,
- base::Unretained(this), url, size));
+ return RunOnThread<PlatformFileError>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&CannedSyncableFileSystem::DoTruncateFile,
+ base::Unretained(this), url, size));
}
PlatformFileError CannedSyncableFileSystem::Remove(
const FileSystemURL& url, bool recursive) {
- return RunOnThread(io_task_runner_,
- FROM_HERE,
- base::Bind(&CannedSyncableFileSystem::DoRemove,
- base::Unretained(this), url, recursive));
+ return RunOnThread<PlatformFileError>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&CannedSyncableFileSystem::DoRemove,
+ base::Unretained(this), url, recursive));
+}
+
+int64 CannedSyncableFileSystem::Write(
+ net::URLRequestContext* url_request_context,
+ const FileSystemURL& url, const GURL& blob_url) {
+ return RunOnThread<int64>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&CannedSyncableFileSystem::DoWrite,
+ base::Unretained(this), url_request_context, url, blob_url));
}
PlatformFileError CannedSyncableFileSystem::DeleteFileSystem() {
EXPECT_TRUE(is_filesystem_set_up_);
- return RunOnThread(io_task_runner_,
- FROM_HERE,
- base::Bind(&FileSystemContext::DeleteFileSystem,
- file_system_context_,
- test_helper_.origin(),
- test_helper_.type()));
+ return RunOnThread<PlatformFileError>(
+ io_task_runner_,
+ FROM_HERE,
+ base::Bind(&FileSystemContext::DeleteFileSystem,
+ file_system_context_,
+ test_helper_.origin(),
+ test_helper_.type()));
}
FileSystemOperation* CannedSyncableFileSystem::NewOperation() {
@@ -259,6 +296,17 @@ void CannedSyncableFileSystem::DoRemove(
NewOperation()->Remove(url, recursive, callback);
}
+void CannedSyncableFileSystem::DoWrite(
+ net::URLRequestContext* url_request_context,
+ const FileSystemURL& url, const GURL& blob_url,
+ const WriteCallback& callback) {
+ EXPECT_TRUE(is_filesystem_opened_);
+ WriteHelper* helper = new WriteHelper;
+ NewOperation()->Write(url_request_context, url, blob_url, 0,
+ base::Bind(&WriteHelper::DidWrite,
+ base::Owned(helper), callback));
+}
+
void CannedSyncableFileSystem::DidOpenFileSystem(
PlatformFileError result, const std::string& name, const GURL& root) {
result_ = result;
diff --git a/webkit/fileapi/syncable/canned_syncable_file_system.h b/webkit/fileapi/syncable/canned_syncable_file_system.h
index 359d53c..4fa634e 100644
--- a/webkit/fileapi/syncable/canned_syncable_file_system.h
+++ b/webkit/fileapi/syncable/canned_syncable_file_system.h
@@ -21,6 +21,10 @@ class SingleThreadTaskRunner;
class Thread;
}
+namespace net {
+class URLRequestContext;
+}
+
namespace quota {
class QuotaManager;
}
@@ -37,6 +41,7 @@ class LocalFileSyncContext;
class CannedSyncableFileSystem {
public:
typedef base::Callback<void(base::PlatformFileError)> StatusCallback;
+ typedef base::Callback<void(int64)> WriteCallback;
CannedSyncableFileSystem(const GURL& origin,
const std::string& service,
@@ -83,6 +88,10 @@ class CannedSyncableFileSystem {
base::PlatformFileError TruncateFile(const FileSystemURL& url, int64 size);
base::PlatformFileError Remove(const FileSystemURL& url, bool recursive);
+ // Returns the # of bytes written (>=0) or an error code (<0).
+ int64 Write(net::URLRequestContext* url_request_context,
+ const FileSystemURL& url, const GURL& blob_url);
+
// Pruges the file system local storage.
base::PlatformFileError DeleteFileSystem();
@@ -107,6 +116,10 @@ class CannedSyncableFileSystem {
void DoRemove(const FileSystemURL& url,
bool recursive,
const StatusCallback& callback);
+ void DoWrite(net::URLRequestContext* url_request_context,
+ const FileSystemURL& url,
+ const GURL& blob_url,
+ const WriteCallback& callback);
// Callbacks.
void DidOpenFileSystem(base::PlatformFileError result,
diff --git a/webkit/fileapi/syncable/local_file_change_tracker_unittest.cc b/webkit/fileapi/syncable/local_file_change_tracker_unittest.cc
index 5f01a8dc..2ce3efb 100644
--- a/webkit/fileapi/syncable/local_file_change_tracker_unittest.cc
+++ b/webkit/fileapi/syncable/local_file_change_tracker_unittest.cc
@@ -10,6 +10,7 @@
#include "base/message_loop_proxy.h"
#include "base/scoped_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "webkit/blob/mock_blob_url_request_context.h"
#include "webkit/fileapi/file_system_context.h"
#include "webkit/fileapi/file_system_task_runners.h"
#include "webkit/fileapi/syncable/canned_syncable_file_system.h"
@@ -18,12 +19,16 @@
#include "webkit/fileapi/syncable/syncable_file_system_util.h"
#include "webkit/quota/quota_manager.h"
+using webkit_blob::MockBlobURLRequestContext;
+using webkit_blob::ScopedTextBlob;
+
namespace fileapi {
class LocalFileChangeTrackerTest : public testing::Test {
public:
LocalFileChangeTrackerTest()
- : file_system_(GURL("http://example.com"), "test",
+ : message_loop_(MessageLoop::TYPE_IO),
+ file_system_(GURL("http://example.com"), "test",
base::MessageLoopProxy::current()) {}
virtual void SetUp() OVERRIDE {
@@ -157,17 +162,24 @@ TEST_F(LocalFileChangeTrackerTest, RestoreCreateAndModifyChanges) {
const char kPath0[] = "file a";
const char kPath1[] = "dir a";
const char kPath2[] = "dir a/dir";
- const char kPath3[] = "dir a/file";
+ const char kPath3[] = "dir a/file a";
+ const char kPath4[] = "dir a/file b";
- const char kPath4[] = "file b"; // To be copied from kPath0
- const char kPath5[] = "dir b"; // To be copied from kPath1
- const char kPath6[] = "dir b/dir"; // To be copied from kPath2
- const char kPath7[] = "dir b/file"; // To be copied from kPath3
+ const char kPath0Copy[] = "file b"; // To be copied from kPath0
+ const char kPath1Copy[] = "dir b"; // To be copied from kPath1
+ const char kPath2Copy[] = "dir b/dir"; // To be copied from kPath2
+ const char kPath3Copy[] = "dir b/file a"; // To be copied from kPath3
+ const char kPath4Copy[] = "dir b/file b"; // To be copied from kPath4
change_tracker()->GetChangedURLs(&urls);
urlset.insert(urls.begin(), urls.end());
ASSERT_EQ(0U, urlset.size());
+ const GURL blob_url("blob:test");
+ const std::string kData("Lorem ipsum.");
+ MockBlobURLRequestContext url_request_context;
+ ScopedTextBlob blob(url_request_context, blob_url, kData);
+
// Creates files and nested directories.
EXPECT_EQ(base::PLATFORM_FILE_OK,
file_system_.CreateFile(URL(kPath0))); // Creates a file.
@@ -179,16 +191,21 @@ TEST_F(LocalFileChangeTrackerTest, RestoreCreateAndModifyChanges) {
file_system_.CreateFile(URL(kPath3))); // Creates a file.
EXPECT_EQ(base::PLATFORM_FILE_OK,
file_system_.TruncateFile(URL(kPath3), 1)); // Modifies the file.
+ EXPECT_EQ(base::PLATFORM_FILE_OK,
+ file_system_.CreateFile(URL(kPath4))); // Creates another file.
+ EXPECT_EQ(static_cast<int64>(kData.size()),
+ file_system_.Write(&url_request_context,
+ URL(kPath4), blob_url)); // Modifies the file.
// Copies the file and the parent directory.
EXPECT_EQ(base::PLATFORM_FILE_OK,
- file_system_.Copy(URL(kPath0), URL(kPath4))); // Copy the file.
+ file_system_.Copy(URL(kPath0), URL(kPath0Copy))); // Copy the file.
EXPECT_EQ(base::PLATFORM_FILE_OK,
- file_system_.Copy(URL(kPath1), URL(kPath5))); // Copy the dir.
+ file_system_.Copy(URL(kPath1), URL(kPath1Copy))); // Copy the dir.
change_tracker()->GetChangedURLs(&urls);
urlset.insert(urls.begin(), urls.end());
- EXPECT_EQ(8U, urlset.size());
+ EXPECT_EQ(10U, urlset.size());
DropChangesInTracker();
@@ -206,7 +223,7 @@ TEST_F(LocalFileChangeTrackerTest, RestoreCreateAndModifyChanges) {
urlset.clear();
change_tracker()->GetChangedURLs(&urls);
urlset.insert(urls.begin(), urls.end());
- EXPECT_EQ(8U, urlset.size());
+ EXPECT_EQ(10U, urlset.size());
VerifyAndClearChange(URL(kPath0),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
@@ -223,13 +240,20 @@ TEST_F(LocalFileChangeTrackerTest, RestoreCreateAndModifyChanges) {
VerifyAndClearChange(URL(kPath4),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
FileChange::FILE_TYPE_FILE));
- VerifyAndClearChange(URL(kPath5),
+
+ VerifyAndClearChange(URL(kPath0Copy),
+ FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
+ FileChange::FILE_TYPE_FILE));
+ VerifyAndClearChange(URL(kPath1Copy),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
FileChange::FILE_TYPE_DIRECTORY));
- VerifyAndClearChange(URL(kPath6),
+ VerifyAndClearChange(URL(kPath2Copy),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
FileChange::FILE_TYPE_DIRECTORY));
- VerifyAndClearChange(URL(kPath7),
+ VerifyAndClearChange(URL(kPath3Copy),
+ FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
+ FileChange::FILE_TYPE_FILE));
+ VerifyAndClearChange(URL(kPath4Copy),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
FileChange::FILE_TYPE_FILE));
}
diff --git a/webkit/fileapi/syncable/local_file_sync_context_unittest.cc b/webkit/fileapi/syncable/local_file_sync_context_unittest.cc
index 48aca29..e711f4a 100644
--- a/webkit/fileapi/syncable/local_file_sync_context_unittest.cc
+++ b/webkit/fileapi/syncable/local_file_sync_context_unittest.cc
@@ -42,7 +42,8 @@ class LocalFileSyncContextTest : public testing::Test {
io_thread_.reset(new base::Thread("Thread_IO"));
file_thread_.reset(new base::Thread("Thread_File"));
- io_thread_->Start();
+ io_thread_->StartWithOptions(
+ base::Thread::Options(MessageLoop::TYPE_IO, 0));
file_thread_->Start();
ui_task_runner_ = MessageLoop::current()->message_loop_proxy();