Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_scan_manager_unittest.cc
blobd2bb73f535db8e479732a1ad575c9fc7d83c44a8
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"
6 #include "base/bind.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"
33 #endif
35 namespace {
37 class MockMediaFolderFinder : MediaFolderFinder {
38 public:
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,
47 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),
57 callback_(callback) {
59 ~MockMediaFolderFinder() override { destruction_callback_.Run(); }
61 void StartScan() override { started_callback_.Run(callback_); }
63 private:
64 FindFoldersStartedCallback started_callback_;
65 base::Closure destruction_callback_;
66 MediaFolderFinderResultsCallback callback_;
68 DISALLOW_COPY_AND_ASSIGN(MockMediaFolderFinder);
71 } // namespace
73 class TestMediaScanManager : public MediaScanManager {
74 public:
75 typedef base::Callback<MediaFolderFinder*(
76 const MediaFolderFinder::MediaFolderFinderResultsCallback&)>
77 MediaFolderFinderFactory;
79 explicit TestMediaScanManager(const MediaFolderFinderFactory& factory) {
80 SetMediaFolderFinderFactory(factory);
82 ~TestMediaScanManager() override {}
84 private:
85 DISALLOW_COPY_AND_ASSIGN(TestMediaScanManager);
88 class MediaScanManagerTest : public MediaScanManagerObserver,
89 public testing::Test {
90 public:
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);
111 gallery_prefs_ =
112 MediaGalleriesPreferencesFactory::GetForProfile(profile_.get());
113 base::RunLoop loop;
114 gallery_prefs_->EnsureInitialized(loop.QuitClosure());
115 loop.Run();
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());
148 *full_path =
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,
156 int audio_count,
157 int image_count,
158 int video_count) {
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,
164 gallery_info.path,
165 type,
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(
175 bool success,
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;
187 void StartScan() {
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)) {
219 EXPECT_TRUE(false);
220 return;
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,
231 int gallery_count,
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);
240 protected:
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);
249 private:
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_;
283 #endif
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;
298 base::FilePath path;
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);
306 StartScan();
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.
316 // A/ - NOT included
317 // A/B/ - NOT included
318 // A/B/C/files
319 // A/D/ - NOT included
320 // A/D/E/files
321 // A/D/F/files
322 // A/D/G/files
323 // A/D/H/
324 // A/H/ - included in results
325 // A/H/I/files
326 // A/H/J/files
327 TEST_F(MediaScanManagerTest, MergeRedundant) {
328 base::FilePath path;
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;
340 // Not dense enough.
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();
362 ++it) {
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
371 // A/B1/B2/files
372 // A/C1/ - NOT included
373 // A/C1/C2/files
374 TEST_F(MediaScanManagerTest, MergeRedundantNoIntermediates) {
375 base::FilePath path;
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();
399 ++it) {
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/".
407 // A/ - NOT included
408 // A/D/ - included in results
409 // A/D/E/files
410 // A/D/F/files
411 // A/D/G/files
412 // A/D/H/files
413 // A/D/I/ - NOT included
414 // A/D/I/J/files
415 TEST_F(MediaScanManagerTest, MergeRedundantVerifyNoOvercount) {
416 base::FilePath path;
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();
444 ++it) {
445 EXPECT_TRUE(results.find(*it) != results.end());
449 // Make sure that sensistive directories are pruned.
451 // A/ - NOT included
452 // A/B/ - sensitive
453 // A/C/ - included in results
454 // A/C/G/files
455 // A/C/H/files
456 // A/D/ - included in results
457 // A/D/I/files
458 // A/D/J/files
459 // A/E/ - included in results
460 // A/E/K/files
461 // A/E/L/files
462 // A/F/ - included in results
463 // A/F/M/files
464 // A/F/N/files
465 TEST_F(MediaScanManagerTest, MergeRedundantWithSensitive) {
466 base::FilePath path;
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();
507 ++it) {
508 EXPECT_TRUE(results.find(*it) != results.end());
512 TEST_F(MediaScanManagerTest, Containers) {
513 MediaGalleryScanResult file_counts;
514 file_counts.audio_count = 1;
515 base::FilePath path;
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);
566 StartScan();
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();
575 ++it) {
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();
581 ++it) {
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,
594 true);
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);
604 StartScan();
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);
614 ungranted_scan = id;
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;
620 base::FilePath path;
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);
633 StartScan();
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,
654 true);
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;
662 base::FilePath path;
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);
680 StartScan();
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);
692 StartScan();
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;
708 base::FilePath path;
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);
726 StartScan();
728 base::RunLoop().RunUntilIdle();
729 EXPECT_EQ(1, FindFolderDestroyCount());
730 EXPECT_EQ(galleries_before + kGalleriesAdded, gallery_count());