Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider_browsertest.cc

Issue 23499006: Media Galleries API Picasa: Add file watch to invalidate database data on disk write. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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:
234 void SetupFoldersAndDataProvider() { 284 void SetupFoldersAndDataProvider() {
235 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 285 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
236 ASSERT_TRUE(test_folder_1_.CreateUniqueTempDir()); 286 ASSERT_TRUE(test_folder_1_.CreateUniqueTempDir());
237 ASSERT_TRUE(test_folder_2_.CreateUniqueTempDir()); 287 ASSERT_TRUE(test_folder_2_.CreateUniqueTempDir());
288 ASSERT_TRUE(database_dir_.CreateUniqueTempDir());
238 ASSERT_TRUE(test_helper_.Init()); 289 ASSERT_TRUE(test_helper_.Init());
239 picasa_data_provider_.reset( 290 picasa_data_provider_.reset(new TestPicasaDataProvider(
240 new PicasaDataProvider(test_helper_.GetTempDirPath())); 291 test_helper_.GetDatabaseDirPath()));
241 } 292 }
242 293
243 virtual void StartTestOnMediaTaskRunner() { 294 virtual void StartTestOnMediaTaskRunner() {
244 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 295 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
245 296
297 data_provider()->MoveTempFilesToDatabase();
298
246 data_provider()->RefreshData( 299 data_provider()->RefreshData(
247 RequestedDataType(), 300 RequestedDataType(),
248 base::Bind(&PicasaDataProviderTest::VerifyRefreshResults, 301 base::Bind(&PicasaDataProviderTest::VerifyRefreshResults,
249 base::Unretained(this))); 302 base::Unretained(this)));
250 } 303 }
251 304
252 void DestructDataProviderThenQuit() { 305 void DestructDataProviderThenQuit() {
253 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 306 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
254 picasa_data_provider_.reset(); 307 picasa_data_provider_.reset();
255 content::BrowserThread::PostTask( 308 content::BrowserThread::PostTask(
256 content::BrowserThread::UI, FROM_HERE, quit_closure_); 309 content::BrowserThread::UI, FROM_HERE, quit_closure_);
257 } 310 }
258 311
259 base::ScopedTempDir test_folder_1_; 312 base::ScopedTempDir test_folder_1_;
260 base::ScopedTempDir test_folder_2_; 313 base::ScopedTempDir test_folder_2_;
314 base::ScopedTempDir database_dir_;
261 315
262 PmpTestHelper test_helper_; 316 PmpTestHelper test_helper_;
263 scoped_ptr<PicasaDataProvider> picasa_data_provider_; 317 scoped_ptr<TestPicasaDataProvider> picasa_data_provider_;
264 318
265 base::Closure quit_closure_; 319 base::Closure quit_closure_;
266 320
267 DISALLOW_COPY_AND_ASSIGN(PicasaDataProviderTest); 321 DISALLOW_COPY_AND_ASSIGN(PicasaDataProviderTest);
268 }; 322 };
269 323
270 class PicasaDataProviderNoDatabaseGetListTest : public PicasaDataProviderTest { 324 class PicasaDataProviderNoDatabaseGetListTest : public PicasaDataProviderTest {
271 protected: 325 protected:
272 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { 326 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE {
273 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; 327 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 void CheckTestDone() { 444 void CheckTestDone() {
391 ASSERT_LE(list_callbacks_called_, 2); 445 ASSERT_LE(list_callbacks_called_, 2);
392 ASSERT_LE(albums_images_callbacks_called_, 2); 446 ASSERT_LE(albums_images_callbacks_called_, 2);
393 if (list_callbacks_called_ == 2 && albums_images_callbacks_called_ == 2) 447 if (list_callbacks_called_ == 2 && albums_images_callbacks_called_ == 2)
394 TestDone(); 448 TestDone();
395 } 449 }
396 450
397 virtual void StartTestOnMediaTaskRunner() OVERRIDE { 451 virtual void StartTestOnMediaTaskRunner() OVERRIDE {
398 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 452 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
399 453
454 data_provider()->MoveTempFilesToDatabase();
455
400 data_provider()->RefreshData( 456 data_provider()->RefreshData(
401 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, 457 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA,
402 base::Bind(&PicasaDataProviderMultipleMixedCallbacksTest::ListCallback, 458 base::Bind(&PicasaDataProviderMultipleMixedCallbacksTest::ListCallback,
403 base::Unretained(this), 459 base::Unretained(this),
404 1)); 460 1));
405 data_provider()->RefreshData( 461 data_provider()->RefreshData(
406 PicasaDataProvider::ALBUMS_IMAGES_DATA, 462 PicasaDataProvider::ALBUMS_IMAGES_DATA,
407 base::Bind( 463 base::Bind(
408 &PicasaDataProviderMultipleMixedCallbacksTest::AlbumsImagesCallback, 464 &PicasaDataProviderMultipleMixedCallbacksTest::AlbumsImagesCallback,
409 base::Unretained(this), 465 base::Unretained(this),
(...skipping 13 matching lines...) Expand all
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 }
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 } 505 }
465 506
466 private: 507 private:
467 virtual void StartTestOnMediaTaskRunner() OVERRIDE { 508 virtual void StartTestOnMediaTaskRunner() OVERRIDE {
468 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 509 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
469 510
511 // Refresh before moving album table to database dir, guaranteeing failure.
470 data_provider()->RefreshData( 512 data_provider()->RefreshData(
471 RequestedDataType(), 513 RequestedDataType(),
472 base::Bind(&PicasaDataProviderInvalidateSimpleTest::FirstListCallback, 514 base::Bind(
473 base::Unretained(this))); 515 &PicasaDataProviderFileWatcherInvalidateTest::ListCallback,
516 base::Unretained(this)));
474 } 517 }
475 }; 518 };
476 519
477 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateSimpleTest, 520 IN_PROC_BROWSER_TEST_F(PicasaDataProviderFileWatcherInvalidateTest,
478 InvalidateSimpleTest) { 521 FileWatcherInvalidateTest) {
479 RunTest(); 522 RunTest();
480 } 523 }
481 524
482 class PicasaDataProviderInvalidateInflightTableReaderTest 525 class PicasaDataProviderInvalidateInflightTableReaderTest
483 : public PicasaDataProviderGetListTest { 526 : public PicasaDataProviderGetListTest {
484 private: 527 private:
485 virtual void StartTestOnMediaTaskRunner() OVERRIDE { 528 virtual void StartTestOnMediaTaskRunner() OVERRIDE {
486 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 529 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
487 530
488 // Temporarily empty the database path to guarantee that the first utility 531 // Refresh before the database files have been moved into the database.
489 // process will fail to read the database. 532 // This is guaranteed to fail to read the album table.
490 data_provider()->SetDatabasePathForTesting(base::FilePath());
491 data_provider()->RefreshData( 533 data_provider()->RefreshData(
492 RequestedDataType(), 534 RequestedDataType(),
493 base::Bind(&PicasaDataProviderInvalidateInflightTableReaderTest:: 535 base::Bind(&PicasaDataProviderInvalidateInflightTableReaderTest::
494 VerifyRefreshResults, 536 VerifyRefreshResults,
495 base::Unretained(this))); 537 base::Unretained(this)));
496 538
497 // Now restore the database path and invalidate the inflight table reader. 539 // Now move table into database and invalidate the inflight table reader.
498 data_provider()->SetDatabasePathForTesting(test_helper()->GetTempDirPath()); 540 data_provider()->MoveTempFilesToDatabase();
499 data_provider()->InvalidateData(); 541 data_provider()->InvalidateData();
vandebo (ex-Chrome) 2013/09/05 16:49:38 This is tricky again - You should only do one of t
tommycli 2013/09/05 21:37:37 Done.
500 542
501 // VerifyRefreshResults callback should receive correct results now. 543 // VerifyRefreshResults callback should receive correct results now.
502 } 544 }
503 }; 545 };
504 546
505 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightTableReaderTest, 547 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightTableReaderTest,
506 InvalidateInflightTableReaderTest) { 548 InvalidateInflightTableReaderTest) {
507 RunTest(); 549 RunTest();
508 } 550 }
509 551
510 class PicasaDataProviderInvalidateInflightAlbumsIndexerTest 552 class PicasaDataProviderInvalidateInflightAlbumsIndexerTest
511 : public PicasaDataProviderGetAlbumsImagesTest { 553 : public PicasaDataProviderGetAlbumsImagesTest {
512 protected: 554 protected:
513 virtual void ListCallback(bool parse_success) { 555 virtual void ListCallback(bool parse_success) {
514 ASSERT_TRUE(parse_success); 556 ASSERT_TRUE(parse_success);
515 557
516 // Empty the album maps to guarantee that the first utility process will 558 // Empty the album maps to guarantee that the first utility process will
517 // give incorrect results. 559 // fail to get the correct albums-images index.
518 data_provider()->SetAlbumMapsForTesting(AlbumMap(), AlbumMap()); 560 data_provider()->SetAlbumMapsForTesting(AlbumMap(), AlbumMap());
519 data_provider()->RefreshData( 561 data_provider()->RefreshData(
520 PicasaDataProvider::ALBUMS_IMAGES_DATA, 562 PicasaDataProvider::ALBUMS_IMAGES_DATA,
521 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: 563 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest::
522 VerifyRefreshResults, 564 VerifyRefreshResults,
523 base::Unretained(this))); 565 base::Unretained(this)));
524 566
525 // Now invalidate all the data. The album maps will be re-read. 567 // Now invalidate all the data. The album maps will be re-read.
526 data_provider()->InvalidateData(); 568 data_provider()->InvalidateData();
527 569
528 // VerifyRefreshResults callback should receive correct results now. 570 // VerifyRefreshResults callback should receive correct results now.
529 } 571 }
530 572
531 private: 573 private:
532 virtual void StartTestOnMediaTaskRunner() OVERRIDE { 574 virtual void StartTestOnMediaTaskRunner() OVERRIDE {
533 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 575 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
534 576
577 data_provider()->MoveTempFilesToDatabase();
578
535 data_provider()->RefreshData( 579 data_provider()->RefreshData(
536 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, 580 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA,
537 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest:: 581 base::Bind(&PicasaDataProviderInvalidateInflightAlbumsIndexerTest::
538 ListCallback, 582 ListCallback,
539 base::Unretained(this))); 583 base::Unretained(this)));
540 } 584 }
541 }; 585 };
542 586
543 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightAlbumsIndexerTest, 587 IN_PROC_BROWSER_TEST_F(PicasaDataProviderInvalidateInflightAlbumsIndexerTest,
544 InvalidateInflightAlbumsIndexerTest) { 588 InvalidateInflightAlbumsIndexerTest) {
545 RunTest(); 589 RunTest();
546 } 590 }
547 591
548 } // namespace picasa 592 } // namespace picasa
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698