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