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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 ASSERT_EQ(base::PLATFORM_FILE_OK, | 63 ASSERT_EQ(base::PLATFORM_FILE_OK, |
64 file_system_.CreateDirectory(URL(kParent))); | 64 file_system_.CreateDirectory(URL(kParent))); |
65 } | 65 } |
66 | 66 |
67 virtual void TearDown() OVERRIDE { | 67 virtual void TearDown() OVERRIDE { |
68 if (sync_context_) | 68 if (sync_context_) |
69 sync_context_->ShutdownOnUIThread(); | 69 sync_context_->ShutdownOnUIThread(); |
70 sync_context_ = NULL; | 70 sync_context_ = NULL; |
71 | 71 |
72 file_system_.TearDown(); | 72 file_system_.TearDown(); |
73 message_loop_.RunAllPending(); | 73 message_loop_.RunUntilIdle(); |
74 RevokeSyncableFileSystem(kServiceName); | 74 RevokeSyncableFileSystem(kServiceName); |
75 } | 75 } |
76 | 76 |
77 FileSystemURL URL(const std::string& path) { | 77 FileSystemURL URL(const std::string& path) { |
78 return file_system_.URL(path); | 78 return file_system_.URL(path); |
79 } | 79 } |
80 | 80 |
81 LocalFileSyncStatus* sync_status() { | 81 LocalFileSyncStatus* sync_status() { |
82 return file_system_.file_system_context()->sync_context()->sync_status(); | 82 return file_system_.file_system_context()->sync_context()->sync_status(); |
83 } | 83 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 ASSERT_FALSE(sync_status()->IsWritable(URL(kFile))); | 138 ASSERT_FALSE(sync_status()->IsWritable(URL(kFile))); |
139 | 139 |
140 // The URL is in syncing so the write operations won't run. | 140 // The URL is in syncing so the write operations won't run. |
141 ResetCallbackStatus(); | 141 ResetCallbackStatus(); |
142 file_system_.NewOperation()->CreateFile( | 142 file_system_.NewOperation()->CreateFile( |
143 URL(kFile), false /* exclusive */, | 143 URL(kFile), false /* exclusive */, |
144 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 144 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
145 file_system_.NewOperation()->Truncate( | 145 file_system_.NewOperation()->Truncate( |
146 URL(kFile), 1, | 146 URL(kFile), 1, |
147 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 147 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
148 MessageLoop::current()->RunAllPending(); | 148 MessageLoop::current()->RunUntilIdle(); |
149 EXPECT_EQ(0, callback_count_); | 149 EXPECT_EQ(0, callback_count_); |
150 | 150 |
151 // Read operations are not blocked (and are executed before queued ones). | 151 // Read operations are not blocked (and are executed before queued ones). |
152 file_system_.NewOperation()->FileExists( | 152 file_system_.NewOperation()->FileExists( |
153 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_NOT_FOUND)); | 153 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_NOT_FOUND)); |
154 MessageLoop::current()->RunAllPending(); | 154 MessageLoop::current()->RunUntilIdle(); |
155 EXPECT_EQ(1, callback_count_); | 155 EXPECT_EQ(1, callback_count_); |
156 | 156 |
157 // End syncing (to enable write). | 157 // End syncing (to enable write). |
158 sync_status()->EndSyncing(URL(kFile)); | 158 sync_status()->EndSyncing(URL(kFile)); |
159 ASSERT_TRUE(sync_status()->IsWritable(URL(kFile))); | 159 ASSERT_TRUE(sync_status()->IsWritable(URL(kFile))); |
160 | 160 |
161 ResetCallbackStatus(); | 161 ResetCallbackStatus(); |
162 MessageLoop::current()->RunAllPending(); | 162 MessageLoop::current()->RunUntilIdle(); |
163 EXPECT_EQ(2, callback_count_); | 163 EXPECT_EQ(2, callback_count_); |
164 | 164 |
165 // Now the file must have been created and updated. | 165 // Now the file must have been created and updated. |
166 ResetCallbackStatus(); | 166 ResetCallbackStatus(); |
167 file_system_.NewOperation()->FileExists( | 167 file_system_.NewOperation()->FileExists( |
168 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 168 URL(kFile), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
169 MessageLoop::current()->RunAllPending(); | 169 MessageLoop::current()->RunUntilIdle(); |
170 EXPECT_EQ(1, callback_count_); | 170 EXPECT_EQ(1, callback_count_); |
171 } | 171 } |
172 | 172 |
173 TEST_F(SyncableFileOperationRunnerTest, WriteToParentAndChild) { | 173 TEST_F(SyncableFileOperationRunnerTest, WriteToParentAndChild) { |
174 // First create the kDir directory and kChild in the dir. | 174 // First create the kDir directory and kChild in the dir. |
175 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); | 175 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); |
176 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); | 176 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); |
177 | 177 |
178 // Start syncing the kDir directory. | 178 // Start syncing the kDir directory. |
179 sync_status()->StartSyncing(URL(kDir)); | 179 sync_status()->StartSyncing(URL(kDir)); |
180 ASSERT_FALSE(sync_status()->IsWritable(URL(kDir))); | 180 ASSERT_FALSE(sync_status()->IsWritable(URL(kDir))); |
181 | 181 |
182 // Writes to kParent and kChild should be all queued up. | 182 // Writes to kParent and kChild should be all queued up. |
183 ResetCallbackStatus(); | 183 ResetCallbackStatus(); |
184 file_system_.NewOperation()->Truncate( | 184 file_system_.NewOperation()->Truncate( |
185 URL(kChild), 1, ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 185 URL(kChild), 1, ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
186 file_system_.NewOperation()->Remove( | 186 file_system_.NewOperation()->Remove( |
187 URL(kParent), true /* recursive */, | 187 URL(kParent), true /* recursive */, |
188 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 188 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
189 MessageLoop::current()->RunAllPending(); | 189 MessageLoop::current()->RunUntilIdle(); |
190 EXPECT_EQ(0, callback_count_); | 190 EXPECT_EQ(0, callback_count_); |
191 | 191 |
192 // Read operations are not blocked (and are executed before queued ones). | 192 // Read operations are not blocked (and are executed before queued ones). |
193 file_system_.NewOperation()->DirectoryExists( | 193 file_system_.NewOperation()->DirectoryExists( |
194 URL(kDir), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 194 URL(kDir), ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
195 MessageLoop::current()->RunAllPending(); | 195 MessageLoop::current()->RunUntilIdle(); |
196 EXPECT_EQ(1, callback_count_); | 196 EXPECT_EQ(1, callback_count_); |
197 | 197 |
198 // Writes to unrelated files must succeed as well. | 198 // Writes to unrelated files must succeed as well. |
199 ResetCallbackStatus(); | 199 ResetCallbackStatus(); |
200 file_system_.NewOperation()->CreateDirectory( | 200 file_system_.NewOperation()->CreateDirectory( |
201 URL(kOther), false /* exclusive */, false /* recursive */, | 201 URL(kOther), false /* exclusive */, false /* recursive */, |
202 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 202 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
203 MessageLoop::current()->RunAllPending(); | 203 MessageLoop::current()->RunUntilIdle(); |
204 EXPECT_EQ(1, callback_count_); | 204 EXPECT_EQ(1, callback_count_); |
205 | 205 |
206 // End syncing (to enable write). | 206 // End syncing (to enable write). |
207 sync_status()->EndSyncing(URL(kDir)); | 207 sync_status()->EndSyncing(URL(kDir)); |
208 ASSERT_TRUE(sync_status()->IsWritable(URL(kDir))); | 208 ASSERT_TRUE(sync_status()->IsWritable(URL(kDir))); |
209 | 209 |
210 ResetCallbackStatus(); | 210 ResetCallbackStatus(); |
211 MessageLoop::current()->RunAllPending(); | 211 MessageLoop::current()->RunUntilIdle(); |
212 EXPECT_EQ(2, callback_count_); | 212 EXPECT_EQ(2, callback_count_); |
213 } | 213 } |
214 | 214 |
215 TEST_F(SyncableFileOperationRunnerTest, CopyAndMove) { | 215 TEST_F(SyncableFileOperationRunnerTest, CopyAndMove) { |
216 // First create the kDir directory and kChild in the dir. | 216 // First create the kDir directory and kChild in the dir. |
217 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); | 217 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateDirectory(URL(kDir))); |
218 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); | 218 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kChild))); |
219 | 219 |
220 // Start syncing the kParent directory. | 220 // Start syncing the kParent directory. |
221 sync_status()->StartSyncing(URL(kParent)); | 221 sync_status()->StartSyncing(URL(kParent)); |
222 | 222 |
223 // Copying kDir to other directory should succeed, while moving would fail | 223 // Copying kDir to other directory should succeed, while moving would fail |
224 // (since the source directory is in syncing). | 224 // (since the source directory is in syncing). |
225 ResetCallbackStatus(); | 225 ResetCallbackStatus(); |
226 file_system_.NewOperation()->Copy( | 226 file_system_.NewOperation()->Copy( |
227 URL(kDir), URL("dest-copy"), | 227 URL(kDir), URL("dest-copy"), |
228 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 228 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
229 file_system_.NewOperation()->Move( | 229 file_system_.NewOperation()->Move( |
230 URL(kDir), URL("dest-move"), | 230 URL(kDir), URL("dest-move"), |
231 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 231 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
232 MessageLoop::current()->RunAllPending(); | 232 MessageLoop::current()->RunUntilIdle(); |
233 EXPECT_EQ(1, callback_count_); | 233 EXPECT_EQ(1, callback_count_); |
234 | 234 |
235 // Only "dest-copy1" should exist. | 235 // Only "dest-copy1" should exist. |
236 EXPECT_EQ(base::PLATFORM_FILE_OK, | 236 EXPECT_EQ(base::PLATFORM_FILE_OK, |
237 file_system_.DirectoryExists(URL("dest-copy"))); | 237 file_system_.DirectoryExists(URL("dest-copy"))); |
238 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 238 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
239 file_system_.DirectoryExists(URL("dest-move"))); | 239 file_system_.DirectoryExists(URL("dest-move"))); |
240 | 240 |
241 // Start syncing the "dest-copy2" directory. | 241 // Start syncing the "dest-copy2" directory. |
242 sync_status()->StartSyncing(URL("dest-copy2")); | 242 sync_status()->StartSyncing(URL("dest-copy2")); |
243 | 243 |
244 // Now the destination is also locked copying kDir should be queued. | 244 // Now the destination is also locked copying kDir should be queued. |
245 ResetCallbackStatus(); | 245 ResetCallbackStatus(); |
246 file_system_.NewOperation()->Copy( | 246 file_system_.NewOperation()->Copy( |
247 URL(kDir), URL("dest-copy2"), | 247 URL(kDir), URL("dest-copy2"), |
248 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); | 248 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_OK)); |
249 MessageLoop::current()->RunAllPending(); | 249 MessageLoop::current()->RunUntilIdle(); |
250 EXPECT_EQ(0, callback_count_); | 250 EXPECT_EQ(0, callback_count_); |
251 | 251 |
252 // Finish syncing the "dest-copy2" directory to unlock Copy. | 252 // Finish syncing the "dest-copy2" directory to unlock Copy. |
253 sync_status()->EndSyncing(URL("dest-copy2")); | 253 sync_status()->EndSyncing(URL("dest-copy2")); |
254 ResetCallbackStatus(); | 254 ResetCallbackStatus(); |
255 MessageLoop::current()->RunAllPending(); | 255 MessageLoop::current()->RunUntilIdle(); |
256 EXPECT_EQ(1, callback_count_); | 256 EXPECT_EQ(1, callback_count_); |
257 | 257 |
258 // Now we should have "dest-copy2". | 258 // Now we should have "dest-copy2". |
259 EXPECT_EQ(base::PLATFORM_FILE_OK, | 259 EXPECT_EQ(base::PLATFORM_FILE_OK, |
260 file_system_.DirectoryExists(URL("dest-copy2"))); | 260 file_system_.DirectoryExists(URL("dest-copy2"))); |
261 | 261 |
262 // Finish syncing the kParent to unlock Move. | 262 // Finish syncing the kParent to unlock Move. |
263 sync_status()->EndSyncing(URL(kParent)); | 263 sync_status()->EndSyncing(URL(kParent)); |
264 ResetCallbackStatus(); | 264 ResetCallbackStatus(); |
265 MessageLoop::current()->RunAllPending(); | 265 MessageLoop::current()->RunUntilIdle(); |
266 EXPECT_EQ(1, callback_count_); | 266 EXPECT_EQ(1, callback_count_); |
267 | 267 |
268 // Now we should have "dest-move". | 268 // Now we should have "dest-move". |
269 EXPECT_EQ(base::PLATFORM_FILE_OK, | 269 EXPECT_EQ(base::PLATFORM_FILE_OK, |
270 file_system_.DirectoryExists(URL("dest-move"))); | 270 file_system_.DirectoryExists(URL("dest-move"))); |
271 } | 271 } |
272 | 272 |
273 TEST_F(SyncableFileOperationRunnerTest, Write) { | 273 TEST_F(SyncableFileOperationRunnerTest, Write) { |
274 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kFile))); | 274 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_.CreateFile(URL(kFile))); |
275 const GURL kBlobURL("blob:foo"); | 275 const GURL kBlobURL("blob:foo"); |
276 const std::string kData("Lorem ipsum."); | 276 const std::string kData("Lorem ipsum."); |
277 ScopedTextBlob blob(url_request_context_, kBlobURL, kData); | 277 ScopedTextBlob blob(url_request_context_, kBlobURL, kData); |
278 | 278 |
279 sync_status()->StartSyncing(URL(kFile)); | 279 sync_status()->StartSyncing(URL(kFile)); |
280 | 280 |
281 ResetCallbackStatus(); | 281 ResetCallbackStatus(); |
282 file_system_.NewOperation()->Write( | 282 file_system_.NewOperation()->Write( |
283 &url_request_context_, | 283 &url_request_context_, |
284 URL(kFile), kBlobURL, 0, GetWriteCallback(FROM_HERE)); | 284 URL(kFile), kBlobURL, 0, GetWriteCallback(FROM_HERE)); |
285 MessageLoop::current()->RunAllPending(); | 285 MessageLoop::current()->RunUntilIdle(); |
286 EXPECT_EQ(0, callback_count_); | 286 EXPECT_EQ(0, callback_count_); |
287 | 287 |
288 sync_status()->EndSyncing(URL(kFile)); | 288 sync_status()->EndSyncing(URL(kFile)); |
289 ResetCallbackStatus(); | 289 ResetCallbackStatus(); |
290 | 290 |
291 while (!write_complete_) | 291 while (!write_complete_) |
292 MessageLoop::current()->RunAllPending(); | 292 MessageLoop::current()->RunUntilIdle(); |
293 | 293 |
294 EXPECT_EQ(base::PLATFORM_FILE_OK, write_status_); | 294 EXPECT_EQ(base::PLATFORM_FILE_OK, write_status_); |
295 EXPECT_EQ(kData.size(), write_bytes_); | 295 EXPECT_EQ(kData.size(), write_bytes_); |
296 EXPECT_TRUE(write_complete_); | 296 EXPECT_TRUE(write_complete_); |
297 } | 297 } |
298 | 298 |
299 TEST_F(SyncableFileOperationRunnerTest, QueueAndCancel) { | 299 TEST_F(SyncableFileOperationRunnerTest, QueueAndCancel) { |
300 sync_status()->StartSyncing(URL(kFile)); | 300 sync_status()->StartSyncing(URL(kFile)); |
301 ASSERT_FALSE(sync_status()->IsWritable(URL(kFile))); | 301 ASSERT_FALSE(sync_status()->IsWritable(URL(kFile))); |
302 | 302 |
303 ResetCallbackStatus(); | 303 ResetCallbackStatus(); |
304 file_system_.NewOperation()->CreateFile( | 304 file_system_.NewOperation()->CreateFile( |
305 URL(kFile), false /* exclusive */, | 305 URL(kFile), false /* exclusive */, |
306 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_ABORT)); | 306 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_ABORT)); |
307 file_system_.NewOperation()->Truncate( | 307 file_system_.NewOperation()->Truncate( |
308 URL(kFile), 1, | 308 URL(kFile), 1, |
309 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_ABORT)); | 309 ExpectStatus(FROM_HERE, base::PLATFORM_FILE_ERROR_ABORT)); |
310 MessageLoop::current()->RunAllPending(); | 310 MessageLoop::current()->RunUntilIdle(); |
311 EXPECT_EQ(0, callback_count_); | 311 EXPECT_EQ(0, callback_count_); |
312 | 312 |
313 ResetCallbackStatus(); | 313 ResetCallbackStatus(); |
314 | 314 |
315 // This shouldn't crash nor leak memory. | 315 // This shouldn't crash nor leak memory. |
316 sync_context_->ShutdownOnUIThread(); | 316 sync_context_->ShutdownOnUIThread(); |
317 sync_context_ = NULL; | 317 sync_context_ = NULL; |
318 MessageLoop::current()->RunAllPending(); | 318 MessageLoop::current()->RunUntilIdle(); |
319 EXPECT_EQ(2, callback_count_); | 319 EXPECT_EQ(2, callback_count_); |
320 } | 320 } |
321 | 321 |
322 } // namespace fileapi | 322 } // namespace fileapi |
OLD | NEW |