| 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" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
| 18 | 19 |
| 19 using chrome::MediaFileSystemBackend; | 20 using chrome::MediaFileSystemBackend; |
| 20 | 21 |
| 21 namespace picasa { | 22 namespace picasa { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 void WriteTestAlbumTable(PmpTestHelper* test_helper, | 26 void WriteTestAlbumTable(const PmpTestHelper* test_helper, |
| 26 base::FilePath test_folder_1_path, | 27 const base::FilePath& test_folder_1_path, |
| 27 base::FilePath test_folder_2_path) { | 28 const base::FilePath& test_folder_2_path) { |
| 28 std::vector<uint32> category_vector; | 29 std::vector<uint32> category_vector; |
| 29 category_vector.push_back(kAlbumCategoryFolder); | 30 category_vector.push_back(kAlbumCategoryFolder); |
| 30 category_vector.push_back(kAlbumCategoryInvalid); | 31 category_vector.push_back(kAlbumCategoryInvalid); |
| 31 category_vector.push_back(kAlbumCategoryAlbum); | 32 category_vector.push_back(kAlbumCategoryAlbum); |
| 32 category_vector.push_back(kAlbumCategoryFolder); | 33 category_vector.push_back(kAlbumCategoryFolder); |
| 33 category_vector.push_back(kAlbumCategoryAlbum); | 34 category_vector.push_back(kAlbumCategoryAlbum); |
| 34 | 35 |
| 35 std::vector<double> date_vector; | 36 std::vector<double> date_vector; |
| 36 date_vector.push_back(0.0); | 37 date_vector.push_back(0.0); |
| 37 date_vector.push_back(0.0); | 38 date_vector.push_back(0.0); |
| (...skipping 130 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) |
| 182 : PicasaDataProvider(database_path) { |
| 183 } |
| 184 |
| 185 virtual ~TestPicasaDataProvider() {} |
| 186 |
| 187 // Simulates the actual writing process of moving all the database files |
| 188 // from the temporary directory to the database directory in a loop. |
| 189 void MoveTempFilesToDatabase() { |
| 190 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 191 |
| 192 base::FileEnumerator file_enumerator( |
| 193 database_path_.DirName().AppendASCII(kPicasaTempDirName), |
| 194 false /* recursive */, |
| 195 base::FileEnumerator::FILES); |
| 196 |
| 197 for (base::FilePath src_path = file_enumerator.Next(); !src_path.empty(); |
| 198 src_path = file_enumerator.Next()) { |
| 199 ASSERT_TRUE( |
| 200 base::Move(src_path, database_path_.Append(src_path.BaseName()))); |
| 201 } |
| 202 } |
| 203 |
| 204 void SetInvalidateCallback(const base::Closure& callback) { |
| 205 DCHECK(invalidate_callback_.is_null()); |
| 206 invalidate_callback_ = callback; |
| 207 } |
| 208 |
| 209 virtual void InvalidateData() OVERRIDE { |
| 210 PicasaDataProvider::InvalidateData(); |
| 211 |
| 212 if (!invalidate_callback_.is_null()) { |
| 213 invalidate_callback_.Run(); |
| 214 invalidate_callback_.Reset(); |
| 215 } |
| 216 } |
| 217 |
| 218 void SetAlbumMapsForTesting(const AlbumMap& album_map, |
| 219 const AlbumMap& folder_map) { |
| 220 album_map_ = album_map; |
| 221 folder_map_ = folder_map; |
| 222 } |
| 223 |
| 224 private: |
| 225 base::Closure invalidate_callback_; |
| 226 }; |
| 227 |
| 178 class PicasaDataProviderTest : public InProcessBrowserTest { | 228 class PicasaDataProviderTest : public InProcessBrowserTest { |
| 179 public: | 229 public: |
| 180 PicasaDataProviderTest() : test_helper_(kPicasaAlbumTableName) {} | 230 PicasaDataProviderTest() : test_helper_(kPicasaAlbumTableName) {} |
| 181 virtual ~PicasaDataProviderTest() {} | 231 virtual ~PicasaDataProviderTest() {} |
| 182 | 232 |
| 183 protected: | 233 protected: |
| 184 // Runs on the MediaTaskRunner and designed to be overridden by subclasses. | 234 // Runs on the MediaTaskRunner and designed to be overridden by subclasses. |
| 185 virtual void InitializeTestData() {} | 235 virtual void InitializeTestData() {} |
| 186 | 236 |
| 187 void RunTest() { | 237 void RunTest() { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 FROM_HERE, | 269 FROM_HERE, |
| 220 base::Bind(&PicasaDataProviderTest::DestructDataProviderThenQuit, | 270 base::Bind(&PicasaDataProviderTest::DestructDataProviderThenQuit, |
| 221 base::Unretained(this))); | 271 base::Unretained(this))); |
| 222 } | 272 } |
| 223 | 273 |
| 224 const base::FilePath& test_folder_1_path() { return test_folder_1_.path(); } | 274 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(); } | 275 const base::FilePath& test_folder_2_path() { return test_folder_2_.path(); } |
| 226 | 276 |
| 227 PmpTestHelper* test_helper() { return &test_helper_; } | 277 PmpTestHelper* test_helper() { return &test_helper_; } |
| 228 | 278 |
| 229 PicasaDataProvider* data_provider() const { | 279 TestPicasaDataProvider* data_provider() const { |
| 230 return picasa_data_provider_.get(); | 280 return picasa_data_provider_.get(); |
| 231 } | 281 } |
| 232 | 282 |
| 233 private: | 283 private: |
| 284 virtual PmpTestHelper::ColumnFileDestination GetColumnFileDestination() { |
| 285 return PmpTestHelper::DATABASE_DIRECTORY; |
| 286 } |
| 287 |
| 234 void SetupFoldersAndDataProvider() { | 288 void SetupFoldersAndDataProvider() { |
| 235 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 289 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 236 ASSERT_TRUE(test_folder_1_.CreateUniqueTempDir()); | 290 ASSERT_TRUE(test_folder_1_.CreateUniqueTempDir()); |
| 237 ASSERT_TRUE(test_folder_2_.CreateUniqueTempDir()); | 291 ASSERT_TRUE(test_folder_2_.CreateUniqueTempDir()); |
| 238 ASSERT_TRUE(test_helper_.Init()); | 292 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); |
| 239 picasa_data_provider_.reset( | 293 ASSERT_TRUE(test_helper_.Init(GetColumnFileDestination())); |
| 240 new PicasaDataProvider(test_helper_.GetTempDirPath())); | 294 picasa_data_provider_.reset(new TestPicasaDataProvider( |
| 295 test_helper_.GetDatabaseDirPath())); |
| 241 } | 296 } |
| 242 | 297 |
| 243 virtual void StartTestOnMediaTaskRunner() { | 298 virtual void StartTestOnMediaTaskRunner() { |
| 244 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 299 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 245 | 300 |
| 246 data_provider()->RefreshData( | 301 data_provider()->RefreshData( |
| 247 RequestedDataType(), | 302 RequestedDataType(), |
| 248 base::Bind(&PicasaDataProviderTest::VerifyRefreshResults, | 303 base::Bind(&PicasaDataProviderTest::VerifyRefreshResults, |
| 249 base::Unretained(this))); | 304 base::Unretained(this))); |
| 250 } | 305 } |
| 251 | 306 |
| 252 void DestructDataProviderThenQuit() { | 307 void DestructDataProviderThenQuit() { |
| 253 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 308 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 254 picasa_data_provider_.reset(); | 309 picasa_data_provider_.reset(); |
| 255 content::BrowserThread::PostTask( | 310 content::BrowserThread::PostTask( |
| 256 content::BrowserThread::UI, FROM_HERE, quit_closure_); | 311 content::BrowserThread::UI, FROM_HERE, quit_closure_); |
| 257 } | 312 } |
| 258 | 313 |
| 259 base::ScopedTempDir test_folder_1_; | 314 base::ScopedTempDir test_folder_1_; |
| 260 base::ScopedTempDir test_folder_2_; | 315 base::ScopedTempDir test_folder_2_; |
| 316 base::ScopedTempDir database_dir_; |
| 261 | 317 |
| 262 PmpTestHelper test_helper_; | 318 PmpTestHelper test_helper_; |
| 263 scoped_ptr<PicasaDataProvider> picasa_data_provider_; | 319 scoped_ptr<TestPicasaDataProvider> picasa_data_provider_; |
| 264 | 320 |
| 265 base::Closure quit_closure_; | 321 base::Closure quit_closure_; |
| 266 | 322 |
| 267 DISALLOW_COPY_AND_ASSIGN(PicasaDataProviderTest); | 323 DISALLOW_COPY_AND_ASSIGN(PicasaDataProviderTest); |
| 268 }; | 324 }; |
| 269 | 325 |
| 270 class PicasaDataProviderNoDatabaseGetListTest : public PicasaDataProviderTest { | 326 class PicasaDataProviderNoDatabaseGetListTest : public PicasaDataProviderTest { |
| 271 protected: | 327 protected: |
| 272 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | 328 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { |
| 273 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; | 329 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 | 479 |
| 424 int list_callbacks_called_; | 480 int list_callbacks_called_; |
| 425 int albums_images_callbacks_called_; | 481 int albums_images_callbacks_called_; |
| 426 }; | 482 }; |
| 427 | 483 |
| 428 IN_PROC_BROWSER_TEST_F(PicasaDataProviderMultipleMixedCallbacksTest, | 484 IN_PROC_BROWSER_TEST_F(PicasaDataProviderMultipleMixedCallbacksTest, |
| 429 MultipleMixedCallbacks) { | 485 MultipleMixedCallbacks) { |
| 430 RunTest(); | 486 RunTest(); |
| 431 } | 487 } |
| 432 | 488 |
| 433 class PicasaDataProviderInvalidateSimpleTest : public PicasaDataProviderTest { | 489 class PicasaDataProviderFileWatcherInvalidateTest |
| 490 : public PicasaDataProviderGetListTest { |
| 434 protected: | 491 protected: |
| 435 virtual void FirstListCallback(bool parse_success) { | 492 virtual void ListCallback(bool parse_success) { |
| 436 ASSERT_FALSE(parse_success); | 493 ASSERT_FALSE(parse_success); |
| 437 WriteTestAlbumTable( | |
| 438 test_helper(), test_folder_1_path(), test_folder_2_path()); | |
| 439 | 494 |
| 440 // TODO(tommycli): Remove this line once database is under file watch. | 495 // Validate the list after the file move triggers an invalidate. |
| 441 data_provider()->InvalidateData(); | 496 data_provider()->SetInvalidateCallback(base::Bind( |
| 497 &PicasaDataProvider::RefreshData, |
| 498 base::Unretained(data_provider()), |
| 499 RequestedDataType(), |
| 500 base::Bind( |
| 501 &PicasaDataProviderFileWatcherInvalidateTest::VerifyRefreshResults, |
| 502 base::Unretained(this)))); |
| 442 | 503 |
| 443 // Have to post this, otherwise this will run the callback immediately. | 504 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 } | 505 } |
| 454 | 506 |
| 455 virtual void SecondListCallback(bool parse_success) { | 507 private: |
| 456 ASSERT_TRUE(parse_success); | 508 virtual PmpTestHelper::ColumnFileDestination |
| 457 VerifyAlbumTable( | 509 GetColumnFileDestination() OVERRIDE { |
| 458 data_provider(), test_folder_1_path(), test_folder_2_path()); | 510 return PmpTestHelper::TEMPORARY_DIRECTORY; |
| 459 TestDone(); | |
| 460 } | 511 } |
| 461 | 512 |
| 462 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | 513 virtual void StartTestOnMediaTaskRunner() OVERRIDE { |
| 463 return PicasaDataProvider::ALBUMS_IMAGES_DATA; | 514 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 515 |
| 516 // Refresh before moving album table to database dir, guaranteeing failure. |
| 517 data_provider()->RefreshData( |
| 518 RequestedDataType(), |
| 519 base::Bind( |
| 520 &PicasaDataProviderFileWatcherInvalidateTest::ListCallback, |
| 521 base::Unretained(this))); |
| 464 } | 522 } |
| 523 }; |
| 524 |
| 525 IN_PROC_BROWSER_TEST_F(PicasaDataProviderFileWatcherInvalidateTest, |
| 526 FileWatcherInvalidateTest) { |
| 527 RunTest(); |
| 528 } |
| 529 |
| 530 class PicasaDataProviderInvalidateInflightTableReaderTest |
| 531 : public PicasaDataProviderGetListTest { |
| 532 protected: |
| 533 // Don't write the database files until later. |
| 534 virtual void InitializeTestData() OVERRIDE {} |
| 465 | 535 |
| 466 private: | 536 private: |
| 467 virtual void StartTestOnMediaTaskRunner() OVERRIDE { | 537 virtual void StartTestOnMediaTaskRunner() OVERRIDE { |
| 468 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 538 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 469 | 539 |
| 470 data_provider()->RefreshData( | 540 // Refresh before the database files have been written. |
| 471 RequestedDataType(), | 541 // This is guaranteed to fail to read the album table. |
| 472 base::Bind(&PicasaDataProviderInvalidateSimpleTest::FirstListCallback, | |
| 473 base::Unretained(this))); | |
| 474 } | |
| 475 }; | |
| 476 | |
| 477 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateSimpleTest, | |
| 478 InvalidateSimpleTest) { | |
| 479 RunTest(); | |
| 480 } | |
| 481 | |
| 482 class PicasaDataProviderInvalidateInflightTableReaderTest | |
| 483 : public PicasaDataProviderGetListTest { | |
| 484 private: | |
| 485 virtual void StartTestOnMediaTaskRunner() OVERRIDE { | |
| 486 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 487 | |
| 488 // Temporarily empty the database path to guarantee that the first utility | |
| 489 // process will fail to read the database. | |
| 490 data_provider()->SetDatabasePathForTesting(base::FilePath()); | |
| 491 data_provider()->RefreshData( | 542 data_provider()->RefreshData( |
| 492 RequestedDataType(), | 543 RequestedDataType(), |
| 493 base::Bind(&PicasaDataProviderInvalidateInflightTableReaderTest:: | 544 base::Bind(&PicasaDataProviderInvalidateInflightTableReaderTest:: |
| 494 VerifyRefreshResults, | 545 VerifyRefreshResults, |
| 495 base::Unretained(this))); | 546 base::Unretained(this))); |
| 496 | 547 |
| 497 // Now restore the database path and invalidate the inflight table reader. | 548 // Now write the album table and invalidate the inflight table reader. |
| 498 data_provider()->SetDatabasePathForTesting(test_helper()->GetTempDirPath()); | 549 PicasaDataProviderGetListTest::InitializeTestData(); |
| 499 data_provider()->InvalidateData(); | 550 data_provider()->InvalidateData(); |
| 500 | 551 |
| 501 // VerifyRefreshResults callback should receive correct results now. | 552 // VerifyRefreshResults callback should receive correct results now. |
| 502 } | 553 } |
| 503 }; | 554 }; |
| 504 | 555 |
| 505 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightTableReaderTest, | 556 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightTableReaderTest, |
| 506 InvalidateInflightTableReaderTest) { | 557 InvalidateInflightTableReaderTest) { |
| 507 RunTest(); | 558 RunTest(); |
| 508 } | 559 } |
| 509 | 560 |
| 510 class PicasaDataProviderInvalidateInflightAlbumsIndexerTest | 561 class PicasaDataProviderInvalidateInflightAlbumsIndexerTest |
| 511 : public PicasaDataProviderGetAlbumsImagesTest { | 562 : public PicasaDataProviderGetAlbumsImagesTest { |
| 512 protected: | 563 protected: |
| 513 virtual void ListCallback(bool parse_success) { | 564 virtual void ListCallback(bool parse_success) { |
| 514 ASSERT_TRUE(parse_success); | 565 ASSERT_TRUE(parse_success); |
| 515 | 566 |
| 516 // Empty the album maps to guarantee that the first utility process will | 567 // Empty the album maps to guarantee that the first utility process will |
| 517 // give incorrect results. | 568 // fail to get the correct albums-images index. |
| 518 data_provider()->SetAlbumMapsForTesting(AlbumMap(), AlbumMap()); | 569 data_provider()->SetAlbumMapsForTesting(AlbumMap(), AlbumMap()); |
| 519 data_provider()->RefreshData( | 570 data_provider()->RefreshData( |
| 520 PicasaDataProvider::ALBUMS_IMAGES_DATA, | 571 PicasaDataProvider::ALBUMS_IMAGES_DATA, |
| 521 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: | 572 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: |
| 522 VerifyRefreshResults, | 573 VerifyRefreshResults, |
| 523 base::Unretained(this))); | 574 base::Unretained(this))); |
| 524 | 575 |
| 525 // Now invalidate all the data. The album maps will be re-read. | 576 // Now invalidate all the data. The album maps will be re-read. |
| 526 data_provider()->InvalidateData(); | 577 data_provider()->InvalidateData(); |
| 527 | 578 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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 |