1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/base_paths.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/test/scoped_path_override.h"
14 #include "chrome/browser/extensions/test_extension_system.h"
15 #include "chrome/browser/media_galleries/media_folder_finder.h"
16 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
17 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h"
18 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
19 #include "chrome/browser/media_galleries/media_scan_manager.h"
20 #include "chrome/browser/media_galleries/media_scan_manager_observer.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "components/storage_monitor/test_storage_monitor.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/common/permissions/media_galleries_permission.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 #if defined(OS_CHROMEOS)
30 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
31 #include "chrome/browser/chromeos/settings/cros_settings.h"
32 #include "chrome/browser/chromeos/settings/device_settings_service.h"
37 class MockMediaFolderFinder
: MediaFolderFinder
{
39 typedef base::Callback
<void(MediaFolderFinderResultsCallback
)>
40 FindFoldersStartedCallback
;
42 static MediaFolderFinder
* CreateMockMediaFolderFinder(
43 const FindFoldersStartedCallback
& started_callback
,
44 const base::Closure destruction_callback
,
45 const MediaFolderFinderResultsCallback
& callback
) {
46 return new MockMediaFolderFinder(started_callback
, destruction_callback
,
50 MockMediaFolderFinder(
51 const FindFoldersStartedCallback
& started_callback
,
52 const base::Closure destruction_callback
,
53 const MediaFolderFinderResultsCallback
& callback
)
54 : MediaFolderFinder(callback
),
55 started_callback_(started_callback
),
56 destruction_callback_(destruction_callback
),
59 ~MockMediaFolderFinder() override
{ destruction_callback_
.Run(); }
61 void StartScan() override
{ started_callback_
.Run(callback_
); }
64 FindFoldersStartedCallback started_callback_
;
65 base::Closure destruction_callback_
;
66 MediaFolderFinderResultsCallback callback_
;
68 DISALLOW_COPY_AND_ASSIGN(MockMediaFolderFinder
);
73 class TestMediaScanManager
: public MediaScanManager
{
75 typedef base::Callback
<MediaFolderFinder
*(
76 const MediaFolderFinder::MediaFolderFinderResultsCallback
&)>
77 MediaFolderFinderFactory
;
79 explicit TestMediaScanManager(const MediaFolderFinderFactory
& factory
) {
80 SetMediaFolderFinderFactory(factory
);
82 ~TestMediaScanManager() override
{}
85 DISALLOW_COPY_AND_ASSIGN(TestMediaScanManager
);
88 class MediaScanManagerTest
: public MediaScanManagerObserver
,
89 public testing::Test
{
91 MediaScanManagerTest()
92 : find_folders_start_count_(0),
93 find_folders_destroy_count_(0),
94 find_folders_success_(false),
95 expected_gallery_count_(0),
96 profile_(new TestingProfile()) {}
98 ~MediaScanManagerTest() override
{
99 EXPECT_EQ(find_folders_start_count_
, find_folders_destroy_count_
);
102 void SetUp() override
{
103 ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateAndInstall());
105 extensions::TestExtensionSystem
* extension_system(
106 static_cast<extensions::TestExtensionSystem
*>(
107 extensions::ExtensionSystem::Get(profile_
.get())));
108 extension_system
->CreateExtensionService(
109 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
112 MediaGalleriesPreferencesFactory::GetForProfile(profile_
.get());
114 gallery_prefs_
->EnsureInitialized(loop
.QuitClosure());
117 std::vector
<std::string
> read_permissions
;
118 read_permissions
.push_back(
119 extensions::MediaGalleriesPermission::kReadPermission
);
120 extension_
= AddMediaGalleriesApp("read", read_permissions
, profile_
.get());
122 ASSERT_TRUE(test_results_dir_
.CreateUniqueTempDir());
124 MockMediaFolderFinder::FindFoldersStartedCallback started_callback
=
125 base::Bind(&MediaScanManagerTest::OnFindFoldersStarted
,
126 base::Unretained(this));
127 base::Closure destruction_callback
=
128 base::Bind(&MediaScanManagerTest::OnFindFoldersDestroyed
,
129 base::Unretained(this));
130 TestMediaScanManager::MediaFolderFinderFactory factory
=
131 base::Bind(&MockMediaFolderFinder::CreateMockMediaFolderFinder
,
132 started_callback
, destruction_callback
);
133 media_scan_manager_
.reset(new TestMediaScanManager(factory
));
134 media_scan_manager_
->AddObserver(profile_
.get(), this);
137 void TearDown() override
{
138 media_scan_manager_
->RemoveObserver(profile_
.get());
139 media_scan_manager_
.reset();
140 storage_monitor::TestStorageMonitor::Destroy();
143 // Create a test folder in the test specific scoped temp dir and return the
144 // final path in |full_path|.
145 void MakeTestFolder(const std::string
& root_relative_path
,
146 base::FilePath
* full_path
) {
147 ASSERT_TRUE(test_results_dir_
.IsValid());
149 test_results_dir_
.path().AppendASCII(root_relative_path
);
150 ASSERT_TRUE(base::CreateDirectory(*full_path
));
153 // Create the specified path, and add it to preferences as a gallery.
154 MediaGalleryPrefId
AddGallery(const std::string
& rel_path
,
155 MediaGalleryPrefInfo::Type type
,
159 base::FilePath full_path
;
160 MakeTestFolder(rel_path
, &full_path
);
161 MediaGalleryPrefInfo gallery_info
;
162 gallery_prefs_
->LookUpGalleryByPath(full_path
, &gallery_info
);
163 return gallery_prefs_
->AddGallery(gallery_info
.device_id
,
166 gallery_info
.volume_label
,
167 gallery_info
.vendor_name
,
168 gallery_info
.model_name
,
169 gallery_info
.total_size_in_bytes
,
170 gallery_info
.last_attach_time
,
171 audio_count
, image_count
, video_count
);
174 void SetFindFoldersResults(
176 const MediaFolderFinder::MediaFolderFinderResults
& results
) {
177 find_folders_success_
= success
;
178 find_folders_results_
= results
;
181 void SetExpectedScanResults(int gallery_count
,
182 const MediaGalleryScanResult
& file_counts
) {
183 expected_gallery_count_
= gallery_count
;
184 expected_file_counts_
= file_counts
;
188 media_scan_manager_
->StartScan(
189 profile_
.get(), extension_
.get(), true /* user_gesture */);
192 MediaGalleriesPreferences
* gallery_prefs() {
193 return gallery_prefs_
;
196 const MediaGalleriesPrefInfoMap
& known_galleries() const {
197 return gallery_prefs_
->known_galleries();
200 size_t gallery_count() const {
201 return known_galleries().size();
204 extensions::Extension
* extension() {
205 return extension_
.get();
208 int FindFoldersStartCount() {
209 return find_folders_start_count_
;
212 int FindFolderDestroyCount() {
213 return find_folders_destroy_count_
;
216 void CheckFileCounts(MediaGalleryPrefId pref_id
, int audio_count
,
217 int image_count
, int video_count
) {
218 if (!ContainsKey(known_galleries(), pref_id
)) {
222 MediaGalleriesPrefInfoMap::const_iterator pref_info
=
223 known_galleries().find(pref_id
);
224 EXPECT_EQ(audio_count
, pref_info
->second
.audio_count
);
225 EXPECT_EQ(image_count
, pref_info
->second
.image_count
);
226 EXPECT_EQ(video_count
, pref_info
->second
.video_count
);
229 // MediaScanManagerObserver implementation.
230 void OnScanFinished(const std::string
& extension_id
,
232 const MediaGalleryScanResult
& file_counts
) override
{
233 EXPECT_EQ(extension_
->id(), extension_id
);
234 EXPECT_EQ(expected_gallery_count_
, gallery_count
);
235 EXPECT_EQ(expected_file_counts_
.audio_count
, file_counts
.audio_count
);
236 EXPECT_EQ(expected_file_counts_
.image_count
, file_counts
.image_count
);
237 EXPECT_EQ(expected_file_counts_
.video_count
, file_counts
.video_count
);
241 // So derived tests can access ...::FindContainerScanResults().
242 MediaFolderFinder::MediaFolderFinderResults
FindContainerScanResults(
243 const MediaFolderFinder::MediaFolderFinderResults
& found_folders
,
244 const std::vector
<base::FilePath
>& sensitive_locations
) {
245 return MediaScanManager::FindContainerScanResults(found_folders
,
246 sensitive_locations
);
250 void OnFindFoldersStarted(
251 MediaFolderFinder::MediaFolderFinderResultsCallback callback
) {
252 find_folders_start_count_
++;
253 callback
.Run(find_folders_success_
, find_folders_results_
);
256 void OnFindFoldersDestroyed() {
257 find_folders_destroy_count_
++;
260 scoped_ptr
<TestMediaScanManager
> media_scan_manager_
;
262 int find_folders_start_count_
;
263 int find_folders_destroy_count_
;
264 bool find_folders_success_
;
265 MediaFolderFinder::MediaFolderFinderResults find_folders_results_
;
267 int expected_gallery_count_
;
268 MediaGalleryScanResult expected_file_counts_
;
270 base::ScopedTempDir test_results_dir_
;
272 // Needed for extension service & friends to work.
273 content::TestBrowserThreadBundle thread_bundle_
;
275 scoped_refptr
<extensions::Extension
> extension_
;
277 EnsureMediaDirectoriesExists mock_gallery_locations_
;
279 #if defined(OS_CHROMEOS)
280 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
281 chromeos::ScopedTestCrosSettings test_cros_settings_
;
282 chromeos::ScopedTestUserManager test_user_manager_
;
285 storage_monitor::TestStorageMonitor monitor_
;
286 scoped_ptr
<TestingProfile
> profile_
;
287 MediaGalleriesPreferences
* gallery_prefs_
;
289 DISALLOW_COPY_AND_ASSIGN(MediaScanManagerTest
);
292 TEST_F(MediaScanManagerTest
, SingleResult
) {
293 size_t galleries_before
= gallery_count();
294 MediaGalleryScanResult file_counts
;
295 file_counts
.audio_count
= 1;
296 file_counts
.image_count
= 2;
297 file_counts
.video_count
= 3;
299 MakeTestFolder("found_media_folder", &path
);
301 MediaFolderFinder::MediaFolderFinderResults found_folders
;
302 found_folders
[path
] = file_counts
;
303 SetFindFoldersResults(true, found_folders
);
305 SetExpectedScanResults(1 /*gallery_count*/, file_counts
);
308 base::RunLoop().RunUntilIdle();
309 EXPECT_EQ(1, FindFolderDestroyCount());
310 EXPECT_EQ(galleries_before
+ 1, gallery_count());
313 // Generally test that it includes directories with sufficient density
314 // and excludes others.
317 // A/B/ - NOT included
319 // A/D/ - NOT included
324 // A/H/ - included in results
327 TEST_F(MediaScanManagerTest
, MergeRedundant
) {
329 MediaFolderFinder::MediaFolderFinderResults found_folders
;
330 std::vector
<base::FilePath
> sensitive_locations
;
331 std::vector
<base::FilePath
> expected_folders
;
332 MediaGalleryScanResult file_counts
;
333 file_counts
.audio_count
= 1;
334 file_counts
.image_count
= 2;
335 file_counts
.video_count
= 3;
336 MakeTestFolder("A", &path
);
337 MakeTestFolder("A/B", &path
);
338 MakeTestFolder("A/B/C", &path
);
339 found_folders
[path
] = file_counts
;
341 MakeTestFolder("A/D", &path
);
342 MakeTestFolder("A/D/E", &path
);
343 found_folders
[path
] = file_counts
;
344 MakeTestFolder("A/D/F", &path
);
345 found_folders
[path
] = file_counts
;
346 MakeTestFolder("A/D/G", &path
);
347 found_folders
[path
] = file_counts
;
348 MakeTestFolder("A/D/H", &path
);
349 // Dense enough to be reported.
350 MakeTestFolder("A/H", &path
);
351 expected_folders
.push_back(path
);
352 MakeTestFolder("A/H/I", &path
);
353 found_folders
[path
] = file_counts
;
354 MakeTestFolder("A/H/J", &path
);
355 found_folders
[path
] = file_counts
;
356 MediaFolderFinder::MediaFolderFinderResults results
=
357 FindContainerScanResults(found_folders
, sensitive_locations
);
358 EXPECT_EQ(expected_folders
.size(), results
.size());
359 for (std::vector
<base::FilePath
>::const_iterator it
=
360 expected_folders
.begin();
361 it
!= expected_folders
.end();
363 EXPECT_TRUE(results
.find(*it
) != results
.end());
367 // Make sure intermediates are not included.
369 // A/ - included in results
370 // A/B1/ - NOT included
372 // A/C1/ - NOT included
374 TEST_F(MediaScanManagerTest
, MergeRedundantNoIntermediates
) {
376 MediaFolderFinder::MediaFolderFinderResults found_folders
;
377 std::vector
<base::FilePath
> sensitive_locations
;
378 std::vector
<base::FilePath
> expected_folders
;
379 MediaGalleryScanResult file_counts
;
380 file_counts
.audio_count
= 1;
381 file_counts
.image_count
= 2;
382 file_counts
.video_count
= 3;
383 MakeTestFolder("A", &path
);
384 expected_folders
.push_back(path
);
385 MakeTestFolder("A/B1", &path
);
386 MakeTestFolder("A/B1/B2", &path
);
387 found_folders
[path
] = file_counts
;
388 MakeTestFolder("A/C1", &path
);
389 MakeTestFolder("A/C1/C2", &path
);
390 found_folders
[path
] = file_counts
;
391 // Make "home dir" not dense enough.
392 MakeTestFolder("D", &path
);
393 MediaFolderFinder::MediaFolderFinderResults results
=
394 FindContainerScanResults(found_folders
, sensitive_locations
);
395 EXPECT_EQ(expected_folders
.size(), results
.size());
396 for (std::vector
<base::FilePath
>::const_iterator it
=
397 expected_folders
.begin();
398 it
!= expected_folders
.end();
400 EXPECT_TRUE(results
.find(*it
) != results
.end());
404 // Make sure "A/" only gets a count of 1, from "A/D/",
405 // not 2 from "A/D/H/" and "A/D/I/".
408 // A/D/ - included in results
413 // A/D/I/ - NOT included
415 TEST_F(MediaScanManagerTest
, MergeRedundantVerifyNoOvercount
) {
417 MediaFolderFinder::MediaFolderFinderResults found_folders
;
418 std::vector
<base::FilePath
> sensitive_locations
;
419 std::vector
<base::FilePath
> expected_folders
;
420 MediaGalleryScanResult file_counts
;
421 file_counts
.audio_count
= 1;
422 file_counts
.image_count
= 2;
423 file_counts
.video_count
= 3;
424 MakeTestFolder("A", &path
);
425 MakeTestFolder("A/D", &path
);
426 expected_folders
.push_back(path
);
427 MakeTestFolder("A/D/E", &path
);
428 found_folders
[path
] = file_counts
;
429 MakeTestFolder("A/D/F", &path
);
430 found_folders
[path
] = file_counts
;
431 MakeTestFolder("A/D/G", &path
);
432 found_folders
[path
] = file_counts
;
433 MakeTestFolder("A/D/H", &path
);
434 found_folders
[path
] = file_counts
;
435 MakeTestFolder("A/D/I", &path
);
436 MakeTestFolder("A/D/I/J", &path
);
437 found_folders
[path
] = file_counts
;
438 MediaFolderFinder::MediaFolderFinderResults results
=
439 FindContainerScanResults(found_folders
, sensitive_locations
);
440 EXPECT_EQ(expected_folders
.size(), results
.size());
441 for (std::vector
<base::FilePath
>::const_iterator it
=
442 expected_folders
.begin();
443 it
!= expected_folders
.end();
445 EXPECT_TRUE(results
.find(*it
) != results
.end());
449 // Make sure that sensistive directories are pruned.
453 // A/C/ - included in results
456 // A/D/ - included in results
459 // A/E/ - included in results
462 // A/F/ - included in results
465 TEST_F(MediaScanManagerTest
, MergeRedundantWithSensitive
) {
467 MediaFolderFinder::MediaFolderFinderResults found_folders
;
468 std::vector
<base::FilePath
> sensitive_locations
;
469 std::vector
<base::FilePath
> expected_folders
;
470 MediaGalleryScanResult file_counts
;
471 file_counts
.audio_count
= 1;
472 file_counts
.image_count
= 2;
473 file_counts
.video_count
= 3;
474 MakeTestFolder("A", &path
);
475 MakeTestFolder("A/B", &path
);
476 sensitive_locations
.push_back(path
);
477 MakeTestFolder("A/C", &path
);
478 expected_folders
.push_back(path
);
479 MakeTestFolder("A/C/G", &path
);
480 found_folders
[path
] = file_counts
;
481 MakeTestFolder("A/C/H", &path
);
482 found_folders
[path
] = file_counts
;
483 MakeTestFolder("A/D", &path
);
484 expected_folders
.push_back(path
);
485 MakeTestFolder("A/D/I", &path
);
486 found_folders
[path
] = file_counts
;
487 MakeTestFolder("A/D/J", &path
);
488 found_folders
[path
] = file_counts
;
489 MakeTestFolder("A/E", &path
);
490 expected_folders
.push_back(path
);
491 MakeTestFolder("A/E/K", &path
);
492 found_folders
[path
] = file_counts
;
493 MakeTestFolder("A/E/L", &path
);
494 found_folders
[path
] = file_counts
;
495 MakeTestFolder("A/F", &path
);
496 expected_folders
.push_back(path
);
497 MakeTestFolder("A/F/M", &path
);
498 found_folders
[path
] = file_counts
;
499 MakeTestFolder("A/F/N", &path
);
500 found_folders
[path
] = file_counts
;
501 MediaFolderFinder::MediaFolderFinderResults results
=
502 FindContainerScanResults(found_folders
, sensitive_locations
);
503 EXPECT_EQ(expected_folders
.size(), results
.size());
504 for (std::vector
<base::FilePath
>::const_iterator it
=
505 expected_folders
.begin();
506 it
!= expected_folders
.end();
508 EXPECT_TRUE(results
.find(*it
) != results
.end());
512 TEST_F(MediaScanManagerTest
, Containers
) {
513 MediaGalleryScanResult file_counts
;
514 file_counts
.audio_count
= 1;
516 std::set
<base::FilePath
> expected_galleries
;
517 std::set
<base::FilePath
> bad_galleries
;
518 MediaFolderFinder::MediaFolderFinderResults found_folders
;
519 size_t galleries_before
= gallery_count();
521 // Should manifest as a gallery in result1.
522 MakeTestFolder("dir1/result1", &path
);
523 expected_galleries
.insert(path
);
524 found_folders
[path
] = file_counts
;
526 // Should manifest as a gallery in dir2.
527 MakeTestFolder("dir2/result2", &path
);
528 bad_galleries
.insert(path
);
529 found_folders
[path
] = file_counts
;
530 MakeTestFolder("dir2/result3", &path
);
531 bad_galleries
.insert(path
);
532 found_folders
[path
] = file_counts
;
533 expected_galleries
.insert(path
.DirName());
535 // Should manifest as a two galleries: result4 and result5.
536 MakeTestFolder("dir3/other", &path
);
537 bad_galleries
.insert(path
);
538 MakeTestFolder("dir3/result4", &path
);
539 expected_galleries
.insert(path
);
540 found_folders
[path
] = file_counts
;
541 MakeTestFolder("dir3/result5", &path
);
542 expected_galleries
.insert(path
);
543 found_folders
[path
] = file_counts
;
545 // Should manifest as a gallery in dir4.
546 MakeTestFolder("dir4/other", &path
);
547 bad_galleries
.insert(path
);
548 MakeTestFolder("dir4/result6", &path
);
549 bad_galleries
.insert(path
);
550 found_folders
[path
] = file_counts
;
551 MakeTestFolder("dir4/result7", &path
);
552 bad_galleries
.insert(path
);
553 found_folders
[path
] = file_counts
;
554 MakeTestFolder("dir4/result8", &path
);
555 bad_galleries
.insert(path
);
556 found_folders
[path
] = file_counts
;
557 MakeTestFolder("dir4/result9", &path
);
558 bad_galleries
.insert(path
);
559 found_folders
[path
] = file_counts
;
560 expected_galleries
.insert(path
.DirName());
562 SetFindFoldersResults(true, found_folders
);
564 file_counts
.audio_count
= 9;
565 SetExpectedScanResults(5 /*gallery_count*/, file_counts
);
568 base::RunLoop().RunUntilIdle();
569 EXPECT_EQ(1, FindFolderDestroyCount());
570 EXPECT_EQ(galleries_before
+ 5, gallery_count());
572 std::set
<base::FilePath
> found_galleries
;
573 for (MediaGalleriesPrefInfoMap::const_iterator it
= known_galleries().begin();
574 it
!= known_galleries().end();
576 found_galleries
.insert(it
->second
.AbsolutePath());
577 DCHECK(!ContainsKey(bad_galleries
, it
->second
.AbsolutePath()));
579 for (std::set
<base::FilePath
>::const_iterator it
= expected_galleries
.begin();
580 it
!= expected_galleries
.end();
582 DCHECK(ContainsKey(found_galleries
, *it
));
586 TEST_F(MediaScanManagerTest
, UpdateExistingScanResults
) {
587 size_t galleries_before
= gallery_count();
589 MediaGalleryPrefId ungranted_scan
=
590 AddGallery("uscan", MediaGalleryPrefInfo::kScanResult
, 1, 0, 0);
591 MediaGalleryPrefId granted_scan
=
592 AddGallery("gscan", MediaGalleryPrefInfo::kScanResult
, 0, 2, 0);
593 gallery_prefs()->SetGalleryPermissionForExtension(*extension(), granted_scan
,
595 EXPECT_EQ(galleries_before
+ 2, gallery_count());
597 // Run once with no scan results. "uscan" should go away and "gscan" should
598 // have its scan counts updated.
599 MediaFolderFinder::MediaFolderFinderResults found_folders
;
600 SetFindFoldersResults(true, found_folders
);
602 MediaGalleryScanResult file_counts
;
603 SetExpectedScanResults(0 /*gallery_count*/, file_counts
);
606 base::RunLoop().RunUntilIdle();
607 EXPECT_EQ(1, FindFolderDestroyCount());
608 EXPECT_EQ(galleries_before
+ 1, gallery_count());
609 CheckFileCounts(granted_scan
, 0, 0, 0);
611 MediaGalleryPrefId id
=
612 AddGallery("uscan", MediaGalleryPrefInfo::kScanResult
, 1, 1, 1);
613 EXPECT_NE(id
, ungranted_scan
);
616 // Add scan results near the existing scan results.
617 file_counts
.audio_count
= 0;
618 file_counts
.image_count
= 0;
619 file_counts
.video_count
= 7;
621 MakeTestFolder("uscan", &path
);
622 found_folders
[path
] = file_counts
;
624 file_counts
.video_count
= 11;
625 MakeTestFolder("gscan/dir1", &path
);
626 found_folders
[path
] = file_counts
;
628 MakeTestFolder("junk", &path
);
630 SetFindFoldersResults(true, found_folders
);
631 file_counts
.video_count
= 7;
632 SetExpectedScanResults(1 /*gallery_count*/, file_counts
);
635 base::RunLoop().RunUntilIdle();
636 EXPECT_EQ(2, FindFolderDestroyCount());
637 EXPECT_EQ(galleries_before
+ 2, gallery_count());
638 CheckFileCounts(granted_scan
, 0, 0, 11);
639 // The new scan result should be one more than it's previous id.
640 CheckFileCounts(ungranted_scan
+ 1, 0, 0, 7);
643 TEST_F(MediaScanManagerTest
, UpdateExistingCounts
) {
644 size_t galleries_before
= gallery_count();
646 MediaGalleryPrefId auto_id
=
647 AddGallery("auto", MediaGalleryPrefInfo::kAutoDetected
, 1, 0, 0);
648 MediaGalleryPrefId user_id
=
649 AddGallery("user", MediaGalleryPrefInfo::kUserAdded
, 0, 2, 0);
650 MediaGalleryPrefId scan_id
=
651 AddGallery("scan", MediaGalleryPrefInfo::kScanResult
, 0, 0, 3);
652 // Grant permission so this one isn't removed and readded.
653 gallery_prefs()->SetGalleryPermissionForExtension(*extension(), scan_id
,
655 CheckFileCounts(auto_id
, 1, 0, 0);
656 CheckFileCounts(user_id
, 0, 2, 0);
657 CheckFileCounts(scan_id
, 0, 0, 3);
659 MediaFolderFinder::MediaFolderFinderResults found_folders
;
660 MediaGalleryScanResult file_counts
;
661 file_counts
.audio_count
= 4;
663 MakeTestFolder("auto/dir1", &path
);
664 found_folders
[path
] = file_counts
;
666 file_counts
.audio_count
= 6;
667 MakeTestFolder("scan", &path
);
668 found_folders
[path
] = file_counts
;
670 MakeTestFolder("junk", &path
);
672 file_counts
.audio_count
= 5;
673 MakeTestFolder("user/dir2", &path
);
674 found_folders
[path
] = file_counts
;
676 SetFindFoldersResults(true, found_folders
);
678 file_counts
.audio_count
= 0;
679 SetExpectedScanResults(0 /*gallery_count*/, file_counts
);
682 base::RunLoop().RunUntilIdle();
683 EXPECT_EQ(1, FindFolderDestroyCount());
684 EXPECT_EQ(galleries_before
+ 3, gallery_count());
685 CheckFileCounts(auto_id
, 4, 0, 0);
686 CheckFileCounts(user_id
, 5, 0, 0);
687 CheckFileCounts(scan_id
, 6, 0, 0);
689 EXPECT_EQ(1U, found_folders
.erase(path
));
690 SetFindFoldersResults(true, found_folders
);
691 SetExpectedScanResults(0 /*gallery_count*/, file_counts
);
694 base::RunLoop().RunUntilIdle();
695 EXPECT_EQ(2, FindFolderDestroyCount());
696 EXPECT_EQ(galleries_before
+ 3, gallery_count());
697 CheckFileCounts(auto_id
, 4, 0, 0);
698 CheckFileCounts(user_id
, 0, 0, 0);
699 CheckFileCounts(scan_id
, 6, 0, 0);
702 TEST_F(MediaScanManagerTest
, Graylist
) {
703 size_t galleries_before
= gallery_count();
704 MediaGalleryScanResult file_counts
;
705 file_counts
.audio_count
= 1;
706 file_counts
.image_count
= 2;
707 file_counts
.video_count
= 3;
709 MakeTestFolder("found_media_folder", &path
);
710 base::ScopedPathOverride
scoped_fake_home_dir_override(base::DIR_HOME
, path
);
712 const size_t kGalleriesAdded
= 3;
713 MediaFolderFinder::MediaFolderFinderResults found_folders
;
714 MakeTestFolder("found_media_folder/dir1", &path
);
715 found_folders
[path
] = file_counts
;
716 MakeTestFolder("found_media_folder/dir2", &path
);
717 found_folders
[path
] = file_counts
;
718 MakeTestFolder("found_media_folder/dir3", &path
);
719 found_folders
[path
] = file_counts
;
720 SetFindFoldersResults(true, found_folders
);
722 file_counts
.audio_count
*= kGalleriesAdded
;
723 file_counts
.image_count
*= kGalleriesAdded
;
724 file_counts
.video_count
*= kGalleriesAdded
;
725 SetExpectedScanResults(kGalleriesAdded
, file_counts
);
728 base::RunLoop().RunUntilIdle();
729 EXPECT_EQ(1, FindFolderDestroyCount());
730 EXPECT_EQ(galleries_before
+ kGalleriesAdded
, gallery_count());