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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/location.h" | 6 #include "base/location.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "webkit/blob/mock_blob_url_request_context.h" | 10 #include "webkit/blob/mock_blob_url_request_context.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 file_system_.TearDown(); | 68 file_system_.TearDown(); |
69 message_loop_.RunAllPending(); | 69 message_loop_.RunAllPending(); |
70 RevokeSyncableFileSystem(kServiceName); | 70 RevokeSyncableFileSystem(kServiceName); |
71 } | 71 } |
72 | 72 |
73 FileSystemURL URL(const std::string& path) { | 73 FileSystemURL URL(const std::string& path) { |
74 return file_system_.URL(path); | 74 return file_system_.URL(path); |
75 } | 75 } |
76 | 76 |
77 LocalFileSyncContext* sync_context() { | |
78 return file_system_.file_system_context()->sync_context(); | |
79 } | |
80 | |
81 SyncableFileOperationRunner* operation_runner() { | |
82 return sync_context()->operation_runner().get(); | |
83 } | |
84 | |
85 LocalFileSyncStatus* sync_status() { | 77 LocalFileSyncStatus* sync_status() { |
86 return operation_runner()->sync_status(); | 78 return file_system_.file_system_context()->sync_context()->sync_status(); |
87 } | 79 } |
88 | 80 |
89 void ResetCallbackStatus() { | 81 void ResetCallbackStatus() { |
90 write_status_ = base::PLATFORM_FILE_ERROR_FAILED; | 82 write_status_ = base::PLATFORM_FILE_ERROR_FAILED; |
91 write_bytes_ = 0; | 83 write_bytes_ = 0; |
92 write_complete_ = false; | 84 write_complete_ = false; |
93 callback_count_ = 0; | 85 callback_count_ = 0; |
94 } | 86 } |
95 | 87 |
96 StatusCallback ExpectStatus(const tracked_objects::Location& location, | 88 StatusCallback ExpectStatus(const tracked_objects::Location& location, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 file_system_.NewOperation()->FileExists( | 148 file_system_.NewOperation()->FileExists( |
157 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_NOT_FOUND)); | 149 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_NOT_FOUND)); |
158 MessageLoop::current()->RunAllPending(); | 150 MessageLoop::current()->RunAllPending(); |
159 EXPECT_EQ(1, callback_count_); | 151 EXPECT_EQ(1, callback_count_); |
160 | 152 |
161 // End syncing (to enable write). | 153 // End syncing (to enable write). |
162 sync_status()->EndSyncing(URL(kFile)); | 154 sync_status()->EndSyncing(URL(kFile)); |
163 ASSERT_TRUE(sync_status()->IsWritable(URL(kFile))); | 155 ASSERT_TRUE(sync_status()->IsWritable(URL(kFile))); |
164 | 156 |
165 ResetCallbackStatus(); | 157 ResetCallbackStatus(); |
166 operation_runner()->RunNextRunnableTask(); | |
167 MessageLoop::current()->RunAllPending(); | 158 MessageLoop::current()->RunAllPending(); |
168 EXPECT_EQ(2, callback_count_); | 159 EXPECT_EQ(2, callback_count_); |
169 | 160 |
170 // Now the file must have been created and updated. | 161 // Now the file must have been created and updated. |
171 ResetCallbackStatus(); | 162 ResetCallbackStatus(); |
172 file_system_.NewOperation()->FileExists( | 163 file_system_.NewOperation()->FileExists( |
173 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 164 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
174 MessageLoop::current()->RunAllPending(); | 165 MessageLoop::current()->RunAllPending(); |
175 EXPECT_EQ(1, callback_count_); | 166 EXPECT_EQ(1, callback_count_); |
176 } | 167 } |
177 | 168 |
178 TEST_F(SyncableFileOperationRunnerTest, WriteToParentAndChild) { | 169 TEST_F(SyncableFileOperationRunnerTest, WriteToParentAndChild) { |
179 // First create the kDir directory and kChild in the dir. | 170 // First create the kDir directory and kChild in the dir. |
180 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); | 171 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); |
181 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); | 172 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); |
182 | 173 |
183 // Start syncing the kDir directory. | 174 // Start syncing the kDir directory. |
184 sync_status()->StartSyncing(URL(kDir)); | 175 sync_status()->StartSyncing(URL(kDir)); |
185 ASSERT_FALSE(sync_status()->IsWritable(URL(kDir))); | 176 ASSERT_FALSE(sync_status()->IsWritable(URL(kDir))); |
186 | 177 |
187 // Writes to kParent, kDir and kChild should be all queued up. | 178 // Writes to kParent and kChild should be all queued up. |
188 ResetCallbackStatus(); | 179 ResetCallbackStatus(); |
189 file_system_.NewOperation()->Truncate( | 180 file_system_.NewOperation()->Truncate( |
190 URL(kChild), 1, ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 181 URL(kChild), 1, ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
191 file_system_.NewOperation()->Remove( | 182 file_system_.NewOperation()->Remove( |
192 URL(kDir), true /* recursive */, | |
193 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | |
194 file_system_.NewOperation()->Remove( | |
195 URL(kParent), true /* recursive */, | 183 URL(kParent), true /* recursive */, |
196 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 184 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
197 MessageLoop::current()->RunAllPending(); | 185 MessageLoop::current()->RunAllPending(); |
198 EXPECT_EQ(0, callback_count_); | 186 EXPECT_EQ(0, callback_count_); |
199 | 187 |
200 // Read operations are not blocked (and are executed before queued ones). | 188 // Read operations are not blocked (and are executed before queued ones). |
201 file_system_.NewOperation()->DirectoryExists( | 189 file_system_.NewOperation()->DirectoryExists( |
202 URL(kDir), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 190 URL(kDir), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
203 MessageLoop::current()->RunAllPending(); | 191 MessageLoop::current()->RunAllPending(); |
204 EXPECT_EQ(1, callback_count_); | 192 EXPECT_EQ(1, callback_count_); |
205 | 193 |
206 // Writes to unrelated files must succeed as well. | 194 // Writes to unrelated files must succeed as well. |
207 ResetCallbackStatus(); | 195 ResetCallbackStatus(); |
208 file_system_.NewOperation()->CreateDirectory( | 196 file_system_.NewOperation()->CreateDirectory( |
209 URL(kOther), false /* exclusive */, false /* recursive */, | 197 URL(kOther), false /* exclusive */, false /* recursive */, |
210 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 198 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
211 MessageLoop::current()->RunAllPending(); | 199 MessageLoop::current()->RunAllPending(); |
212 EXPECT_EQ(1, callback_count_); | 200 EXPECT_EQ(1, callback_count_); |
213 | 201 |
214 // End syncing (to enable write). | 202 // End syncing (to enable write). |
215 sync_status()->EndSyncing(URL(kDir)); | 203 sync_status()->EndSyncing(URL(kDir)); |
216 ASSERT_TRUE(sync_status()->IsWritable(URL(kDir))); | 204 ASSERT_TRUE(sync_status()->IsWritable(URL(kDir))); |
217 | 205 |
218 ResetCallbackStatus(); | 206 ResetCallbackStatus(); |
219 operation_runner()->RunNextRunnableTask(); | |
220 MessageLoop::current()->RunAllPending(); | 207 MessageLoop::current()->RunAllPending(); |
221 EXPECT_EQ(3, callback_count_); | 208 EXPECT_EQ(2, callback_count_); |
222 } | 209 } |
223 | 210 |
224 TEST_F(SyncableFileOperationRunnerTest, CopyAndMove) { | 211 TEST_F(SyncableFileOperationRunnerTest, CopyAndMove) { |
225 // First create the kDir directory and kChild in the dir. | 212 // First create the kDir directory and kChild in the dir. |
226 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); | 213 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); |
227 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); | 214 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); |
228 | 215 |
229 // Start syncing the kParent directory. | 216 // Start syncing the kParent directory. |
230 sync_status()->StartSyncing(URL(kParent)); | 217 sync_status()->StartSyncing(URL(kParent)); |
231 | 218 |
(...skipping 22 matching lines...) Expand all Loading... |
254 ResetCallbackStatus(); | 241 ResetCallbackStatus(); |
255 file_system_.NewOperation()->Copy( | 242 file_system_.NewOperation()->Copy( |
256 URL(kDir), URL("dest-copy2"), | 243 URL(kDir), URL("dest-copy2"), |
257 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 244 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
258 MessageLoop::current()->RunAllPending(); | 245 MessageLoop::current()->RunAllPending(); |
259 EXPECT_EQ(0, callback_count_); | 246 EXPECT_EQ(0, callback_count_); |
260 | 247 |
261 // Finish syncing the "dest-copy2" directory to unlock Copy. | 248 // Finish syncing the "dest-copy2" directory to unlock Copy. |
262 sync_status()->EndSyncing(URL("dest-copy2")); | 249 sync_status()->EndSyncing(URL("dest-copy2")); |
263 ResetCallbackStatus(); | 250 ResetCallbackStatus(); |
264 operation_runner()->RunNextRunnableTask(); | |
265 MessageLoop::current()->RunAllPending(); | 251 MessageLoop::current()->RunAllPending(); |
266 EXPECT_EQ(1, callback_count_); | 252 EXPECT_EQ(1, callback_count_); |
267 | 253 |
268 // Now we should have "dest-copy2". | 254 // Now we should have "dest-copy2". |
269 EXPECT_EQ(base::PLATFORM_FILE_OK, | 255 EXPECT_EQ(base::PLATFORM_FILE_OK, |
270 file_system_.DirectoryExists(URL("dest-copy2"))); | 256 file_system_.DirectoryExists(URL("dest-copy2"))); |
271 | 257 |
272 // Finish syncing the kParent to unlock Move. | 258 // Finish syncing the kParent to unlock Move. |
273 sync_status()->EndSyncing(URL(kParent)); | 259 sync_status()->EndSyncing(URL(kParent)); |
274 ResetCallbackStatus(); | 260 ResetCallbackStatus(); |
275 operation_runner()->RunNextRunnableTask(); | |
276 MessageLoop::current()->RunAllPending(); | 261 MessageLoop::current()->RunAllPending(); |
277 EXPECT_EQ(1, callback_count_); | 262 EXPECT_EQ(1, callback_count_); |
278 | 263 |
279 // Now we should have "dest-move". | 264 // Now we should have "dest-move". |
280 EXPECT_EQ(base::PLATFORM_FILE_OK, | 265 EXPECT_EQ(base::PLATFORM_FILE_OK, |
281 file_system_.DirectoryExists(URL("dest-move"))); | 266 file_system_.DirectoryExists(URL("dest-move"))); |
282 } | 267 } |
283 | 268 |
284 TEST_F(SyncableFileOperationRunnerTest, Write) { | 269 TEST_F(SyncableFileOperationRunnerTest, Write) { |
285 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kFile))); | 270 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kFile))); |
286 const GURL kBlobURL("blob:foo"); | 271 const GURL kBlobURL("blob:foo"); |
287 const std::string kData("Lorem ipsum."); | 272 const std::string kData("Lorem ipsum."); |
288 ScopedTextBlob blob(url_request_context_, kBlobURL, kData); | 273 ScopedTextBlob blob(url_request_context_, kBlobURL, kData); |
289 | 274 |
290 sync_status()->StartSyncing(URL(kFile)); | 275 sync_status()->StartSyncing(URL(kFile)); |
291 | 276 |
292 ResetCallbackStatus(); | 277 ResetCallbackStatus(); |
293 file_system_.NewOperation()->Write( | 278 file_system_.NewOperation()->Write( |
294 &url_request_context_, | 279 &url_request_context_, |
295 URL(kFile), kBlobURL, 0, GetWriteCallback(FROM_HERE)); | 280 URL(kFile), kBlobURL, 0, GetWriteCallback(FROM_HERE)); |
296 MessageLoop::current()->RunAllPending(); | 281 MessageLoop::current()->RunAllPending(); |
297 EXPECT_EQ(0, callback_count_); | 282 EXPECT_EQ(0, callback_count_); |
298 | 283 |
299 sync_status()->EndSyncing(URL(kFile)); | 284 sync_status()->EndSyncing(URL(kFile)); |
300 ResetCallbackStatus(); | 285 ResetCallbackStatus(); |
301 operation_runner()->RunNextRunnableTask(); | |
302 | 286 |
303 while (!write_complete_) | 287 while (!write_complete_) |
304 MessageLoop::current()->RunAllPending(); | 288 MessageLoop::current()->RunAllPending(); |
305 | 289 |
306 EXPECT_EQ(base::PLATFORM_FILE_OK, write_status_); | 290 EXPECT_EQ(base::PLATFORM_FILE_OK, write_status_); |
307 EXPECT_EQ(kData.size(), write_bytes_); | 291 EXPECT_EQ(kData.size(), write_bytes_); |
308 EXPECT_TRUE(write_complete_); | 292 EXPECT_TRUE(write_complete_); |
309 } | 293 } |
310 | 294 |
311 TEST_F(SyncableFileOperationRunnerTest, QueueAndCancel) { | 295 TEST_F(SyncableFileOperationRunnerTest, QueueAndCancel) { |
(...skipping 13 matching lines...) Expand all Loading... |
325 ResetCallbackStatus(); | 309 ResetCallbackStatus(); |
326 | 310 |
327 // This shouldn't crash nor leak memory. | 311 // This shouldn't crash nor leak memory. |
328 sync_context_->ShutdownOnUIThread(); | 312 sync_context_->ShutdownOnUIThread(); |
329 sync_context_ = NULL; | 313 sync_context_ = NULL; |
330 MessageLoop::current()->RunAllPending(); | 314 MessageLoop::current()->RunAllPending(); |
331 EXPECT_EQ(2, callback_count_); | 315 EXPECT_EQ(2, callback_count_); |
332 } | 316 } |
333 | 317 |
334 } // namespace fileapi | 318 } // namespace fileapi |
OLD | NEW |