| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 | |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/files/scoped_temp_dir.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/test/scoped_path_override.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "chrome/browser/extensions/test_extension_system.h" | |
| 19 #include "chrome/browser/media_galleries/media_folder_finder.h" | |
| 20 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | |
| 21 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" | |
| 22 #include "chrome/browser/media_galleries/media_galleries_test_util.h" | |
| 23 #include "chrome/browser/media_galleries/media_scan_manager.h" | |
| 24 #include "chrome/browser/media_galleries/media_scan_manager_observer.h" | |
| 25 #include "chrome/test/base/testing_profile.h" | |
| 26 #include "components/storage_monitor/test_storage_monitor.h" | |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | |
| 28 #include "extensions/browser/extension_system.h" | |
| 29 #include "extensions/common/extension.h" | |
| 30 #include "extensions/common/permissions/media_galleries_permission.h" | |
| 31 #include "testing/gtest/include/gtest/gtest.h" | |
| 32 | |
| 33 #if defined(OS_CHROMEOS) | |
| 34 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" | |
| 35 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 36 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 37 #endif | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 class MockMediaFolderFinder : MediaFolderFinder { | |
| 42 public: | |
| 43 typedef base::Callback<void(MediaFolderFinderResultsCallback)> | |
| 44 FindFoldersStartedCallback; | |
| 45 | |
| 46 static MediaFolderFinder* CreateMockMediaFolderFinder( | |
| 47 const FindFoldersStartedCallback& started_callback, | |
| 48 const base::Closure destruction_callback, | |
| 49 const MediaFolderFinderResultsCallback& callback) { | |
| 50 return new MockMediaFolderFinder(started_callback, destruction_callback, | |
| 51 callback); | |
| 52 } | |
| 53 | |
| 54 MockMediaFolderFinder( | |
| 55 const FindFoldersStartedCallback& started_callback, | |
| 56 const base::Closure destruction_callback, | |
| 57 const MediaFolderFinderResultsCallback& callback) | |
| 58 : MediaFolderFinder(callback), | |
| 59 started_callback_(started_callback), | |
| 60 destruction_callback_(destruction_callback), | |
| 61 callback_(callback) { | |
| 62 } | |
| 63 ~MockMediaFolderFinder() override { destruction_callback_.Run(); } | |
| 64 | |
| 65 void StartScan() override { started_callback_.Run(callback_); } | |
| 66 | |
| 67 private: | |
| 68 FindFoldersStartedCallback started_callback_; | |
| 69 base::Closure destruction_callback_; | |
| 70 MediaFolderFinderResultsCallback callback_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(MockMediaFolderFinder); | |
| 73 }; | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 class TestMediaScanManager : public MediaScanManager { | |
| 78 public: | |
| 79 typedef base::Callback<MediaFolderFinder*( | |
| 80 const MediaFolderFinder::MediaFolderFinderResultsCallback&)> | |
| 81 MediaFolderFinderFactory; | |
| 82 | |
| 83 explicit TestMediaScanManager(const MediaFolderFinderFactory& factory) { | |
| 84 SetMediaFolderFinderFactory(factory); | |
| 85 } | |
| 86 ~TestMediaScanManager() override {} | |
| 87 | |
| 88 private: | |
| 89 DISALLOW_COPY_AND_ASSIGN(TestMediaScanManager); | |
| 90 }; | |
| 91 | |
| 92 class MediaScanManagerTest : public MediaScanManagerObserver, | |
| 93 public testing::Test { | |
| 94 public: | |
| 95 MediaScanManagerTest() | |
| 96 : find_folders_start_count_(0), | |
| 97 find_folders_destroy_count_(0), | |
| 98 find_folders_success_(false), | |
| 99 expected_gallery_count_(0), | |
| 100 profile_(new TestingProfile()) {} | |
| 101 | |
| 102 ~MediaScanManagerTest() override { | |
| 103 EXPECT_EQ(find_folders_start_count_, find_folders_destroy_count_); | |
| 104 } | |
| 105 | |
| 106 void SetUp() override { | |
| 107 ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateAndInstall()); | |
| 108 | |
| 109 extensions::TestExtensionSystem* extension_system( | |
| 110 static_cast<extensions::TestExtensionSystem*>( | |
| 111 extensions::ExtensionSystem::Get(profile_.get()))); | |
| 112 extension_system->CreateExtensionService( | |
| 113 base::CommandLine::ForCurrentProcess(), base::FilePath(), false); | |
| 114 | |
| 115 gallery_prefs_ = | |
| 116 MediaGalleriesPreferencesFactory::GetForProfile(profile_.get()); | |
| 117 base::RunLoop loop; | |
| 118 gallery_prefs_->EnsureInitialized(loop.QuitClosure()); | |
| 119 loop.Run(); | |
| 120 | |
| 121 std::vector<std::string> read_permissions; | |
| 122 read_permissions.push_back( | |
| 123 extensions::MediaGalleriesPermission::kReadPermission); | |
| 124 extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get()); | |
| 125 | |
| 126 ASSERT_TRUE(test_results_dir_.CreateUniqueTempDir()); | |
| 127 | |
| 128 MockMediaFolderFinder::FindFoldersStartedCallback started_callback = | |
| 129 base::Bind(&MediaScanManagerTest::OnFindFoldersStarted, | |
| 130 base::Unretained(this)); | |
| 131 base::Closure destruction_callback = | |
| 132 base::Bind(&MediaScanManagerTest::OnFindFoldersDestroyed, | |
| 133 base::Unretained(this)); | |
| 134 TestMediaScanManager::MediaFolderFinderFactory factory = | |
| 135 base::Bind(&MockMediaFolderFinder::CreateMockMediaFolderFinder, | |
| 136 started_callback, destruction_callback); | |
| 137 media_scan_manager_.reset(new TestMediaScanManager(factory)); | |
| 138 media_scan_manager_->AddObserver(profile_.get(), this); | |
| 139 } | |
| 140 | |
| 141 void TearDown() override { | |
| 142 media_scan_manager_->RemoveObserver(profile_.get()); | |
| 143 media_scan_manager_.reset(); | |
| 144 storage_monitor::TestStorageMonitor::Destroy(); | |
| 145 } | |
| 146 | |
| 147 // Create a test folder in the test specific scoped temp dir and return the | |
| 148 // final path in |full_path|. | |
| 149 void MakeTestFolder(const std::string& root_relative_path, | |
| 150 base::FilePath* full_path) { | |
| 151 ASSERT_TRUE(test_results_dir_.IsValid()); | |
| 152 *full_path = | |
| 153 test_results_dir_.path().AppendASCII(root_relative_path); | |
| 154 ASSERT_TRUE(base::CreateDirectory(*full_path)); | |
| 155 } | |
| 156 | |
| 157 // Create the specified path, and add it to preferences as a gallery. | |
| 158 MediaGalleryPrefId AddGallery(const std::string& rel_path, | |
| 159 MediaGalleryPrefInfo::Type type, | |
| 160 int audio_count, | |
| 161 int image_count, | |
| 162 int video_count) { | |
| 163 base::FilePath full_path; | |
| 164 MakeTestFolder(rel_path, &full_path); | |
| 165 MediaGalleryPrefInfo gallery_info; | |
| 166 gallery_prefs_->LookUpGalleryByPath(full_path, &gallery_info); | |
| 167 return gallery_prefs_->AddGallery(gallery_info.device_id, | |
| 168 gallery_info.path, | |
| 169 type, | |
| 170 gallery_info.volume_label, | |
| 171 gallery_info.vendor_name, | |
| 172 gallery_info.model_name, | |
| 173 gallery_info.total_size_in_bytes, | |
| 174 gallery_info.last_attach_time, | |
| 175 audio_count, image_count, video_count); | |
| 176 } | |
| 177 | |
| 178 void SetFindFoldersResults( | |
| 179 bool success, | |
| 180 const MediaFolderFinder::MediaFolderFinderResults& results) { | |
| 181 find_folders_success_ = success; | |
| 182 find_folders_results_ = results; | |
| 183 } | |
| 184 | |
| 185 void SetExpectedScanResults(int gallery_count, | |
| 186 const MediaGalleryScanResult& file_counts) { | |
| 187 expected_gallery_count_ = gallery_count; | |
| 188 expected_file_counts_ = file_counts; | |
| 189 } | |
| 190 | |
| 191 void StartScan() { | |
| 192 media_scan_manager_->StartScan( | |
| 193 profile_.get(), extension_.get(), true /* user_gesture */); | |
| 194 } | |
| 195 | |
| 196 MediaGalleriesPreferences* gallery_prefs() { | |
| 197 return gallery_prefs_; | |
| 198 } | |
| 199 | |
| 200 const MediaGalleriesPrefInfoMap& known_galleries() const { | |
| 201 return gallery_prefs_->known_galleries(); | |
| 202 } | |
| 203 | |
| 204 size_t gallery_count() const { | |
| 205 return known_galleries().size(); | |
| 206 } | |
| 207 | |
| 208 extensions::Extension* extension() { | |
| 209 return extension_.get(); | |
| 210 } | |
| 211 | |
| 212 int FindFoldersStartCount() { | |
| 213 return find_folders_start_count_; | |
| 214 } | |
| 215 | |
| 216 int FindFolderDestroyCount() { | |
| 217 return find_folders_destroy_count_; | |
| 218 } | |
| 219 | |
| 220 void CheckFileCounts(MediaGalleryPrefId pref_id, int audio_count, | |
| 221 int image_count, int video_count) { | |
| 222 if (!ContainsKey(known_galleries(), pref_id)) { | |
| 223 EXPECT_TRUE(false); | |
| 224 return; | |
| 225 } | |
| 226 MediaGalleriesPrefInfoMap::const_iterator pref_info = | |
| 227 known_galleries().find(pref_id); | |
| 228 EXPECT_EQ(audio_count, pref_info->second.audio_count); | |
| 229 EXPECT_EQ(image_count, pref_info->second.image_count); | |
| 230 EXPECT_EQ(video_count, pref_info->second.video_count); | |
| 231 } | |
| 232 | |
| 233 // MediaScanManagerObserver implementation. | |
| 234 void OnScanFinished(const std::string& extension_id, | |
| 235 int gallery_count, | |
| 236 const MediaGalleryScanResult& file_counts) override { | |
| 237 EXPECT_EQ(extension_->id(), extension_id); | |
| 238 EXPECT_EQ(expected_gallery_count_, gallery_count); | |
| 239 EXPECT_EQ(expected_file_counts_.audio_count, file_counts.audio_count); | |
| 240 EXPECT_EQ(expected_file_counts_.image_count, file_counts.image_count); | |
| 241 EXPECT_EQ(expected_file_counts_.video_count, file_counts.video_count); | |
| 242 } | |
| 243 | |
| 244 protected: | |
| 245 // So derived tests can access ...::FindContainerScanResults(). | |
| 246 MediaFolderFinder::MediaFolderFinderResults FindContainerScanResults( | |
| 247 const MediaFolderFinder::MediaFolderFinderResults& found_folders, | |
| 248 const std::vector<base::FilePath>& sensitive_locations) { | |
| 249 return MediaScanManager::FindContainerScanResults(found_folders, | |
| 250 sensitive_locations); | |
| 251 } | |
| 252 | |
| 253 private: | |
| 254 void OnFindFoldersStarted( | |
| 255 MediaFolderFinder::MediaFolderFinderResultsCallback callback) { | |
| 256 find_folders_start_count_++; | |
| 257 callback.Run(find_folders_success_, find_folders_results_); | |
| 258 } | |
| 259 | |
| 260 void OnFindFoldersDestroyed() { | |
| 261 find_folders_destroy_count_++; | |
| 262 } | |
| 263 | |
| 264 scoped_ptr<TestMediaScanManager> media_scan_manager_; | |
| 265 | |
| 266 int find_folders_start_count_; | |
| 267 int find_folders_destroy_count_; | |
| 268 bool find_folders_success_; | |
| 269 MediaFolderFinder::MediaFolderFinderResults find_folders_results_; | |
| 270 | |
| 271 int expected_gallery_count_; | |
| 272 MediaGalleryScanResult expected_file_counts_; | |
| 273 | |
| 274 base::ScopedTempDir test_results_dir_; | |
| 275 | |
| 276 // Needed for extension service & friends to work. | |
| 277 content::TestBrowserThreadBundle thread_bundle_; | |
| 278 | |
| 279 scoped_refptr<extensions::Extension> extension_; | |
| 280 | |
| 281 EnsureMediaDirectoriesExists mock_gallery_locations_; | |
| 282 | |
| 283 #if defined(OS_CHROMEOS) | |
| 284 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
| 285 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
| 286 chromeos::ScopedTestUserManager test_user_manager_; | |
| 287 #endif | |
| 288 | |
| 289 storage_monitor::TestStorageMonitor monitor_; | |
| 290 scoped_ptr<TestingProfile> profile_; | |
| 291 MediaGalleriesPreferences* gallery_prefs_; | |
| 292 | |
| 293 DISALLOW_COPY_AND_ASSIGN(MediaScanManagerTest); | |
| 294 }; | |
| 295 | |
| 296 TEST_F(MediaScanManagerTest, SingleResult) { | |
| 297 size_t galleries_before = gallery_count(); | |
| 298 MediaGalleryScanResult file_counts; | |
| 299 file_counts.audio_count = 1; | |
| 300 file_counts.image_count = 2; | |
| 301 file_counts.video_count = 3; | |
| 302 base::FilePath path; | |
| 303 MakeTestFolder("found_media_folder", &path); | |
| 304 | |
| 305 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 306 found_folders[path] = file_counts; | |
| 307 SetFindFoldersResults(true, found_folders); | |
| 308 | |
| 309 SetExpectedScanResults(1 /*gallery_count*/, file_counts); | |
| 310 StartScan(); | |
| 311 | |
| 312 base::RunLoop().RunUntilIdle(); | |
| 313 EXPECT_EQ(1, FindFolderDestroyCount()); | |
| 314 EXPECT_EQ(galleries_before + 1, gallery_count()); | |
| 315 } | |
| 316 | |
| 317 // Generally test that it includes directories with sufficient density | |
| 318 // and excludes others. | |
| 319 // | |
| 320 // A/ - NOT included | |
| 321 // A/B/ - NOT included | |
| 322 // A/B/C/files | |
| 323 // A/D/ - NOT included | |
| 324 // A/D/E/files | |
| 325 // A/D/F/files | |
| 326 // A/D/G/files | |
| 327 // A/D/H/ | |
| 328 // A/H/ - included in results | |
| 329 // A/H/I/files | |
| 330 // A/H/J/files | |
| 331 TEST_F(MediaScanManagerTest, MergeRedundant) { | |
| 332 base::FilePath path; | |
| 333 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 334 std::vector<base::FilePath> sensitive_locations; | |
| 335 std::vector<base::FilePath> expected_folders; | |
| 336 MediaGalleryScanResult file_counts; | |
| 337 file_counts.audio_count = 1; | |
| 338 file_counts.image_count = 2; | |
| 339 file_counts.video_count = 3; | |
| 340 MakeTestFolder("A", &path); | |
| 341 MakeTestFolder("A/B", &path); | |
| 342 MakeTestFolder("A/B/C", &path); | |
| 343 found_folders[path] = file_counts; | |
| 344 // Not dense enough. | |
| 345 MakeTestFolder("A/D", &path); | |
| 346 MakeTestFolder("A/D/E", &path); | |
| 347 found_folders[path] = file_counts; | |
| 348 MakeTestFolder("A/D/F", &path); | |
| 349 found_folders[path] = file_counts; | |
| 350 MakeTestFolder("A/D/G", &path); | |
| 351 found_folders[path] = file_counts; | |
| 352 MakeTestFolder("A/D/H", &path); | |
| 353 // Dense enough to be reported. | |
| 354 MakeTestFolder("A/H", &path); | |
| 355 expected_folders.push_back(path); | |
| 356 MakeTestFolder("A/H/I", &path); | |
| 357 found_folders[path] = file_counts; | |
| 358 MakeTestFolder("A/H/J", &path); | |
| 359 found_folders[path] = file_counts; | |
| 360 MediaFolderFinder::MediaFolderFinderResults results = | |
| 361 FindContainerScanResults(found_folders, sensitive_locations); | |
| 362 EXPECT_EQ(expected_folders.size(), results.size()); | |
| 363 for (std::vector<base::FilePath>::const_iterator it = | |
| 364 expected_folders.begin(); | |
| 365 it != expected_folders.end(); | |
| 366 ++it) { | |
| 367 EXPECT_TRUE(results.find(*it) != results.end()); | |
| 368 } | |
| 369 } | |
| 370 | |
| 371 // Make sure intermediates are not included. | |
| 372 // | |
| 373 // A/ - included in results | |
| 374 // A/B1/ - NOT included | |
| 375 // A/B1/B2/files | |
| 376 // A/C1/ - NOT included | |
| 377 // A/C1/C2/files | |
| 378 TEST_F(MediaScanManagerTest, MergeRedundantNoIntermediates) { | |
| 379 base::FilePath path; | |
| 380 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 381 std::vector<base::FilePath> sensitive_locations; | |
| 382 std::vector<base::FilePath> expected_folders; | |
| 383 MediaGalleryScanResult file_counts; | |
| 384 file_counts.audio_count = 1; | |
| 385 file_counts.image_count = 2; | |
| 386 file_counts.video_count = 3; | |
| 387 MakeTestFolder("A", &path); | |
| 388 expected_folders.push_back(path); | |
| 389 MakeTestFolder("A/B1", &path); | |
| 390 MakeTestFolder("A/B1/B2", &path); | |
| 391 found_folders[path] = file_counts; | |
| 392 MakeTestFolder("A/C1", &path); | |
| 393 MakeTestFolder("A/C1/C2", &path); | |
| 394 found_folders[path] = file_counts; | |
| 395 // Make "home dir" not dense enough. | |
| 396 MakeTestFolder("D", &path); | |
| 397 MediaFolderFinder::MediaFolderFinderResults results = | |
| 398 FindContainerScanResults(found_folders, sensitive_locations); | |
| 399 EXPECT_EQ(expected_folders.size(), results.size()); | |
| 400 for (std::vector<base::FilePath>::const_iterator it = | |
| 401 expected_folders.begin(); | |
| 402 it != expected_folders.end(); | |
| 403 ++it) { | |
| 404 EXPECT_TRUE(results.find(*it) != results.end()); | |
| 405 } | |
| 406 } | |
| 407 | |
| 408 // Make sure "A/" only gets a count of 1, from "A/D/", | |
| 409 // not 2 from "A/D/H/" and "A/D/I/". | |
| 410 // | |
| 411 // A/ - NOT included | |
| 412 // A/D/ - included in results | |
| 413 // A/D/E/files | |
| 414 // A/D/F/files | |
| 415 // A/D/G/files | |
| 416 // A/D/H/files | |
| 417 // A/D/I/ - NOT included | |
| 418 // A/D/I/J/files | |
| 419 TEST_F(MediaScanManagerTest, MergeRedundantVerifyNoOvercount) { | |
| 420 base::FilePath path; | |
| 421 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 422 std::vector<base::FilePath> sensitive_locations; | |
| 423 std::vector<base::FilePath> expected_folders; | |
| 424 MediaGalleryScanResult file_counts; | |
| 425 file_counts.audio_count = 1; | |
| 426 file_counts.image_count = 2; | |
| 427 file_counts.video_count = 3; | |
| 428 MakeTestFolder("A", &path); | |
| 429 MakeTestFolder("A/D", &path); | |
| 430 expected_folders.push_back(path); | |
| 431 MakeTestFolder("A/D/E", &path); | |
| 432 found_folders[path] = file_counts; | |
| 433 MakeTestFolder("A/D/F", &path); | |
| 434 found_folders[path] = file_counts; | |
| 435 MakeTestFolder("A/D/G", &path); | |
| 436 found_folders[path] = file_counts; | |
| 437 MakeTestFolder("A/D/H", &path); | |
| 438 found_folders[path] = file_counts; | |
| 439 MakeTestFolder("A/D/I", &path); | |
| 440 MakeTestFolder("A/D/I/J", &path); | |
| 441 found_folders[path] = file_counts; | |
| 442 MediaFolderFinder::MediaFolderFinderResults results = | |
| 443 FindContainerScanResults(found_folders, sensitive_locations); | |
| 444 EXPECT_EQ(expected_folders.size(), results.size()); | |
| 445 for (std::vector<base::FilePath>::const_iterator it = | |
| 446 expected_folders.begin(); | |
| 447 it != expected_folders.end(); | |
| 448 ++it) { | |
| 449 EXPECT_TRUE(results.find(*it) != results.end()); | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 // Make sure that sensistive directories are pruned. | |
| 454 // | |
| 455 // A/ - NOT included | |
| 456 // A/B/ - sensitive | |
| 457 // A/C/ - included in results | |
| 458 // A/C/G/files | |
| 459 // A/C/H/files | |
| 460 // A/D/ - included in results | |
| 461 // A/D/I/files | |
| 462 // A/D/J/files | |
| 463 // A/E/ - included in results | |
| 464 // A/E/K/files | |
| 465 // A/E/L/files | |
| 466 // A/F/ - included in results | |
| 467 // A/F/M/files | |
| 468 // A/F/N/files | |
| 469 TEST_F(MediaScanManagerTest, MergeRedundantWithSensitive) { | |
| 470 base::FilePath path; | |
| 471 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 472 std::vector<base::FilePath> sensitive_locations; | |
| 473 std::vector<base::FilePath> expected_folders; | |
| 474 MediaGalleryScanResult file_counts; | |
| 475 file_counts.audio_count = 1; | |
| 476 file_counts.image_count = 2; | |
| 477 file_counts.video_count = 3; | |
| 478 MakeTestFolder("A", &path); | |
| 479 MakeTestFolder("A/B", &path); | |
| 480 sensitive_locations.push_back(path); | |
| 481 MakeTestFolder("A/C", &path); | |
| 482 expected_folders.push_back(path); | |
| 483 MakeTestFolder("A/C/G", &path); | |
| 484 found_folders[path] = file_counts; | |
| 485 MakeTestFolder("A/C/H", &path); | |
| 486 found_folders[path] = file_counts; | |
| 487 MakeTestFolder("A/D", &path); | |
| 488 expected_folders.push_back(path); | |
| 489 MakeTestFolder("A/D/I", &path); | |
| 490 found_folders[path] = file_counts; | |
| 491 MakeTestFolder("A/D/J", &path); | |
| 492 found_folders[path] = file_counts; | |
| 493 MakeTestFolder("A/E", &path); | |
| 494 expected_folders.push_back(path); | |
| 495 MakeTestFolder("A/E/K", &path); | |
| 496 found_folders[path] = file_counts; | |
| 497 MakeTestFolder("A/E/L", &path); | |
| 498 found_folders[path] = file_counts; | |
| 499 MakeTestFolder("A/F", &path); | |
| 500 expected_folders.push_back(path); | |
| 501 MakeTestFolder("A/F/M", &path); | |
| 502 found_folders[path] = file_counts; | |
| 503 MakeTestFolder("A/F/N", &path); | |
| 504 found_folders[path] = file_counts; | |
| 505 MediaFolderFinder::MediaFolderFinderResults results = | |
| 506 FindContainerScanResults(found_folders, sensitive_locations); | |
| 507 EXPECT_EQ(expected_folders.size(), results.size()); | |
| 508 for (std::vector<base::FilePath>::const_iterator it = | |
| 509 expected_folders.begin(); | |
| 510 it != expected_folders.end(); | |
| 511 ++it) { | |
| 512 EXPECT_TRUE(results.find(*it) != results.end()); | |
| 513 } | |
| 514 } | |
| 515 | |
| 516 TEST_F(MediaScanManagerTest, Containers) { | |
| 517 MediaGalleryScanResult file_counts; | |
| 518 file_counts.audio_count = 1; | |
| 519 base::FilePath path; | |
| 520 std::set<base::FilePath> expected_galleries; | |
| 521 std::set<base::FilePath> bad_galleries; | |
| 522 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 523 size_t galleries_before = gallery_count(); | |
| 524 | |
| 525 // Should manifest as a gallery in result1. | |
| 526 MakeTestFolder("dir1/result1", &path); | |
| 527 expected_galleries.insert(path); | |
| 528 found_folders[path] = file_counts; | |
| 529 | |
| 530 // Should manifest as a gallery in dir2. | |
| 531 MakeTestFolder("dir2/result2", &path); | |
| 532 bad_galleries.insert(path); | |
| 533 found_folders[path] = file_counts; | |
| 534 MakeTestFolder("dir2/result3", &path); | |
| 535 bad_galleries.insert(path); | |
| 536 found_folders[path] = file_counts; | |
| 537 expected_galleries.insert(path.DirName()); | |
| 538 | |
| 539 // Should manifest as a two galleries: result4 and result5. | |
| 540 MakeTestFolder("dir3/other", &path); | |
| 541 bad_galleries.insert(path); | |
| 542 MakeTestFolder("dir3/result4", &path); | |
| 543 expected_galleries.insert(path); | |
| 544 found_folders[path] = file_counts; | |
| 545 MakeTestFolder("dir3/result5", &path); | |
| 546 expected_galleries.insert(path); | |
| 547 found_folders[path] = file_counts; | |
| 548 | |
| 549 // Should manifest as a gallery in dir4. | |
| 550 MakeTestFolder("dir4/other", &path); | |
| 551 bad_galleries.insert(path); | |
| 552 MakeTestFolder("dir4/result6", &path); | |
| 553 bad_galleries.insert(path); | |
| 554 found_folders[path] = file_counts; | |
| 555 MakeTestFolder("dir4/result7", &path); | |
| 556 bad_galleries.insert(path); | |
| 557 found_folders[path] = file_counts; | |
| 558 MakeTestFolder("dir4/result8", &path); | |
| 559 bad_galleries.insert(path); | |
| 560 found_folders[path] = file_counts; | |
| 561 MakeTestFolder("dir4/result9", &path); | |
| 562 bad_galleries.insert(path); | |
| 563 found_folders[path] = file_counts; | |
| 564 expected_galleries.insert(path.DirName()); | |
| 565 | |
| 566 SetFindFoldersResults(true, found_folders); | |
| 567 | |
| 568 file_counts.audio_count = 9; | |
| 569 SetExpectedScanResults(5 /*gallery_count*/, file_counts); | |
| 570 StartScan(); | |
| 571 | |
| 572 base::RunLoop().RunUntilIdle(); | |
| 573 EXPECT_EQ(1, FindFolderDestroyCount()); | |
| 574 EXPECT_EQ(galleries_before + 5, gallery_count()); | |
| 575 | |
| 576 std::set<base::FilePath> found_galleries; | |
| 577 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries().begin(); | |
| 578 it != known_galleries().end(); | |
| 579 ++it) { | |
| 580 found_galleries.insert(it->second.AbsolutePath()); | |
| 581 DCHECK(!ContainsKey(bad_galleries, it->second.AbsolutePath())); | |
| 582 } | |
| 583 for (std::set<base::FilePath>::const_iterator it = expected_galleries.begin(); | |
| 584 it != expected_galleries.end(); | |
| 585 ++it) { | |
| 586 DCHECK(ContainsKey(found_galleries, *it)); | |
| 587 } | |
| 588 } | |
| 589 | |
| 590 TEST_F(MediaScanManagerTest, UpdateExistingScanResults) { | |
| 591 size_t galleries_before = gallery_count(); | |
| 592 | |
| 593 MediaGalleryPrefId ungranted_scan = | |
| 594 AddGallery("uscan", MediaGalleryPrefInfo::kScanResult, 1, 0, 0); | |
| 595 MediaGalleryPrefId granted_scan = | |
| 596 AddGallery("gscan", MediaGalleryPrefInfo::kScanResult, 0, 2, 0); | |
| 597 gallery_prefs()->SetGalleryPermissionForExtension(*extension(), granted_scan, | |
| 598 true); | |
| 599 EXPECT_EQ(galleries_before + 2, gallery_count()); | |
| 600 | |
| 601 // Run once with no scan results. "uscan" should go away and "gscan" should | |
| 602 // have its scan counts updated. | |
| 603 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 604 SetFindFoldersResults(true, found_folders); | |
| 605 | |
| 606 MediaGalleryScanResult file_counts; | |
| 607 SetExpectedScanResults(0 /*gallery_count*/, file_counts); | |
| 608 StartScan(); | |
| 609 | |
| 610 base::RunLoop().RunUntilIdle(); | |
| 611 EXPECT_EQ(1, FindFolderDestroyCount()); | |
| 612 EXPECT_EQ(galleries_before + 1, gallery_count()); | |
| 613 CheckFileCounts(granted_scan, 0, 0, 0); | |
| 614 | |
| 615 MediaGalleryPrefId id = | |
| 616 AddGallery("uscan", MediaGalleryPrefInfo::kScanResult, 1, 1, 1); | |
| 617 EXPECT_NE(id, ungranted_scan); | |
| 618 ungranted_scan = id; | |
| 619 | |
| 620 // Add scan results near the existing scan results. | |
| 621 file_counts.audio_count = 0; | |
| 622 file_counts.image_count = 0; | |
| 623 file_counts.video_count = 7; | |
| 624 base::FilePath path; | |
| 625 MakeTestFolder("uscan", &path); | |
| 626 found_folders[path] = file_counts; | |
| 627 | |
| 628 file_counts.video_count = 11; | |
| 629 MakeTestFolder("gscan/dir1", &path); | |
| 630 found_folders[path] = file_counts; | |
| 631 | |
| 632 MakeTestFolder("junk", &path); | |
| 633 | |
| 634 SetFindFoldersResults(true, found_folders); | |
| 635 file_counts.video_count = 7; | |
| 636 SetExpectedScanResults(1 /*gallery_count*/, file_counts); | |
| 637 StartScan(); | |
| 638 | |
| 639 base::RunLoop().RunUntilIdle(); | |
| 640 EXPECT_EQ(2, FindFolderDestroyCount()); | |
| 641 EXPECT_EQ(galleries_before + 2, gallery_count()); | |
| 642 CheckFileCounts(granted_scan, 0, 0, 11); | |
| 643 // The new scan result should be one more than it's previous id. | |
| 644 CheckFileCounts(ungranted_scan + 1, 0, 0, 7); | |
| 645 } | |
| 646 | |
| 647 TEST_F(MediaScanManagerTest, UpdateExistingCounts) { | |
| 648 size_t galleries_before = gallery_count(); | |
| 649 | |
| 650 MediaGalleryPrefId auto_id = | |
| 651 AddGallery("auto", MediaGalleryPrefInfo::kAutoDetected, 1, 0, 0); | |
| 652 MediaGalleryPrefId user_id = | |
| 653 AddGallery("user", MediaGalleryPrefInfo::kUserAdded, 0, 2, 0); | |
| 654 MediaGalleryPrefId scan_id = | |
| 655 AddGallery("scan", MediaGalleryPrefInfo::kScanResult, 0, 0, 3); | |
| 656 // Grant permission so this one isn't removed and readded. | |
| 657 gallery_prefs()->SetGalleryPermissionForExtension(*extension(), scan_id, | |
| 658 true); | |
| 659 CheckFileCounts(auto_id, 1, 0, 0); | |
| 660 CheckFileCounts(user_id, 0, 2, 0); | |
| 661 CheckFileCounts(scan_id, 0, 0, 3); | |
| 662 | |
| 663 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 664 MediaGalleryScanResult file_counts; | |
| 665 file_counts.audio_count = 4; | |
| 666 base::FilePath path; | |
| 667 MakeTestFolder("auto/dir1", &path); | |
| 668 found_folders[path] = file_counts; | |
| 669 | |
| 670 file_counts.audio_count = 6; | |
| 671 MakeTestFolder("scan", &path); | |
| 672 found_folders[path] = file_counts; | |
| 673 | |
| 674 MakeTestFolder("junk", &path); | |
| 675 | |
| 676 file_counts.audio_count = 5; | |
| 677 MakeTestFolder("user/dir2", &path); | |
| 678 found_folders[path] = file_counts; | |
| 679 | |
| 680 SetFindFoldersResults(true, found_folders); | |
| 681 | |
| 682 file_counts.audio_count = 0; | |
| 683 SetExpectedScanResults(0 /*gallery_count*/, file_counts); | |
| 684 StartScan(); | |
| 685 | |
| 686 base::RunLoop().RunUntilIdle(); | |
| 687 EXPECT_EQ(1, FindFolderDestroyCount()); | |
| 688 EXPECT_EQ(galleries_before + 3, gallery_count()); | |
| 689 CheckFileCounts(auto_id, 4, 0, 0); | |
| 690 CheckFileCounts(user_id, 5, 0, 0); | |
| 691 CheckFileCounts(scan_id, 6, 0, 0); | |
| 692 | |
| 693 EXPECT_EQ(1U, found_folders.erase(path)); | |
| 694 SetFindFoldersResults(true, found_folders); | |
| 695 SetExpectedScanResults(0 /*gallery_count*/, file_counts); | |
| 696 StartScan(); | |
| 697 | |
| 698 base::RunLoop().RunUntilIdle(); | |
| 699 EXPECT_EQ(2, FindFolderDestroyCount()); | |
| 700 EXPECT_EQ(galleries_before + 3, gallery_count()); | |
| 701 CheckFileCounts(auto_id, 4, 0, 0); | |
| 702 CheckFileCounts(user_id, 0, 0, 0); | |
| 703 CheckFileCounts(scan_id, 6, 0, 0); | |
| 704 } | |
| 705 | |
| 706 TEST_F(MediaScanManagerTest, Graylist) { | |
| 707 size_t galleries_before = gallery_count(); | |
| 708 MediaGalleryScanResult file_counts; | |
| 709 file_counts.audio_count = 1; | |
| 710 file_counts.image_count = 2; | |
| 711 file_counts.video_count = 3; | |
| 712 base::FilePath path; | |
| 713 MakeTestFolder("found_media_folder", &path); | |
| 714 base::ScopedPathOverride scoped_fake_home_dir_override(base::DIR_HOME, path); | |
| 715 | |
| 716 const size_t kGalleriesAdded = 3; | |
| 717 MediaFolderFinder::MediaFolderFinderResults found_folders; | |
| 718 MakeTestFolder("found_media_folder/dir1", &path); | |
| 719 found_folders[path] = file_counts; | |
| 720 MakeTestFolder("found_media_folder/dir2", &path); | |
| 721 found_folders[path] = file_counts; | |
| 722 MakeTestFolder("found_media_folder/dir3", &path); | |
| 723 found_folders[path] = file_counts; | |
| 724 SetFindFoldersResults(true, found_folders); | |
| 725 | |
| 726 file_counts.audio_count *= kGalleriesAdded; | |
| 727 file_counts.image_count *= kGalleriesAdded; | |
| 728 file_counts.video_count *= kGalleriesAdded; | |
| 729 SetExpectedScanResults(kGalleriesAdded, file_counts); | |
| 730 StartScan(); | |
| 731 | |
| 732 base::RunLoop().RunUntilIdle(); | |
| 733 EXPECT_EQ(1, FindFolderDestroyCount()); | |
| 734 EXPECT_EQ(galleries_before + kGalleriesAdded, gallery_count()); | |
| 735 } | |
| OLD | NEW |