NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_preferences_unittest.cc
blob2cc4780eb02f267aeef88332c560ff844bbfd9f4
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 "base/command_line.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/extensions/test_extension_system.h"
16 #include "chrome/browser/media_galleries/media_file_system_registry.h"
17 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
18 #include "chrome/browser/storage_monitor/media_storage_util.h"
19 #include "chrome/browser/storage_monitor/storage_monitor.h"
20 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
21 #include "chrome/common/extensions/permissions/media_galleries_permission.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/common/manifest_handlers/background_info.h"
27 #include "grit/generated_resources.h"
28 #include "sync/api/string_ordinal.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/base/l10n/l10n_util.h"
32 #if defined(OS_CHROMEOS)
33 #include "chrome/browser/chromeos/login/user_manager.h"
34 #include "chrome/browser/chromeos/settings/cros_settings.h"
35 #include "chrome/browser/chromeos/settings/device_settings_service.h"
36 #endif
38 using base::ASCIIToUTF16;
40 namespace {
42 class MockGalleryChangeObserver
43 : public MediaGalleriesPreferences::GalleryChangeObserver {
44 public:
45 explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref)
46 : pref_(pref),
47 notifications_(0) {}
48 virtual ~MockGalleryChangeObserver() {}
50 int notifications() const { return notifications_;}
52 private:
53 // MediaGalleriesPreferences::GalleryChangeObserver implementation.
54 virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
55 const std::string& extension_id,
56 MediaGalleryPrefId pref_id) OVERRIDE {
57 EXPECT_EQ(pref_, pref);
58 ++notifications_;
61 virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
62 const std::string& extension_id,
63 MediaGalleryPrefId pref_id) OVERRIDE {
64 EXPECT_EQ(pref_, pref);
65 ++notifications_;
68 virtual void OnGalleryAdded(MediaGalleriesPreferences* pref,
69 MediaGalleryPrefId pref_id) OVERRIDE {
70 EXPECT_EQ(pref_, pref);
71 ++notifications_;
74 virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
75 MediaGalleryPrefId pref_id) OVERRIDE {
76 EXPECT_EQ(pref_, pref);
77 ++notifications_;
80 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
81 MediaGalleryPrefId pref_id) OVERRIDE {
82 EXPECT_EQ(pref_, pref);
83 ++notifications_;
86 MediaGalleriesPreferences* pref_;
87 int notifications_;
89 DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver);
92 } // namespace
94 class MediaGalleriesPreferencesTest : public testing::Test {
95 public:
96 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
97 DeviceIdPrefIdsMap;
99 MediaGalleriesPreferencesTest()
100 : profile_(new TestingProfile()),
101 default_galleries_count_(0) {
104 virtual ~MediaGalleriesPreferencesTest() {
107 virtual void SetUp() OVERRIDE {
108 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
110 extensions::TestExtensionSystem* extension_system(
111 static_cast<extensions::TestExtensionSystem*>(
112 extensions::ExtensionSystem::Get(profile_.get())));
113 extension_system->CreateExtensionService(
114 CommandLine::ForCurrentProcess(), base::FilePath(), false);
116 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
117 base::RunLoop loop;
118 gallery_prefs_->EnsureInitialized(loop.QuitClosure());
119 loop.Run();
121 // Load the default galleries into the expectations.
122 const MediaGalleriesPrefInfoMap& known_galleries =
123 gallery_prefs_->known_galleries();
124 if (known_galleries.size()) {
125 ASSERT_EQ(3U, known_galleries.size());
126 default_galleries_count_ = 3;
127 MediaGalleriesPrefInfoMap::const_iterator it;
128 for (it = known_galleries.begin(); it != known_galleries.end(); ++it) {
129 expected_galleries_[it->first] = it->second;
130 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
131 expected_galleries_for_all.insert(it->first);
135 std::vector<std::string> all_permissions;
136 all_permissions.push_back(
137 extensions::MediaGalleriesPermission::kReadPermission);
138 all_permissions.push_back(
139 extensions::MediaGalleriesPermission::kAllAutoDetectedPermission);
140 std::vector<std::string> read_permissions;
141 read_permissions.push_back(
142 extensions::MediaGalleriesPermission::kReadPermission);
144 all_permission_extension =
145 AddMediaGalleriesApp("all", all_permissions, profile_.get());
146 regular_permission_extension =
147 AddMediaGalleriesApp("regular", read_permissions, profile_.get());
148 no_permissions_extension =
149 AddMediaGalleriesApp("no", read_permissions, profile_.get());
152 virtual void TearDown() OVERRIDE {
153 Verify();
154 TestStorageMonitor::RemoveSingleton();
157 void Verify() {
158 const MediaGalleriesPrefInfoMap& known_galleries =
159 gallery_prefs_->known_galleries();
160 EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
161 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
162 it != known_galleries.end();
163 ++it) {
164 VerifyGalleryInfo(it->second, it->first);
165 if (it->second.type != MediaGalleryPrefInfo::kAutoDetected &&
166 it->second.type != MediaGalleryPrefInfo::kBlackListed) {
167 if (!ContainsKey(expected_galleries_for_all, it->first) &&
168 !ContainsKey(expected_galleries_for_regular, it->first)) {
169 EXPECT_FALSE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
170 } else {
171 EXPECT_TRUE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
176 for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin();
177 it != expected_device_map.end();
178 ++it) {
179 MediaGalleryPrefIdSet actual_id_set =
180 gallery_prefs_->LookUpGalleriesByDeviceId(it->first);
181 EXPECT_EQ(it->second, actual_id_set);
184 std::set<MediaGalleryPrefId> galleries_for_all =
185 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get());
186 EXPECT_EQ(expected_galleries_for_all, galleries_for_all);
188 std::set<MediaGalleryPrefId> galleries_for_regular =
189 gallery_prefs_->GalleriesForExtension(
190 *regular_permission_extension.get());
191 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular);
193 std::set<MediaGalleryPrefId> galleries_for_no =
194 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get());
195 EXPECT_EQ(0U, galleries_for_no.size());
198 void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual,
199 MediaGalleryPrefId expected_id) const {
200 MediaGalleriesPrefInfoMap::const_iterator in_expectation =
201 expected_galleries_.find(expected_id);
202 ASSERT_FALSE(in_expectation == expected_galleries_.end()) << expected_id;
203 EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id);
204 EXPECT_EQ(in_expectation->second.display_name, actual.display_name);
205 EXPECT_EQ(in_expectation->second.device_id, actual.device_id);
206 EXPECT_EQ(in_expectation->second.path.value(), actual.path.value());
207 EXPECT_EQ(in_expectation->second.type, actual.type);
208 EXPECT_EQ(in_expectation->second.audio_count, actual.audio_count);
209 EXPECT_EQ(in_expectation->second.image_count, actual.image_count);
210 EXPECT_EQ(in_expectation->second.video_count, actual.video_count);
213 MediaGalleriesPreferences* gallery_prefs() {
214 return gallery_prefs_.get();
217 uint64 default_galleries_count() {
218 return default_galleries_count_;
221 void AddGalleryExpectation(MediaGalleryPrefId id, base::string16 display_name,
222 std::string device_id,
223 base::FilePath relative_path,
224 MediaGalleryPrefInfo::Type type) {
225 expected_galleries_[id].pref_id = id;
226 expected_galleries_[id].display_name = display_name;
227 expected_galleries_[id].device_id = device_id;
228 expected_galleries_[id].path = relative_path.NormalizePathSeparators();
229 expected_galleries_[id].type = type;
231 if (type == MediaGalleryPrefInfo::kAutoDetected)
232 expected_galleries_for_all.insert(id);
234 expected_device_map[device_id].insert(id);
237 void AddScanResultExpectation(MediaGalleryPrefId id,
238 base::string16 display_name,
239 std::string device_id,
240 base::FilePath relative_path,
241 int audio_count,
242 int image_count,
243 int video_count) {
244 AddGalleryExpectation(id, display_name, device_id, relative_path,
245 MediaGalleryPrefInfo::kScanResult);
246 expected_galleries_[id].audio_count = audio_count;
247 expected_galleries_[id].image_count = image_count;
248 expected_galleries_[id].video_count = video_count;
251 MediaGalleryPrefId AddGalleryWithNameV0(const std::string& device_id,
252 const base::string16& display_name,
253 const base::FilePath& relative_path,
254 bool user_added) {
255 MediaGalleryPrefInfo::Type type =
256 user_added ? MediaGalleryPrefInfo::kUserAdded
257 : MediaGalleryPrefInfo::kAutoDetected;
258 return gallery_prefs()->AddGalleryInternal(
259 device_id, display_name, relative_path, type,
260 base::string16(), base::string16(), base::string16(), 0, base::Time(),
261 false, 0, 0, 0, 0);
264 MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id,
265 const base::string16& display_name,
266 const base::FilePath& relative_path,
267 bool user_added) {
268 MediaGalleryPrefInfo::Type type =
269 user_added ? MediaGalleryPrefInfo::kUserAdded
270 : MediaGalleryPrefInfo::kAutoDetected;
271 return gallery_prefs()->AddGalleryInternal(
272 device_id, display_name, relative_path, type,
273 base::string16(), base::string16(), base::string16(), 0, base::Time(),
274 false, 0, 0, 0, 1);
277 MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id,
278 const base::string16& display_name,
279 const base::FilePath& relative_path,
280 MediaGalleryPrefInfo::Type type) {
281 return gallery_prefs()->AddGalleryInternal(
282 device_id, display_name, relative_path, type,
283 base::string16(), base::string16(), base::string16(), 0, base::Time(),
284 false, 0, 0, 0, 2);
287 MediaGalleryPrefId AddFixedGalleryWithExepectation(
288 const std::string& path_name, const std::string& name,
289 MediaGalleryPrefInfo::Type type) {
290 base::FilePath path = MakeMediaGalleriesTestingPath(path_name);
291 StorageInfo info;
292 base::FilePath relative_path;
293 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
294 info.set_name(ASCIIToUTF16(name));
295 MediaGalleryPrefId id = AddGalleryWithNameV2(info.device_id(), info.name(),
296 relative_path, type);
297 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
298 type);
299 Verify();
300 return id;
303 bool UpdateDeviceIDForSingletonType(const std::string& device_id) {
304 return gallery_prefs()->UpdateDeviceIDForSingletonType(device_id);
307 scoped_refptr<extensions::Extension> all_permission_extension;
308 scoped_refptr<extensions::Extension> regular_permission_extension;
309 scoped_refptr<extensions::Extension> no_permissions_extension;
311 std::set<MediaGalleryPrefId> expected_galleries_for_all;
312 std::set<MediaGalleryPrefId> expected_galleries_for_regular;
314 DeviceIdPrefIdsMap expected_device_map;
316 MediaGalleriesPrefInfoMap expected_galleries_;
318 private:
319 // Needed for extension service & friends to work.
320 content::TestBrowserThreadBundle thread_bundle_;
322 EnsureMediaDirectoriesExists mock_gallery_locations_;
324 #if defined(OS_CHROMEOS)
325 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
326 chromeos::ScopedTestCrosSettings test_cros_settings_;
327 chromeos::ScopedTestUserManager test_user_manager_;
328 #endif
330 TestStorageMonitor monitor_;
331 scoped_ptr<TestingProfile> profile_;
332 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
334 uint64 default_galleries_count_;
336 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest);
339 TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) {
340 MediaGalleryPrefId auto_id, user_added_id, scan_id, id;
341 base::FilePath path;
342 base::FilePath relative_path;
343 Verify();
345 // Add a new auto detected gallery.
346 path = MakeMediaGalleriesTestingPath("new_auto");
347 StorageInfo info;
348 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
349 info.set_name(ASCIIToUTF16("NewAutoGallery"));
350 id = AddGalleryWithNameV2(info.device_id(), info.name(),
351 relative_path, MediaGalleryPrefInfo::kAutoDetected);
352 EXPECT_EQ(default_galleries_count() + 1UL, id);
353 auto_id = id;
354 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
355 MediaGalleryPrefInfo::kAutoDetected);
356 Verify();
358 // Add it as other types, nothing should happen.
359 id = AddGalleryWithNameV2(info.device_id(), info.name(),
360 relative_path, MediaGalleryPrefInfo::kUserAdded);
361 EXPECT_EQ(auto_id, id);
362 Verify();
363 id = AddGalleryWithNameV2(info.device_id(), info.name(),
364 relative_path, MediaGalleryPrefInfo::kAutoDetected);
365 EXPECT_EQ(auto_id, id);
366 Verify();
367 id = AddGalleryWithNameV2(info.device_id(), info.name(),
368 relative_path, MediaGalleryPrefInfo::kScanResult);
369 EXPECT_EQ(auto_id, id);
371 // Add a new user added gallery.
372 path = MakeMediaGalleriesTestingPath("new_user");
373 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
374 info.set_name(ASCIIToUTF16("NewUserGallery"));
375 id = AddGalleryWithNameV2(info.device_id(), info.name(),
376 relative_path, MediaGalleryPrefInfo::kUserAdded);
377 EXPECT_EQ(default_galleries_count() + 2UL, id);
378 user_added_id = id;
379 const std::string user_added_device_id = info.device_id();
380 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
381 MediaGalleryPrefInfo::kUserAdded);
382 Verify();
384 // Add it as other types, nothing should happen.
385 id = AddGalleryWithNameV2(info.device_id(), info.name(),
386 relative_path, MediaGalleryPrefInfo::kUserAdded);
387 EXPECT_EQ(user_added_id, id);
388 Verify();
389 id = AddGalleryWithNameV2(info.device_id(), info.name(),
390 relative_path, MediaGalleryPrefInfo::kAutoDetected);
391 EXPECT_EQ(user_added_id, id);
392 Verify();
393 id = AddGalleryWithNameV2(info.device_id(), info.name(),
394 relative_path, MediaGalleryPrefInfo::kScanResult);
395 EXPECT_EQ(user_added_id, id);
396 Verify();
398 // Add a new scan result gallery.
399 path = MakeMediaGalleriesTestingPath("new_scan");
400 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
401 info.set_name(ASCIIToUTF16("NewScanGallery"));
402 id = AddGalleryWithNameV2(info.device_id(), info.name(),
403 relative_path, MediaGalleryPrefInfo::kScanResult);
404 EXPECT_EQ(default_galleries_count() + 3UL, id);
405 scan_id = id;
406 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
407 MediaGalleryPrefInfo::kScanResult);
408 Verify();
410 // Add it as other types, nothing should happen.
411 id = AddGalleryWithNameV2(info.device_id(), info.name(),
412 relative_path, MediaGalleryPrefInfo::kUserAdded);
413 EXPECT_EQ(scan_id, id);
414 Verify();
415 id = AddGalleryWithNameV2(info.device_id(), info.name(),
416 relative_path, MediaGalleryPrefInfo::kAutoDetected);
417 EXPECT_EQ(scan_id, id);
418 Verify();
419 id = AddGalleryWithNameV2(info.device_id(), info.name(),
420 relative_path, MediaGalleryPrefInfo::kScanResult);
421 EXPECT_EQ(scan_id, id);
422 Verify();
424 // Lookup some galleries.
425 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
426 MakeMediaGalleriesTestingPath("new_auto"), NULL));
427 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
428 MakeMediaGalleriesTestingPath("new_user"), NULL));
429 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
430 MakeMediaGalleriesTestingPath("new_scan"), NULL));
431 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(
432 MakeMediaGalleriesTestingPath("other"), NULL));
434 // Check that we always get the gallery info.
435 MediaGalleryPrefInfo gallery_info;
436 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
437 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
438 VerifyGalleryInfo(gallery_info, auto_id);
439 EXPECT_FALSE(gallery_info.volume_metadata_valid);
440 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
441 MakeMediaGalleriesTestingPath("new_user"), &gallery_info));
442 VerifyGalleryInfo(gallery_info, user_added_id);
443 EXPECT_FALSE(gallery_info.volume_metadata_valid);
444 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
445 MakeMediaGalleriesTestingPath("new_scan"), &gallery_info));
446 VerifyGalleryInfo(gallery_info, scan_id);
447 EXPECT_FALSE(gallery_info.volume_metadata_valid);
449 path = MakeMediaGalleriesTestingPath("other");
450 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info));
451 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id);
453 StorageInfo other_info;
454 MediaStorageUtil::GetDeviceInfoFromPath(path, &other_info, &relative_path);
455 EXPECT_EQ(other_info.device_id(), gallery_info.device_id);
456 EXPECT_EQ(relative_path.value(), gallery_info.path.value());
458 // Remove an auto added gallery (i.e. make it blacklisted).
459 gallery_prefs()->ForgetGalleryById(auto_id);
460 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
461 expected_galleries_for_all.erase(auto_id);
462 Verify();
464 // Remove a scan result (i.e. make it blacklisted).
465 gallery_prefs()->ForgetGalleryById(scan_id);
466 expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
467 Verify();
469 // Remove a user added gallery and it should go away.
470 gallery_prefs()->ForgetGalleryById(user_added_id);
471 expected_galleries_.erase(user_added_id);
472 expected_device_map[user_added_device_id].erase(user_added_id);
473 Verify();
476 TEST_F(MediaGalleriesPreferencesTest, ForgetAndErase) {
477 MediaGalleryPrefId user_erase =
478 AddFixedGalleryWithExepectation("user_erase", "UserErase",
479 MediaGalleryPrefInfo::kUserAdded);
480 EXPECT_EQ(default_galleries_count() + 1UL, user_erase);
481 MediaGalleryPrefId user_forget =
482 AddFixedGalleryWithExepectation("user_forget", "UserForget",
483 MediaGalleryPrefInfo::kUserAdded);
484 EXPECT_EQ(default_galleries_count() + 2UL, user_forget);
486 MediaGalleryPrefId auto_erase =
487 AddFixedGalleryWithExepectation("auto_erase", "AutoErase",
488 MediaGalleryPrefInfo::kAutoDetected);
489 EXPECT_EQ(default_galleries_count() + 3UL, auto_erase);
490 MediaGalleryPrefId auto_forget =
491 AddFixedGalleryWithExepectation("auto_forget", "AutoForget",
492 MediaGalleryPrefInfo::kAutoDetected);
493 EXPECT_EQ(default_galleries_count() + 4UL, auto_forget);
495 MediaGalleryPrefId scan_erase =
496 AddFixedGalleryWithExepectation("scan_erase", "ScanErase",
497 MediaGalleryPrefInfo::kScanResult);
498 EXPECT_EQ(default_galleries_count() + 5UL, scan_erase);
499 MediaGalleryPrefId scan_forget =
500 AddFixedGalleryWithExepectation("scan_forget", "ScanForget",
501 MediaGalleryPrefInfo::kScanResult);
502 EXPECT_EQ(default_galleries_count() + 6UL, scan_forget);
504 Verify();
505 std::string device_id;
507 gallery_prefs()->ForgetGalleryById(user_forget);
508 device_id = expected_galleries_[user_forget].device_id;
509 expected_galleries_.erase(user_forget);
510 expected_device_map[device_id].erase(user_forget);
511 Verify();
513 gallery_prefs()->ForgetGalleryById(auto_forget);
514 expected_galleries_[auto_forget].type = MediaGalleryPrefInfo::kBlackListed;
515 expected_galleries_for_all.erase(auto_forget);
516 Verify();
518 gallery_prefs()->ForgetGalleryById(scan_forget);
519 expected_galleries_[scan_forget].type = MediaGalleryPrefInfo::kRemovedScan;
520 Verify();
522 gallery_prefs()->EraseGalleryById(user_erase);
523 device_id = expected_galleries_[user_erase].device_id;
524 expected_galleries_.erase(user_erase);
525 expected_device_map[device_id].erase(user_erase);
526 Verify();
528 gallery_prefs()->EraseGalleryById(auto_erase);
529 device_id = expected_galleries_[auto_erase].device_id;
530 expected_galleries_.erase(auto_erase);
531 expected_device_map[device_id].erase(auto_erase);
532 expected_galleries_for_all.erase(auto_erase);
533 Verify();
535 gallery_prefs()->EraseGalleryById(scan_erase);
536 device_id = expected_galleries_[scan_erase].device_id;
537 expected_galleries_.erase(scan_erase);
538 expected_device_map[device_id].erase(scan_erase);
539 Verify();
541 // Also erase the previously forgetten ones to check erasing blacklisted ones.
542 gallery_prefs()->EraseGalleryById(auto_forget);
543 device_id = expected_galleries_[auto_forget].device_id;
544 expected_galleries_.erase(auto_forget);
545 expected_device_map[device_id].erase(auto_forget);
546 Verify();
548 gallery_prefs()->EraseGalleryById(scan_forget);
549 device_id = expected_galleries_[scan_forget].device_id;
550 expected_galleries_.erase(scan_forget);
551 expected_device_map[device_id].erase(scan_forget);
552 Verify();
555 TEST_F(MediaGalleriesPreferencesTest, AddGalleryWithVolumeMetadata) {
556 MediaGalleryPrefId id;
557 StorageInfo info;
558 base::FilePath path;
559 base::FilePath relative_path;
560 base::Time now = base::Time::Now();
561 Verify();
563 // Add a new auto detected gallery.
564 path = MakeMediaGalleriesTestingPath("new_auto");
565 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
566 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
567 MediaGalleryPrefInfo::kAutoDetected,
568 ASCIIToUTF16("volume label"),
569 ASCIIToUTF16("vendor name"),
570 ASCIIToUTF16("model name"),
571 1000000ULL, now, 0, 0, 0);
572 EXPECT_EQ(default_galleries_count() + 1UL, id);
573 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
574 MediaGalleryPrefInfo::kAutoDetected);
575 Verify();
577 MediaGalleryPrefInfo gallery_info;
578 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
579 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
580 EXPECT_TRUE(gallery_info.volume_metadata_valid);
581 EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info.volume_label);
582 EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info.vendor_name);
583 EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info.model_name);
584 EXPECT_EQ(1000000ULL, gallery_info.total_size_in_bytes);
585 // Note: we put the microseconds time into a double, so there'll
586 // be some possible rounding errors. If it's less than 100, we don't
587 // care.
588 EXPECT_LE(abs(now.ToInternalValue() -
589 gallery_info.last_attach_time.ToInternalValue()), 100);
592 TEST_F(MediaGalleriesPreferencesTest, ReplaceGalleryWithVolumeMetadata) {
593 MediaGalleryPrefId id, metadata_id;
594 base::FilePath path;
595 StorageInfo info;
596 base::FilePath relative_path;
597 base::Time now = base::Time::Now();
598 Verify();
600 // Add an auto detected gallery in the prefs version 0 format.
601 path = MakeMediaGalleriesTestingPath("new_auto");
602 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
603 info.set_name(ASCIIToUTF16("NewAutoGallery"));
604 id = AddGalleryWithNameV0(info.device_id(), info.name(),
605 relative_path, false /*auto*/);
606 EXPECT_EQ(default_galleries_count() + 1UL, id);
607 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
608 MediaGalleryPrefInfo::kAutoDetected);
609 Verify();
611 metadata_id = gallery_prefs()->AddGallery(info.device_id(),
612 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(id, metadata_id);
619 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
620 MediaGalleryPrefInfo::kAutoDetected);
622 // Make sure the display_name is set to empty now, as the metadata
623 // upgrade should set the manual override name empty.
624 Verify();
627 // Whenever an "AutoDetected" gallery is removed, it is moved to a black listed
628 // state. When the gallery is added again, the black listed state is updated
629 // back to the "AutoDetected" type.
630 TEST_F(MediaGalleriesPreferencesTest, AutoAddedBlackListing) {
631 MediaGalleryPrefId auto_id, id;
632 base::FilePath path;
633 StorageInfo info;
634 base::FilePath relative_path;
635 Verify();
637 // Add a new auto detect gallery to test with.
638 path = MakeMediaGalleriesTestingPath("new_auto");
639 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
640 info.set_name(ASCIIToUTF16("NewAutoGallery"));
641 id = AddGalleryWithNameV1(info.device_id(), info.name(),
642 relative_path, false /*auto*/);
643 EXPECT_EQ(default_galleries_count() + 1UL, id);
644 auto_id = id;
645 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
646 MediaGalleryPrefInfo::kAutoDetected);
647 Verify();
649 // Remove an auto added gallery (i.e. make it blacklisted).
650 gallery_prefs()->ForgetGalleryById(auto_id);
651 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
652 expected_galleries_for_all.erase(auto_id);
653 Verify();
655 // Try adding the gallery again automatically and it should be a no-op.
656 id = AddGalleryWithNameV1(info.device_id(), info.name(),
657 relative_path, false /*auto*/);
658 EXPECT_EQ(auto_id, id);
659 Verify();
661 // Add the gallery again as a user action.
662 id = gallery_prefs()->AddGalleryByPath(path,
663 MediaGalleryPrefInfo::kUserAdded);
664 EXPECT_EQ(auto_id, id);
665 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
666 MediaGalleryPrefInfo::kAutoDetected);
667 Verify();
670 // Whenever a "ScanResult" gallery is removed, it is moved to a black listed
671 // state. When the gallery is added again, the black listed state is updated
672 // back to the "ScanResult" type.
673 TEST_F(MediaGalleriesPreferencesTest, ScanResultBlackListing) {
674 MediaGalleryPrefId scan_id, id;
675 base::FilePath path;
676 StorageInfo info;
677 base::FilePath relative_path;
678 Verify();
680 // Add a new scan result gallery to test with.
681 path = MakeMediaGalleriesTestingPath("new_scan");
682 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
683 info.set_name(ASCIIToUTF16("NewScanGallery"));
684 id = AddGalleryWithNameV2(info.device_id(), info.name(),
685 relative_path, MediaGalleryPrefInfo::kScanResult);
686 EXPECT_EQ(default_galleries_count() + 1UL, id);
687 scan_id = id;
688 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
689 MediaGalleryPrefInfo::kScanResult);
690 Verify();
692 // Remove a scan result gallery (i.e. make it blacklisted).
693 gallery_prefs()->ForgetGalleryById(scan_id);
694 expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
695 expected_galleries_for_all.erase(scan_id);
696 Verify();
698 // Try adding the gallery again as a scan result it should be a no-op.
699 id = AddGalleryWithNameV2(info.device_id(), info.name(),
700 relative_path, MediaGalleryPrefInfo::kScanResult);
701 EXPECT_EQ(scan_id, id);
702 Verify();
704 // Add the gallery again as a user action.
705 id = gallery_prefs()->AddGalleryByPath(path,
706 MediaGalleryPrefInfo::kUserAdded);
707 EXPECT_EQ(scan_id, id);
708 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
709 MediaGalleryPrefInfo::kUserAdded);
710 Verify();
713 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryNameV2) {
714 // Add a new auto detect gallery to test with.
715 base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
716 StorageInfo info;
717 base::FilePath relative_path;
718 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
719 info.set_name(ASCIIToUTF16("NewAutoGallery"));
720 MediaGalleryPrefId id =
721 AddGalleryWithNameV2(info.device_id(), info.name(),
722 relative_path, MediaGalleryPrefInfo::kAutoDetected);
723 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
724 MediaGalleryPrefInfo::kAutoDetected);
725 Verify();
727 // Won't override the name -- don't change any expectation.
728 info.set_name(base::string16());
729 AddGalleryWithNameV2(info.device_id(), info.name(), relative_path,
730 MediaGalleryPrefInfo::kAutoDetected);
731 Verify();
733 info.set_name(ASCIIToUTF16("NewName"));
734 id = AddGalleryWithNameV2(info.device_id(), info.name(),
735 relative_path, MediaGalleryPrefInfo::kAutoDetected);
736 // Note: will really just update the existing expectation.
737 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
738 MediaGalleryPrefInfo::kAutoDetected);
739 Verify();
742 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
743 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, scan_id,
744 to_scan_remove_id, id;
745 base::FilePath path;
746 StorageInfo info;
747 base::FilePath relative_path;
748 Verify();
750 // Add some galleries to test with.
751 path = MakeMediaGalleriesTestingPath("new_user");
752 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
753 info.set_name(ASCIIToUTF16("NewUserGallery"));
754 id = AddGalleryWithNameV1(info.device_id(), info.name(),
755 relative_path, true /*user*/);
756 EXPECT_EQ(default_galleries_count() + 1UL, id);
757 user_added_id = id;
758 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
759 MediaGalleryPrefInfo::kUserAdded);
760 Verify();
762 path = MakeMediaGalleriesTestingPath("new_auto");
763 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
764 info.set_name(ASCIIToUTF16("NewAutoGallery"));
765 id = AddGalleryWithNameV1(info.device_id(), info.name(),
766 relative_path, false /*auto*/);
767 EXPECT_EQ(default_galleries_count() + 2UL, id);
768 auto_id = id;
769 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
770 MediaGalleryPrefInfo::kAutoDetected);
771 Verify();
773 path = MakeMediaGalleriesTestingPath("to_blacklist");
774 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
775 info.set_name(ASCIIToUTF16("ToBlacklistGallery"));
776 id = AddGalleryWithNameV1(info.device_id(), info.name(),
777 relative_path, false /*auto*/);
778 EXPECT_EQ(default_galleries_count() + 3UL, id);
779 to_blacklist_id = id;
780 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
781 MediaGalleryPrefInfo::kAutoDetected);
782 Verify();
784 path = MakeMediaGalleriesTestingPath("new_scan");
785 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
786 info.set_name(ASCIIToUTF16("NewScanGallery"));
787 id = AddGalleryWithNameV2(info.device_id(), info.name(),
788 relative_path, MediaGalleryPrefInfo::kScanResult);
789 EXPECT_EQ(default_galleries_count() + 4UL, id);
790 scan_id = id;
791 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
792 MediaGalleryPrefInfo::kScanResult);
793 Verify();
795 path = MakeMediaGalleriesTestingPath("to_scan_remove");
796 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
797 info.set_name(ASCIIToUTF16("ToScanRemoveGallery"));
798 id = AddGalleryWithNameV2(info.device_id(), info.name(),
799 relative_path, MediaGalleryPrefInfo::kScanResult);
800 EXPECT_EQ(default_galleries_count() + 5UL, id);
801 to_scan_remove_id = id;
802 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
803 MediaGalleryPrefInfo::kScanResult);
804 Verify();
806 // Remove permission for all galleries from the all-permission extension.
807 gallery_prefs()->SetGalleryPermissionForExtension(
808 *all_permission_extension.get(), auto_id, false);
809 expected_galleries_for_all.erase(auto_id);
810 Verify();
812 gallery_prefs()->SetGalleryPermissionForExtension(
813 *all_permission_extension.get(), user_added_id, false);
814 expected_galleries_for_all.erase(user_added_id);
815 Verify();
817 gallery_prefs()->SetGalleryPermissionForExtension(
818 *all_permission_extension.get(), to_blacklist_id, false);
819 expected_galleries_for_all.erase(to_blacklist_id);
820 Verify();
822 gallery_prefs()->SetGalleryPermissionForExtension(
823 *all_permission_extension.get(), scan_id, false);
824 expected_galleries_for_all.erase(scan_id);
825 Verify();
827 gallery_prefs()->SetGalleryPermissionForExtension(
828 *all_permission_extension.get(), to_scan_remove_id, false);
829 expected_galleries_for_all.erase(to_scan_remove_id);
830 Verify();
832 // Add permission back for all galleries to the all-permission extension.
833 gallery_prefs()->SetGalleryPermissionForExtension(
834 *all_permission_extension.get(), auto_id, true);
835 expected_galleries_for_all.insert(auto_id);
836 Verify();
838 gallery_prefs()->SetGalleryPermissionForExtension(
839 *all_permission_extension.get(), user_added_id, true);
840 expected_galleries_for_all.insert(user_added_id);
841 Verify();
843 gallery_prefs()->SetGalleryPermissionForExtension(
844 *all_permission_extension.get(), to_blacklist_id, true);
845 expected_galleries_for_all.insert(to_blacklist_id);
846 Verify();
848 gallery_prefs()->SetGalleryPermissionForExtension(
849 *all_permission_extension.get(), scan_id, true);
850 expected_galleries_for_all.insert(scan_id);
851 Verify();
853 gallery_prefs()->SetGalleryPermissionForExtension(
854 *all_permission_extension.get(), to_scan_remove_id, true);
855 expected_galleries_for_all.insert(to_scan_remove_id);
856 Verify();
858 // Add permission for all galleries to the regular permission extension.
859 gallery_prefs()->SetGalleryPermissionForExtension(
860 *regular_permission_extension.get(), auto_id, true);
861 expected_galleries_for_regular.insert(auto_id);
862 Verify();
864 gallery_prefs()->SetGalleryPermissionForExtension(
865 *regular_permission_extension.get(), user_added_id, true);
866 expected_galleries_for_regular.insert(user_added_id);
867 Verify();
869 gallery_prefs()->SetGalleryPermissionForExtension(
870 *regular_permission_extension.get(), to_blacklist_id, true);
871 expected_galleries_for_regular.insert(to_blacklist_id);
872 Verify();
874 gallery_prefs()->SetGalleryPermissionForExtension(
875 *regular_permission_extension.get(), scan_id, true);
876 expected_galleries_for_regular.insert(scan_id);
877 Verify();
879 gallery_prefs()->SetGalleryPermissionForExtension(
880 *regular_permission_extension.get(), to_scan_remove_id, true);
881 expected_galleries_for_regular.insert(to_scan_remove_id);
882 Verify();
884 // Blacklist the to be black listed gallery
885 gallery_prefs()->ForgetGalleryById(to_blacklist_id);
886 expected_galleries_[to_blacklist_id].type =
887 MediaGalleryPrefInfo::kBlackListed;
888 expected_galleries_for_all.erase(to_blacklist_id);
889 expected_galleries_for_regular.erase(to_blacklist_id);
890 Verify();
892 gallery_prefs()->ForgetGalleryById(to_scan_remove_id);
893 expected_galleries_[to_scan_remove_id].type =
894 MediaGalleryPrefInfo::kRemovedScan;
895 expected_galleries_for_all.erase(to_scan_remove_id);
896 expected_galleries_for_regular.erase(to_scan_remove_id);
897 Verify();
899 // Remove permission for all galleries to the regular permission extension.
900 gallery_prefs()->SetGalleryPermissionForExtension(
901 *regular_permission_extension.get(), auto_id, false);
902 expected_galleries_for_regular.erase(auto_id);
903 Verify();
905 gallery_prefs()->SetGalleryPermissionForExtension(
906 *regular_permission_extension.get(), user_added_id, false);
907 expected_galleries_for_regular.erase(user_added_id);
908 Verify();
910 gallery_prefs()->SetGalleryPermissionForExtension(
911 *regular_permission_extension.get(), scan_id, false);
912 expected_galleries_for_regular.erase(scan_id);
913 Verify();
915 // Add permission for an invalid gallery id.
916 gallery_prefs()->SetGalleryPermissionForExtension(
917 *regular_permission_extension.get(), 9999L, true);
918 Verify();
921 // What an existing gallery is added again, update the gallery information if
922 // needed.
923 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryDetails) {
924 MediaGalleryPrefId auto_id, id;
925 base::FilePath path;
926 StorageInfo info;
927 base::FilePath relative_path;
928 Verify();
930 // Add a new auto detect gallery to test with.
931 path = MakeMediaGalleriesTestingPath("new_auto");
932 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
933 info.set_name(ASCIIToUTF16("NewAutoGallery"));
934 id = AddGalleryWithNameV1(info.device_id(), info.name(),
935 relative_path, false /*auto*/);
936 EXPECT_EQ(default_galleries_count() + 1UL, id);
937 auto_id = id;
938 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
939 MediaGalleryPrefInfo::kAutoDetected);
940 Verify();
942 // Update the device name and add the gallery again.
943 info.set_name(ASCIIToUTF16("AutoGallery2"));
944 id = AddGalleryWithNameV1(info.device_id(), info.name(),
945 relative_path, false /*auto*/);
946 EXPECT_EQ(auto_id, id);
947 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
948 MediaGalleryPrefInfo::kAutoDetected);
949 Verify();
952 TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) {
953 base::FilePath path;
954 StorageInfo info;
955 base::FilePath relative_path;
956 Verify();
958 // Add a regular gallery
959 path = MakeMediaGalleriesTestingPath("new_user");
960 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
961 info.set_name(ASCIIToUTF16("NewUserGallery"));
962 MediaGalleryPrefId user_added_id =
963 AddGalleryWithNameV1(info.device_id(), info.name(),
964 relative_path, true /*user*/);
965 EXPECT_EQ(default_galleries_count() + 1UL, user_added_id);
966 AddGalleryExpectation(user_added_id, info.name(), info.device_id(),
967 relative_path, MediaGalleryPrefInfo::kUserAdded);
968 Verify();
970 // Find it by device id and fail to find something related.
971 MediaGalleryPrefIdSet pref_id_set;
972 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
973 EXPECT_EQ(1U, pref_id_set.size());
974 EXPECT_TRUE(pref_id_set.find(user_added_id) != pref_id_set.end());
976 MediaStorageUtil::GetDeviceInfoFromPath(
977 MakeMediaGalleriesTestingPath("new_user/foo"), &info, &relative_path);
978 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
979 EXPECT_EQ(0U, pref_id_set.size());
981 // Add some galleries on the same device.
982 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device1"));
983 info.set_name(ASCIIToUTF16("Device1Path1"));
984 std::string device_id = "path:device1";
985 MediaGalleryPrefId dev1_path1_id = AddGalleryWithNameV1(
986 device_id, info.name(), relative_path, true /*user*/);
987 EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id);
988 AddGalleryExpectation(dev1_path1_id, info.name(), device_id, relative_path,
989 MediaGalleryPrefInfo::kUserAdded);
990 Verify();
992 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device1"));
993 info.set_name(ASCIIToUTF16("Device1Path2"));
994 MediaGalleryPrefId dev1_path2_id = AddGalleryWithNameV1(
995 device_id, info.name(), relative_path, true /*user*/);
996 EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id);
997 AddGalleryExpectation(dev1_path2_id, info.name(), device_id, relative_path,
998 MediaGalleryPrefInfo::kUserAdded);
999 Verify();
1001 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device2"));
1002 info.set_name(ASCIIToUTF16("Device2Path1"));
1003 device_id = "path:device2";
1004 MediaGalleryPrefId dev2_path1_id = AddGalleryWithNameV1(
1005 device_id, info.name(), relative_path, true /*user*/);
1006 EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id);
1007 AddGalleryExpectation(dev2_path1_id, info.name(), device_id, relative_path,
1008 MediaGalleryPrefInfo::kUserAdded);
1009 Verify();
1011 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device2"));
1012 info.set_name(ASCIIToUTF16("Device2Path2"));
1013 MediaGalleryPrefId dev2_path2_id = AddGalleryWithNameV1(
1014 device_id, info.name(), relative_path, true /*user*/);
1015 EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id);
1016 AddGalleryExpectation(dev2_path2_id, info.name(), device_id, relative_path,
1017 MediaGalleryPrefInfo::kUserAdded);
1018 Verify();
1020 // Check that adding one of them again works as expected.
1021 MediaGalleryPrefId id = AddGalleryWithNameV1(
1022 device_id, info.name(), relative_path, true /*user*/);
1023 EXPECT_EQ(dev2_path2_id, id);
1024 Verify();
1027 TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) {
1028 // Start with one observer.
1029 MockGalleryChangeObserver observer1(gallery_prefs());
1030 gallery_prefs()->AddGalleryChangeObserver(&observer1);
1032 // Add a new auto detected gallery.
1033 base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
1034 StorageInfo info;
1035 base::FilePath relative_path;
1036 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1037 info.set_name(ASCIIToUTF16("NewAutoGallery"));
1038 MediaGalleryPrefId auto_id = AddGalleryWithNameV1(
1039 info.device_id(), info.name(), relative_path, false /*auto*/);
1040 EXPECT_EQ(default_galleries_count() + 1UL, auto_id);
1041 AddGalleryExpectation(auto_id, info.name(), info.device_id(),
1042 relative_path, MediaGalleryPrefInfo::kAutoDetected);
1043 EXPECT_EQ(1, observer1.notifications());
1045 // Add a second observer.
1046 MockGalleryChangeObserver observer2(gallery_prefs());
1047 gallery_prefs()->AddGalleryChangeObserver(&observer2);
1049 // Add a new user added gallery.
1050 path = MakeMediaGalleriesTestingPath("new_user");
1051 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1052 info.set_name(ASCIIToUTF16("NewUserGallery"));
1053 MediaGalleryPrefId user_added_id =
1054 AddGalleryWithNameV1(info.device_id(), info.name(),
1055 relative_path, true /*user*/);
1056 AddGalleryExpectation(user_added_id, info.name(), info.device_id(),
1057 relative_path, MediaGalleryPrefInfo::kUserAdded);
1058 EXPECT_EQ(default_galleries_count() + 2UL, user_added_id);
1059 EXPECT_EQ(2, observer1.notifications());
1060 EXPECT_EQ(1, observer2.notifications());
1062 // Remove the first observer.
1063 gallery_prefs()->RemoveGalleryChangeObserver(&observer1);
1065 // Remove an auto added gallery (i.e. make it blacklisted).
1066 gallery_prefs()->ForgetGalleryById(auto_id);
1067 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
1068 expected_galleries_for_all.erase(auto_id);
1070 EXPECT_EQ(2, observer1.notifications());
1071 EXPECT_EQ(2, observer2.notifications());
1073 // Remove a user added gallery and it should go away.
1074 gallery_prefs()->ForgetGalleryById(user_added_id);
1075 expected_galleries_.erase(user_added_id);
1076 expected_device_map[info.device_id()].erase(user_added_id);
1078 EXPECT_EQ(2, observer1.notifications());
1079 EXPECT_EQ(3, observer2.notifications());
1082 TEST_F(MediaGalleriesPreferencesTest, UpdateSingletonDeviceIdType) {
1083 MediaGalleryPrefId id;
1084 base::FilePath path;
1085 StorageInfo info;
1086 base::FilePath relative_path;
1087 Verify();
1089 // Add a new auto detect gallery to test with.
1090 path = MakeMediaGalleriesTestingPath("new_auto");
1091 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1092 info.set_name(ASCIIToUTF16("NewAutoGallery"));
1093 info.set_device_id(StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
1094 path.AsUTF8Unsafe()));
1095 id = AddGalleryWithNameV2(info.device_id(), info.name(), relative_path,
1096 MediaGalleryPrefInfo::kAutoDetected);
1097 EXPECT_EQ(default_galleries_count() + 1UL, id);
1098 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
1099 MediaGalleryPrefInfo::kAutoDetected);
1100 Verify();
1102 // Update the device id.
1103 MockGalleryChangeObserver observer(gallery_prefs());
1104 gallery_prefs()->AddGalleryChangeObserver(&observer);
1106 path = MakeMediaGalleriesTestingPath("updated_path");
1107 std::string updated_device_id =
1108 StorageInfo::MakeDeviceId(StorageInfo::ITUNES, path.AsUTF8Unsafe());
1109 EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id));
1110 AddGalleryExpectation(id, info.name(), updated_device_id, relative_path,
1111 MediaGalleryPrefInfo::kAutoDetected);
1112 expected_device_map[info.device_id()].erase(id);
1113 expected_device_map[updated_device_id].insert(id);
1114 Verify();
1115 EXPECT_EQ(1, observer.notifications());
1117 // No gallery for type.
1118 std::string new_device_id =
1119 StorageInfo::MakeDeviceId(StorageInfo::PICASA, path.AsUTF8Unsafe());
1120 EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id));
1123 TEST_F(MediaGalleriesPreferencesTest, ScanResults) {
1124 MediaGalleryPrefId id;
1125 base::FilePath path;
1126 StorageInfo info;
1127 base::FilePath relative_path;
1128 base::Time now = base::Time::Now();
1129 Verify();
1131 // Add a new scan result gallery to test with.
1132 path = MakeMediaGalleriesTestingPath("new_scan");
1133 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1134 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1135 MediaGalleryPrefInfo::kScanResult,
1136 ASCIIToUTF16("volume label"),
1137 ASCIIToUTF16("vendor name"),
1138 ASCIIToUTF16("model name"),
1139 1000000ULL, now, 1, 2, 3);
1140 EXPECT_EQ(default_galleries_count() + 1UL, id);
1141 AddScanResultExpectation(id, base::string16(), info.device_id(),
1142 relative_path, 1, 2, 3);
1143 Verify();
1145 // Update the found media count.
1146 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1147 MediaGalleryPrefInfo::kScanResult,
1148 ASCIIToUTF16("volume label"),
1149 ASCIIToUTF16("vendor name"),
1150 ASCIIToUTF16("model name"),
1151 1000000ULL, now, 4, 5, 6);
1152 EXPECT_EQ(default_galleries_count() + 1UL, id);
1153 AddScanResultExpectation(id, base::string16(), info.device_id(),
1154 relative_path, 4, 5, 6);
1155 Verify();
1157 // Remove a scan result (i.e. make it blacklisted).
1158 gallery_prefs()->ForgetGalleryById(id);
1159 expected_galleries_[id].type = MediaGalleryPrefInfo::kRemovedScan;
1160 expected_galleries_[id].audio_count = 0;
1161 expected_galleries_[id].image_count = 0;
1162 expected_galleries_[id].video_count = 0;
1163 Verify();
1165 // Try adding the gallery again as a scan result it should be a no-op.
1166 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1167 MediaGalleryPrefInfo::kScanResult,
1168 ASCIIToUTF16("volume label"),
1169 ASCIIToUTF16("vendor name"),
1170 ASCIIToUTF16("model name"),
1171 1000000ULL, now, 7, 8, 9);
1172 EXPECT_EQ(default_galleries_count() + 1UL, id);
1173 Verify();
1175 // Add the gallery again as a user action.
1176 id = gallery_prefs()->AddGalleryByPath(path,
1177 MediaGalleryPrefInfo::kUserAdded);
1178 EXPECT_EQ(default_galleries_count() + 1UL, id);
1179 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
1180 MediaGalleryPrefInfo::kUserAdded);
1181 Verify();
1184 TEST(MediaGalleriesPrefInfoTest, NameGeneration) {
1185 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
1187 MediaGalleryPrefInfo info;
1188 info.pref_id = 1;
1189 info.display_name = ASCIIToUTF16("override");
1190 info.device_id = StorageInfo::MakeDeviceId(
1191 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, "unique");
1193 EXPECT_EQ(ASCIIToUTF16("override"), info.GetGalleryDisplayName());
1195 info.display_name = ASCIIToUTF16("o2");
1196 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1198 EXPECT_EQ(l10n_util::GetStringUTF16(
1199 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1200 info.GetGalleryAdditionalDetails());
1202 info.last_attach_time = base::Time::Now();
1203 EXPECT_NE(l10n_util::GetStringUTF16(
1204 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1205 info.GetGalleryAdditionalDetails());
1206 EXPECT_NE(l10n_util::GetStringUTF16(
1207 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED),
1208 info.GetGalleryAdditionalDetails());
1210 info.volume_label = ASCIIToUTF16("vol");
1211 info.vendor_name = ASCIIToUTF16("vendor");
1212 info.model_name = ASCIIToUTF16("model");
1213 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1215 info.display_name = base::string16();
1216 EXPECT_EQ(ASCIIToUTF16("vol"), info.GetGalleryDisplayName());
1217 info.volume_label = base::string16();
1218 EXPECT_EQ(ASCIIToUTF16("vendor, model"), info.GetGalleryDisplayName());
1220 info.device_id = StorageInfo::MakeDeviceId(
1221 StorageInfo::FIXED_MASS_STORAGE, "unique");
1222 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(),
1223 base::UTF16ToUTF8(info.GetGalleryTooltip()));
1225 TestStorageMonitor::RemoveSingleton();