EME test page application.
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_preferences_unittest.cc
blob27b04e10d281ef1af91cb40b13a5742016910b4c
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/test/base/testing_profile.h"
19 #include "components/storage_monitor/media_storage_util.h"
20 #include "components/storage_monitor/storage_monitor.h"
21 #include "components/storage_monitor/test_storage_monitor.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/common/extension.h"
25 #include "extensions/common/manifest_handlers/background_info.h"
26 #include "extensions/common/permissions/media_galleries_permission.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/users/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;
39 using storage_monitor::MediaStorageUtil;
40 using storage_monitor::StorageInfo;
41 using storage_monitor::TestStorageMonitor;
43 namespace {
45 class MockGalleryChangeObserver
46 : public MediaGalleriesPreferences::GalleryChangeObserver {
47 public:
48 explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref)
49 : pref_(pref),
50 notifications_(0) {}
51 virtual ~MockGalleryChangeObserver() {}
53 int notifications() const { return notifications_;}
55 private:
56 // MediaGalleriesPreferences::GalleryChangeObserver implementation.
57 virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
58 const std::string& extension_id,
59 MediaGalleryPrefId pref_id) OVERRIDE {
60 EXPECT_EQ(pref_, pref);
61 ++notifications_;
64 virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
65 const std::string& extension_id,
66 MediaGalleryPrefId pref_id) OVERRIDE {
67 EXPECT_EQ(pref_, pref);
68 ++notifications_;
71 virtual void OnGalleryAdded(MediaGalleriesPreferences* pref,
72 MediaGalleryPrefId pref_id) OVERRIDE {
73 EXPECT_EQ(pref_, pref);
74 ++notifications_;
77 virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
78 MediaGalleryPrefId pref_id) OVERRIDE {
79 EXPECT_EQ(pref_, pref);
80 ++notifications_;
83 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
84 MediaGalleryPrefId pref_id) OVERRIDE {
85 EXPECT_EQ(pref_, pref);
86 ++notifications_;
89 MediaGalleriesPreferences* pref_;
90 int notifications_;
92 DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver);
95 } // namespace
97 class MediaGalleriesPreferencesTest : public testing::Test {
98 public:
99 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
100 DeviceIdPrefIdsMap;
102 MediaGalleriesPreferencesTest()
103 : profile_(new TestingProfile()),
104 default_galleries_count_(0) {
107 virtual ~MediaGalleriesPreferencesTest() {
110 virtual void SetUp() OVERRIDE {
111 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
113 extensions::TestExtensionSystem* extension_system(
114 static_cast<extensions::TestExtensionSystem*>(
115 extensions::ExtensionSystem::Get(profile_.get())));
116 extension_system->CreateExtensionService(
117 CommandLine::ForCurrentProcess(), base::FilePath(), false);
119 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
120 base::RunLoop loop;
121 gallery_prefs_->EnsureInitialized(loop.QuitClosure());
122 loop.Run();
124 // Load the default galleries into the expectations.
125 const MediaGalleriesPrefInfoMap& known_galleries =
126 gallery_prefs_->known_galleries();
127 if (known_galleries.size()) {
128 ASSERT_EQ(3U, known_galleries.size());
129 default_galleries_count_ = 3;
130 MediaGalleriesPrefInfoMap::const_iterator it;
131 for (it = known_galleries.begin(); it != known_galleries.end(); ++it) {
132 expected_galleries_[it->first] = it->second;
133 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
134 expected_galleries_for_all.insert(it->first);
138 std::vector<std::string> all_permissions;
139 all_permissions.push_back(
140 extensions::MediaGalleriesPermission::kReadPermission);
141 all_permissions.push_back(
142 extensions::MediaGalleriesPermission::kAllAutoDetectedPermission);
143 std::vector<std::string> read_permissions;
144 read_permissions.push_back(
145 extensions::MediaGalleriesPermission::kReadPermission);
147 all_permission_extension =
148 AddMediaGalleriesApp("all", all_permissions, profile_.get());
149 regular_permission_extension =
150 AddMediaGalleriesApp("regular", read_permissions, profile_.get());
151 no_permissions_extension =
152 AddMediaGalleriesApp("no", read_permissions, profile_.get());
155 virtual void TearDown() OVERRIDE {
156 Verify();
157 TestStorageMonitor::Destroy();
160 void Verify() {
161 const MediaGalleriesPrefInfoMap& known_galleries =
162 gallery_prefs_->known_galleries();
163 EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
164 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
165 it != known_galleries.end();
166 ++it) {
167 VerifyGalleryInfo(it->second, it->first);
168 if (it->second.type != MediaGalleryPrefInfo::kAutoDetected &&
169 it->second.type != MediaGalleryPrefInfo::kBlackListed) {
170 if (!ContainsKey(expected_galleries_for_all, it->first) &&
171 !ContainsKey(expected_galleries_for_regular, it->first)) {
172 EXPECT_FALSE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
173 } else {
174 EXPECT_TRUE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
179 for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin();
180 it != expected_device_map.end();
181 ++it) {
182 MediaGalleryPrefIdSet actual_id_set =
183 gallery_prefs_->LookUpGalleriesByDeviceId(it->first);
184 EXPECT_EQ(it->second, actual_id_set);
187 std::set<MediaGalleryPrefId> galleries_for_all =
188 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get());
189 EXPECT_EQ(expected_galleries_for_all, galleries_for_all);
191 std::set<MediaGalleryPrefId> galleries_for_regular =
192 gallery_prefs_->GalleriesForExtension(
193 *regular_permission_extension.get());
194 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular);
196 std::set<MediaGalleryPrefId> galleries_for_no =
197 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get());
198 EXPECT_EQ(0U, galleries_for_no.size());
201 void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual,
202 MediaGalleryPrefId expected_id) const {
203 MediaGalleriesPrefInfoMap::const_iterator in_expectation =
204 expected_galleries_.find(expected_id);
205 ASSERT_FALSE(in_expectation == expected_galleries_.end()) << expected_id;
206 EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id);
207 EXPECT_EQ(in_expectation->second.display_name, actual.display_name);
208 EXPECT_EQ(in_expectation->second.device_id, actual.device_id);
209 EXPECT_EQ(in_expectation->second.path.value(), actual.path.value());
210 EXPECT_EQ(in_expectation->second.type, actual.type);
211 EXPECT_EQ(in_expectation->second.audio_count, actual.audio_count);
212 EXPECT_EQ(in_expectation->second.image_count, actual.image_count);
213 EXPECT_EQ(in_expectation->second.video_count, actual.video_count);
216 MediaGalleriesPreferences* gallery_prefs() {
217 return gallery_prefs_.get();
220 uint64 default_galleries_count() {
221 return default_galleries_count_;
224 void AddGalleryExpectation(MediaGalleryPrefId id, base::string16 display_name,
225 std::string device_id,
226 base::FilePath relative_path,
227 MediaGalleryPrefInfo::Type type) {
228 expected_galleries_[id].pref_id = id;
229 expected_galleries_[id].display_name = display_name;
230 expected_galleries_[id].device_id = device_id;
231 expected_galleries_[id].path = relative_path.NormalizePathSeparators();
232 expected_galleries_[id].type = type;
234 if (type == MediaGalleryPrefInfo::kAutoDetected)
235 expected_galleries_for_all.insert(id);
237 expected_device_map[device_id].insert(id);
240 void AddScanResultExpectation(MediaGalleryPrefId id,
241 base::string16 display_name,
242 std::string device_id,
243 base::FilePath relative_path,
244 int audio_count,
245 int image_count,
246 int video_count) {
247 AddGalleryExpectation(id, display_name, device_id, relative_path,
248 MediaGalleryPrefInfo::kScanResult);
249 expected_galleries_[id].audio_count = audio_count;
250 expected_galleries_[id].image_count = image_count;
251 expected_galleries_[id].video_count = video_count;
254 MediaGalleryPrefId AddGalleryWithNameV0(const std::string& device_id,
255 const base::string16& display_name,
256 const base::FilePath& relative_path,
257 bool user_added) {
258 MediaGalleryPrefInfo::Type type =
259 user_added ? MediaGalleryPrefInfo::kUserAdded
260 : MediaGalleryPrefInfo::kAutoDetected;
261 return gallery_prefs()->AddGalleryInternal(
262 device_id, display_name, relative_path, type,
263 base::string16(), base::string16(), base::string16(), 0, base::Time(),
264 false, 0, 0, 0, 0);
267 MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id,
268 const base::string16& display_name,
269 const base::FilePath& relative_path,
270 bool user_added) {
271 MediaGalleryPrefInfo::Type type =
272 user_added ? MediaGalleryPrefInfo::kUserAdded
273 : MediaGalleryPrefInfo::kAutoDetected;
274 return gallery_prefs()->AddGalleryInternal(
275 device_id, display_name, relative_path, type,
276 base::string16(), base::string16(), base::string16(), 0, base::Time(),
277 false, 0, 0, 0, 1);
280 MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id,
281 const base::string16& display_name,
282 const base::FilePath& relative_path,
283 MediaGalleryPrefInfo::Type type) {
284 return gallery_prefs()->AddGalleryInternal(
285 device_id, display_name, relative_path, type,
286 base::string16(), base::string16(), base::string16(), 0, base::Time(),
287 false, 0, 0, 0, 2);
290 MediaGalleryPrefId AddFixedGalleryWithExepectation(
291 const std::string& path_name, const std::string& name,
292 MediaGalleryPrefInfo::Type type) {
293 base::FilePath path = MakeMediaGalleriesTestingPath(path_name);
294 StorageInfo info;
295 base::FilePath relative_path;
296 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
297 base::string16 gallery_name = base::ASCIIToUTF16(name);
298 MediaGalleryPrefId id = AddGalleryWithNameV2(info.device_id(), gallery_name,
299 relative_path, type);
300 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
301 type);
302 Verify();
303 return id;
306 bool UpdateDeviceIDForSingletonType(const std::string& device_id) {
307 return gallery_prefs()->UpdateDeviceIDForSingletonType(device_id);
310 scoped_refptr<extensions::Extension> all_permission_extension;
311 scoped_refptr<extensions::Extension> regular_permission_extension;
312 scoped_refptr<extensions::Extension> no_permissions_extension;
314 std::set<MediaGalleryPrefId> expected_galleries_for_all;
315 std::set<MediaGalleryPrefId> expected_galleries_for_regular;
317 DeviceIdPrefIdsMap expected_device_map;
319 MediaGalleriesPrefInfoMap expected_galleries_;
321 private:
322 // Needed for extension service & friends to work.
323 content::TestBrowserThreadBundle thread_bundle_;
325 EnsureMediaDirectoriesExists mock_gallery_locations_;
327 #if defined(OS_CHROMEOS)
328 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
329 chromeos::ScopedTestCrosSettings test_cros_settings_;
330 chromeos::ScopedTestUserManager test_user_manager_;
331 #endif
333 TestStorageMonitor monitor_;
334 scoped_ptr<TestingProfile> profile_;
335 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
337 uint64 default_galleries_count_;
339 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest);
342 TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) {
343 MediaGalleryPrefId auto_id, user_added_id, scan_id, id;
344 base::FilePath path;
345 base::FilePath relative_path;
346 Verify();
348 // Add a new auto detected gallery.
349 path = MakeMediaGalleriesTestingPath("new_auto");
350 StorageInfo info;
351 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
352 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
353 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
354 MediaGalleryPrefInfo::kAutoDetected);
355 EXPECT_EQ(default_galleries_count() + 1UL, id);
356 auto_id = id;
357 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
358 MediaGalleryPrefInfo::kAutoDetected);
359 Verify();
361 // Add it as other types, nothing should happen.
362 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
363 MediaGalleryPrefInfo::kUserAdded);
364 EXPECT_EQ(auto_id, id);
365 Verify();
366 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
367 MediaGalleryPrefInfo::kAutoDetected);
368 EXPECT_EQ(auto_id, id);
369 Verify();
370 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
371 MediaGalleryPrefInfo::kScanResult);
372 EXPECT_EQ(auto_id, id);
374 // Add a new user added gallery.
375 path = MakeMediaGalleriesTestingPath("new_user");
376 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
377 gallery_name = base::ASCIIToUTF16("NewUserGallery");
378 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
379 MediaGalleryPrefInfo::kUserAdded);
380 EXPECT_EQ(default_galleries_count() + 2UL, id);
381 user_added_id = id;
382 const std::string user_added_device_id = info.device_id();
383 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
384 MediaGalleryPrefInfo::kUserAdded);
385 Verify();
387 // Add it as other types, nothing should happen.
388 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
389 MediaGalleryPrefInfo::kUserAdded);
390 EXPECT_EQ(user_added_id, id);
391 Verify();
392 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
393 MediaGalleryPrefInfo::kAutoDetected);
394 EXPECT_EQ(user_added_id, id);
395 Verify();
396 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
397 MediaGalleryPrefInfo::kScanResult);
398 EXPECT_EQ(user_added_id, id);
399 Verify();
401 // Add a new scan result gallery.
402 path = MakeMediaGalleriesTestingPath("new_scan");
403 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
404 gallery_name = base::ASCIIToUTF16("NewScanGallery");
405 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
406 MediaGalleryPrefInfo::kScanResult);
407 EXPECT_EQ(default_galleries_count() + 3UL, id);
408 scan_id = id;
409 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
410 MediaGalleryPrefInfo::kScanResult);
411 Verify();
413 // Add it as other types, nothing should happen.
414 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
415 MediaGalleryPrefInfo::kUserAdded);
416 EXPECT_EQ(scan_id, id);
417 Verify();
418 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
419 MediaGalleryPrefInfo::kAutoDetected);
420 EXPECT_EQ(scan_id, id);
421 Verify();
422 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
423 MediaGalleryPrefInfo::kScanResult);
424 EXPECT_EQ(scan_id, id);
425 Verify();
427 // Lookup some galleries.
428 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
429 MakeMediaGalleriesTestingPath("new_auto"), NULL));
430 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
431 MakeMediaGalleriesTestingPath("new_user"), NULL));
432 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
433 MakeMediaGalleriesTestingPath("new_scan"), NULL));
434 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(
435 MakeMediaGalleriesTestingPath("other"), NULL));
437 // Check that we always get the gallery info.
438 MediaGalleryPrefInfo gallery_info;
439 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
440 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
441 VerifyGalleryInfo(gallery_info, auto_id);
442 EXPECT_FALSE(gallery_info.volume_metadata_valid);
443 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
444 MakeMediaGalleriesTestingPath("new_user"), &gallery_info));
445 VerifyGalleryInfo(gallery_info, user_added_id);
446 EXPECT_FALSE(gallery_info.volume_metadata_valid);
447 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
448 MakeMediaGalleriesTestingPath("new_scan"), &gallery_info));
449 VerifyGalleryInfo(gallery_info, scan_id);
450 EXPECT_FALSE(gallery_info.volume_metadata_valid);
452 path = MakeMediaGalleriesTestingPath("other");
453 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info));
454 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id);
456 StorageInfo other_info;
457 MediaStorageUtil::GetDeviceInfoFromPath(path, &other_info, &relative_path);
458 EXPECT_EQ(other_info.device_id(), gallery_info.device_id);
459 EXPECT_EQ(relative_path.value(), gallery_info.path.value());
461 // Remove an auto added gallery (i.e. make it blacklisted).
462 gallery_prefs()->ForgetGalleryById(auto_id);
463 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
464 expected_galleries_for_all.erase(auto_id);
465 Verify();
467 // Remove a scan result (i.e. make it blacklisted).
468 gallery_prefs()->ForgetGalleryById(scan_id);
469 expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
470 Verify();
472 // Remove a user added gallery and it should go away.
473 gallery_prefs()->ForgetGalleryById(user_added_id);
474 expected_galleries_.erase(user_added_id);
475 expected_device_map[user_added_device_id].erase(user_added_id);
476 Verify();
479 TEST_F(MediaGalleriesPreferencesTest, ForgetAndErase) {
480 MediaGalleryPrefId user_erase =
481 AddFixedGalleryWithExepectation("user_erase", "UserErase",
482 MediaGalleryPrefInfo::kUserAdded);
483 EXPECT_EQ(default_galleries_count() + 1UL, user_erase);
484 MediaGalleryPrefId user_forget =
485 AddFixedGalleryWithExepectation("user_forget", "UserForget",
486 MediaGalleryPrefInfo::kUserAdded);
487 EXPECT_EQ(default_galleries_count() + 2UL, user_forget);
489 MediaGalleryPrefId auto_erase =
490 AddFixedGalleryWithExepectation("auto_erase", "AutoErase",
491 MediaGalleryPrefInfo::kAutoDetected);
492 EXPECT_EQ(default_galleries_count() + 3UL, auto_erase);
493 MediaGalleryPrefId auto_forget =
494 AddFixedGalleryWithExepectation("auto_forget", "AutoForget",
495 MediaGalleryPrefInfo::kAutoDetected);
496 EXPECT_EQ(default_galleries_count() + 4UL, auto_forget);
498 MediaGalleryPrefId scan_erase =
499 AddFixedGalleryWithExepectation("scan_erase", "ScanErase",
500 MediaGalleryPrefInfo::kScanResult);
501 EXPECT_EQ(default_galleries_count() + 5UL, scan_erase);
502 MediaGalleryPrefId scan_forget =
503 AddFixedGalleryWithExepectation("scan_forget", "ScanForget",
504 MediaGalleryPrefInfo::kScanResult);
505 EXPECT_EQ(default_galleries_count() + 6UL, scan_forget);
507 Verify();
508 std::string device_id;
510 gallery_prefs()->ForgetGalleryById(user_forget);
511 device_id = expected_galleries_[user_forget].device_id;
512 expected_galleries_.erase(user_forget);
513 expected_device_map[device_id].erase(user_forget);
514 Verify();
516 gallery_prefs()->ForgetGalleryById(auto_forget);
517 expected_galleries_[auto_forget].type = MediaGalleryPrefInfo::kBlackListed;
518 expected_galleries_for_all.erase(auto_forget);
519 Verify();
521 gallery_prefs()->ForgetGalleryById(scan_forget);
522 expected_galleries_[scan_forget].type = MediaGalleryPrefInfo::kRemovedScan;
523 Verify();
525 gallery_prefs()->EraseGalleryById(user_erase);
526 device_id = expected_galleries_[user_erase].device_id;
527 expected_galleries_.erase(user_erase);
528 expected_device_map[device_id].erase(user_erase);
529 Verify();
531 gallery_prefs()->EraseGalleryById(auto_erase);
532 device_id = expected_galleries_[auto_erase].device_id;
533 expected_galleries_.erase(auto_erase);
534 expected_device_map[device_id].erase(auto_erase);
535 expected_galleries_for_all.erase(auto_erase);
536 Verify();
538 gallery_prefs()->EraseGalleryById(scan_erase);
539 device_id = expected_galleries_[scan_erase].device_id;
540 expected_galleries_.erase(scan_erase);
541 expected_device_map[device_id].erase(scan_erase);
542 Verify();
544 // Also erase the previously forgetten ones to check erasing blacklisted ones.
545 gallery_prefs()->EraseGalleryById(auto_forget);
546 device_id = expected_galleries_[auto_forget].device_id;
547 expected_galleries_.erase(auto_forget);
548 expected_device_map[device_id].erase(auto_forget);
549 Verify();
551 gallery_prefs()->EraseGalleryById(scan_forget);
552 device_id = expected_galleries_[scan_forget].device_id;
553 expected_galleries_.erase(scan_forget);
554 expected_device_map[device_id].erase(scan_forget);
555 Verify();
558 TEST_F(MediaGalleriesPreferencesTest, AddGalleryWithVolumeMetadata) {
559 MediaGalleryPrefId id;
560 StorageInfo info;
561 base::FilePath path;
562 base::FilePath relative_path;
563 base::Time now = base::Time::Now();
564 Verify();
566 // Add a new auto detected gallery.
567 path = MakeMediaGalleriesTestingPath("new_auto");
568 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
569 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
570 MediaGalleryPrefInfo::kAutoDetected,
571 ASCIIToUTF16("volume label"),
572 ASCIIToUTF16("vendor name"),
573 ASCIIToUTF16("model name"),
574 1000000ULL, now, 0, 0, 0);
575 EXPECT_EQ(default_galleries_count() + 1UL, id);
576 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
577 MediaGalleryPrefInfo::kAutoDetected);
578 Verify();
580 MediaGalleryPrefInfo gallery_info;
581 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
582 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
583 EXPECT_TRUE(gallery_info.volume_metadata_valid);
584 EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info.volume_label);
585 EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info.vendor_name);
586 EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info.model_name);
587 EXPECT_EQ(1000000ULL, gallery_info.total_size_in_bytes);
588 // Note: we put the microseconds time into a double, so there'll
589 // be some possible rounding errors. If it's less than 100, we don't
590 // care.
591 EXPECT_LE(std::abs(now.ToInternalValue() -
592 gallery_info.last_attach_time.ToInternalValue()),
593 100);
596 TEST_F(MediaGalleriesPreferencesTest, ReplaceGalleryWithVolumeMetadata) {
597 MediaGalleryPrefId id, metadata_id;
598 base::FilePath path;
599 StorageInfo info;
600 base::FilePath relative_path;
601 base::Time now = base::Time::Now();
602 Verify();
604 // Add an auto detected gallery in the prefs version 0 format.
605 path = MakeMediaGalleriesTestingPath("new_auto");
606 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
607 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
608 id = AddGalleryWithNameV0(info.device_id(), gallery_name, relative_path,
609 false /*auto*/);
610 EXPECT_EQ(default_galleries_count() + 1UL, id);
611 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
612 MediaGalleryPrefInfo::kAutoDetected);
613 Verify();
615 metadata_id = gallery_prefs()->AddGallery(info.device_id(),
616 relative_path,
617 MediaGalleryPrefInfo::kAutoDetected,
618 ASCIIToUTF16("volume label"),
619 ASCIIToUTF16("vendor name"),
620 ASCIIToUTF16("model name"),
621 1000000ULL, now, 0, 0, 0);
622 EXPECT_EQ(id, metadata_id);
623 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
624 MediaGalleryPrefInfo::kAutoDetected);
626 // Make sure the display_name is set to empty now, as the metadata
627 // upgrade should set the manual override name empty.
628 Verify();
631 // Whenever an "AutoDetected" gallery is removed, it is moved to a black listed
632 // state. When the gallery is added again, the black listed state is updated
633 // back to the "AutoDetected" type.
634 TEST_F(MediaGalleriesPreferencesTest, AutoAddedBlackListing) {
635 MediaGalleryPrefId auto_id, id;
636 base::FilePath path;
637 StorageInfo info;
638 base::FilePath relative_path;
639 Verify();
641 // Add a new auto detect gallery to test with.
642 path = MakeMediaGalleriesTestingPath("new_auto");
643 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
644 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
645 id = AddGalleryWithNameV1(info.device_id(), gallery_name,
646 relative_path, false /*auto*/);
647 EXPECT_EQ(default_galleries_count() + 1UL, id);
648 auto_id = id;
649 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
650 MediaGalleryPrefInfo::kAutoDetected);
651 Verify();
653 // Remove an auto added gallery (i.e. make it blacklisted).
654 gallery_prefs()->ForgetGalleryById(auto_id);
655 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
656 expected_galleries_for_all.erase(auto_id);
657 Verify();
659 // Try adding the gallery again automatically and it should be a no-op.
660 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
661 false /*auto*/);
662 EXPECT_EQ(auto_id, id);
663 Verify();
665 // Add the gallery again as a user action.
666 id = gallery_prefs()->AddGalleryByPath(path,
667 MediaGalleryPrefInfo::kUserAdded);
668 EXPECT_EQ(auto_id, id);
669 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
670 MediaGalleryPrefInfo::kAutoDetected);
671 Verify();
674 // Whenever a "ScanResult" 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 "ScanResult" type.
677 TEST_F(MediaGalleriesPreferencesTest, ScanResultBlackListing) {
678 MediaGalleryPrefId scan_id, id;
679 base::FilePath path;
680 StorageInfo info;
681 base::FilePath relative_path;
682 Verify();
684 // Add a new scan result gallery to test with.
685 path = MakeMediaGalleriesTestingPath("new_scan");
686 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
687 base::string16 gallery_name = base::ASCIIToUTF16("NewScanGallery");
688 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
689 MediaGalleryPrefInfo::kScanResult);
690 EXPECT_EQ(default_galleries_count() + 1UL, id);
691 scan_id = id;
692 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
693 MediaGalleryPrefInfo::kScanResult);
694 Verify();
696 // Remove a scan result gallery (i.e. make it blacklisted).
697 gallery_prefs()->ForgetGalleryById(scan_id);
698 expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
699 expected_galleries_for_all.erase(scan_id);
700 Verify();
702 // Try adding the gallery again as a scan result it should be a no-op.
703 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
704 MediaGalleryPrefInfo::kScanResult);
705 EXPECT_EQ(scan_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(scan_id, id);
712 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
713 MediaGalleryPrefInfo::kUserAdded);
714 Verify();
717 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryNameV2) {
718 // Add a new auto detect gallery to test with.
719 base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
720 StorageInfo info;
721 base::FilePath relative_path;
722 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
723 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
724 MediaGalleryPrefId id =
725 AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
726 MediaGalleryPrefInfo::kAutoDetected);
727 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
728 MediaGalleryPrefInfo::kAutoDetected);
729 Verify();
731 // Won't override the name -- don't change any expectation.
732 gallery_name = base::string16();
733 AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
734 MediaGalleryPrefInfo::kAutoDetected);
735 Verify();
737 gallery_name = base::ASCIIToUTF16("NewName");
738 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
739 MediaGalleryPrefInfo::kAutoDetected);
740 // Note: will really just update the existing expectation.
741 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
742 MediaGalleryPrefInfo::kAutoDetected);
743 Verify();
746 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
747 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, scan_id,
748 to_scan_remove_id, id;
749 base::FilePath path;
750 StorageInfo info;
751 base::FilePath relative_path;
752 Verify();
754 // Add some galleries to test with.
755 path = MakeMediaGalleriesTestingPath("new_user");
756 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
757 base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
758 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
759 true /*user*/);
760 EXPECT_EQ(default_galleries_count() + 1UL, id);
761 user_added_id = id;
762 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
763 MediaGalleryPrefInfo::kUserAdded);
764 Verify();
766 path = MakeMediaGalleriesTestingPath("new_auto");
767 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
768 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
769 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
770 false /*auto*/);
771 EXPECT_EQ(default_galleries_count() + 2UL, id);
772 auto_id = id;
773 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
774 MediaGalleryPrefInfo::kAutoDetected);
775 Verify();
777 path = MakeMediaGalleriesTestingPath("to_blacklist");
778 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
779 gallery_name = base::ASCIIToUTF16("ToBlacklistGallery");
780 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
781 false /*auto*/);
782 EXPECT_EQ(default_galleries_count() + 3UL, id);
783 to_blacklist_id = id;
784 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
785 MediaGalleryPrefInfo::kAutoDetected);
786 Verify();
788 path = MakeMediaGalleriesTestingPath("new_scan");
789 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
790 gallery_name = base::ASCIIToUTF16("NewScanGallery");
791 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
792 MediaGalleryPrefInfo::kScanResult);
793 EXPECT_EQ(default_galleries_count() + 4UL, id);
794 scan_id = id;
795 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
796 MediaGalleryPrefInfo::kScanResult);
797 Verify();
799 path = MakeMediaGalleriesTestingPath("to_scan_remove");
800 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
801 gallery_name = base::ASCIIToUTF16("ToScanRemoveGallery");
802 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
803 MediaGalleryPrefInfo::kScanResult);
804 EXPECT_EQ(default_galleries_count() + 5UL, id);
805 to_scan_remove_id = id;
806 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
807 MediaGalleryPrefInfo::kScanResult);
808 Verify();
810 // Remove permission for all galleries from the all-permission extension.
811 gallery_prefs()->SetGalleryPermissionForExtension(
812 *all_permission_extension.get(), auto_id, false);
813 expected_galleries_for_all.erase(auto_id);
814 Verify();
816 gallery_prefs()->SetGalleryPermissionForExtension(
817 *all_permission_extension.get(), user_added_id, false);
818 expected_galleries_for_all.erase(user_added_id);
819 Verify();
821 gallery_prefs()->SetGalleryPermissionForExtension(
822 *all_permission_extension.get(), to_blacklist_id, false);
823 expected_galleries_for_all.erase(to_blacklist_id);
824 Verify();
826 gallery_prefs()->SetGalleryPermissionForExtension(
827 *all_permission_extension.get(), scan_id, false);
828 expected_galleries_for_all.erase(scan_id);
829 Verify();
831 gallery_prefs()->SetGalleryPermissionForExtension(
832 *all_permission_extension.get(), to_scan_remove_id, false);
833 expected_galleries_for_all.erase(to_scan_remove_id);
834 Verify();
836 // Add permission back for all galleries to the all-permission extension.
837 gallery_prefs()->SetGalleryPermissionForExtension(
838 *all_permission_extension.get(), auto_id, true);
839 expected_galleries_for_all.insert(auto_id);
840 Verify();
842 gallery_prefs()->SetGalleryPermissionForExtension(
843 *all_permission_extension.get(), user_added_id, true);
844 expected_galleries_for_all.insert(user_added_id);
845 Verify();
847 gallery_prefs()->SetGalleryPermissionForExtension(
848 *all_permission_extension.get(), to_blacklist_id, true);
849 expected_galleries_for_all.insert(to_blacklist_id);
850 Verify();
852 gallery_prefs()->SetGalleryPermissionForExtension(
853 *all_permission_extension.get(), scan_id, true);
854 expected_galleries_for_all.insert(scan_id);
855 Verify();
857 gallery_prefs()->SetGalleryPermissionForExtension(
858 *all_permission_extension.get(), to_scan_remove_id, true);
859 expected_galleries_for_all.insert(to_scan_remove_id);
860 Verify();
862 // Add permission for all galleries to the regular permission extension.
863 gallery_prefs()->SetGalleryPermissionForExtension(
864 *regular_permission_extension.get(), auto_id, true);
865 expected_galleries_for_regular.insert(auto_id);
866 Verify();
868 gallery_prefs()->SetGalleryPermissionForExtension(
869 *regular_permission_extension.get(), user_added_id, true);
870 expected_galleries_for_regular.insert(user_added_id);
871 Verify();
873 gallery_prefs()->SetGalleryPermissionForExtension(
874 *regular_permission_extension.get(), to_blacklist_id, true);
875 expected_galleries_for_regular.insert(to_blacklist_id);
876 Verify();
878 gallery_prefs()->SetGalleryPermissionForExtension(
879 *regular_permission_extension.get(), scan_id, true);
880 expected_galleries_for_regular.insert(scan_id);
881 Verify();
883 gallery_prefs()->SetGalleryPermissionForExtension(
884 *regular_permission_extension.get(), to_scan_remove_id, true);
885 expected_galleries_for_regular.insert(to_scan_remove_id);
886 Verify();
888 // Blacklist the to be black listed gallery
889 gallery_prefs()->ForgetGalleryById(to_blacklist_id);
890 expected_galleries_[to_blacklist_id].type =
891 MediaGalleryPrefInfo::kBlackListed;
892 expected_galleries_for_all.erase(to_blacklist_id);
893 expected_galleries_for_regular.erase(to_blacklist_id);
894 Verify();
896 gallery_prefs()->ForgetGalleryById(to_scan_remove_id);
897 expected_galleries_[to_scan_remove_id].type =
898 MediaGalleryPrefInfo::kRemovedScan;
899 expected_galleries_for_all.erase(to_scan_remove_id);
900 expected_galleries_for_regular.erase(to_scan_remove_id);
901 Verify();
903 // Remove permission for all galleries to the regular permission extension.
904 gallery_prefs()->SetGalleryPermissionForExtension(
905 *regular_permission_extension.get(), auto_id, false);
906 expected_galleries_for_regular.erase(auto_id);
907 Verify();
909 gallery_prefs()->SetGalleryPermissionForExtension(
910 *regular_permission_extension.get(), user_added_id, false);
911 expected_galleries_for_regular.erase(user_added_id);
912 Verify();
914 gallery_prefs()->SetGalleryPermissionForExtension(
915 *regular_permission_extension.get(), scan_id, false);
916 expected_galleries_for_regular.erase(scan_id);
917 Verify();
919 // Add permission for an invalid gallery id.
920 gallery_prefs()->SetGalleryPermissionForExtension(
921 *regular_permission_extension.get(), 9999L, true);
922 Verify();
925 // What an existing gallery is added again, update the gallery information if
926 // needed.
927 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryDetails) {
928 MediaGalleryPrefId auto_id, id;
929 base::FilePath path;
930 StorageInfo info;
931 base::FilePath relative_path;
932 Verify();
934 // Add a new auto detect gallery to test with.
935 path = MakeMediaGalleriesTestingPath("new_auto");
936 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
937 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
938 id = AddGalleryWithNameV1(info.device_id(), gallery_name,
939 relative_path, false /*auto*/);
940 EXPECT_EQ(default_galleries_count() + 1UL, id);
941 auto_id = id;
942 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
943 MediaGalleryPrefInfo::kAutoDetected);
944 Verify();
946 // Update the device name and add the gallery again.
947 gallery_name = base::ASCIIToUTF16("AutoGallery2");
948 id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
949 false /*auto*/);
950 EXPECT_EQ(auto_id, id);
951 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
952 MediaGalleryPrefInfo::kAutoDetected);
953 Verify();
956 TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) {
957 base::FilePath path;
958 StorageInfo info;
959 base::FilePath relative_path;
960 Verify();
962 // Add a regular gallery
963 path = MakeMediaGalleriesTestingPath("new_user");
964 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
965 base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
966 MediaGalleryPrefId user_added_id =
967 AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
968 true /*user*/);
969 EXPECT_EQ(default_galleries_count() + 1UL, user_added_id);
970 AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
971 relative_path, MediaGalleryPrefInfo::kUserAdded);
972 Verify();
974 // Find it by device id and fail to find something related.
975 MediaGalleryPrefIdSet pref_id_set;
976 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
977 EXPECT_EQ(1U, pref_id_set.size());
978 EXPECT_TRUE(pref_id_set.find(user_added_id) != pref_id_set.end());
980 MediaStorageUtil::GetDeviceInfoFromPath(
981 MakeMediaGalleriesTestingPath("new_user/foo"), &info, &relative_path);
982 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
983 EXPECT_EQ(0U, pref_id_set.size());
985 // Add some galleries on the same device.
986 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device1"));
987 gallery_name = base::ASCIIToUTF16("Device1Path1");
988 std::string device_id = "path:device1";
989 MediaGalleryPrefId dev1_path1_id = AddGalleryWithNameV1(
990 device_id, gallery_name, relative_path, true /*user*/);
991 EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id);
992 AddGalleryExpectation(dev1_path1_id, gallery_name, device_id, relative_path,
993 MediaGalleryPrefInfo::kUserAdded);
994 Verify();
996 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device1"));
997 gallery_name = base::ASCIIToUTF16("Device1Path2");
998 MediaGalleryPrefId dev1_path2_id = AddGalleryWithNameV1(
999 device_id, gallery_name, relative_path, true /*user*/);
1000 EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id);
1001 AddGalleryExpectation(dev1_path2_id, gallery_name, device_id, relative_path,
1002 MediaGalleryPrefInfo::kUserAdded);
1003 Verify();
1005 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device2"));
1006 gallery_name = base::ASCIIToUTF16("Device2Path1");
1007 device_id = "path:device2";
1008 MediaGalleryPrefId dev2_path1_id = AddGalleryWithNameV1(
1009 device_id, gallery_name, relative_path, true /*user*/);
1010 EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id);
1011 AddGalleryExpectation(dev2_path1_id, gallery_name, device_id, relative_path,
1012 MediaGalleryPrefInfo::kUserAdded);
1013 Verify();
1015 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device2"));
1016 gallery_name = base::ASCIIToUTF16("Device2Path2");
1017 MediaGalleryPrefId dev2_path2_id = AddGalleryWithNameV1(
1018 device_id, gallery_name, relative_path, true /*user*/);
1019 EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id);
1020 AddGalleryExpectation(dev2_path2_id, gallery_name, device_id, relative_path,
1021 MediaGalleryPrefInfo::kUserAdded);
1022 Verify();
1024 // Check that adding one of them again works as expected.
1025 MediaGalleryPrefId id = AddGalleryWithNameV1(
1026 device_id, gallery_name, relative_path, true /*user*/);
1027 EXPECT_EQ(dev2_path2_id, id);
1028 Verify();
1031 TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) {
1032 // Start with one observer.
1033 MockGalleryChangeObserver observer1(gallery_prefs());
1034 gallery_prefs()->AddGalleryChangeObserver(&observer1);
1036 // Add a new auto detected gallery.
1037 base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
1038 StorageInfo info;
1039 base::FilePath relative_path;
1040 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1041 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1042 MediaGalleryPrefId auto_id = AddGalleryWithNameV1(
1043 info.device_id(), gallery_name, relative_path, false /*auto*/);
1044 EXPECT_EQ(default_galleries_count() + 1UL, auto_id);
1045 AddGalleryExpectation(auto_id, gallery_name, info.device_id(),
1046 relative_path, MediaGalleryPrefInfo::kAutoDetected);
1047 EXPECT_EQ(1, observer1.notifications());
1049 // Add a second observer.
1050 MockGalleryChangeObserver observer2(gallery_prefs());
1051 gallery_prefs()->AddGalleryChangeObserver(&observer2);
1053 // Add a new user added gallery.
1054 path = MakeMediaGalleriesTestingPath("new_user");
1055 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1056 gallery_name = base::ASCIIToUTF16("NewUserGallery");
1057 MediaGalleryPrefId user_added_id =
1058 AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
1059 true /*user*/);
1060 AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
1061 relative_path, MediaGalleryPrefInfo::kUserAdded);
1062 EXPECT_EQ(default_galleries_count() + 2UL, user_added_id);
1063 EXPECT_EQ(2, observer1.notifications());
1064 EXPECT_EQ(1, observer2.notifications());
1066 // Remove the first observer.
1067 gallery_prefs()->RemoveGalleryChangeObserver(&observer1);
1069 // Remove an auto added gallery (i.e. make it blacklisted).
1070 gallery_prefs()->ForgetGalleryById(auto_id);
1071 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
1072 expected_galleries_for_all.erase(auto_id);
1074 EXPECT_EQ(2, observer1.notifications());
1075 EXPECT_EQ(2, observer2.notifications());
1077 // Remove a user added gallery and it should go away.
1078 gallery_prefs()->ForgetGalleryById(user_added_id);
1079 expected_galleries_.erase(user_added_id);
1080 expected_device_map[info.device_id()].erase(user_added_id);
1082 EXPECT_EQ(2, observer1.notifications());
1083 EXPECT_EQ(3, observer2.notifications());
1086 TEST_F(MediaGalleriesPreferencesTest, UpdateSingletonDeviceIdType) {
1087 MediaGalleryPrefId id;
1088 base::FilePath path;
1089 StorageInfo info;
1090 base::FilePath relative_path;
1091 Verify();
1093 // Add a new auto detect gallery to test with.
1094 path = MakeMediaGalleriesTestingPath("new_auto");
1095 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1096 base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1097 info.set_device_id(StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
1098 path.AsUTF8Unsafe()));
1099 id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
1100 MediaGalleryPrefInfo::kAutoDetected);
1101 EXPECT_EQ(default_galleries_count() + 1UL, id);
1102 AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
1103 MediaGalleryPrefInfo::kAutoDetected);
1104 Verify();
1106 // Update the device id.
1107 MockGalleryChangeObserver observer(gallery_prefs());
1108 gallery_prefs()->AddGalleryChangeObserver(&observer);
1110 path = MakeMediaGalleriesTestingPath("updated_path");
1111 std::string updated_device_id =
1112 StorageInfo::MakeDeviceId(StorageInfo::ITUNES, path.AsUTF8Unsafe());
1113 EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id));
1114 AddGalleryExpectation(id, gallery_name, updated_device_id, relative_path,
1115 MediaGalleryPrefInfo::kAutoDetected);
1116 expected_device_map[info.device_id()].erase(id);
1117 expected_device_map[updated_device_id].insert(id);
1118 Verify();
1119 EXPECT_EQ(1, observer.notifications());
1121 // No gallery for type.
1122 std::string new_device_id =
1123 StorageInfo::MakeDeviceId(StorageInfo::PICASA, path.AsUTF8Unsafe());
1124 EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id));
1127 TEST_F(MediaGalleriesPreferencesTest, ScanResults) {
1128 MediaGalleryPrefId id;
1129 base::FilePath path;
1130 StorageInfo info;
1131 base::FilePath relative_path;
1132 base::Time now = base::Time::Now();
1133 Verify();
1135 // Add a new scan result gallery to test with.
1136 path = MakeMediaGalleriesTestingPath("new_scan");
1137 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1138 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1139 MediaGalleryPrefInfo::kScanResult,
1140 ASCIIToUTF16("volume label"),
1141 ASCIIToUTF16("vendor name"),
1142 ASCIIToUTF16("model name"),
1143 1000000ULL, now, 1, 2, 3);
1144 EXPECT_EQ(default_galleries_count() + 1UL, id);
1145 AddScanResultExpectation(id, base::string16(), info.device_id(),
1146 relative_path, 1, 2, 3);
1147 Verify();
1149 // Update the found media count.
1150 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1151 MediaGalleryPrefInfo::kScanResult,
1152 ASCIIToUTF16("volume label"),
1153 ASCIIToUTF16("vendor name"),
1154 ASCIIToUTF16("model name"),
1155 1000000ULL, now, 4, 5, 6);
1156 EXPECT_EQ(default_galleries_count() + 1UL, id);
1157 AddScanResultExpectation(id, base::string16(), info.device_id(),
1158 relative_path, 4, 5, 6);
1159 Verify();
1161 // Remove a scan result (i.e. make it blacklisted).
1162 gallery_prefs()->ForgetGalleryById(id);
1163 expected_galleries_[id].type = MediaGalleryPrefInfo::kRemovedScan;
1164 expected_galleries_[id].audio_count = 0;
1165 expected_galleries_[id].image_count = 0;
1166 expected_galleries_[id].video_count = 0;
1167 Verify();
1169 // Try adding the gallery again as a scan result it should be a no-op.
1170 id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1171 MediaGalleryPrefInfo::kScanResult,
1172 ASCIIToUTF16("volume label"),
1173 ASCIIToUTF16("vendor name"),
1174 ASCIIToUTF16("model name"),
1175 1000000ULL, now, 7, 8, 9);
1176 EXPECT_EQ(default_galleries_count() + 1UL, id);
1177 Verify();
1179 // Add the gallery again as a user action.
1180 id = gallery_prefs()->AddGalleryByPath(path,
1181 MediaGalleryPrefInfo::kUserAdded);
1182 EXPECT_EQ(default_galleries_count() + 1UL, id);
1183 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
1184 MediaGalleryPrefInfo::kUserAdded);
1185 Verify();
1188 TEST(MediaGalleriesPrefInfoTest, NameGeneration) {
1189 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
1191 MediaGalleryPrefInfo info;
1192 info.pref_id = 1;
1193 info.display_name = ASCIIToUTF16("override");
1194 info.device_id = StorageInfo::MakeDeviceId(
1195 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, "unique");
1197 EXPECT_EQ(ASCIIToUTF16("override"), info.GetGalleryDisplayName());
1199 info.display_name = ASCIIToUTF16("o2");
1200 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1202 EXPECT_EQ(l10n_util::GetStringUTF16(
1203 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1204 info.GetGalleryAdditionalDetails());
1206 info.last_attach_time = base::Time::Now();
1207 EXPECT_NE(l10n_util::GetStringUTF16(
1208 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1209 info.GetGalleryAdditionalDetails());
1210 EXPECT_NE(l10n_util::GetStringUTF16(
1211 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED),
1212 info.GetGalleryAdditionalDetails());
1214 info.volume_label = ASCIIToUTF16("vol");
1215 info.vendor_name = ASCIIToUTF16("vendor");
1216 info.model_name = ASCIIToUTF16("model");
1217 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1219 info.display_name = base::string16();
1220 EXPECT_EQ(ASCIIToUTF16("vol"), info.GetGalleryDisplayName());
1221 info.volume_label = base::string16();
1222 EXPECT_EQ(ASCIIToUTF16("vendor, model"), info.GetGalleryDisplayName());
1224 info.device_id = StorageInfo::MakeDeviceId(
1225 StorageInfo::FIXED_MASS_STORAGE, "unique");
1226 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(),
1227 base::UTF16ToUTF8(info.GetGalleryTooltip()));
1229 TestStorageMonitor::Destroy();