| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" |
| 13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h" | 15 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h" |
| 15 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" | 16 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" |
| 16 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" | 17 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" |
| 17 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h" | 18 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h" |
| 18 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" | 19 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" |
| 19 #include "chrome/browser/sync_file_system/local/syncable_file_operation_runner.h
" | 20 #include "chrome/browser/sync_file_system/local/syncable_file_operation_runner.h
" |
| 20 #include "chrome/browser/sync_file_system/local/syncable_file_system_operation.h
" | 21 #include "chrome/browser/sync_file_system/local/syncable_file_system_operation.h
" |
| 21 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" | 22 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" |
| 22 #include "content/public/test/mock_blob_url_request_context.h" | 23 #include "content/public/test/mock_blob_url_request_context.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 write_bytes_ += bytes; | 125 write_bytes_ += bytes; |
| 125 write_complete_ = complete; | 126 write_complete_ = complete; |
| 126 ++callback_count_; | 127 ++callback_count_; |
| 127 } | 128 } |
| 128 | 129 |
| 129 void DidFinish(const tracked_objects::Location& location, | 130 void DidFinish(const tracked_objects::Location& location, |
| 130 File::Error expect, File::Error status) { | 131 File::Error expect, File::Error status) { |
| 131 SCOPED_TRACE(testing::Message() << location.ToString()); | 132 SCOPED_TRACE(testing::Message() << location.ToString()); |
| 132 EXPECT_EQ(expect, status); | 133 EXPECT_EQ(expect, status); |
| 133 ++callback_count_; | 134 ++callback_count_; |
| 135 base::MessageLoop::current()->Quit(); |
| 134 } | 136 } |
| 135 | 137 |
| 136 bool CreateTempFile(base::FilePath* path) { | 138 bool CreateTempFile(base::FilePath* path) { |
| 137 return base::CreateTemporaryFileInDir(dir_.path(), path); | 139 return base::CreateTemporaryFileInDir(dir_.path(), path); |
| 138 } | 140 } |
| 139 | 141 |
| 140 content::TestBrowserThreadBundle thread_bundle_; | 142 content::TestBrowserThreadBundle thread_bundle_; |
| 141 | 143 |
| 142 base::ScopedTempDir dir_; | 144 base::ScopedTempDir dir_; |
| 143 scoped_ptr<leveldb::Env> in_memory_env_; | 145 scoped_ptr<leveldb::Env> in_memory_env_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 163 ASSERT_FALSE(sync_status()->IsWritable(URL(kFile))); | 165 ASSERT_FALSE(sync_status()->IsWritable(URL(kFile))); |
| 164 | 166 |
| 165 // The URL is in syncing so the write operations won't run. | 167 // The URL is in syncing so the write operations won't run. |
| 166 ResetCallbackStatus(); | 168 ResetCallbackStatus(); |
| 167 file_system_.operation_runner()->CreateFile( | 169 file_system_.operation_runner()->CreateFile( |
| 168 URL(kFile), false /* exclusive */, | 170 URL(kFile), false /* exclusive */, |
| 169 ExpectStatus(FROM_HERE, File::FILE_OK)); | 171 ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 170 file_system_.operation_runner()->Truncate( | 172 file_system_.operation_runner()->Truncate( |
| 171 URL(kFile), 1, | 173 URL(kFile), 1, |
| 172 ExpectStatus(FROM_HERE, File::FILE_OK)); | 174 ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 173 base::MessageLoop::current()->RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 174 EXPECT_EQ(0, callback_count_); | 176 EXPECT_EQ(0, callback_count_); |
| 175 | 177 |
| 176 // Read operations are not blocked (and are executed before queued ones). | 178 // Read operations are not blocked (and are executed before queued ones). |
| 177 file_system_.operation_runner()->FileExists( | 179 file_system_.operation_runner()->FileExists( |
| 178 URL(kFile), ExpectStatus(FROM_HERE, File::FILE_ERROR_NOT_FOUND)); | 180 URL(kFile), ExpectStatus(FROM_HERE, File::FILE_ERROR_NOT_FOUND)); |
| 179 base::MessageLoop::current()->RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
| 180 EXPECT_EQ(1, callback_count_); | 182 EXPECT_EQ(1, callback_count_); |
| 181 | 183 |
| 182 // End syncing (to enable write). | 184 // End syncing (to enable write). |
| 183 sync_status()->EndSyncing(URL(kFile)); | 185 sync_status()->EndSyncing(URL(kFile)); |
| 184 ASSERT_TRUE(sync_status()->IsWritable(URL(kFile))); | 186 ASSERT_TRUE(sync_status()->IsWritable(URL(kFile))); |
| 185 | 187 |
| 186 ResetCallbackStatus(); | 188 ResetCallbackStatus(); |
| 187 base::MessageLoop::current()->RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
| 188 EXPECT_EQ(2, callback_count_); | 190 EXPECT_EQ(2, callback_count_); |
| 189 | 191 |
| 190 // Now the file must have been created and updated. | 192 // Now the file must have been created and updated. |
| 191 ResetCallbackStatus(); | 193 ResetCallbackStatus(); |
| 192 file_system_.operation_runner()->FileExists( | 194 file_system_.operation_runner()->FileExists( |
| 193 URL(kFile), ExpectStatus(FROM_HERE, File::FILE_OK)); | 195 URL(kFile), ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 194 base::MessageLoop::current()->RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
| 195 EXPECT_EQ(1, callback_count_); | 197 EXPECT_EQ(1, callback_count_); |
| 196 } | 198 } |
| 197 | 199 |
| 198 TEST_F(SyncableFileOperationRunnerTest, WriteToParentAndChild) { | 200 TEST_F(SyncableFileOperationRunnerTest, WriteToParentAndChild) { |
| 199 // First create the kDir directory and kChild in the dir. | 201 // First create the kDir directory and kChild in the dir. |
| 200 EXPECT_EQ(File::FILE_OK, file_system_.CreateDirectory(URL(kDir))); | 202 EXPECT_EQ(File::FILE_OK, file_system_.CreateDirectory(URL(kDir))); |
| 201 EXPECT_EQ(File::FILE_OK, file_system_.CreateFile(URL(kChild))); | 203 EXPECT_EQ(File::FILE_OK, file_system_.CreateFile(URL(kChild))); |
| 202 | 204 |
| 203 // Start syncing the kDir directory. | 205 // Start syncing the kDir directory. |
| 204 sync_status()->StartSyncing(URL(kDir)); | 206 sync_status()->StartSyncing(URL(kDir)); |
| 205 ASSERT_FALSE(sync_status()->IsWritable(URL(kDir))); | 207 ASSERT_FALSE(sync_status()->IsWritable(URL(kDir))); |
| 206 | 208 |
| 207 // Writes to kParent and kChild should be all queued up. | 209 // Writes to kParent and kChild should be all queued up. |
| 208 ResetCallbackStatus(); | 210 ResetCallbackStatus(); |
| 209 file_system_.operation_runner()->Truncate( | 211 file_system_.operation_runner()->Truncate( |
| 210 URL(kChild), 1, ExpectStatus(FROM_HERE, File::FILE_OK)); | 212 URL(kChild), 1, ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 211 file_system_.operation_runner()->Remove( | 213 file_system_.operation_runner()->Remove( |
| 212 URL(kParent), true /* recursive */, | 214 URL(kParent), true /* recursive */, |
| 213 ExpectStatus(FROM_HERE, File::FILE_OK)); | 215 ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 214 base::MessageLoop::current()->RunUntilIdle(); | 216 base::RunLoop().RunUntilIdle(); |
| 215 EXPECT_EQ(0, callback_count_); | 217 EXPECT_EQ(0, callback_count_); |
| 216 | 218 |
| 217 // Read operations are not blocked (and are executed before queued ones). | 219 // Read operations are not blocked (and are executed before queued ones). |
| 218 file_system_.operation_runner()->DirectoryExists( | 220 file_system_.operation_runner()->DirectoryExists( |
| 219 URL(kDir), ExpectStatus(FROM_HERE, File::FILE_OK)); | 221 URL(kDir), ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 220 base::MessageLoop::current()->RunUntilIdle(); | 222 base::RunLoop().Run(); |
| 221 EXPECT_EQ(1, callback_count_); | 223 EXPECT_EQ(1, callback_count_); |
| 222 | 224 |
| 223 // Writes to unrelated files must succeed as well. | 225 // Writes to unrelated files must succeed as well. |
| 224 ResetCallbackStatus(); | 226 ResetCallbackStatus(); |
| 225 file_system_.operation_runner()->CreateDirectory( | 227 file_system_.operation_runner()->CreateDirectory( |
| 226 URL(kOther), false /* exclusive */, false /* recursive */, | 228 URL(kOther), false /* exclusive */, false /* recursive */, |
| 227 ExpectStatus(FROM_HERE, File::FILE_OK)); | 229 ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 228 base::MessageLoop::current()->RunUntilIdle(); | 230 base::RunLoop().Run(); |
| 229 EXPECT_EQ(1, callback_count_); | 231 EXPECT_EQ(1, callback_count_); |
| 230 | 232 |
| 231 // End syncing (to enable write). | 233 // End syncing (to enable write). |
| 232 sync_status()->EndSyncing(URL(kDir)); | 234 sync_status()->EndSyncing(URL(kDir)); |
| 233 ASSERT_TRUE(sync_status()->IsWritable(URL(kDir))); | 235 ASSERT_TRUE(sync_status()->IsWritable(URL(kDir))); |
| 234 | 236 |
| 235 ResetCallbackStatus(); | 237 ResetCallbackStatus(); |
| 236 base::MessageLoop::current()->RunUntilIdle(); | 238 base::RunLoop().Run(); |
| 237 EXPECT_EQ(2, callback_count_); | 239 EXPECT_EQ(2, callback_count_); |
| 238 } | 240 } |
| 239 | 241 |
| 240 TEST_F(SyncableFileOperationRunnerTest, CopyAndMove) { | 242 TEST_F(SyncableFileOperationRunnerTest, CopyAndMove) { |
| 241 // First create the kDir directory and kChild in the dir. | 243 // First create the kDir directory and kChild in the dir. |
| 242 EXPECT_EQ(File::FILE_OK, file_system_.CreateDirectory(URL(kDir))); | 244 EXPECT_EQ(File::FILE_OK, file_system_.CreateDirectory(URL(kDir))); |
| 243 EXPECT_EQ(File::FILE_OK, file_system_.CreateFile(URL(kChild))); | 245 EXPECT_EQ(File::FILE_OK, file_system_.CreateFile(URL(kChild))); |
| 244 | 246 |
| 245 // Start syncing the kParent directory. | 247 // Start syncing the kParent directory. |
| 246 sync_status()->StartSyncing(URL(kParent)); | 248 sync_status()->StartSyncing(URL(kParent)); |
| 247 | 249 |
| 248 // Copying kDir to other directory should succeed, while moving would fail | 250 // Copying kDir to other directory should succeed, while moving would fail |
| 249 // (since the source directory is in syncing). | 251 // (since the source directory is in syncing). |
| 250 ResetCallbackStatus(); | 252 ResetCallbackStatus(); |
| 251 file_system_.operation_runner()->Copy( | 253 file_system_.operation_runner()->Copy( |
| 252 URL(kDir), | 254 URL(kDir), |
| 253 URL("dest-copy"), | 255 URL("dest-copy"), |
| 254 storage::FileSystemOperation::OPTION_NONE, | 256 storage::FileSystemOperation::OPTION_NONE, |
| 255 storage::FileSystemOperationRunner::CopyProgressCallback(), | 257 storage::FileSystemOperationRunner::CopyProgressCallback(), |
| 256 ExpectStatus(FROM_HERE, File::FILE_OK)); | 258 ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 257 file_system_.operation_runner()->Move( | 259 file_system_.operation_runner()->Move( |
| 258 URL(kDir), | 260 URL(kDir), |
| 259 URL("dest-move"), | 261 URL("dest-move"), |
| 260 storage::FileSystemOperation::OPTION_NONE, | 262 storage::FileSystemOperation::OPTION_NONE, |
| 261 ExpectStatus(FROM_HERE, File::FILE_OK)); | 263 ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 262 base::MessageLoop::current()->RunUntilIdle(); | 264 base::RunLoop().Run(); |
| 263 EXPECT_EQ(1, callback_count_); | 265 EXPECT_EQ(1, callback_count_); |
| 264 | 266 |
| 265 // Only "dest-copy1" should exist. | 267 // Only "dest-copy1" should exist. |
| 266 EXPECT_EQ(File::FILE_OK, | 268 EXPECT_EQ(File::FILE_OK, |
| 267 file_system_.DirectoryExists(URL("dest-copy"))); | 269 file_system_.DirectoryExists(URL("dest-copy"))); |
| 268 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, | 270 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, |
| 269 file_system_.DirectoryExists(URL("dest-move"))); | 271 file_system_.DirectoryExists(URL("dest-move"))); |
| 270 | 272 |
| 271 // Start syncing the "dest-copy2" directory. | 273 // Start syncing the "dest-copy2" directory. |
| 272 sync_status()->StartSyncing(URL("dest-copy2")); | 274 sync_status()->StartSyncing(URL("dest-copy2")); |
| 273 | 275 |
| 274 // Now the destination is also locked copying kDir should be queued. | 276 // Now the destination is also locked copying kDir should be queued. |
| 275 ResetCallbackStatus(); | 277 ResetCallbackStatus(); |
| 276 file_system_.operation_runner()->Copy( | 278 file_system_.operation_runner()->Copy( |
| 277 URL(kDir), | 279 URL(kDir), |
| 278 URL("dest-copy2"), | 280 URL("dest-copy2"), |
| 279 storage::FileSystemOperation::OPTION_NONE, | 281 storage::FileSystemOperation::OPTION_NONE, |
| 280 storage::FileSystemOperationRunner::CopyProgressCallback(), | 282 storage::FileSystemOperationRunner::CopyProgressCallback(), |
| 281 ExpectStatus(FROM_HERE, File::FILE_OK)); | 283 ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 282 base::MessageLoop::current()->RunUntilIdle(); | 284 base::RunLoop().RunUntilIdle(); |
| 283 EXPECT_EQ(0, callback_count_); | 285 EXPECT_EQ(0, callback_count_); |
| 284 | 286 |
| 285 // Finish syncing the "dest-copy2" directory to unlock Copy. | 287 // Finish syncing the "dest-copy2" directory to unlock Copy. |
| 286 sync_status()->EndSyncing(URL("dest-copy2")); | 288 sync_status()->EndSyncing(URL("dest-copy2")); |
| 287 ResetCallbackStatus(); | 289 ResetCallbackStatus(); |
| 288 base::MessageLoop::current()->RunUntilIdle(); | 290 base::RunLoop().Run(); |
| 289 EXPECT_EQ(1, callback_count_); | 291 EXPECT_EQ(1, callback_count_); |
| 290 | 292 |
| 291 // Now we should have "dest-copy2". | 293 // Now we should have "dest-copy2". |
| 292 EXPECT_EQ(File::FILE_OK, | 294 EXPECT_EQ(File::FILE_OK, |
| 293 file_system_.DirectoryExists(URL("dest-copy2"))); | 295 file_system_.DirectoryExists(URL("dest-copy2"))); |
| 294 | 296 |
| 295 // Finish syncing the kParent to unlock Move. | 297 // Finish syncing the kParent to unlock Move. |
| 296 sync_status()->EndSyncing(URL(kParent)); | 298 sync_status()->EndSyncing(URL(kParent)); |
| 297 ResetCallbackStatus(); | 299 ResetCallbackStatus(); |
| 298 base::MessageLoop::current()->RunUntilIdle(); | 300 base::RunLoop().Run(); |
| 299 EXPECT_EQ(1, callback_count_); | 301 EXPECT_EQ(1, callback_count_); |
| 300 | 302 |
| 301 // Now we should have "dest-move". | 303 // Now we should have "dest-move". |
| 302 EXPECT_EQ(File::FILE_OK, | 304 EXPECT_EQ(File::FILE_OK, |
| 303 file_system_.DirectoryExists(URL("dest-move"))); | 305 file_system_.DirectoryExists(URL("dest-move"))); |
| 304 } | 306 } |
| 305 | 307 |
| 306 TEST_F(SyncableFileOperationRunnerTest, Write) { | 308 TEST_F(SyncableFileOperationRunnerTest, Write) { |
| 307 EXPECT_EQ(File::FILE_OK, file_system_.CreateFile(URL(kFile))); | 309 EXPECT_EQ(File::FILE_OK, file_system_.CreateFile(URL(kFile))); |
| 308 const std::string kData("Lorem ipsum."); | 310 const std::string kData("Lorem ipsum."); |
| 309 ScopedTextBlob blob(url_request_context_, "blob:foo", kData); | 311 ScopedTextBlob blob(url_request_context_, "blob:foo", kData); |
| 310 | 312 |
| 311 sync_status()->StartSyncing(URL(kFile)); | 313 sync_status()->StartSyncing(URL(kFile)); |
| 312 | 314 |
| 313 ResetCallbackStatus(); | 315 ResetCallbackStatus(); |
| 314 file_system_.operation_runner()->Write( | 316 file_system_.operation_runner()->Write( |
| 315 &url_request_context_, | 317 &url_request_context_, |
| 316 URL(kFile), blob.GetBlobDataHandle(), 0, GetWriteCallback(FROM_HERE)); | 318 URL(kFile), blob.GetBlobDataHandle(), 0, GetWriteCallback(FROM_HERE)); |
| 317 base::MessageLoop::current()->RunUntilIdle(); | 319 base::RunLoop().RunUntilIdle(); |
| 318 EXPECT_EQ(0, callback_count_); | 320 EXPECT_EQ(0, callback_count_); |
| 319 | 321 |
| 320 sync_status()->EndSyncing(URL(kFile)); | 322 sync_status()->EndSyncing(URL(kFile)); |
| 321 ResetCallbackStatus(); | 323 ResetCallbackStatus(); |
| 322 | 324 |
| 323 while (!write_complete_) | 325 while (!write_complete_) |
| 324 base::MessageLoop::current()->RunUntilIdle(); | 326 base::MessageLoop::current()->RunUntilIdle(); |
| 325 | 327 |
| 326 EXPECT_EQ(File::FILE_OK, write_status_); | 328 EXPECT_EQ(File::FILE_OK, write_status_); |
| 327 EXPECT_EQ(kData.size(), write_bytes_); | 329 EXPECT_EQ(kData.size(), write_bytes_); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 base::MessageLoop::current()->RunUntilIdle(); | 399 base::MessageLoop::current()->RunUntilIdle(); |
| 398 EXPECT_EQ(1, callback_count_); | 400 EXPECT_EQ(1, callback_count_); |
| 399 | 401 |
| 400 // Run Truncate and immediately cancel. This shouldn't crash. | 402 // Run Truncate and immediately cancel. This shouldn't crash. |
| 401 ResetCallbackStatus(); | 403 ResetCallbackStatus(); |
| 402 storage::FileSystemOperationRunner::OperationID id = | 404 storage::FileSystemOperationRunner::OperationID id = |
| 403 file_system_.operation_runner()->Truncate( | 405 file_system_.operation_runner()->Truncate( |
| 404 URL(kFile), 10, ExpectStatus(FROM_HERE, File::FILE_OK)); | 406 URL(kFile), 10, ExpectStatus(FROM_HERE, File::FILE_OK)); |
| 405 file_system_.operation_runner()->Cancel( | 407 file_system_.operation_runner()->Cancel( |
| 406 id, ExpectStatus(FROM_HERE, File::FILE_ERROR_INVALID_OPERATION)); | 408 id, ExpectStatus(FROM_HERE, File::FILE_ERROR_INVALID_OPERATION)); |
| 407 base::MessageLoop::current()->RunUntilIdle(); | 409 base::RunLoop().Run(); |
| 408 EXPECT_EQ(2, callback_count_); | 410 EXPECT_EQ(2, callback_count_); |
| 409 } | 411 } |
| 410 | 412 |
| 411 } // namespace sync_file_system | 413 } // namespace sync_file_system |
| OLD | NEW |