OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/fileapi/syncable/canned_syncable_file_system.h" | 5 #include "webkit/fileapi/syncable/canned_syncable_file_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "webkit/fileapi/file_system_context.h" | 11 #include "webkit/fileapi/file_system_context.h" |
12 #include "webkit/fileapi/file_system_operation_context.h" | 12 #include "webkit/fileapi/file_system_operation_context.h" |
13 #include "webkit/fileapi/file_system_task_runners.h" | 13 #include "webkit/fileapi/file_system_task_runners.h" |
14 #include "webkit/fileapi/file_system_util.h" | 14 #include "webkit/fileapi/file_system_util.h" |
15 #include "webkit/fileapi/isolated_context.h" | 15 #include "webkit/fileapi/isolated_context.h" |
16 #include "webkit/fileapi/local_file_system_operation.h" | 16 #include "webkit/fileapi/local_file_system_operation.h" |
17 #include "webkit/fileapi/mock_file_system_options.h" | 17 #include "webkit/fileapi/mock_file_system_options.h" |
| 18 #include "webkit/fileapi/syncable/syncable_context.h" |
18 #include "webkit/quota/mock_special_storage_policy.h" | 19 #include "webkit/quota/mock_special_storage_policy.h" |
19 #include "webkit/quota/quota_manager.h" | 20 #include "webkit/quota/quota_manager.h" |
20 | 21 |
21 using base::PlatformFileError; | 22 using base::PlatformFileError; |
22 using quota::QuotaManager; | 23 using quota::QuotaManager; |
23 | 24 |
24 namespace fileapi { | 25 namespace fileapi { |
25 | 26 |
26 CannedSyncableFileSystem::CannedSyncableFileSystem( | 27 CannedSyncableFileSystem::CannedSyncableFileSystem( |
27 const GURL& origin, const std::string& service) | 28 const GURL& origin, const std::string& service) |
28 : service_name_(service), | 29 : service_name_(service), |
29 test_helper_(origin, kFileSystemTypeSyncable), | 30 test_helper_(origin, kFileSystemTypeSyncable), |
30 result_(base::PLATFORM_FILE_OK), | 31 result_(base::PLATFORM_FILE_OK), |
31 sync_status_(SYNC_STATUS_OK), | 32 sync_status_(SYNC_STATUS_OK), |
| 33 is_filesystem_set_up_(false), |
| 34 is_filesystem_opened_(false), |
32 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 35 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
33 } | 36 } |
34 | 37 |
35 CannedSyncableFileSystem::~CannedSyncableFileSystem() {} | 38 CannedSyncableFileSystem::~CannedSyncableFileSystem() {} |
36 | 39 |
37 void CannedSyncableFileSystem::SetUp() { | 40 void CannedSyncableFileSystem::SetUp() { |
| 41 ASSERT_FALSE(is_filesystem_set_up_); |
38 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 42 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
39 | 43 |
40 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = | 44 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = |
41 new quota::MockSpecialStoragePolicy(); | 45 new quota::MockSpecialStoragePolicy(); |
42 | 46 |
43 quota_manager_ = new QuotaManager( | 47 quota_manager_ = new QuotaManager( |
44 false /* is_incognito */, | 48 false /* is_incognito */, |
45 data_dir_.path(), | 49 data_dir_.path(), |
46 base::MessageLoopProxy::current(), | 50 base::MessageLoopProxy::current(), |
47 base::MessageLoopProxy::current(), | 51 base::MessageLoopProxy::current(), |
48 storage_policy); | 52 storage_policy); |
49 | 53 |
50 file_system_context_ = new FileSystemContext( | 54 file_system_context_ = new FileSystemContext( |
51 FileSystemTaskRunners::CreateMockTaskRunners(), | 55 FileSystemTaskRunners::CreateMockTaskRunners(), |
52 storage_policy, | 56 storage_policy, |
53 quota_manager_->proxy(), | 57 quota_manager_->proxy(), |
54 data_dir_.path(), | 58 data_dir_.path(), |
55 CreateAllowFileAccessOptions()); | 59 CreateAllowFileAccessOptions()); |
56 | 60 |
57 test_helper_.SetUp(file_system_context_.get(), NULL); | 61 test_helper_.SetUp(file_system_context_.get(), NULL); |
| 62 is_filesystem_set_up_ = true; |
58 } | 63 } |
59 | 64 |
60 void CannedSyncableFileSystem::TearDown() { | 65 void CannedSyncableFileSystem::TearDown() { |
61 quota_manager_ = NULL; | 66 quota_manager_ = NULL; |
62 test_helper_.TearDown(); | 67 test_helper_.TearDown(); |
63 } | 68 } |
64 | 69 |
65 FileSystemURL CannedSyncableFileSystem::URL(const std::string& path) const { | 70 FileSystemURL CannedSyncableFileSystem::URL(const std::string& path) const { |
| 71 EXPECT_TRUE(is_filesystem_set_up_); |
| 72 EXPECT_TRUE(is_filesystem_opened_); |
66 return FileSystemURL(GURL(root_url_.spec() + path)); | 73 return FileSystemURL(GURL(root_url_.spec() + path)); |
67 } | 74 } |
68 | 75 |
69 PlatformFileError CannedSyncableFileSystem::OpenFileSystem() { | 76 PlatformFileError CannedSyncableFileSystem::OpenFileSystem() { |
| 77 EXPECT_TRUE(is_filesystem_set_up_); |
| 78 EXPECT_FALSE(is_filesystem_opened_); |
70 file_system_context_->OpenSyncableFileSystem( | 79 file_system_context_->OpenSyncableFileSystem( |
71 service_name_, | 80 service_name_, |
72 test_helper_.origin(), test_helper_.type(), | 81 test_helper_.origin(), test_helper_.type(), |
73 true /* create */, | 82 true /* create */, |
74 base::Bind(&CannedSyncableFileSystem::DidOpenFileSystem, | 83 base::Bind(&CannedSyncableFileSystem::DidOpenFileSystem, |
75 weak_factory_.GetWeakPtr())); | 84 weak_factory_.GetWeakPtr())); |
76 MessageLoop::current()->RunAllPending(); | 85 MessageLoop::current()->RunAllPending(); |
77 return result_; | 86 return result_; |
78 } | 87 } |
79 | 88 |
| 89 SyncStatusCode CannedSyncableFileSystem::MaybeInitializeFileSystemContext( |
| 90 SyncableContext* syncable_context) { |
| 91 DCHECK(syncable_context); |
| 92 sync_status_ = SYNC_FILE_ERROR_FAILED; |
| 93 syncable_context->MaybeInitializeFileSystemContext( |
| 94 test_helper_.origin(), |
| 95 file_system_context_, |
| 96 base::Bind(&CannedSyncableFileSystem::DidInitializeFileSystemContext, |
| 97 base::Unretained(this))); |
| 98 MessageLoop::current()->Run(); |
| 99 return sync_status_; |
| 100 } |
| 101 |
80 PlatformFileError CannedSyncableFileSystem::CreateDirectory( | 102 PlatformFileError CannedSyncableFileSystem::CreateDirectory( |
81 const FileSystemURL& url) { | 103 const FileSystemURL& url) { |
| 104 EXPECT_TRUE(is_filesystem_opened_); |
82 result_ = base::PLATFORM_FILE_ERROR_FAILED; | 105 result_ = base::PLATFORM_FILE_ERROR_FAILED; |
83 test_helper_.NewOperation()->CreateDirectory( | 106 test_helper_.NewOperation()->CreateDirectory( |
84 url, false /* exclusive */, false /* recursive */, | 107 url, false /* exclusive */, false /* recursive */, |
85 base::Bind(&CannedSyncableFileSystem::StatusCallback, | 108 base::Bind(&CannedSyncableFileSystem::StatusCallback, |
86 weak_factory_.GetWeakPtr())); | 109 weak_factory_.GetWeakPtr())); |
87 MessageLoop::current()->RunAllPending(); | 110 MessageLoop::current()->RunAllPending(); |
88 return result_; | 111 return result_; |
89 } | 112 } |
90 | 113 |
91 PlatformFileError CannedSyncableFileSystem::CreateFile( | 114 PlatformFileError CannedSyncableFileSystem::CreateFile( |
92 const FileSystemURL& url) { | 115 const FileSystemURL& url) { |
| 116 EXPECT_TRUE(is_filesystem_opened_); |
93 result_ = base::PLATFORM_FILE_ERROR_FAILED; | 117 result_ = base::PLATFORM_FILE_ERROR_FAILED; |
94 test_helper_.NewOperation()->CreateFile( | 118 test_helper_.NewOperation()->CreateFile( |
95 url, false /* exclusive */, | 119 url, false /* exclusive */, |
96 base::Bind(&CannedSyncableFileSystem::StatusCallback, | 120 base::Bind(&CannedSyncableFileSystem::StatusCallback, |
97 weak_factory_.GetWeakPtr())); | 121 weak_factory_.GetWeakPtr())); |
98 MessageLoop::current()->RunAllPending(); | 122 MessageLoop::current()->RunAllPending(); |
99 return result_; | 123 return result_; |
100 } | 124 } |
101 | 125 |
102 PlatformFileError CannedSyncableFileSystem::TruncateFile( | 126 PlatformFileError CannedSyncableFileSystem::TruncateFile( |
103 const FileSystemURL& url, int64 size) { | 127 const FileSystemURL& url, int64 size) { |
| 128 EXPECT_TRUE(is_filesystem_opened_); |
104 result_ = base::PLATFORM_FILE_ERROR_FAILED; | 129 result_ = base::PLATFORM_FILE_ERROR_FAILED; |
105 test_helper_.NewOperation()->Truncate( | 130 test_helper_.NewOperation()->Truncate( |
106 url, size, | 131 url, size, |
107 base::Bind(&CannedSyncableFileSystem::StatusCallback, | 132 base::Bind(&CannedSyncableFileSystem::StatusCallback, |
108 weak_factory_.GetWeakPtr())); | 133 weak_factory_.GetWeakPtr())); |
109 MessageLoop::current()->RunAllPending(); | 134 MessageLoop::current()->RunAllPending(); |
110 return result_; | 135 return result_; |
111 } | 136 } |
112 | 137 |
113 PlatformFileError CannedSyncableFileSystem::Remove( | 138 PlatformFileError CannedSyncableFileSystem::Remove( |
114 const FileSystemURL& url, bool recursive) { | 139 const FileSystemURL& url, bool recursive) { |
| 140 EXPECT_TRUE(is_filesystem_opened_); |
115 result_ = base::PLATFORM_FILE_ERROR_FAILED; | 141 result_ = base::PLATFORM_FILE_ERROR_FAILED; |
116 test_helper_.NewOperation()->Remove( | 142 test_helper_.NewOperation()->Remove( |
117 url, recursive, | 143 url, recursive, |
118 base::Bind(&CannedSyncableFileSystem::StatusCallback, | 144 base::Bind(&CannedSyncableFileSystem::StatusCallback, |
119 weak_factory_.GetWeakPtr())); | 145 weak_factory_.GetWeakPtr())); |
120 MessageLoop::current()->RunAllPending(); | 146 MessageLoop::current()->RunAllPending(); |
121 return result_; | 147 return result_; |
122 } | 148 } |
123 | 149 |
124 PlatformFileError CannedSyncableFileSystem::DeleteFileSystem() { | 150 PlatformFileError CannedSyncableFileSystem::DeleteFileSystem() { |
| 151 EXPECT_TRUE(is_filesystem_set_up_); |
125 file_system_context_->DeleteFileSystem( | 152 file_system_context_->DeleteFileSystem( |
126 test_helper_.origin(), test_helper_.type(), | 153 test_helper_.origin(), test_helper_.type(), |
127 base::Bind(&CannedSyncableFileSystem::StatusCallback, | 154 base::Bind(&CannedSyncableFileSystem::StatusCallback, |
128 weak_factory_.GetWeakPtr())); | 155 weak_factory_.GetWeakPtr())); |
129 MessageLoop::current()->RunAllPending(); | 156 MessageLoop::current()->RunAllPending(); |
130 return result_; | 157 return result_; |
131 } | 158 } |
132 | 159 |
133 void CannedSyncableFileSystem::DidOpenFileSystem( | 160 void CannedSyncableFileSystem::DidOpenFileSystem( |
134 PlatformFileError result, const std::string& name, const GURL& root) { | 161 PlatformFileError result, const std::string& name, const GURL& root) { |
135 result_ = result; | 162 result_ = result; |
136 root_url_ = root; | 163 root_url_ = root; |
| 164 is_filesystem_opened_ = true; |
| 165 } |
| 166 |
| 167 void CannedSyncableFileSystem::DidInitializeFileSystemContext( |
| 168 SyncStatusCode status) { |
| 169 sync_status_ = status; |
| 170 MessageLoop::current()->Quit(); |
137 } | 171 } |
138 | 172 |
139 void CannedSyncableFileSystem::StatusCallback(PlatformFileError result) { | 173 void CannedSyncableFileSystem::StatusCallback(PlatformFileError result) { |
140 result_ = result; | 174 result_ = result; |
141 } | 175 } |
142 | 176 |
143 FileSystemOperationContext* CannedSyncableFileSystem::NewOperationContext() { | 177 FileSystemOperationContext* CannedSyncableFileSystem::NewOperationContext() { |
144 FileSystemOperationContext* context = test_helper_.NewOperationContext(); | 178 FileSystemOperationContext* context = test_helper_.NewOperationContext(); |
145 context->set_allowed_bytes_growth(kint64max); | 179 context->set_allowed_bytes_growth(kint64max); |
146 return context; | 180 return context; |
147 } | 181 } |
148 | 182 |
149 } // namespace fileapi | 183 } // namespace fileapi |
OLD | NEW |