[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_preferences_unittest.cc
blob3c8775e60789575d359a0ef9472f5a67da121b19
1 // Copyright (c) 2012 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 // MediaGalleriesPreferences unit tests.
7 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
9 #include <string>
10 #include <vector>
12 #include "base/command_line.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16 #include "base/prefs/scoped_user_pref_update.h"
17 #include "base/run_loop.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "chrome/browser/extensions/test_extension_system.h"
21 #include "chrome/browser/media_galleries/media_file_system_registry.h"
22 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/grit/generated_resources.h"
26 #include "chrome/test/base/testing_profile.h"
27 #include "components/storage_monitor/media_storage_util.h"
28 #include "components/storage_monitor/storage_monitor.h"
29 #include "components/storage_monitor/test_storage_monitor.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "extensions/browser/extension_system.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/manifest_handlers/background_info.h"
34 #include "extensions/common/permissions/media_galleries_permission.h"
35 #include "sync/api/string_ordinal.h"
36 #include "testing/gtest/include/gtest/gtest.h"
37 #include "ui/base/l10n/l10n_util.h"
39 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
41 #include "chrome/browser/chromeos/settings/cros_settings.h"
42 #include "chrome/browser/chromeos/settings/device_settings_service.h"
43 #endif
45 using base::ASCIIToUTF16;
46 using storage_monitor::MediaStorageUtil;
47 using storage_monitor::StorageInfo;
48 using storage_monitor::TestStorageMonitor;
50 namespace {
52 class MockGalleryChangeObserver
53 : public MediaGalleriesPreferences::GalleryChangeObserver {
54 public:
55 explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref)
56 : pref_(pref),
57 notifications_(0) {}
58 ~MockGalleryChangeObserver() override {}
60 int notifications() const { return notifications_;}
62 private:
63 // MediaGalleriesPreferences::GalleryChangeObserver implementation.
64 void OnPermissionAdded(MediaGalleriesPreferences* pref,
65 const std::string& extension_id,
66 MediaGalleryPrefId pref_id) override {
67 EXPECT_EQ(pref_, pref);
68 ++notifications_;
71 void OnPermissionRemoved(MediaGalleriesPreferences* pref,
72 const std::string& extension_id,
73 MediaGalleryPrefId pref_id) override {
74 EXPECT_EQ(pref_, pref);
75 ++notifications_;
78 void OnGalleryAdded(MediaGalleriesPreferences* pref,
79 MediaGalleryPrefId pref_id) override {
80 EXPECT_EQ(pref_, pref);
81 ++notifications_;
84 void OnGalleryRemoved(MediaGalleriesPreferences* pref,
85 MediaGalleryPrefId pref_id) override {
86 EXPECT_EQ(pref_, pref);
87 ++notifications_;
90 void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
91 MediaGalleryPrefId pref_id) override {
92 EXPECT_EQ(pref_, pref);
93 ++notifications_;
96 MediaGalleriesPreferences* pref_;
97 int notifications_;
99 DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver);
102 } // namespace
104 class MediaGalleriesPreferencesTest : public testing::Test {
105 public:
106 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
107 DeviceIdPrefIdsMap;
109 MediaGalleriesPreferencesTest()
110 : profile_(new TestingProfile()),
111 default_galleries_count_(0) {
114 ~MediaGalleriesPreferencesTest() override {}
116 void SetUp() override {
117 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
119 extensions::TestExtensionSystem* extension_system(
120 static_cast<extensions::TestExtensionSystem*>(
121 extensions::ExtensionSystem::Get(profile_.get())));
122 extension_system->CreateExtensionService(
123 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
125 ReinitPrefsAndExpectations();
127 const MediaGalleriesPrefInfoMap& known_galleries =
128 gallery_prefs_->known_galleries();
129 if (!known_galleries.empty()) {
130 ASSERT_EQ(3U, known_galleries.size());
133 std::vector<std::string> all_permissions;
134 all_permissions.push_back(
135 extensions::MediaGalleriesPermission::kReadPermission);
136 all_permissions.push_back(
137 extensions::MediaGalleriesPermission::kAllAutoDetectedPermission);
138 std::vector<std::string> read_permissions;
139 read_permissions.push_back(
140 extensions::MediaGalleriesPermission::kReadPermission);
142 all_permission_extension =
143 AddMediaGalleriesApp("all", all_permissions, profile_.get());
144 regular_permission_extension =
145 AddMediaGalleriesApp("regular", read_permissions, profile_.get());
146 no_permissions_extension =
147 AddMediaGalleriesApp("no", read_permissions, profile_.get());
150 void TearDown() override {
151 Verify();
152 TestStorageMonitor::Destroy();
155 void ChangeMediaPathOverrides() {
156 mock_gallery_locations_.ChangeMediaPathOverrides();
159 void ReinitPrefsAndExpectations() {
160 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
161 base::RunLoop loop;
162 gallery_prefs_->EnsureInitialized(loop.QuitClosure());
163 loop.Run();
165 // Load the default galleries into the expectations.
166 const MediaGalleriesPrefInfoMap& known_galleries =
167 gallery_prefs_->known_galleries();
168 if (!known_galleries.empty()) {
169 default_galleries_count_ = 3;
170 MediaGalleriesPrefInfoMap::const_iterator it;
171 for (it = known_galleries.begin(); it != known_galleries.end(); ++it) {
172 expected_galleries_[it->first] = it->second;
173 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
174 expected_galleries_for_all.insert(it->first);
179 void RemovePersistedDefaultGalleryValues() {
180 PrefService* prefs = profile_->GetPrefs();
181 scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate(
182 prefs, prefs::kMediaGalleriesRememberedGalleries));
183 base::ListValue* list = update->Get();
185 for (base::ListValue::iterator iter = list->begin();
186 iter != list->end();
187 ++iter) {
188 base::DictionaryValue* dict;
190 if ((*iter)->GetAsDictionary(&dict)) {
191 // Setting the prefs version to 2 which is the version before
192 // default_gallery_type was added.
193 dict->SetInteger(kMediaGalleriesPrefsVersionKey, 2);
194 dict->Remove(kMediaGalleriesDefaultGalleryTypeKey, NULL);
197 update.reset();
200 void Verify() {
201 const MediaGalleriesPrefInfoMap& known_galleries =
202 gallery_prefs_->known_galleries();
203 EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
204 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
205 it != known_galleries.end();
206 ++it) {
207 VerifyGalleryInfo(it->second, it->first);
208 if (it->second.type != MediaGalleryPrefInfo::kAutoDetected &&
209 it->second.type != MediaGalleryPrefInfo::kBlackListed) {
210 if (!ContainsKey(expected_galleries_for_all, it->first) &&
211 !ContainsKey(expected_galleries_for_regular, it->first)) {
212 EXPECT_FALSE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
213 } else {
214 EXPECT_TRUE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
219 for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin();
220 it != expected_device_map.end();
221 ++it) {
222 MediaGalleryPrefIdSet actual_id_set =
223 gallery_prefs_->LookUpGalleriesByDeviceId(it->first);
224 EXPECT_EQ(it->second, actual_id_set);
227 std::set<MediaGalleryPrefId> galleries_for_all =
228 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get());
229 EXPECT_EQ(expected_galleries_for_all, galleries_for_all);
231 std::set<MediaGalleryPrefId> galleries_for_regular =
232 gallery_prefs_->GalleriesForExtension(
233 *regular_permission_extension.get());
234 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular);
236 std::set<MediaGalleryPrefId> galleries_for_no =
237 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get());
238 EXPECT_EQ(0U, galleries_for_no.size());
241 void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual,
242 MediaGalleryPrefId expected_id) const {
243 MediaGalleriesPrefInfoMap::const_iterator in_expectation =
244 expected_galleries_.find(expected_id);
245 ASSERT_FALSE(in_expectation == expected_galleries_.end()) << expected_id;
246 EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id);
247 EXPECT_EQ(in_expectation->second.display_name, actual.display_name);
248 EXPECT_EQ(in_expectation->second.device_id, actual.device_id);
249 EXPECT_EQ(in_expectation->second.path.value(), actual.path.value());
250 EXPECT_EQ(in_expectation->second.type, actual.type);
251 EXPECT_EQ(in_expectation->second.audio_count, actual.audio_count);
252 EXPECT_EQ(in_expectation->second.image_count, actual.image_count);
253 EXPECT_EQ(in_expectation->second.video_count, actual.video_count);
254 EXPECT_EQ(
255 in_expectation->second.default_gallery_type,
256 actual.default_gallery_type);
259 MediaGalleriesPreferences* gallery_prefs() {
260 return gallery_prefs_.get();
263 uint64 default_galleries_count() {
264 return default_galleries_count_;
267 void AddGalleryExpectation(MediaGalleryPrefId id, base::string16 display_name,
268 std::string device_id,
269 base::FilePath relative_path,
270 MediaGalleryPrefInfo::Type type) {
271 expected_galleries_[id].pref_id = id;
272 expected_galleries_[id].display_name = display_name;
273 expected_galleries_[id].device_id = device_id;
274 expected_galleries_[id].path = relative_path.NormalizePathSeparators();
275 expected_galleries_[id].type = type;
277 if (type == MediaGalleryPrefInfo::kAutoDetected)
278 expected_galleries_for_all.insert(id);
280 expected_device_map[device_id].insert(id);
283 void AddScanResultExpectation(MediaGalleryPrefId id,
284 base::string16 display_name,
285 std::string device_id,
286 base::FilePath relative_path,
287 int audio_count,
288 int image_count,
289 int video_count) {
290 AddGalleryExpectation(id, display_name, device_id, relative_path,
291 MediaGalleryPrefInfo::kScanResult);
292 expected_galleries_[id].audio_count = audio_count;
293 expected_galleries_[id].image_count = image_count;
294 expected_galleries_[id].video_count = video_count;
297 MediaGalleryPrefId AddGalleryWithNameV0(const std::string& device_id,
298 const base::string16& display_name,
299 const base::FilePath& relative_path,
300 bool user_added) {
301 MediaGalleryPrefInfo::Type type =
302 user_added ? MediaGalleryPrefInfo::kUserAdded
303 : MediaGalleryPrefInfo::kAutoDetected;
304 return gallery_prefs()->AddOrUpdateGalleryInternal(
305 device_id, display_name, relative_path, type,
306 base::string16(), base::string16(), base::string16(), 0, base::Time(),
307 false, 0, 0, 0, 0, MediaGalleryPrefInfo::kNotDefault);
310 MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id,
311 const base::string16& display_name,
312 const base::FilePath& relative_path,
313 bool user_added) {
314 MediaGalleryPrefInfo::Type type =
315 user_added ? MediaGalleryPrefInfo::kUserAdded
316 : MediaGalleryPrefInfo::kAutoDetected;
317 return gallery_prefs()->AddOrUpdateGalleryInternal(
318 device_id, display_name, relative_path, type,
319 base::string16(), base::string16(), base::string16(), 0, base::Time(),
320 false, 0, 0, 0, 1, MediaGalleryPrefInfo::kNotDefault);
323 MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id,
324 const base::string16& display_name,
325 const base::FilePath& relative_path,
326 MediaGalleryPrefInfo::Type type) {
327 return gallery_prefs()->AddOrUpdateGalleryInternal(
328 device_id, display_name, relative_path, type,
329 base::string16(), base::string16(), base::string16(), 0, base::Time(),
330 false, 0, 0, 0, 2, MediaGalleryPrefInfo::kNotDefault);
333 MediaGalleryPrefId AddFixedGalleryWithExepectation(
334 const std::string& path_name, const std::string& name,
335 MediaGalleryPrefInfo::Type type) {
336 base::FilePath path = MakeMediaGalleriesTestingPath(path_name);
337 StorageInfo info;
338 base::FilePath relative_path;
339 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
340 base::string16 gallery_name = base::ASCIIToUTF16(name);
341 MediaGalleryPrefId id = AddGalleryWithNameV2(info.device_id(), gallery_name,
342 relative_path, type);
343 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
344 type);
345 Verify();
346 return id;
349 bool UpdateDeviceIDForSingletonType(const std::string& device_id) {
350 return gallery_prefs()->UpdateDeviceIDForSingletonType(device_id);
353 scoped_refptr<extensions::Extension> all_permission_extension;
354 scoped_refptr<extensions::Extension> regular_permission_extension;
355 scoped_refptr<extensions::Extension> no_permissions_extension;
357 std::set<MediaGalleryPrefId> expected_galleries_for_all;
358 std::set<MediaGalleryPrefId> expected_galleries_for_regular;
360 DeviceIdPrefIdsMap expected_device_map;
362 MediaGalleriesPrefInfoMap expected_galleries_;
364 private:
365 // Needed for extension service & friends to work.
366 content::TestBrowserThreadBundle thread_bundle_;
368 EnsureMediaDirectoriesExists mock_gallery_locations_;
370 #if defined(OS_CHROMEOS)
371 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
372 chromeos::ScopedTestCrosSettings test_cros_settings_;
373 chromeos::ScopedTestUserManager test_user_manager_;
374 #endif
376 TestStorageMonitor monitor_;
377 scoped_ptr<TestingProfile> profile_;
378 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
380 uint64 default_galleries_count_;
382 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest);
385 TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) {
386 MediaGalleryPrefId auto_id, user_added_id, scan_id, id;
387 base::FilePath path;
388 base::FilePath relative_path;
389 Verify();
391 // Add a new auto detected gallery.
392 path = MakeMediaGalleriesTestingPath("new_auto");
393 StorageInfo info;
394 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
395 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
396 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
397 MediaGalleryPrefInfo::kAutoDetected);
398 EXPECT_EQ(default_galleries_count() + 1UL, id);
399 auto_id = id;
400 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
401 MediaGalleryPrefInfo::kAutoDetected);
402 Verify();
404 // Add it as other types, nothing should happen.
405 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
406 MediaGalleryPrefInfo::kUserAdded);
407 EXPECT_EQ(auto_id, id);
408 Verify();
409 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
410 MediaGalleryPrefInfo::kAutoDetected);
411 EXPECT_EQ(auto_id, id);
412 Verify();
413 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
414 MediaGalleryPrefInfo::kScanResult);
415 EXPECT_EQ(auto_id, id);
417 // Add a new user added gallery.
418 path = MakeMediaGalleriesTestingPath("new_user");
419 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
420 gallery_name = base::ASCIIToUTF16("NewUserGallery");
421 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
422 MediaGalleryPrefInfo::kUserAdded);
423 EXPECT_EQ(default_galleries_count() + 2UL, id);
424 user_added_id = id;
425 const std::string user_added_device_id = info.device_id();
426 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
427 MediaGalleryPrefInfo::kUserAdded);
428 Verify();
430 // Add it as other types, nothing should happen.
431 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
432 MediaGalleryPrefInfo::kUserAdded);
433 EXPECT_EQ(user_added_id, id);
434 Verify();
435 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
436 MediaGalleryPrefInfo::kAutoDetected);
437 EXPECT_EQ(user_added_id, id);
438 Verify();
439 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
440 MediaGalleryPrefInfo::kScanResult);
441 EXPECT_EQ(user_added_id, id);
442 Verify();
444 // Add a new scan result gallery.
445 path = MakeMediaGalleriesTestingPath("new_scan");
446 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
447 gallery_name = base::ASCIIToUTF16("NewScanGallery");
448 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
449 MediaGalleryPrefInfo::kScanResult);
450 EXPECT_EQ(default_galleries_count() + 3UL, id);
451 scan_id = id;
452 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
453 MediaGalleryPrefInfo::kScanResult);
454 Verify();
456 // Add it as other types, nothing should happen.
457 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
458 MediaGalleryPrefInfo::kUserAdded);
459 EXPECT_EQ(scan_id, id);
460 Verify();
461 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
462 MediaGalleryPrefInfo::kAutoDetected);
463 EXPECT_EQ(scan_id, id);
464 Verify();
465 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
466 MediaGalleryPrefInfo::kScanResult);
467 EXPECT_EQ(scan_id, id);
468 Verify();
470 // Lookup some galleries.
471 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
472 MakeMediaGalleriesTestingPath("new_auto"), NULL));
473 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
474 MakeMediaGalleriesTestingPath("new_user"), NULL));
475 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
476 MakeMediaGalleriesTestingPath("new_scan"), NULL));
477 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(
478 MakeMediaGalleriesTestingPath("other"), NULL));
480 // Check that we always get the gallery info.
481 MediaGalleryPrefInfo gallery_info;
482 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
483 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
484 VerifyGalleryInfo(gallery_info, auto_id);
485 EXPECT_FALSE(gallery_info.volume_metadata_valid);
486 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
487 MakeMediaGalleriesTestingPath("new_user"), &gallery_info));
488 VerifyGalleryInfo(gallery_info, user_added_id);
489 EXPECT_FALSE(gallery_info.volume_metadata_valid);
490 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
491 MakeMediaGalleriesTestingPath("new_scan"), &gallery_info));
492 VerifyGalleryInfo(gallery_info, scan_id);
493 EXPECT_FALSE(gallery_info.volume_metadata_valid);
495 path = MakeMediaGalleriesTestingPath("other");
496 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info));
497 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id);
499 StorageInfo other_info;
500 MediaStorageUtil::GetDeviceInfoFromPath(path, &other_info, &relative_path);
501 EXPECT_EQ(other_info.device_id(), gallery_info.device_id);
502 EXPECT_EQ(relative_path.value(), gallery_info.path.value());
504 // Remove an auto added gallery (i.e. make it blacklisted).
505 gallery_prefs()->ForgetGalleryById(auto_id);
506 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
507 expected_galleries_for_all.erase(auto_id);
508 Verify();
510 // Remove a scan result (i.e. make it blacklisted).
511 gallery_prefs()->ForgetGalleryById(scan_id);
512 expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
513 Verify();
515 // Remove a user added gallery and it should go away.
516 gallery_prefs()->ForgetGalleryById(user_added_id);
517 expected_galleries_.erase(user_added_id);
518 expected_device_map[user_added_device_id].erase(user_added_id);
519 Verify();
522 TEST_F(MediaGalleriesPreferencesTest, ForgetAndErase) {
523 MediaGalleryPrefId user_erase =
524 AddFixedGalleryWithExepectation("user_erase", "UserErase",
525 MediaGalleryPrefInfo::kUserAdded);
526 EXPECT_EQ(default_galleries_count() + 1UL, user_erase);
527 MediaGalleryPrefId user_forget =
528 AddFixedGalleryWithExepectation("user_forget", "UserForget",
529 MediaGalleryPrefInfo::kUserAdded);
530 EXPECT_EQ(default_galleries_count() + 2UL, user_forget);
532 MediaGalleryPrefId auto_erase =
533 AddFixedGalleryWithExepectation("auto_erase", "AutoErase",
534 MediaGalleryPrefInfo::kAutoDetected);
535 EXPECT_EQ(default_galleries_count() + 3UL, auto_erase);
536 MediaGalleryPrefId auto_forget =
537 AddFixedGalleryWithExepectation("auto_forget", "AutoForget",
538 MediaGalleryPrefInfo::kAutoDetected);
539 EXPECT_EQ(default_galleries_count() + 4UL, auto_forget);
541 MediaGalleryPrefId scan_erase =
542 AddFixedGalleryWithExepectation("scan_erase", "ScanErase",
543 MediaGalleryPrefInfo::kScanResult);
544 EXPECT_EQ(default_galleries_count() + 5UL, scan_erase);
545 MediaGalleryPrefId scan_forget =
546 AddFixedGalleryWithExepectation("scan_forget", "ScanForget",
547 MediaGalleryPrefInfo::kScanResult);
548 EXPECT_EQ(default_galleries_count() + 6UL, scan_forget);
550 Verify();
551 std::string device_id;
553 gallery_prefs()->ForgetGalleryById(user_forget);
554 device_id = expected_galleries_[user_forget].device_id;
555 expected_galleries_.erase(user_forget);
556 expected_device_map[device_id].erase(user_forget);
557 Verify();
559 gallery_prefs()->ForgetGalleryById(auto_forget);
560 expected_galleries_[auto_forget].type = MediaGalleryPrefInfo::kBlackListed;
561 expected_galleries_for_all.erase(auto_forget);
562 Verify();
564 gallery_prefs()->ForgetGalleryById(scan_forget);
565 expected_galleries_[scan_forget].type = MediaGalleryPrefInfo::kRemovedScan;
566 Verify();
568 gallery_prefs()->EraseGalleryById(user_erase);
569 device_id = expected_galleries_[user_erase].device_id;
570 expected_galleries_.erase(user_erase);
571 expected_device_map[device_id].erase(user_erase);
572 Verify();
574 gallery_prefs()->EraseGalleryById(auto_erase);
575 device_id = expected_galleries_[auto_erase].device_id;
576 expected_galleries_.erase(auto_erase);
577 expected_device_map[device_id].erase(auto_erase);
578 expected_galleries_for_all.erase(auto_erase);
579 Verify();
581 gallery_prefs()->EraseGalleryById(scan_erase);
582 device_id = expected_galleries_[scan_erase].device_id;
583 expected_galleries_.erase(scan_erase);
584 expected_device_map[device_id].erase(scan_erase);
585 Verify();
587 // Also erase the previously forgetten ones to check erasing blacklisted ones.
588 gallery_prefs()->EraseGalleryById(auto_forget);
589 device_id = expected_galleries_[auto_forget].device_id;
590 expected_galleries_.erase(auto_forget);
591 expected_device_map[device_id].erase(auto_forget);
592 Verify();
594 gallery_prefs()->EraseGalleryById(scan_forget);
595 device_id = expected_galleries_[scan_forget].device_id;
596 expected_galleries_.erase(scan_forget);
597 expected_device_map[device_id].erase(scan_forget);
598 Verify();
601 TEST_F(MediaGalleriesPreferencesTest, AddGalleryWithVolumeMetadata) {
602 MediaGalleryPrefId id;
603 StorageInfo info;
604 base::FilePath path;
605 base::FilePath relative_path;
606 base::Time now = base::Time::Now();
607 Verify();
609 // Add a new auto detected gallery.
610 path = MakeMediaGalleriesTestingPath("new_auto");
611 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
612 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
613 MediaGalleryPrefInfo::kAutoDetected,
614 ASCIIToUTF16("volume label"),
615 ASCIIToUTF16("vendor name"),
616 ASCIIToUTF16("model name"),
617 1000000ULL, now, 0, 0, 0);
618 EXPECT_EQ(default_galleries_count() + 1UL, id);
619 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
620 MediaGalleryPrefInfo::kAutoDetected);
621 Verify();
623 MediaGalleryPrefInfo gallery_info;
624 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
625 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
626 EXPECT_TRUE(gallery_info.volume_metadata_valid);
627 EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info.volume_label);
628 EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info.vendor_name);
629 EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info.model_name);
630 EXPECT_EQ(1000000ULL, gallery_info.total_size_in_bytes);
631 // Note: we put the microseconds time into a double, so there'll
632 // be some possible rounding errors. If it's less than 100, we don't
633 // care.
634 EXPECT_LE(std::abs(now.ToInternalValue() -
635 gallery_info.last_attach_time.ToInternalValue()),
636 100);
639 TEST_F(MediaGalleriesPreferencesTest, ReplaceGalleryWithVolumeMetadata) {
640 MediaGalleryPrefId id, metadata_id;
641 base::FilePath path;
642 StorageInfo info;
643 base::FilePath relative_path;
644 base::Time now = base::Time::Now();
645 Verify();
647 // Add an auto detected gallery in the prefs version 0 format.
648 path = MakeMediaGalleriesTestingPath("new_auto");
649 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
650 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
651 id = AddGalleryWithNameV0(info.device_id(), gallery_name, relative_path,
652 false /*auto*/);
653 EXPECT_EQ(default_galleries_count() + 1UL, id);
654 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
655 MediaGalleryPrefInfo::kAutoDetected);
656 Verify();
658 metadata_id = gallery_prefs()->AddGallery(info.device_id(),
659 relative_path,
660 MediaGalleryPrefInfo::kAutoDetected,
661 ASCIIToUTF16("volume label"),
662 ASCIIToUTF16("vendor name"),
663 ASCIIToUTF16("model name"),
664 1000000ULL, now, 0, 0, 0);
665 EXPECT_EQ(id, metadata_id);
666 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
667 MediaGalleryPrefInfo::kAutoDetected);
669 // Make sure the display_name is set to empty now, as the metadata
670 // upgrade should set the manual override name empty.
671 Verify();
674 // Whenever an "AutoDetected" gallery is removed, it is moved to a black listed
675 // state. When the gallery is added again, the black listed state is updated
676 // back to the "AutoDetected" type.
677 TEST_F(MediaGalleriesPreferencesTest, AutoAddedBlackListing) {
678 MediaGalleryPrefId auto_id, id;
679 base::FilePath path;
680 StorageInfo info;
681 base::FilePath relative_path;
682 Verify();
684 // Add a new auto detect gallery to test with.
685 path = MakeMediaGalleriesTestingPath("new_auto");
686 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
687 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
688 id = AddGalleryWithNameV1(info.device_id(), gallery_name,
689 relative_path, false /*auto*/);
690 EXPECT_EQ(default_galleries_count() + 1UL, id);
691 auto_id = id;
692 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
693 MediaGalleryPrefInfo::kAutoDetected);
694 Verify();
696 // Remove an auto added gallery (i.e. make it blacklisted).
697 gallery_prefs()->ForgetGalleryById(auto_id);
698 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
699 expected_galleries_for_all.erase(auto_id);
700 Verify();
702 // Try adding the gallery again automatically and it should be a no-op.
703 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
704 false /*auto*/);
705 EXPECT_EQ(auto_id, id);
706 Verify();
708 // Add the gallery again as a user action.
709 id = gallery_prefs()->AddGalleryByPath(path,
710 MediaGalleryPrefInfo::kUserAdded);
711 EXPECT_EQ(auto_id, id);
712 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
713 MediaGalleryPrefInfo::kAutoDetected);
714 Verify();
717 // Whenever a "ScanResult" gallery is removed, it is moved to a black listed
718 // state. When the gallery is added again, the black listed state is updated
719 // back to the "ScanResult" type.
720 TEST_F(MediaGalleriesPreferencesTest, ScanResultBlackListing) {
721 MediaGalleryPrefId scan_id, id;
722 base::FilePath path;
723 StorageInfo info;
724 base::FilePath relative_path;
725 Verify();
727 // Add a new scan result gallery to test with.
728 path = MakeMediaGalleriesTestingPath("new_scan");
729 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
730 base::string16 gallery_name = base::ASCIIToUTF16("NewScanGallery");
731 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
732 MediaGalleryPrefInfo::kScanResult);
733 EXPECT_EQ(default_galleries_count() + 1UL, id);
734 scan_id = id;
735 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
736 MediaGalleryPrefInfo::kScanResult);
737 Verify();
739 // Remove a scan result gallery (i.e. make it blacklisted).
740 gallery_prefs()->ForgetGalleryById(scan_id);
741 expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
742 expected_galleries_for_all.erase(scan_id);
743 Verify();
745 // Try adding the gallery again as a scan result it should be a no-op.
746 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
747 MediaGalleryPrefInfo::kScanResult);
748 EXPECT_EQ(scan_id, id);
749 Verify();
751 // Add the gallery again as a user action.
752 id = gallery_prefs()->AddGalleryByPath(path,
753 MediaGalleryPrefInfo::kUserAdded);
754 EXPECT_EQ(scan_id, id);
755 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
756 MediaGalleryPrefInfo::kUserAdded);
757 Verify();
760 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryNameV2) {
761 // Add a new auto detect gallery to test with.
762 base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
763 StorageInfo info;
764 base::FilePath relative_path;
765 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
766 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
767 MediaGalleryPrefId id =
768 AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
769 MediaGalleryPrefInfo::kAutoDetected);
770 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
771 MediaGalleryPrefInfo::kAutoDetected);
772 Verify();
774 // Won't override the name -- don't change any expectation.
775 gallery_name = base::string16();
776 AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
777 MediaGalleryPrefInfo::kAutoDetected);
778 Verify();
780 gallery_name = base::ASCIIToUTF16("NewName");
781 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
782 MediaGalleryPrefInfo::kAutoDetected);
783 // Note: will really just update the existing expectation.
784 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
785 MediaGalleryPrefInfo::kAutoDetected);
786 Verify();
789 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
790 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, scan_id,
791 to_scan_remove_id, id;
792 base::FilePath path;
793 StorageInfo info;
794 base::FilePath relative_path;
795 Verify();
797 // Add some galleries to test with.
798 path = MakeMediaGalleriesTestingPath("new_user");
799 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
800 base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
801 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
802 true /*user*/);
803 EXPECT_EQ(default_galleries_count() + 1UL, id);
804 user_added_id = id;
805 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
806 MediaGalleryPrefInfo::kUserAdded);
807 Verify();
809 path = MakeMediaGalleriesTestingPath("new_auto");
810 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
811 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
812 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
813 false /*auto*/);
814 EXPECT_EQ(default_galleries_count() + 2UL, id);
815 auto_id = id;
816 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
817 MediaGalleryPrefInfo::kAutoDetected);
818 Verify();
820 path = MakeMediaGalleriesTestingPath("to_blacklist");
821 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
822 gallery_name = base::ASCIIToUTF16("ToBlacklistGallery");
823 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
824 false /*auto*/);
825 EXPECT_EQ(default_galleries_count() + 3UL, id);
826 to_blacklist_id = id;
827 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
828 MediaGalleryPrefInfo::kAutoDetected);
829 Verify();
831 path = MakeMediaGalleriesTestingPath("new_scan");
832 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
833 gallery_name = base::ASCIIToUTF16("NewScanGallery");
834 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
835 MediaGalleryPrefInfo::kScanResult);
836 EXPECT_EQ(default_galleries_count() + 4UL, id);
837 scan_id = id;
838 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
839 MediaGalleryPrefInfo::kScanResult);
840 Verify();
842 path = MakeMediaGalleriesTestingPath("to_scan_remove");
843 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
844 gallery_name = base::ASCIIToUTF16("ToScanRemoveGallery");
845 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
846 MediaGalleryPrefInfo::kScanResult);
847 EXPECT_EQ(default_galleries_count() + 5UL, id);
848 to_scan_remove_id = id;
849 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
850 MediaGalleryPrefInfo::kScanResult);
851 Verify();
853 // Remove permission for all galleries from the all-permission extension.
854 gallery_prefs()->SetGalleryPermissionForExtension(
855 *all_permission_extension.get(), auto_id, false);
856 expected_galleries_for_all.erase(auto_id);
857 Verify();
859 gallery_prefs()->SetGalleryPermissionForExtension(
860 *all_permission_extension.get(), user_added_id, false);
861 expected_galleries_for_all.erase(user_added_id);
862 Verify();
864 gallery_prefs()->SetGalleryPermissionForExtension(
865 *all_permission_extension.get(), to_blacklist_id, false);
866 expected_galleries_for_all.erase(to_blacklist_id);
867 Verify();
869 gallery_prefs()->SetGalleryPermissionForExtension(
870 *all_permission_extension.get(), scan_id, false);
871 expected_galleries_for_all.erase(scan_id);
872 Verify();
874 gallery_prefs()->SetGalleryPermissionForExtension(
875 *all_permission_extension.get(), to_scan_remove_id, false);
876 expected_galleries_for_all.erase(to_scan_remove_id);
877 Verify();
879 // Add permission back for all galleries to the all-permission extension.
880 gallery_prefs()->SetGalleryPermissionForExtension(
881 *all_permission_extension.get(), auto_id, true);
882 expected_galleries_for_all.insert(auto_id);
883 Verify();
885 gallery_prefs()->SetGalleryPermissionForExtension(
886 *all_permission_extension.get(), user_added_id, true);
887 expected_galleries_for_all.insert(user_added_id);
888 Verify();
890 gallery_prefs()->SetGalleryPermissionForExtension(
891 *all_permission_extension.get(), to_blacklist_id, true);
892 expected_galleries_for_all.insert(to_blacklist_id);
893 Verify();
895 gallery_prefs()->SetGalleryPermissionForExtension(
896 *all_permission_extension.get(), scan_id, true);
897 expected_galleries_for_all.insert(scan_id);
898 Verify();
900 gallery_prefs()->SetGalleryPermissionForExtension(
901 *all_permission_extension.get(), to_scan_remove_id, true);
902 expected_galleries_for_all.insert(to_scan_remove_id);
903 Verify();
905 // Add permission for all galleries to the regular permission extension.
906 gallery_prefs()->SetGalleryPermissionForExtension(
907 *regular_permission_extension.get(), auto_id, true);
908 expected_galleries_for_regular.insert(auto_id);
909 Verify();
911 gallery_prefs()->SetGalleryPermissionForExtension(
912 *regular_permission_extension.get(), user_added_id, true);
913 expected_galleries_for_regular.insert(user_added_id);
914 Verify();
916 gallery_prefs()->SetGalleryPermissionForExtension(
917 *regular_permission_extension.get(), to_blacklist_id, true);
918 expected_galleries_for_regular.insert(to_blacklist_id);
919 Verify();
921 gallery_prefs()->SetGalleryPermissionForExtension(
922 *regular_permission_extension.get(), scan_id, true);
923 expected_galleries_for_regular.insert(scan_id);
924 Verify();
926 gallery_prefs()->SetGalleryPermissionForExtension(
927 *regular_permission_extension.get(), to_scan_remove_id, true);
928 expected_galleries_for_regular.insert(to_scan_remove_id);
929 Verify();
931 // Blacklist the to be black listed gallery
932 gallery_prefs()->ForgetGalleryById(to_blacklist_id);
933 expected_galleries_[to_blacklist_id].type =
934 MediaGalleryPrefInfo::kBlackListed;
935 expected_galleries_for_all.erase(to_blacklist_id);
936 expected_galleries_for_regular.erase(to_blacklist_id);
937 Verify();
939 gallery_prefs()->ForgetGalleryById(to_scan_remove_id);
940 expected_galleries_[to_scan_remove_id].type =
941 MediaGalleryPrefInfo::kRemovedScan;
942 expected_galleries_for_all.erase(to_scan_remove_id);
943 expected_galleries_for_regular.erase(to_scan_remove_id);
944 Verify();
946 // Remove permission for all galleries to the regular permission extension.
947 gallery_prefs()->SetGalleryPermissionForExtension(
948 *regular_permission_extension.get(), auto_id, false);
949 expected_galleries_for_regular.erase(auto_id);
950 Verify();
952 gallery_prefs()->SetGalleryPermissionForExtension(
953 *regular_permission_extension.get(), user_added_id, false);
954 expected_galleries_for_regular.erase(user_added_id);
955 Verify();
957 gallery_prefs()->SetGalleryPermissionForExtension(
958 *regular_permission_extension.get(), scan_id, false);
959 expected_galleries_for_regular.erase(scan_id);
960 Verify();
962 // Add permission for an invalid gallery id.
963 gallery_prefs()->SetGalleryPermissionForExtension(
964 *regular_permission_extension.get(), 9999L, true);
965 Verify();
968 // What an existing gallery is added again, update the gallery information if
969 // needed.
970 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryDetails) {
971 MediaGalleryPrefId auto_id, id;
972 base::FilePath path;
973 StorageInfo info;
974 base::FilePath relative_path;
975 Verify();
977 // Add a new auto detect gallery to test with.
978 path = MakeMediaGalleriesTestingPath("new_auto");
979 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
980 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
981 id = AddGalleryWithNameV1(info.device_id(), gallery_name,
982 relative_path, false /*auto*/);
983 EXPECT_EQ(default_galleries_count() + 1UL, id);
984 auto_id = id;
985 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
986 MediaGalleryPrefInfo::kAutoDetected);
987 Verify();
989 // Update the device name and add the gallery again.
990 gallery_name = base::ASCIIToUTF16("AutoGallery2");
991 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
992 false /*auto*/);
993 EXPECT_EQ(auto_id, id);
994 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
995 MediaGalleryPrefInfo::kAutoDetected);
996 Verify();
999 TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) {
1000 base::FilePath path;
1001 StorageInfo info;
1002 base::FilePath relative_path;
1003 Verify();
1005 // Add a regular gallery
1006 path = MakeMediaGalleriesTestingPath("new_user");
1007 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1008 base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
1009 MediaGalleryPrefId user_added_id =
1010 AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
1011 true /*user*/);
1012 EXPECT_EQ(default_galleries_count() + 1UL, user_added_id);
1013 AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
1014 relative_path, MediaGalleryPrefInfo::kUserAdded);
1015 Verify();
1017 // Find it by device id and fail to find something related.
1018 MediaGalleryPrefIdSet pref_id_set;
1019 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
1020 EXPECT_EQ(1U, pref_id_set.size());
1021 EXPECT_TRUE(pref_id_set.find(user_added_id) != pref_id_set.end());
1023 MediaStorageUtil::GetDeviceInfoFromPath(
1024 MakeMediaGalleriesTestingPath("new_user/foo"), &info, &relative_path);
1025 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
1026 EXPECT_EQ(0U, pref_id_set.size());
1028 // Add some galleries on the same device.
1029 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device1"));
1030 gallery_name = base::ASCIIToUTF16("Device1Path1");
1031 std::string device_id = "path:device1";
1032 MediaGalleryPrefId dev1_path1_id = AddGalleryWithNameV1(
1033 device_id, gallery_name, relative_path, true /*user*/);
1034 EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id);
1035 AddGalleryExpectation(dev1_path1_id, gallery_name, device_id, relative_path,
1036 MediaGalleryPrefInfo::kUserAdded);
1037 Verify();
1039 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device1"));
1040 gallery_name = base::ASCIIToUTF16("Device1Path2");
1041 MediaGalleryPrefId dev1_path2_id = AddGalleryWithNameV1(
1042 device_id, gallery_name, relative_path, true /*user*/);
1043 EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id);
1044 AddGalleryExpectation(dev1_path2_id, gallery_name, device_id, relative_path,
1045 MediaGalleryPrefInfo::kUserAdded);
1046 Verify();
1048 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device2"));
1049 gallery_name = base::ASCIIToUTF16("Device2Path1");
1050 device_id = "path:device2";
1051 MediaGalleryPrefId dev2_path1_id = AddGalleryWithNameV1(
1052 device_id, gallery_name, relative_path, true /*user*/);
1053 EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id);
1054 AddGalleryExpectation(dev2_path1_id, gallery_name, device_id, relative_path,
1055 MediaGalleryPrefInfo::kUserAdded);
1056 Verify();
1058 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device2"));
1059 gallery_name = base::ASCIIToUTF16("Device2Path2");
1060 MediaGalleryPrefId dev2_path2_id = AddGalleryWithNameV1(
1061 device_id, gallery_name, relative_path, true /*user*/);
1062 EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id);
1063 AddGalleryExpectation(dev2_path2_id, gallery_name, device_id, relative_path,
1064 MediaGalleryPrefInfo::kUserAdded);
1065 Verify();
1067 // Check that adding one of them again works as expected.
1068 MediaGalleryPrefId id = AddGalleryWithNameV1(
1069 device_id, gallery_name, relative_path, true /*user*/);
1070 EXPECT_EQ(dev2_path2_id, id);
1071 Verify();
1074 TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) {
1075 // Start with one observer.
1076 MockGalleryChangeObserver observer1(gallery_prefs());
1077 gallery_prefs()->AddGalleryChangeObserver(&observer1);
1079 // Add a new auto detected gallery.
1080 base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
1081 StorageInfo info;
1082 base::FilePath relative_path;
1083 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1084 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1085 MediaGalleryPrefId auto_id = AddGalleryWithNameV1(
1086 info.device_id(), gallery_name, relative_path, false /*auto*/);
1087 EXPECT_EQ(default_galleries_count() + 1UL, auto_id);
1088 AddGalleryExpectation(auto_id, gallery_name, info.device_id(),
1089 relative_path, MediaGalleryPrefInfo::kAutoDetected);
1090 EXPECT_EQ(1, observer1.notifications());
1092 // Add a second observer.
1093 MockGalleryChangeObserver observer2(gallery_prefs());
1094 gallery_prefs()->AddGalleryChangeObserver(&observer2);
1096 // Add a new user added gallery.
1097 path = MakeMediaGalleriesTestingPath("new_user");
1098 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1099 gallery_name = base::ASCIIToUTF16("NewUserGallery");
1100 MediaGalleryPrefId user_added_id =
1101 AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
1102 true /*user*/);
1103 AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
1104 relative_path, MediaGalleryPrefInfo::kUserAdded);
1105 EXPECT_EQ(default_galleries_count() + 2UL, user_added_id);
1106 EXPECT_EQ(2, observer1.notifications());
1107 EXPECT_EQ(1, observer2.notifications());
1109 // Remove the first observer.
1110 gallery_prefs()->RemoveGalleryChangeObserver(&observer1);
1112 // Remove an auto added gallery (i.e. make it blacklisted).
1113 gallery_prefs()->ForgetGalleryById(auto_id);
1114 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
1115 expected_galleries_for_all.erase(auto_id);
1117 EXPECT_EQ(2, observer1.notifications());
1118 EXPECT_EQ(2, observer2.notifications());
1120 // Remove a user added gallery and it should go away.
1121 gallery_prefs()->ForgetGalleryById(user_added_id);
1122 expected_galleries_.erase(user_added_id);
1123 expected_device_map[info.device_id()].erase(user_added_id);
1125 EXPECT_EQ(2, observer1.notifications());
1126 EXPECT_EQ(3, observer2.notifications());
1129 TEST_F(MediaGalleriesPreferencesTest, UpdateSingletonDeviceIdType) {
1130 MediaGalleryPrefId id;
1131 base::FilePath path;
1132 Verify();
1134 // Add a new auto detect gallery to test with.
1135 path = MakeMediaGalleriesTestingPath("new_auto");
1136 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1137 std::string device_id = StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
1138 path.AsUTF8Unsafe());
1139 id = AddGalleryWithNameV2(device_id, gallery_name, base::FilePath(),
1140 MediaGalleryPrefInfo::kAutoDetected);
1141 EXPECT_EQ(default_galleries_count() + 1UL, id);
1142 AddGalleryExpectation(id, gallery_name, device_id, base::FilePath(),
1143 MediaGalleryPrefInfo::kAutoDetected);
1144 Verify();
1146 // Update the device id.
1147 MockGalleryChangeObserver observer(gallery_prefs());
1148 gallery_prefs()->AddGalleryChangeObserver(&observer);
1150 path = MakeMediaGalleriesTestingPath("updated_path");
1151 std::string updated_device_id =
1152 StorageInfo::MakeDeviceId(StorageInfo::ITUNES, path.AsUTF8Unsafe());
1153 EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id));
1154 AddGalleryExpectation(id, gallery_name, updated_device_id, base::FilePath(),
1155 MediaGalleryPrefInfo::kAutoDetected);
1156 expected_device_map[device_id].erase(id);
1157 expected_device_map[updated_device_id].insert(id);
1158 Verify();
1159 EXPECT_EQ(1, observer.notifications());
1161 // No gallery for type.
1162 std::string new_device_id =
1163 StorageInfo::MakeDeviceId(StorageInfo::PICASA, path.AsUTF8Unsafe());
1164 EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id));
1167 TEST_F(MediaGalleriesPreferencesTest, LookupImportedGalleryByPath) {
1168 MediaGalleryPrefId id;
1169 base::FilePath path;
1170 Verify();
1172 // iTunes device path points to an XML file in the library directory.
1173 path = MakeMediaGalleriesTestingPath("new_auto").AppendASCII("library.xml");
1174 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1175 std::string device_id = StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
1176 path.AsUTF8Unsafe());
1177 id = AddGalleryWithNameV2(device_id, gallery_name, base::FilePath(),
1178 MediaGalleryPrefInfo::kAutoDetected);
1179 EXPECT_EQ(default_galleries_count() + 1UL, id);
1180 AddGalleryExpectation(id, gallery_name, device_id, base::FilePath(),
1181 MediaGalleryPrefInfo::kAutoDetected);
1182 Verify();
1184 // Verify we can look up the imported gallery by its path.
1185 MediaGalleryPrefInfo gallery_info;
1186 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(path.DirName(),
1187 &gallery_info));
1188 EXPECT_EQ(id, gallery_info.pref_id);
1191 TEST_F(MediaGalleriesPreferencesTest, ScanResults) {
1192 MediaGalleryPrefId id;
1193 base::FilePath path;
1194 StorageInfo info;
1195 base::FilePath relative_path;
1196 base::Time now = base::Time::Now();
1197 Verify();
1199 // Add a new scan result gallery to test with.
1200 path = MakeMediaGalleriesTestingPath("new_scan");
1201 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1202 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1203 MediaGalleryPrefInfo::kScanResult,
1204 ASCIIToUTF16("volume label"),
1205 ASCIIToUTF16("vendor name"),
1206 ASCIIToUTF16("model name"),
1207 1000000ULL, now, 1, 2, 3);
1208 EXPECT_EQ(default_galleries_count() + 1UL, id);
1209 AddScanResultExpectation(id, base::string16(), info.device_id(),
1210 relative_path, 1, 2, 3);
1211 Verify();
1213 // Update the found media count.
1214 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1215 MediaGalleryPrefInfo::kScanResult,
1216 ASCIIToUTF16("volume label"),
1217 ASCIIToUTF16("vendor name"),
1218 ASCIIToUTF16("model name"),
1219 1000000ULL, now, 4, 5, 6);
1220 EXPECT_EQ(default_galleries_count() + 1UL, id);
1221 AddScanResultExpectation(id, base::string16(), info.device_id(),
1222 relative_path, 4, 5, 6);
1223 Verify();
1225 // Remove a scan result (i.e. make it blacklisted).
1226 gallery_prefs()->ForgetGalleryById(id);
1227 expected_galleries_[id].type = MediaGalleryPrefInfo::kRemovedScan;
1228 expected_galleries_[id].audio_count = 0;
1229 expected_galleries_[id].image_count = 0;
1230 expected_galleries_[id].video_count = 0;
1231 Verify();
1233 // Try adding the gallery again as a scan result it should be a no-op.
1234 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1235 MediaGalleryPrefInfo::kScanResult,
1236 ASCIIToUTF16("volume label"),
1237 ASCIIToUTF16("vendor name"),
1238 ASCIIToUTF16("model name"),
1239 1000000ULL, now, 7, 8, 9);
1240 EXPECT_EQ(default_galleries_count() + 1UL, id);
1241 Verify();
1243 // Add the gallery again as a user action.
1244 id = gallery_prefs()->AddGalleryByPath(path,
1245 MediaGalleryPrefInfo::kUserAdded);
1246 EXPECT_EQ(default_galleries_count() + 1UL, id);
1247 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
1248 MediaGalleryPrefInfo::kUserAdded);
1249 Verify();
1252 TEST(MediaGalleriesPrefInfoTest, NameGeneration) {
1253 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
1255 MediaGalleryPrefInfo info;
1256 info.pref_id = 1;
1257 info.display_name = ASCIIToUTF16("override");
1258 info.device_id = StorageInfo::MakeDeviceId(
1259 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, "unique");
1261 EXPECT_EQ(ASCIIToUTF16("override"), info.GetGalleryDisplayName());
1263 info.display_name = ASCIIToUTF16("o2");
1264 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1266 EXPECT_EQ(l10n_util::GetStringUTF16(
1267 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1268 info.GetGalleryAdditionalDetails());
1270 info.last_attach_time = base::Time::Now();
1271 EXPECT_NE(l10n_util::GetStringUTF16(
1272 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1273 info.GetGalleryAdditionalDetails());
1274 EXPECT_NE(l10n_util::GetStringUTF16(
1275 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED),
1276 info.GetGalleryAdditionalDetails());
1278 info.volume_label = ASCIIToUTF16("vol");
1279 info.vendor_name = ASCIIToUTF16("vendor");
1280 info.model_name = ASCIIToUTF16("model");
1281 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1283 info.display_name = base::string16();
1284 EXPECT_EQ(ASCIIToUTF16("vol"), info.GetGalleryDisplayName());
1285 info.volume_label = base::string16();
1286 EXPECT_EQ(ASCIIToUTF16("vendor, model"), info.GetGalleryDisplayName());
1288 info.device_id = StorageInfo::MakeDeviceId(
1289 StorageInfo::FIXED_MASS_STORAGE, "unique");
1290 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(),
1291 base::UTF16ToUTF8(info.GetGalleryTooltip()));
1293 TestStorageMonitor::Destroy();
1296 TEST_F(MediaGalleriesPreferencesTest, SetsDefaultGalleryTypeField) {
1297 // Tests that default galleries (Music, Pictures, Video) have the correct
1298 // default_gallery field set.
1300 // No default galleries exist on CrOS so this test isn't relevant there.
1301 #if defined(OS_CHROMEOS)
1302 return;
1303 #endif
1305 base::FilePath music_path;
1306 base::FilePath pictures_path;
1307 base::FilePath videos_path;
1308 bool got_music_path = PathService::Get(chrome::DIR_USER_MUSIC, &music_path);
1309 bool got_pictures_path =
1310 PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path);
1311 bool got_videos_path =
1312 PathService::Get(chrome::DIR_USER_VIDEOS, &videos_path);
1314 int num_default_galleries = 0;
1316 const MediaGalleriesPrefInfoMap& known_galleries =
1317 gallery_prefs()->known_galleries();
1318 for (MediaGalleriesPrefInfoMap::const_iterator it =
1319 known_galleries.begin();
1320 it != known_galleries.end();
1321 ++it) {
1322 if (it->second.type != MediaGalleryPrefInfo::kAutoDetected)
1323 continue;
1325 std::string unique_id;
1326 if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1327 continue;
1329 if (got_music_path && unique_id == music_path.AsUTF8Unsafe()) {
1330 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault,
1331 it->second.default_gallery_type);
1332 num_default_galleries++;
1333 } else if (got_pictures_path && unique_id == pictures_path.AsUTF8Unsafe()) {
1334 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault,
1335 it->second.default_gallery_type);
1336 num_default_galleries++;
1337 } else if (got_videos_path && unique_id == videos_path.AsUTF8Unsafe()) {
1338 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault,
1339 it->second.default_gallery_type);
1340 num_default_galleries++;
1341 } else {
1342 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault,
1343 it->second.default_gallery_type);
1347 EXPECT_EQ(3, num_default_galleries);
1350 TEST_F(MediaGalleriesPreferencesTest, UpdatesDefaultGalleryType) {
1351 // Tests that if the path of a default gallery changed since last init,
1352 // then when the MediaGalleriesPreferences is initialized, it will
1353 // rewrite the device ID in prefs to include the new path.
1355 // No default galleries exist on CrOS so this test isn't relevant there.
1356 #if defined(OS_CHROMEOS)
1357 return;
1358 #endif
1360 base::FilePath old_music_path;
1361 base::FilePath old_pictures_path;
1362 base::FilePath old_videos_path;
1363 bool got_old_music_path =
1364 PathService::Get(chrome::DIR_USER_MUSIC, &old_music_path);
1365 bool got_old_pictures_path =
1366 PathService::Get(chrome::DIR_USER_PICTURES, &old_pictures_path);
1367 bool got_old_videos_path =
1368 PathService::Get(chrome::DIR_USER_VIDEOS, &old_videos_path);
1370 bool found_music = false;
1371 bool found_pictures = false;
1372 bool found_videos = false;
1374 const MediaGalleriesPrefInfoMap& old_known_galleries =
1375 gallery_prefs()->known_galleries();
1376 for (MediaGalleriesPrefInfoMap::const_iterator it =
1377 old_known_galleries.begin();
1378 it != old_known_galleries.end();
1379 ++it) {
1380 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) {
1381 std::string unique_id;
1382 if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1383 continue;
1385 if (got_old_music_path &&
1386 it->second.default_gallery_type ==
1387 MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
1388 EXPECT_EQ(old_music_path.AsUTF8Unsafe(), unique_id);
1389 found_music = true;
1390 } else if (got_old_pictures_path &&
1391 it->second.default_gallery_type ==
1392 MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
1393 EXPECT_EQ(old_pictures_path.AsUTF8Unsafe(), unique_id);
1394 found_pictures = true;
1395 } else if (got_old_videos_path &&
1396 it->second.default_gallery_type ==
1397 MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
1398 EXPECT_EQ(old_videos_path.AsUTF8Unsafe(), unique_id);
1399 found_videos = true;
1404 EXPECT_TRUE(found_music);
1405 EXPECT_TRUE(found_pictures);
1406 EXPECT_TRUE(found_videos);
1408 ChangeMediaPathOverrides();
1409 ReinitPrefsAndExpectations();
1411 base::FilePath new_music_path;
1412 base::FilePath new_pictures_path;
1413 base::FilePath new_videos_path;
1414 bool got_new_music_path =
1415 PathService::Get(chrome::DIR_USER_MUSIC, &new_music_path);
1416 bool got_new_pictures_path =
1417 PathService::Get(chrome::DIR_USER_PICTURES, &new_pictures_path);
1418 bool got_new_videos_path =
1419 PathService::Get(chrome::DIR_USER_VIDEOS, &new_videos_path);
1421 EXPECT_NE(new_music_path, old_music_path);
1422 EXPECT_NE(new_pictures_path, old_pictures_path);
1423 EXPECT_NE(new_videos_path, old_videos_path);
1425 found_music = false;
1426 found_pictures = false;
1427 found_videos = false;
1429 const MediaGalleriesPrefInfoMap& known_galleries =
1430 gallery_prefs()->known_galleries();
1431 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
1432 it != known_galleries.end();
1433 ++it) {
1434 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) {
1435 std::string unique_id;
1436 if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1437 continue;
1439 if (got_new_music_path &&
1440 it->second.default_gallery_type ==
1441 MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
1442 EXPECT_EQ(new_music_path.AsUTF8Unsafe(), unique_id);
1443 found_music = true;
1444 } else if (got_new_pictures_path &&
1445 it->second.default_gallery_type ==
1446 MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
1447 EXPECT_EQ(new_pictures_path.AsUTF8Unsafe(), unique_id);
1448 found_pictures = true;
1449 } else if (got_new_videos_path &&
1450 it->second.default_gallery_type ==
1451 MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
1452 EXPECT_EQ(new_videos_path.AsUTF8Unsafe(), unique_id);
1453 found_videos = true;
1458 EXPECT_TRUE(found_music);
1459 EXPECT_TRUE(found_pictures);
1460 EXPECT_TRUE(found_videos);
1463 TEST_F(MediaGalleriesPreferencesTest, UpdateAddsDefaultGalleryTypeIfMissing) {
1464 // Tests that if no default_gallery_type was specified for an existing prefs
1465 // info object corresponding to a particular gallery, then when the
1466 // MediaGalleriesPreferences is initialized, it assigns the proper one.
1468 // No default galleries exist on CrOS so this test isn't relevant there.
1469 #if defined(OS_CHROMEOS)
1470 return;
1471 #endif
1473 // Add a new user added gallery.
1474 AddFixedGalleryWithExepectation("user_added", "UserAdded",
1475 MediaGalleryPrefInfo::kUserAdded);
1477 // Remove the "default_gallery_type" field completely from the persisted data
1478 // for the prefs info object. This simulates the case where a user updated
1479 // Chrome from a version without that field to one with it.
1480 RemovePersistedDefaultGalleryValues();
1482 // Reinitializing the MediaGalleriesPreferences should populate the
1483 // default_gallery_type field with the correct value for each gallery.
1484 ReinitPrefsAndExpectations();
1486 base::FilePath music_path;
1487 base::FilePath pictures_path;
1488 base::FilePath videos_path;
1489 bool got_music_path = PathService::Get(chrome::DIR_USER_MUSIC, &music_path);
1490 bool got_pictures_path =
1491 PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path);
1492 bool got_videos_path =
1493 PathService::Get(chrome::DIR_USER_VIDEOS, &videos_path);
1495 bool found_music = false;
1496 bool found_pictures = false;
1497 bool found_videos = false;
1498 bool found_user_added = false;
1500 const MediaGalleriesPrefInfoMap& known_galleries =
1501 gallery_prefs()->known_galleries();
1502 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
1503 it != known_galleries.end();
1504 ++it) {
1505 std::string unique_id;
1506 if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1507 continue;
1509 if (got_music_path &&
1510 it->second.default_gallery_type ==
1511 MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
1512 EXPECT_EQ(music_path.AsUTF8Unsafe(), unique_id);
1513 found_music = true;
1514 } else if (got_pictures_path &&
1515 it->second.default_gallery_type ==
1516 MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
1517 EXPECT_EQ(pictures_path.AsUTF8Unsafe(), unique_id);
1518 found_pictures = true;
1519 } else if (got_videos_path &&
1520 it->second.default_gallery_type ==
1521 MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
1522 EXPECT_EQ(videos_path.AsUTF8Unsafe(), unique_id);
1523 found_videos = true;
1524 } else if (it->second.default_gallery_type ==
1525 MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault) {
1526 found_user_added = true;
1530 EXPECT_TRUE(found_music);
1531 EXPECT_TRUE(found_pictures);
1532 EXPECT_TRUE(found_videos);
1533 EXPECT_TRUE(found_user_added);