| 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/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/strings/string16.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "chrome/browser/extensions/test_extension_system.h" | |
| 19 #include "chrome/browser/media_galleries/media_galleries_dialog_controller_test_
util.h" | |
| 20 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | |
| 21 #include "chrome/browser/media_galleries/media_galleries_scan_result_controller.
h" | |
| 22 #include "chrome/browser/media_galleries/media_galleries_test_util.h" | |
| 23 #include "chrome/test/base/testing_profile.h" | |
| 24 #include "components/storage_monitor/test_storage_monitor.h" | |
| 25 #include "content/public/test/test_browser_thread_bundle.h" | |
| 26 #include "extensions/browser/extension_system.h" | |
| 27 #include "extensions/common/extension.h" | |
| 28 #include "extensions/common/permissions/media_galleries_permission.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | |
| 30 | |
| 31 #if defined(OS_CHROMEOS) | |
| 32 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" | |
| 33 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 34 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 35 #endif | |
| 36 | |
| 37 class MediaGalleriesScanResultControllerTest : public testing::Test { | |
| 38 public: | |
| 39 MediaGalleriesScanResultControllerTest() | |
| 40 : dialog_(NULL), | |
| 41 dialog_update_count_at_destruction_(0), | |
| 42 controller_(NULL), | |
| 43 profile_(new TestingProfile()), | |
| 44 weak_factory_(this) { | |
| 45 } | |
| 46 | |
| 47 ~MediaGalleriesScanResultControllerTest() override { | |
| 48 EXPECT_FALSE(controller_); | |
| 49 EXPECT_FALSE(dialog_); | |
| 50 } | |
| 51 | |
| 52 void SetUp() override { | |
| 53 ASSERT_TRUE(storage_monitor::TestStorageMonitor::CreateAndInstall()); | |
| 54 | |
| 55 extensions::TestExtensionSystem* extension_system( | |
| 56 static_cast<extensions::TestExtensionSystem*>( | |
| 57 extensions::ExtensionSystem::Get(profile_.get()))); | |
| 58 extension_system->CreateExtensionService( | |
| 59 base::CommandLine::ForCurrentProcess(), base::FilePath(), false); | |
| 60 | |
| 61 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get())); | |
| 62 base::RunLoop loop; | |
| 63 gallery_prefs_->EnsureInitialized(loop.QuitClosure()); | |
| 64 loop.Run(); | |
| 65 | |
| 66 std::vector<std::string> read_permissions; | |
| 67 read_permissions.push_back( | |
| 68 extensions::MediaGalleriesPermission::kReadPermission); | |
| 69 extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get()); | |
| 70 } | |
| 71 | |
| 72 void TearDown() override { storage_monitor::TestStorageMonitor::Destroy(); } | |
| 73 | |
| 74 void StartDialog() { | |
| 75 ASSERT_FALSE(controller_); | |
| 76 controller_ = new MediaGalleriesScanResultController( | |
| 77 *extension_.get(), | |
| 78 gallery_prefs_.get(), | |
| 79 base::Bind( | |
| 80 &MediaGalleriesScanResultControllerTest::CreateMockDialog, | |
| 81 base::Unretained(this)), | |
| 82 base::Bind( | |
| 83 &MediaGalleriesScanResultControllerTest::OnControllerDone, | |
| 84 base::Unretained(this))); | |
| 85 } | |
| 86 | |
| 87 size_t GetFirstSectionSize() const { | |
| 88 return controller()->GetSectionEntries(0).size(); | |
| 89 } | |
| 90 | |
| 91 MediaGalleriesScanResultController* controller() const { | |
| 92 return controller_; | |
| 93 } | |
| 94 | |
| 95 MockMediaGalleriesDialog* dialog() { | |
| 96 return dialog_; | |
| 97 } | |
| 98 | |
| 99 int dialog_update_count_at_destruction() { | |
| 100 EXPECT_FALSE(dialog_); | |
| 101 return dialog_update_count_at_destruction_; | |
| 102 } | |
| 103 | |
| 104 extensions::Extension* extension() { | |
| 105 return extension_.get(); | |
| 106 } | |
| 107 | |
| 108 MediaGalleriesPreferences* gallery_prefs() { | |
| 109 return gallery_prefs_.get(); | |
| 110 } | |
| 111 | |
| 112 MediaGalleryPrefId AddGallery(const std::string& path, | |
| 113 MediaGalleryPrefInfo::Type type, | |
| 114 int audio_count, int image_count, | |
| 115 int video_count) { | |
| 116 MediaGalleryPrefInfo gallery_info; | |
| 117 gallery_prefs_->LookUpGalleryByPath(MakeMediaGalleriesTestingPath(path), | |
| 118 &gallery_info); | |
| 119 return gallery_prefs_->AddGallery( | |
| 120 gallery_info.device_id, | |
| 121 gallery_info.path, | |
| 122 type, | |
| 123 gallery_info.volume_label, | |
| 124 gallery_info.vendor_name, | |
| 125 gallery_info.model_name, | |
| 126 gallery_info.total_size_in_bytes, | |
| 127 gallery_info.last_attach_time, | |
| 128 audio_count, image_count, video_count); | |
| 129 } | |
| 130 | |
| 131 MediaGalleryPrefId AddScanResult(const std::string& path, int audio_count, | |
| 132 int image_count, int video_count) { | |
| 133 return AddGallery(path, MediaGalleryPrefInfo::kScanResult, audio_count, | |
| 134 image_count, video_count); | |
| 135 } | |
| 136 | |
| 137 private: | |
| 138 MediaGalleriesDialog* CreateMockDialog( | |
| 139 MediaGalleriesDialogController* controller) { | |
| 140 EXPECT_FALSE(dialog_); | |
| 141 dialog_update_count_at_destruction_ = 0; | |
| 142 dialog_ = new MockMediaGalleriesDialog(base::Bind( | |
| 143 &MediaGalleriesScanResultControllerTest::OnDialogDestroyed, | |
| 144 weak_factory_.GetWeakPtr())); | |
| 145 return dialog_; | |
| 146 } | |
| 147 | |
| 148 void OnDialogDestroyed(int update_count) { | |
| 149 EXPECT_TRUE(dialog_); | |
| 150 dialog_update_count_at_destruction_ = update_count; | |
| 151 dialog_ = NULL; | |
| 152 } | |
| 153 | |
| 154 void OnControllerDone() { | |
| 155 controller_ = NULL; | |
| 156 } | |
| 157 | |
| 158 // Needed for extension service & friends to work. | |
| 159 content::TestBrowserThreadBundle thread_bundle_; | |
| 160 | |
| 161 // The dialog is owned by the controller, but this pointer should only be | |
| 162 // valid while the dialog is live within the controller. | |
| 163 MockMediaGalleriesDialog* dialog_; | |
| 164 int dialog_update_count_at_destruction_; | |
| 165 | |
| 166 // The controller owns itself. | |
| 167 MediaGalleriesScanResultController* controller_; | |
| 168 | |
| 169 scoped_refptr<extensions::Extension> extension_; | |
| 170 | |
| 171 EnsureMediaDirectoriesExists mock_gallery_locations_; | |
| 172 | |
| 173 #if defined OS_CHROMEOS | |
| 174 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
| 175 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
| 176 chromeos::ScopedTestUserManager test_user_manager_; | |
| 177 #endif | |
| 178 | |
| 179 storage_monitor::TestStorageMonitor monitor_; | |
| 180 scoped_ptr<TestingProfile> profile_; | |
| 181 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_; | |
| 182 | |
| 183 base::WeakPtrFactory<MediaGalleriesScanResultControllerTest> weak_factory_; | |
| 184 | |
| 185 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesScanResultControllerTest); | |
| 186 }; | |
| 187 | |
| 188 TEST_F(MediaGalleriesScanResultControllerTest, EmptyDialog) { | |
| 189 StartDialog(); | |
| 190 EXPECT_TRUE(controller()); | |
| 191 EXPECT_TRUE(dialog()); | |
| 192 EXPECT_EQ(0U, GetFirstSectionSize()); | |
| 193 | |
| 194 controller()->DialogFinished(true); | |
| 195 EXPECT_FALSE(controller()); | |
| 196 EXPECT_FALSE(dialog()); | |
| 197 EXPECT_EQ(0, dialog_update_count_at_destruction()); | |
| 198 } | |
| 199 | |
| 200 TEST_F(MediaGalleriesScanResultControllerTest, AddScanResults) { | |
| 201 // Start with two scan results. | |
| 202 MediaGalleryPrefId scan_id = AddScanResult("scan_id", 1, 0, 0); | |
| 203 MediaGalleryPrefId auto_id = | |
| 204 AddGallery("auto_id", MediaGalleryPrefInfo::kAutoDetected, 2, 0, 0); | |
| 205 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 206 | |
| 207 // Show the dialog, but cancel it. | |
| 208 StartDialog(); | |
| 209 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 210 controller()->DialogFinished(false); | |
| 211 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 212 | |
| 213 // Show the dialog, unselect both and accept it. | |
| 214 StartDialog(); | |
| 215 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 216 controller()->DidToggleEntry(scan_id, false); | |
| 217 controller()->DidToggleEntry(auto_id, false); | |
| 218 controller()->DialogFinished(true); | |
| 219 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 220 | |
| 221 // Show the dialog, leave one selected and accept it. | |
| 222 StartDialog(); | |
| 223 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 224 controller()->DidToggleEntry(scan_id, false); | |
| 225 controller()->DialogFinished(true); | |
| 226 MediaGalleryPrefIdSet permitted = | |
| 227 gallery_prefs()->GalleriesForExtension(*extension()); | |
| 228 ASSERT_EQ(1U, permitted.size()); | |
| 229 EXPECT_EQ(auto_id, *permitted.begin()); | |
| 230 | |
| 231 // Show the dialog, toggle the remaining entry twice and then accept it. | |
| 232 StartDialog(); | |
| 233 EXPECT_EQ(1U, GetFirstSectionSize()); | |
| 234 controller()->DidToggleEntry(scan_id, false); | |
| 235 controller()->DidToggleEntry(scan_id, true); | |
| 236 controller()->DialogFinished(true); | |
| 237 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 238 } | |
| 239 | |
| 240 TEST_F(MediaGalleriesScanResultControllerTest, Blacklisted) { | |
| 241 // Start with two scan results. | |
| 242 MediaGalleryPrefId scan_id = AddScanResult("scan_id", 1, 0, 0); | |
| 243 MediaGalleryPrefId auto_id = | |
| 244 AddGallery("auto_id", MediaGalleryPrefInfo::kAutoDetected, 2, 0, 0); | |
| 245 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 246 | |
| 247 // Show the dialog, but cancel it. | |
| 248 StartDialog(); | |
| 249 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 250 controller()->DialogFinished(false); | |
| 251 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 252 | |
| 253 // Blacklist one and try again. | |
| 254 gallery_prefs()->ForgetGalleryById(scan_id); | |
| 255 StartDialog(); | |
| 256 EXPECT_EQ(1U, GetFirstSectionSize()); | |
| 257 controller()->DialogFinished(false); | |
| 258 | |
| 259 // Adding it as a user gallery should change its type. | |
| 260 AddGallery("scan_id", MediaGalleryPrefInfo::kUserAdded, 1, 0, 0); | |
| 261 StartDialog(); | |
| 262 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 263 | |
| 264 // Blacklisting the other while the dialog is open should remove it. | |
| 265 gallery_prefs()->ForgetGalleryById(auto_id); | |
| 266 EXPECT_EQ(1U, GetFirstSectionSize()); | |
| 267 controller()->DialogFinished(false); | |
| 268 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 269 EXPECT_EQ(1, dialog_update_count_at_destruction()); | |
| 270 } | |
| 271 | |
| 272 TEST_F(MediaGalleriesScanResultControllerTest, PrefUpdates) { | |
| 273 MediaGalleryPrefId selected = AddScanResult("selected", 1, 0, 0); | |
| 274 MediaGalleryPrefId unselected = AddScanResult("unselected", 1, 0, 0); | |
| 275 MediaGalleryPrefId selected_add_permission = | |
| 276 AddScanResult("selected_add_permission", 1, 0, 0); | |
| 277 MediaGalleryPrefId unselected_add_permission = | |
| 278 AddScanResult("unselected_add_permission", 1, 0, 0); | |
| 279 MediaGalleryPrefId selected_removed = | |
| 280 AddScanResult("selected_removed", 1, 0, 0); | |
| 281 MediaGalleryPrefId unselected_removed = | |
| 282 AddScanResult("unselected_removed", 1, 0, 0); | |
| 283 MediaGalleryPrefId selected_update = | |
| 284 AddScanResult("selected_update", 1, 0, 0); | |
| 285 MediaGalleryPrefId unselected_update = | |
| 286 AddScanResult("unselected_update", 1, 0, 0); | |
| 287 | |
| 288 gallery_prefs()->AddGalleryByPath(MakeMediaGalleriesTestingPath("user"), | |
| 289 MediaGalleryPrefInfo::kUserAdded); | |
| 290 gallery_prefs()->AddGalleryByPath( | |
| 291 MakeMediaGalleriesTestingPath("auto_detected"), | |
| 292 MediaGalleryPrefInfo::kAutoDetected); | |
| 293 MediaGalleryPrefId blacklisted = gallery_prefs()->AddGalleryByPath( | |
| 294 MakeMediaGalleriesTestingPath("blacklisted"), | |
| 295 MediaGalleryPrefInfo::kAutoDetected); | |
| 296 gallery_prefs()->ForgetGalleryById(blacklisted); | |
| 297 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 298 | |
| 299 StartDialog(); | |
| 300 EXPECT_EQ(8U, GetFirstSectionSize()); | |
| 301 controller()->DidToggleEntry(unselected, false); | |
| 302 controller()->DidToggleEntry(unselected_add_permission, false); | |
| 303 controller()->DidToggleEntry(unselected_removed, false); | |
| 304 controller()->DidToggleEntry(unselected_update, false); | |
| 305 EXPECT_EQ(0, dialog()->update_count()); | |
| 306 EXPECT_EQ(8U, GetFirstSectionSize()); | |
| 307 | |
| 308 // Add permission. | |
| 309 gallery_prefs()->SetGalleryPermissionForExtension(*extension(), | |
| 310 unselected_add_permission, | |
| 311 true); | |
| 312 EXPECT_EQ(1, dialog()->update_count()); | |
| 313 EXPECT_EQ(7U, GetFirstSectionSize()); | |
| 314 gallery_prefs()->SetGalleryPermissionForExtension(*extension(), | |
| 315 selected_add_permission, | |
| 316 true); | |
| 317 EXPECT_EQ(2, dialog()->update_count()); | |
| 318 EXPECT_EQ(6U, GetFirstSectionSize()); | |
| 319 | |
| 320 // Blacklist scan results. | |
| 321 gallery_prefs()->ForgetGalleryById(unselected_removed); | |
| 322 EXPECT_EQ(3, dialog()->update_count()); | |
| 323 EXPECT_EQ(5U, GetFirstSectionSize()); | |
| 324 gallery_prefs()->ForgetGalleryById(selected_removed); | |
| 325 EXPECT_EQ(4, dialog()->update_count()); | |
| 326 EXPECT_EQ(4U, GetFirstSectionSize()); | |
| 327 | |
| 328 // Update names. | |
| 329 const MediaGalleryPrefInfo& unselected_update_info = | |
| 330 gallery_prefs()->known_galleries().find(unselected_update)->second; | |
| 331 gallery_prefs()->AddGallery( | |
| 332 unselected_update_info.device_id, base::FilePath(), | |
| 333 MediaGalleryPrefInfo::kScanResult, | |
| 334 base::ASCIIToUTF16("Updated & Unselected"), | |
| 335 base::string16(), base::string16(), 0, base::Time(), 1, 0, 0); | |
| 336 EXPECT_EQ(5, dialog()->update_count()); | |
| 337 EXPECT_EQ(4U, GetFirstSectionSize()); | |
| 338 const MediaGalleryPrefInfo& selected_update_info = | |
| 339 gallery_prefs()->known_galleries().find(selected_update)->second; | |
| 340 gallery_prefs()->AddGallery( | |
| 341 selected_update_info.device_id, base::FilePath(), | |
| 342 MediaGalleryPrefInfo::kScanResult, | |
| 343 base::ASCIIToUTF16("Updated & Selected"), | |
| 344 base::string16(), base::string16(), 0, base::Time(), 1, 0, 0); | |
| 345 EXPECT_EQ(6, dialog()->update_count()); | |
| 346 EXPECT_EQ(4U, GetFirstSectionSize()); | |
| 347 | |
| 348 MediaGalleriesDialogController::Entries results = | |
| 349 controller()->GetSectionEntries(0); | |
| 350 EXPECT_EQ(selected, results[0].pref_info.pref_id); | |
| 351 EXPECT_TRUE(results[0].selected); | |
| 352 EXPECT_EQ(selected_update, results[1].pref_info.pref_id); | |
| 353 EXPECT_TRUE(results[1].selected); | |
| 354 EXPECT_EQ(base::ASCIIToUTF16("Updated & Selected"), | |
| 355 results[1].pref_info.volume_label); | |
| 356 EXPECT_EQ(unselected, results[2].pref_info.pref_id); | |
| 357 EXPECT_FALSE(results[2].selected); | |
| 358 EXPECT_EQ(unselected_update, results[3].pref_info.pref_id); | |
| 359 EXPECT_FALSE(results[3].selected); | |
| 360 EXPECT_EQ(base::ASCIIToUTF16("Updated & Unselected"), | |
| 361 results[3].pref_info.volume_label); | |
| 362 | |
| 363 controller()->DialogFinished(true); | |
| 364 EXPECT_EQ(4U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 365 StartDialog(); | |
| 366 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 367 controller()->DialogFinished(false); | |
| 368 } | |
| 369 | |
| 370 TEST_F(MediaGalleriesScanResultControllerTest, ForgetGallery) { | |
| 371 // Start with two scan results. | |
| 372 MediaGalleryPrefId scan1 = AddScanResult("scan1", 1, 0, 0); | |
| 373 MediaGalleryPrefId scan2 = AddScanResult("scan2", 2, 0, 0); | |
| 374 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 375 | |
| 376 // Remove one and then cancel. | |
| 377 StartDialog(); | |
| 378 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 379 controller()->DidForgetEntry(scan1); | |
| 380 controller()->DialogFinished(false); | |
| 381 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 382 | |
| 383 // Remove one and then have it blacklisted from prefs. | |
| 384 StartDialog(); | |
| 385 EXPECT_EQ(2U, GetFirstSectionSize()); | |
| 386 controller()->DidForgetEntry(scan1); | |
| 387 EXPECT_EQ(1, dialog()->update_count()); | |
| 388 controller()->DidToggleEntry(scan2, false); // Uncheck the second. | |
| 389 gallery_prefs()->ForgetGalleryById(scan1); | |
| 390 controller()->DialogFinished(true); | |
| 391 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 392 EXPECT_EQ(2, dialog_update_count_at_destruction()); | |
| 393 | |
| 394 // Remove the other. | |
| 395 StartDialog(); | |
| 396 EXPECT_EQ(1U, GetFirstSectionSize()); | |
| 397 controller()->DidForgetEntry(scan2); | |
| 398 controller()->DialogFinished(true); | |
| 399 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
| 400 | |
| 401 // Check that nothing shows up. | |
| 402 StartDialog(); | |
| 403 EXPECT_EQ(0U, GetFirstSectionSize()); | |
| 404 controller()->DialogFinished(false); | |
| 405 } | |
| 406 | |
| 407 TEST_F(MediaGalleriesScanResultControllerTest, SortOrder) { | |
| 408 // Intentionally out of order numerically and alphabetically. | |
| 409 MediaGalleryPrefId third = AddScanResult("third", 2, 2, 2); | |
| 410 MediaGalleryPrefId second = | |
| 411 AddGallery("second", MediaGalleryPrefInfo::kAutoDetected, 9, 0, 0); | |
| 412 MediaGalleryPrefId first = AddScanResult("first", 8, 2, 3); | |
| 413 MediaGalleryPrefId fifth = AddScanResult("abb", 3, 0, 0); | |
| 414 MediaGalleryPrefId fourth = AddScanResult("aaa", 3, 0, 0); | |
| 415 | |
| 416 StartDialog(); | |
| 417 MediaGalleriesDialogController::Entries results = | |
| 418 controller()->GetSectionEntries(0); | |
| 419 ASSERT_EQ(5U, results.size()); | |
| 420 EXPECT_EQ(first, results[0].pref_info.pref_id); | |
| 421 EXPECT_EQ(second, results[1].pref_info.pref_id); | |
| 422 EXPECT_EQ(third, results[2].pref_info.pref_id); | |
| 423 EXPECT_EQ(fourth, results[3].pref_info.pref_id); | |
| 424 EXPECT_EQ(fifth, results[4].pref_info.pref_id); | |
| 425 controller()->DialogFinished(false); | |
| 426 } | |
| OLD | NEW |