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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_enumerator.h" | |
6 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
11 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 12 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
12 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" | 13 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" |
13 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" | 14 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" |
14 #include "chrome/common/media_galleries/picasa_types.h" | 15 #include "chrome/common/media_galleries/picasa_types.h" |
15 #include "chrome/common/media_galleries/pmp_test_helper.h" | 16 #include "chrome/common/media_galleries/pmp_test_helper.h" |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 EXPECT_EQ(test_folder_1_path.AppendASCII("InBoth.jpg"), | 169 EXPECT_EQ(test_folder_1_path.AppendASCII("InBoth.jpg"), |
169 (*album_2_images)["InBoth.jpg"]); | 170 (*album_2_images)["InBoth.jpg"]); |
170 EXPECT_NE(album_2_images->end(), | 171 EXPECT_NE(album_2_images->end(), |
171 album_2_images->find("InSecondAlbumOnly.jpg")); | 172 album_2_images->find("InSecondAlbumOnly.jpg")); |
172 EXPECT_EQ(test_folder_1_path.AppendASCII("InSecondAlbumOnly.jpg"), | 173 EXPECT_EQ(test_folder_1_path.AppendASCII("InSecondAlbumOnly.jpg"), |
173 (*album_2_images)["InSecondAlbumOnly.jpg"]); | 174 (*album_2_images)["InSecondAlbumOnly.jpg"]); |
174 } | 175 } |
175 | 176 |
176 } // namespace | 177 } // namespace |
177 | 178 |
179 class TestPicasaDataProvider : public PicasaDataProvider { | |
180 public: | |
181 explicit TestPicasaDataProvider(const base::FilePath& database_path, | |
vandebo (ex-Chrome)
2013/08/29 23:19:22
Yea, it seems like it wouldn't be too much trouble
tommycli
2013/09/03 20:20:43
Done.
| |
182 const base::FilePath& temp_dir_path) | |
183 : PicasaDataProvider(database_path, temp_dir_path), | |
184 temp_dir_path_(temp_dir_path) {} | |
185 | |
186 virtual ~TestPicasaDataProvider() {} | |
187 | |
188 // Simulates the actual writing process of moving all the database files | |
189 // from the temporary directory to the database directory in a loop. | |
190 void MoveTempFilesToDatabase() { | |
191 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
192 | |
193 base::FileEnumerator file_enumerator( | |
194 temp_dir_path_, false /* recursive */, base::FileEnumerator::FILES); | |
195 | |
196 for (base::FilePath src_path = file_enumerator.Next(); !src_path.empty(); | |
197 src_path = file_enumerator.Next()) { | |
198 ASSERT_TRUE( | |
199 base::Move(src_path, database_path_.Append(src_path.BaseName()))); | |
200 } | |
201 } | |
202 | |
203 void SetInvalidateCallback(const base::Closure& callback) { | |
204 DCHECK(!invalidate_callback_.get()); | |
205 invalidate_callback_.reset(new base::Closure(callback)); | |
206 } | |
207 | |
208 virtual void InvalidateData() OVERRIDE { | |
vandebo (ex-Chrome)
2013/08/29 23:19:22
Should this be private?
tommycli
2013/09/03 20:20:43
No. This is explicitly called by a few tests. (I c
| |
209 PicasaDataProvider::InvalidateData(); | |
210 | |
211 if (invalidate_callback_.get()) { | |
212 invalidate_callback_->Run(); | |
213 invalidate_callback_.reset(); | |
214 } | |
215 } | |
216 | |
217 // Methods are used in the browser test to tweak internal data for testing. | |
218 void SetDatabasePathForTesting(const base::FilePath& database_path) { | |
vandebo (ex-Chrome)
2013/08/29 23:19:22
Do you still need this?
tommycli
2013/09/03 20:20:43
Done.
| |
219 database_path_ = database_path; | |
220 } | |
221 | |
222 void SetAlbumMapsForTesting(const AlbumMap& album_map, | |
223 const AlbumMap& folder_map) { | |
224 album_map_ = album_map; | |
225 folder_map_ = folder_map; | |
226 } | |
227 | |
228 private: | |
229 scoped_ptr<base::Closure> invalidate_callback_; | |
vandebo (ex-Chrome)
2013/08/29 23:19:22
You don't need a scoped_ptr for Closure, just take
tommycli
2013/09/03 20:20:43
Done.
| |
230 | |
231 base::FilePath temp_dir_path_; | |
232 }; | |
233 | |
178 class PicasaDataProviderTest : public InProcessBrowserTest { | 234 class PicasaDataProviderTest : public InProcessBrowserTest { |
179 public: | 235 public: |
180 PicasaDataProviderTest() : test_helper_(kPicasaAlbumTableName) {} | 236 PicasaDataProviderTest() : test_helper_(kPicasaAlbumTableName) {} |
181 virtual ~PicasaDataProviderTest() {} | 237 virtual ~PicasaDataProviderTest() {} |
182 | 238 |
183 protected: | 239 protected: |
184 // Runs on the MediaTaskRunner and designed to be overridden by subclasses. | 240 // Runs on the MediaTaskRunner and designed to be overridden by subclasses. |
185 virtual void InitializeTestData() {} | 241 virtual void InitializeTestData() {} |
186 | 242 |
187 void RunTest() { | 243 void RunTest() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 FROM_HERE, | 275 FROM_HERE, |
220 base::Bind(&PicasaDataProviderTest::DestructDataProviderThenQuit, | 276 base::Bind(&PicasaDataProviderTest::DestructDataProviderThenQuit, |
221 base::Unretained(this))); | 277 base::Unretained(this))); |
222 } | 278 } |
223 | 279 |
224 const base::FilePath& test_folder_1_path() { return test_folder_1_.path(); } | 280 const base::FilePath& test_folder_1_path() { return test_folder_1_.path(); } |
225 const base::FilePath& test_folder_2_path() { return test_folder_2_.path(); } | 281 const base::FilePath& test_folder_2_path() { return test_folder_2_.path(); } |
226 | 282 |
227 PmpTestHelper* test_helper() { return &test_helper_; } | 283 PmpTestHelper* test_helper() { return &test_helper_; } |
228 | 284 |
229 PicasaDataProvider* data_provider() const { | 285 TestPicasaDataProvider* data_provider() const { |
230 return picasa_data_provider_.get(); | 286 return picasa_data_provider_.get(); |
231 } | 287 } |
232 | 288 |
233 private: | 289 private: |
234 void SetupFoldersAndDataProvider() { | 290 void SetupFoldersAndDataProvider() { |
235 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 291 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
236 ASSERT_TRUE(test_folder_1_.CreateUniqueTempDir()); | 292 ASSERT_TRUE(test_folder_1_.CreateUniqueTempDir()); |
237 ASSERT_TRUE(test_folder_2_.CreateUniqueTempDir()); | 293 ASSERT_TRUE(test_folder_2_.CreateUniqueTempDir()); |
294 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); | |
238 ASSERT_TRUE(test_helper_.Init()); | 295 ASSERT_TRUE(test_helper_.Init()); |
239 picasa_data_provider_.reset( | 296 picasa_data_provider_.reset(new TestPicasaDataProvider( |
240 new PicasaDataProvider(test_helper_.GetTempDirPath())); | 297 database_dir_.path(), test_helper_.GetTempDirPath())); |
241 } | 298 } |
242 | 299 |
243 virtual void StartTestOnMediaTaskRunner() { | 300 virtual void StartTestOnMediaTaskRunner() { |
244 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 301 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
245 | 302 |
246 data_provider()->RefreshData( | 303 data_provider()->MoveTempFilesToDatabase(); |
247 RequestedDataType(), | 304 |
248 base::Bind(&PicasaDataProviderTest::VerifyRefreshResults, | 305 data_provider() |
249 base::Unretained(this))); | 306 ->RefreshData(RequestedDataType(), |
vandebo (ex-Chrome)
2013/08/29 23:19:22
Don't change this formatting.
tommycli
2013/09/03 20:20:43
Done.
| |
307 base::Bind(&PicasaDataProviderTest::VerifyRefreshResults, | |
308 base::Unretained(this))); | |
250 } | 309 } |
251 | 310 |
252 void DestructDataProviderThenQuit() { | 311 void DestructDataProviderThenQuit() { |
253 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 312 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
254 picasa_data_provider_.reset(); | 313 picasa_data_provider_.reset(); |
255 content::BrowserThread::PostTask( | 314 content::BrowserThread::PostTask( |
256 content::BrowserThread::UI, FROM_HERE, quit_closure_); | 315 content::BrowserThread::UI, FROM_HERE, quit_closure_); |
257 } | 316 } |
258 | 317 |
259 base::ScopedTempDir test_folder_1_; | 318 base::ScopedTempDir test_folder_1_; |
260 base::ScopedTempDir test_folder_2_; | 319 base::ScopedTempDir test_folder_2_; |
320 base::ScopedTempDir database_dir_; | |
261 | 321 |
262 PmpTestHelper test_helper_; | 322 PmpTestHelper test_helper_; |
263 scoped_ptr<PicasaDataProvider> picasa_data_provider_; | 323 scoped_ptr<TestPicasaDataProvider> picasa_data_provider_; |
264 | 324 |
265 base::Closure quit_closure_; | 325 base::Closure quit_closure_; |
266 | 326 |
267 DISALLOW_COPY_AND_ASSIGN(PicasaDataProviderTest); | 327 DISALLOW_COPY_AND_ASSIGN(PicasaDataProviderTest); |
268 }; | 328 }; |
269 | 329 |
270 class PicasaDataProviderNoDatabaseGetListTest : public PicasaDataProviderTest { | 330 class PicasaDataProviderNoDatabaseGetListTest : public PicasaDataProviderTest { |
271 protected: | 331 protected: |
272 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | 332 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { |
273 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; | 333 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 void CheckTestDone() { | 450 void CheckTestDone() { |
391 ASSERT_LE(list_callbacks_called_, 2); | 451 ASSERT_LE(list_callbacks_called_, 2); |
392 ASSERT_LE(albums_images_callbacks_called_, 2); | 452 ASSERT_LE(albums_images_callbacks_called_, 2); |
393 if (list_callbacks_called_ == 2 && albums_images_callbacks_called_ == 2) | 453 if (list_callbacks_called_ == 2 && albums_images_callbacks_called_ == 2) |
394 TestDone(); | 454 TestDone(); |
395 } | 455 } |
396 | 456 |
397 virtual void StartTestOnMediaTaskRunner() OVERRIDE { | 457 virtual void StartTestOnMediaTaskRunner() OVERRIDE { |
398 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 458 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
399 | 459 |
460 data_provider()->MoveTempFilesToDatabase(); | |
461 | |
vandebo (ex-Chrome)
2013/08/29 23:19:22
There's a race here - you want to wait until it's
tommycli
2013/09/03 20:20:43
Move is synchronous I believe.
vandebo (ex-Chrome)
2013/09/05 16:49:38
The move is synchronous, but the invalidation trig
tommycli
2013/09/05 21:37:37
Oh, I see what you're saying. I took your recommen
| |
400 data_provider()->RefreshData( | 462 data_provider()->RefreshData( |
401 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, | 463 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, |
402 base::Bind(&PicasaDataProviderMultipleMixedCallbacksTest::ListCallback, | 464 base::Bind(&PicasaDataProviderMultipleMixedCallbacksTest::ListCallback, |
403 base::Unretained(this), | 465 base::Unretained(this), |
404 1)); | 466 1)); |
405 data_provider()->RefreshData( | 467 data_provider()->RefreshData( |
406 PicasaDataProvider::ALBUMS_IMAGES_DATA, | 468 PicasaDataProvider::ALBUMS_IMAGES_DATA, |
407 base::Bind( | 469 base::Bind( |
408 &PicasaDataProviderMultipleMixedCallbacksTest::AlbumsImagesCallback, | 470 &PicasaDataProviderMultipleMixedCallbacksTest::AlbumsImagesCallback, |
409 base::Unretained(this), | 471 base::Unretained(this), |
(...skipping 13 matching lines...) Expand all Loading... | |
423 | 485 |
424 int list_callbacks_called_; | 486 int list_callbacks_called_; |
425 int albums_images_callbacks_called_; | 487 int albums_images_callbacks_called_; |
426 }; | 488 }; |
427 | 489 |
428 IN_PROC_BROWSER_TEST_F(PicasaDataProviderMultipleMixedCallbacksTest, | 490 IN_PROC_BROWSER_TEST_F(PicasaDataProviderMultipleMixedCallbacksTest, |
429 MultipleMixedCallbacks) { | 491 MultipleMixedCallbacks) { |
430 RunTest(); | 492 RunTest(); |
431 } | 493 } |
432 | 494 |
433 class PicasaDataProviderInvalidateSimpleTest : public PicasaDataProviderTest { | 495 class PicasaDataProviderFileWatcherInvalidateTest |
496 : public PicasaDataProviderGetListTest { | |
434 protected: | 497 protected: |
435 virtual void FirstListCallback(bool parse_success) { | 498 virtual void ListCallback(bool parse_success) { |
436 ASSERT_FALSE(parse_success); | 499 ASSERT_FALSE(parse_success); |
437 WriteTestAlbumTable( | |
438 test_helper(), test_folder_1_path(), test_folder_2_path()); | |
439 | 500 |
440 // TODO(tommycli): Remove this line once database is under file watch. | 501 // Validate the list after the file move triggers an invalidate. |
441 data_provider()->InvalidateData(); | 502 data_provider()->SetInvalidateCallback(base::Bind( |
503 &PicasaDataProvider::RefreshData, | |
504 base::Unretained(data_provider()), | |
505 RequestedDataType(), | |
506 base::Bind( | |
507 &PicasaDataProviderFileWatcherInvalidateTest::VerifyRefreshResults, | |
508 base::Unretained(this)))); | |
442 | 509 |
443 // Have to post this, otherwise this will run the callback immediately. | 510 data_provider()->MoveTempFilesToDatabase(); |
444 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
445 FROM_HERE, | |
446 base::Bind( | |
447 &PicasaDataProvider::RefreshData, | |
448 base::Unretained(data_provider()), | |
449 RequestedDataType(), | |
450 base::Bind( | |
451 &PicasaDataProviderInvalidateSimpleTest::SecondListCallback, | |
452 base::Unretained(this)))); | |
453 } | |
454 | |
455 virtual void SecondListCallback(bool parse_success) { | |
456 ASSERT_TRUE(parse_success); | |
457 VerifyAlbumTable( | |
458 data_provider(), test_folder_1_path(), test_folder_2_path()); | |
459 TestDone(); | |
460 } | |
461 | |
462 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | |
463 return PicasaDataProvider::ALBUMS_IMAGES_DATA; | |
464 } | 511 } |
465 | 512 |
466 private: | 513 private: |
467 virtual void StartTestOnMediaTaskRunner() OVERRIDE { | 514 virtual void StartTestOnMediaTaskRunner() OVERRIDE { |
468 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 515 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
469 | 516 |
517 // Refresh before moving album table to database dir, guaranteeing failure. | |
470 data_provider()->RefreshData( | 518 data_provider()->RefreshData( |
471 RequestedDataType(), | 519 RequestedDataType(), |
472 base::Bind(&PicasaDataProviderInvalidateSimpleTest::FirstListCallback, | 520 base::Bind( |
473 base::Unretained(this))); | 521 &PicasaDataProviderFileWatcherInvalidateTest::ListCallback, |
522 base::Unretained(this))); | |
474 } | 523 } |
475 }; | 524 }; |
476 | 525 |
477 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateSimpleTest, | 526 IN_PROC_BROWSER_TEST_F(PicasaDataProviderFileWatcherInvalidateTest, |
478 InvalidateSimpleTest) { | 527 FileWatcherInvalidateTest) { |
479 RunTest(); | 528 RunTest(); |
480 } | 529 } |
481 | 530 |
482 class PicasaDataProviderInvalidateInflightTableReaderTest | 531 class PicasaDataProviderInvalidateInflightTableReaderTest |
483 : public PicasaDataProviderGetListTest { | 532 : public PicasaDataProviderGetListTest { |
484 private: | 533 private: |
485 virtual void StartTestOnMediaTaskRunner() OVERRIDE { | 534 virtual void StartTestOnMediaTaskRunner() OVERRIDE { |
486 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 535 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
487 | 536 |
488 // Temporarily empty the database path to guarantee that the first utility | 537 // Temporarily empty the database path to guarantee that the first utility |
(...skipping 18 matching lines...) Expand all Loading... | |
507 RunTest(); | 556 RunTest(); |
508 } | 557 } |
509 | 558 |
510 class PicasaDataProviderInvalidateInflightAlbumsIndexerTest | 559 class PicasaDataProviderInvalidateInflightAlbumsIndexerTest |
511 : public PicasaDataProviderGetAlbumsImagesTest { | 560 : public PicasaDataProviderGetAlbumsImagesTest { |
512 protected: | 561 protected: |
513 virtual void ListCallback(bool parse_success) { | 562 virtual void ListCallback(bool parse_success) { |
514 ASSERT_TRUE(parse_success); | 563 ASSERT_TRUE(parse_success); |
515 | 564 |
516 // Empty the album maps to guarantee that the first utility process will | 565 // Empty the album maps to guarantee that the first utility process will |
517 // give incorrect results. | 566 // fail to get the correct albums-images index. |
518 data_provider()->SetAlbumMapsForTesting(AlbumMap(), AlbumMap()); | 567 data_provider()->SetAlbumMapsForTesting(AlbumMap(), AlbumMap()); |
519 data_provider()->RefreshData( | 568 data_provider()->RefreshData( |
520 PicasaDataProvider::ALBUMS_IMAGES_DATA, | 569 PicasaDataProvider::ALBUMS_IMAGES_DATA, |
521 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: | 570 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: |
522 VerifyRefreshResults, | 571 VerifyRefreshResults, |
523 base::Unretained(this))); | 572 base::Unretained(this))); |
524 | 573 |
525 // Now invalidate all the data. The album maps will be re-read. | 574 // Now invalidate all the data. The album maps will be re-read. |
526 data_provider()->InvalidateData(); | 575 data_provider()->InvalidateData(); |
527 | 576 |
528 // VerifyRefreshResults callback should receive correct results now. | 577 // VerifyRefreshResults callback should receive correct results now. |
529 } | 578 } |
530 | 579 |
531 private: | 580 private: |
532 virtual void StartTestOnMediaTaskRunner() OVERRIDE { | 581 virtual void StartTestOnMediaTaskRunner() OVERRIDE { |
533 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 582 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
534 | 583 |
584 data_provider()->MoveTempFilesToDatabase(); | |
585 | |
vandebo (ex-Chrome)
2013/08/29 23:19:22
And here
tommycli
2013/09/03 20:20:43
See above.
| |
535 data_provider()->RefreshData( | 586 data_provider()->RefreshData( |
536 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, | 587 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, |
537 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: | 588 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: |
538 ListCallback, | 589 ListCallback, |
539 base::Unretained(this))); | 590 base::Unretained(this))); |
540 } | 591 } |
541 }; | 592 }; |
542 | 593 |
543 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightAlbumsIndexerTest, | 594 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightAlbumsIndexerTest, |
544 InvalidateInflightAlbumsIndexerTest) { | 595 InvalidateInflightAlbumsIndexerTest) { |
545 RunTest(); | 596 RunTest(); |
546 } | 597 } |
547 | 598 |
548 } // namespace picasa | 599 } // namespace picasa |
OLD | NEW |