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"
12 #include "base/command_line.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16 #include "base/prefs/scoped_user_pref_update.h"
17 #include "base/run_loop.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "chrome/browser/extensions/test_extension_system.h"
21 #include "chrome/browser/media_galleries/media_file_system_registry.h"
22 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/grit/generated_resources.h"
26 #include "chrome/test/base/testing_profile.h"
27 #include "components/storage_monitor/media_storage_util.h"
28 #include "components/storage_monitor/storage_monitor.h"
29 #include "components/storage_monitor/test_storage_monitor.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "extensions/browser/extension_system.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/manifest_handlers/background_info.h"
34 #include "extensions/common/permissions/media_galleries_permission.h"
35 #include "sync/api/string_ordinal.h"
36 #include "testing/gtest/include/gtest/gtest.h"
37 #include "ui/base/l10n/l10n_util.h"
39 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
41 #include "chrome/browser/chromeos/settings/cros_settings.h"
42 #include "chrome/browser/chromeos/settings/device_settings_service.h"
45 using base::ASCIIToUTF16
;
46 using storage_monitor::MediaStorageUtil
;
47 using storage_monitor::StorageInfo
;
48 using storage_monitor::TestStorageMonitor
;
52 class MockGalleryChangeObserver
53 : public MediaGalleriesPreferences::GalleryChangeObserver
{
55 explicit MockGalleryChangeObserver(MediaGalleriesPreferences
* pref
)
58 virtual ~MockGalleryChangeObserver() {}
60 int notifications() const { return notifications_
;}
63 // MediaGalleriesPreferences::GalleryChangeObserver implementation.
64 virtual void OnPermissionAdded(MediaGalleriesPreferences
* pref
,
65 const std::string
& extension_id
,
66 MediaGalleryPrefId pref_id
) override
{
67 EXPECT_EQ(pref_
, pref
);
71 virtual void OnPermissionRemoved(MediaGalleriesPreferences
* pref
,
72 const std::string
& extension_id
,
73 MediaGalleryPrefId pref_id
) override
{
74 EXPECT_EQ(pref_
, pref
);
78 virtual void OnGalleryAdded(MediaGalleriesPreferences
* pref
,
79 MediaGalleryPrefId pref_id
) override
{
80 EXPECT_EQ(pref_
, pref
);
84 virtual void OnGalleryRemoved(MediaGalleriesPreferences
* pref
,
85 MediaGalleryPrefId pref_id
) override
{
86 EXPECT_EQ(pref_
, pref
);
90 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences
* pref
,
91 MediaGalleryPrefId pref_id
) override
{
92 EXPECT_EQ(pref_
, pref
);
96 MediaGalleriesPreferences
* pref_
;
99 DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver
);
104 class MediaGalleriesPreferencesTest
: public testing::Test
{
106 typedef std::map
<std::string
/*device id*/, MediaGalleryPrefIdSet
>
109 MediaGalleriesPreferencesTest()
110 : profile_(new TestingProfile()),
111 default_galleries_count_(0) {
114 virtual ~MediaGalleriesPreferencesTest() {
117 virtual void SetUp() override
{
118 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
120 extensions::TestExtensionSystem
* extension_system(
121 static_cast<extensions::TestExtensionSystem
*>(
122 extensions::ExtensionSystem::Get(profile_
.get())));
123 extension_system
->CreateExtensionService(
124 CommandLine::ForCurrentProcess(), base::FilePath(), false);
126 ReinitPrefsAndExpectations();
128 const MediaGalleriesPrefInfoMap
& known_galleries
=
129 gallery_prefs_
->known_galleries();
130 if (!known_galleries
.empty()) {
131 ASSERT_EQ(3U, known_galleries
.size());
134 std::vector
<std::string
> all_permissions
;
135 all_permissions
.push_back(
136 extensions::MediaGalleriesPermission::kReadPermission
);
137 all_permissions
.push_back(
138 extensions::MediaGalleriesPermission::kAllAutoDetectedPermission
);
139 std::vector
<std::string
> read_permissions
;
140 read_permissions
.push_back(
141 extensions::MediaGalleriesPermission::kReadPermission
);
143 all_permission_extension
=
144 AddMediaGalleriesApp("all", all_permissions
, profile_
.get());
145 regular_permission_extension
=
146 AddMediaGalleriesApp("regular", read_permissions
, profile_
.get());
147 no_permissions_extension
=
148 AddMediaGalleriesApp("no", read_permissions
, profile_
.get());
151 virtual void TearDown() override
{
153 TestStorageMonitor::Destroy();
156 void ChangeMediaPathOverrides() {
157 mock_gallery_locations_
.ChangeMediaPathOverrides();
160 void ReinitPrefsAndExpectations() {
161 gallery_prefs_
.reset(new MediaGalleriesPreferences(profile_
.get()));
163 gallery_prefs_
->EnsureInitialized(loop
.QuitClosure());
166 // Load the default galleries into the expectations.
167 const MediaGalleriesPrefInfoMap
& known_galleries
=
168 gallery_prefs_
->known_galleries();
169 if (!known_galleries
.empty()) {
170 default_galleries_count_
= 3;
171 MediaGalleriesPrefInfoMap::const_iterator it
;
172 for (it
= known_galleries
.begin(); it
!= known_galleries
.end(); ++it
) {
173 expected_galleries_
[it
->first
] = it
->second
;
174 if (it
->second
.type
== MediaGalleryPrefInfo::kAutoDetected
)
175 expected_galleries_for_all
.insert(it
->first
);
180 void RemovePersistedDefaultGalleryValues() {
181 PrefService
* prefs
= profile_
->GetPrefs();
182 scoped_ptr
<ListPrefUpdate
> update(new ListPrefUpdate(
183 prefs
, prefs::kMediaGalleriesRememberedGalleries
));
184 base::ListValue
* list
= update
->Get();
186 for (base::ListValue::iterator iter
= list
->begin();
189 base::DictionaryValue
* dict
;
191 if ((*iter
)->GetAsDictionary(&dict
)) {
192 // Setting the prefs version to 2 which is the version before
193 // default_gallery_type was added.
194 dict
->SetInteger(kMediaGalleriesPrefsVersionKey
, 2);
195 dict
->Remove(kMediaGalleriesDefaultGalleryTypeKey
, NULL
);
202 const MediaGalleriesPrefInfoMap
& known_galleries
=
203 gallery_prefs_
->known_galleries();
204 EXPECT_EQ(expected_galleries_
.size(), known_galleries
.size());
205 for (MediaGalleriesPrefInfoMap::const_iterator it
= known_galleries
.begin();
206 it
!= known_galleries
.end();
208 VerifyGalleryInfo(it
->second
, it
->first
);
209 if (it
->second
.type
!= MediaGalleryPrefInfo::kAutoDetected
&&
210 it
->second
.type
!= MediaGalleryPrefInfo::kBlackListed
) {
211 if (!ContainsKey(expected_galleries_for_all
, it
->first
) &&
212 !ContainsKey(expected_galleries_for_regular
, it
->first
)) {
213 EXPECT_FALSE(gallery_prefs_
->NonAutoGalleryHasPermission(it
->first
));
215 EXPECT_TRUE(gallery_prefs_
->NonAutoGalleryHasPermission(it
->first
));
220 for (DeviceIdPrefIdsMap::const_iterator it
= expected_device_map
.begin();
221 it
!= expected_device_map
.end();
223 MediaGalleryPrefIdSet actual_id_set
=
224 gallery_prefs_
->LookUpGalleriesByDeviceId(it
->first
);
225 EXPECT_EQ(it
->second
, actual_id_set
);
228 std::set
<MediaGalleryPrefId
> galleries_for_all
=
229 gallery_prefs_
->GalleriesForExtension(*all_permission_extension
.get());
230 EXPECT_EQ(expected_galleries_for_all
, galleries_for_all
);
232 std::set
<MediaGalleryPrefId
> galleries_for_regular
=
233 gallery_prefs_
->GalleriesForExtension(
234 *regular_permission_extension
.get());
235 EXPECT_EQ(expected_galleries_for_regular
, galleries_for_regular
);
237 std::set
<MediaGalleryPrefId
> galleries_for_no
=
238 gallery_prefs_
->GalleriesForExtension(*no_permissions_extension
.get());
239 EXPECT_EQ(0U, galleries_for_no
.size());
242 void VerifyGalleryInfo(const MediaGalleryPrefInfo
& actual
,
243 MediaGalleryPrefId expected_id
) const {
244 MediaGalleriesPrefInfoMap::const_iterator in_expectation
=
245 expected_galleries_
.find(expected_id
);
246 ASSERT_FALSE(in_expectation
== expected_galleries_
.end()) << expected_id
;
247 EXPECT_EQ(in_expectation
->second
.pref_id
, actual
.pref_id
);
248 EXPECT_EQ(in_expectation
->second
.display_name
, actual
.display_name
);
249 EXPECT_EQ(in_expectation
->second
.device_id
, actual
.device_id
);
250 EXPECT_EQ(in_expectation
->second
.path
.value(), actual
.path
.value());
251 EXPECT_EQ(in_expectation
->second
.type
, actual
.type
);
252 EXPECT_EQ(in_expectation
->second
.audio_count
, actual
.audio_count
);
253 EXPECT_EQ(in_expectation
->second
.image_count
, actual
.image_count
);
254 EXPECT_EQ(in_expectation
->second
.video_count
, actual
.video_count
);
256 in_expectation
->second
.default_gallery_type
,
257 actual
.default_gallery_type
);
260 MediaGalleriesPreferences
* gallery_prefs() {
261 return gallery_prefs_
.get();
264 uint64
default_galleries_count() {
265 return default_galleries_count_
;
268 void AddGalleryExpectation(MediaGalleryPrefId id
, base::string16 display_name
,
269 std::string device_id
,
270 base::FilePath relative_path
,
271 MediaGalleryPrefInfo::Type type
) {
272 expected_galleries_
[id
].pref_id
= id
;
273 expected_galleries_
[id
].display_name
= display_name
;
274 expected_galleries_
[id
].device_id
= device_id
;
275 expected_galleries_
[id
].path
= relative_path
.NormalizePathSeparators();
276 expected_galleries_
[id
].type
= type
;
278 if (type
== MediaGalleryPrefInfo::kAutoDetected
)
279 expected_galleries_for_all
.insert(id
);
281 expected_device_map
[device_id
].insert(id
);
284 void AddScanResultExpectation(MediaGalleryPrefId id
,
285 base::string16 display_name
,
286 std::string device_id
,
287 base::FilePath relative_path
,
291 AddGalleryExpectation(id
, display_name
, device_id
, relative_path
,
292 MediaGalleryPrefInfo::kScanResult
);
293 expected_galleries_
[id
].audio_count
= audio_count
;
294 expected_galleries_
[id
].image_count
= image_count
;
295 expected_galleries_
[id
].video_count
= video_count
;
298 MediaGalleryPrefId
AddGalleryWithNameV0(const std::string
& device_id
,
299 const base::string16
& display_name
,
300 const base::FilePath
& relative_path
,
302 MediaGalleryPrefInfo::Type type
=
303 user_added
? MediaGalleryPrefInfo::kUserAdded
304 : MediaGalleryPrefInfo::kAutoDetected
;
305 return gallery_prefs()->AddOrUpdateGalleryInternal(
306 device_id
, display_name
, relative_path
, type
,
307 base::string16(), base::string16(), base::string16(), 0, base::Time(),
308 false, 0, 0, 0, 0, MediaGalleryPrefInfo::kNotDefault
);
311 MediaGalleryPrefId
AddGalleryWithNameV1(const std::string
& device_id
,
312 const base::string16
& display_name
,
313 const base::FilePath
& relative_path
,
315 MediaGalleryPrefInfo::Type type
=
316 user_added
? MediaGalleryPrefInfo::kUserAdded
317 : MediaGalleryPrefInfo::kAutoDetected
;
318 return gallery_prefs()->AddOrUpdateGalleryInternal(
319 device_id
, display_name
, relative_path
, type
,
320 base::string16(), base::string16(), base::string16(), 0, base::Time(),
321 false, 0, 0, 0, 1, MediaGalleryPrefInfo::kNotDefault
);
324 MediaGalleryPrefId
AddGalleryWithNameV2(const std::string
& device_id
,
325 const base::string16
& display_name
,
326 const base::FilePath
& relative_path
,
327 MediaGalleryPrefInfo::Type type
) {
328 return gallery_prefs()->AddOrUpdateGalleryInternal(
329 device_id
, display_name
, relative_path
, type
,
330 base::string16(), base::string16(), base::string16(), 0, base::Time(),
331 false, 0, 0, 0, 2, MediaGalleryPrefInfo::kNotDefault
);
334 MediaGalleryPrefId
AddFixedGalleryWithExepectation(
335 const std::string
& path_name
, const std::string
& name
,
336 MediaGalleryPrefInfo::Type type
) {
337 base::FilePath path
= MakeMediaGalleriesTestingPath(path_name
);
339 base::FilePath relative_path
;
340 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
341 base::string16 gallery_name
= base::ASCIIToUTF16(name
);
342 MediaGalleryPrefId id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
,
343 relative_path
, type
);
344 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
350 bool UpdateDeviceIDForSingletonType(const std::string
& device_id
) {
351 return gallery_prefs()->UpdateDeviceIDForSingletonType(device_id
);
354 scoped_refptr
<extensions::Extension
> all_permission_extension
;
355 scoped_refptr
<extensions::Extension
> regular_permission_extension
;
356 scoped_refptr
<extensions::Extension
> no_permissions_extension
;
358 std::set
<MediaGalleryPrefId
> expected_galleries_for_all
;
359 std::set
<MediaGalleryPrefId
> expected_galleries_for_regular
;
361 DeviceIdPrefIdsMap expected_device_map
;
363 MediaGalleriesPrefInfoMap expected_galleries_
;
366 // Needed for extension service & friends to work.
367 content::TestBrowserThreadBundle thread_bundle_
;
369 EnsureMediaDirectoriesExists mock_gallery_locations_
;
371 #if defined(OS_CHROMEOS)
372 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
373 chromeos::ScopedTestCrosSettings test_cros_settings_
;
374 chromeos::ScopedTestUserManager test_user_manager_
;
377 TestStorageMonitor monitor_
;
378 scoped_ptr
<TestingProfile
> profile_
;
379 scoped_ptr
<MediaGalleriesPreferences
> gallery_prefs_
;
381 uint64 default_galleries_count_
;
383 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest
);
386 TEST_F(MediaGalleriesPreferencesTest
, GalleryManagement
) {
387 MediaGalleryPrefId auto_id
, user_added_id
, scan_id
, id
;
389 base::FilePath relative_path
;
392 // Add a new auto detected gallery.
393 path
= MakeMediaGalleriesTestingPath("new_auto");
395 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
396 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
397 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
398 MediaGalleryPrefInfo::kAutoDetected
);
399 EXPECT_EQ(default_galleries_count() + 1UL, id
);
401 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
402 MediaGalleryPrefInfo::kAutoDetected
);
405 // Add it as other types, nothing should happen.
406 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
407 MediaGalleryPrefInfo::kUserAdded
);
408 EXPECT_EQ(auto_id
, id
);
410 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
411 MediaGalleryPrefInfo::kAutoDetected
);
412 EXPECT_EQ(auto_id
, id
);
414 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
415 MediaGalleryPrefInfo::kScanResult
);
416 EXPECT_EQ(auto_id
, id
);
418 // Add a new user added gallery.
419 path
= MakeMediaGalleriesTestingPath("new_user");
420 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
421 gallery_name
= base::ASCIIToUTF16("NewUserGallery");
422 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
423 MediaGalleryPrefInfo::kUserAdded
);
424 EXPECT_EQ(default_galleries_count() + 2UL, id
);
426 const std::string user_added_device_id
= info
.device_id();
427 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
428 MediaGalleryPrefInfo::kUserAdded
);
431 // Add it as other types, nothing should happen.
432 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
433 MediaGalleryPrefInfo::kUserAdded
);
434 EXPECT_EQ(user_added_id
, id
);
436 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
437 MediaGalleryPrefInfo::kAutoDetected
);
438 EXPECT_EQ(user_added_id
, id
);
440 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
441 MediaGalleryPrefInfo::kScanResult
);
442 EXPECT_EQ(user_added_id
, id
);
445 // Add a new scan result gallery.
446 path
= MakeMediaGalleriesTestingPath("new_scan");
447 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
448 gallery_name
= base::ASCIIToUTF16("NewScanGallery");
449 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
450 MediaGalleryPrefInfo::kScanResult
);
451 EXPECT_EQ(default_galleries_count() + 3UL, id
);
453 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
454 MediaGalleryPrefInfo::kScanResult
);
457 // Add it as other types, nothing should happen.
458 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
459 MediaGalleryPrefInfo::kUserAdded
);
460 EXPECT_EQ(scan_id
, id
);
462 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
463 MediaGalleryPrefInfo::kAutoDetected
);
464 EXPECT_EQ(scan_id
, id
);
466 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
467 MediaGalleryPrefInfo::kScanResult
);
468 EXPECT_EQ(scan_id
, id
);
471 // Lookup some galleries.
472 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
473 MakeMediaGalleriesTestingPath("new_auto"), NULL
));
474 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
475 MakeMediaGalleriesTestingPath("new_user"), NULL
));
476 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
477 MakeMediaGalleriesTestingPath("new_scan"), NULL
));
478 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(
479 MakeMediaGalleriesTestingPath("other"), NULL
));
481 // Check that we always get the gallery info.
482 MediaGalleryPrefInfo gallery_info
;
483 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
484 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info
));
485 VerifyGalleryInfo(gallery_info
, auto_id
);
486 EXPECT_FALSE(gallery_info
.volume_metadata_valid
);
487 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
488 MakeMediaGalleriesTestingPath("new_user"), &gallery_info
));
489 VerifyGalleryInfo(gallery_info
, user_added_id
);
490 EXPECT_FALSE(gallery_info
.volume_metadata_valid
);
491 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
492 MakeMediaGalleriesTestingPath("new_scan"), &gallery_info
));
493 VerifyGalleryInfo(gallery_info
, scan_id
);
494 EXPECT_FALSE(gallery_info
.volume_metadata_valid
);
496 path
= MakeMediaGalleriesTestingPath("other");
497 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path
, &gallery_info
));
498 EXPECT_EQ(kInvalidMediaGalleryPrefId
, gallery_info
.pref_id
);
500 StorageInfo other_info
;
501 MediaStorageUtil::GetDeviceInfoFromPath(path
, &other_info
, &relative_path
);
502 EXPECT_EQ(other_info
.device_id(), gallery_info
.device_id
);
503 EXPECT_EQ(relative_path
.value(), gallery_info
.path
.value());
505 // Remove an auto added gallery (i.e. make it blacklisted).
506 gallery_prefs()->ForgetGalleryById(auto_id
);
507 expected_galleries_
[auto_id
].type
= MediaGalleryPrefInfo::kBlackListed
;
508 expected_galleries_for_all
.erase(auto_id
);
511 // Remove a scan result (i.e. make it blacklisted).
512 gallery_prefs()->ForgetGalleryById(scan_id
);
513 expected_galleries_
[scan_id
].type
= MediaGalleryPrefInfo::kRemovedScan
;
516 // Remove a user added gallery and it should go away.
517 gallery_prefs()->ForgetGalleryById(user_added_id
);
518 expected_galleries_
.erase(user_added_id
);
519 expected_device_map
[user_added_device_id
].erase(user_added_id
);
523 TEST_F(MediaGalleriesPreferencesTest
, ForgetAndErase
) {
524 MediaGalleryPrefId user_erase
=
525 AddFixedGalleryWithExepectation("user_erase", "UserErase",
526 MediaGalleryPrefInfo::kUserAdded
);
527 EXPECT_EQ(default_galleries_count() + 1UL, user_erase
);
528 MediaGalleryPrefId user_forget
=
529 AddFixedGalleryWithExepectation("user_forget", "UserForget",
530 MediaGalleryPrefInfo::kUserAdded
);
531 EXPECT_EQ(default_galleries_count() + 2UL, user_forget
);
533 MediaGalleryPrefId auto_erase
=
534 AddFixedGalleryWithExepectation("auto_erase", "AutoErase",
535 MediaGalleryPrefInfo::kAutoDetected
);
536 EXPECT_EQ(default_galleries_count() + 3UL, auto_erase
);
537 MediaGalleryPrefId auto_forget
=
538 AddFixedGalleryWithExepectation("auto_forget", "AutoForget",
539 MediaGalleryPrefInfo::kAutoDetected
);
540 EXPECT_EQ(default_galleries_count() + 4UL, auto_forget
);
542 MediaGalleryPrefId scan_erase
=
543 AddFixedGalleryWithExepectation("scan_erase", "ScanErase",
544 MediaGalleryPrefInfo::kScanResult
);
545 EXPECT_EQ(default_galleries_count() + 5UL, scan_erase
);
546 MediaGalleryPrefId scan_forget
=
547 AddFixedGalleryWithExepectation("scan_forget", "ScanForget",
548 MediaGalleryPrefInfo::kScanResult
);
549 EXPECT_EQ(default_galleries_count() + 6UL, scan_forget
);
552 std::string device_id
;
554 gallery_prefs()->ForgetGalleryById(user_forget
);
555 device_id
= expected_galleries_
[user_forget
].device_id
;
556 expected_galleries_
.erase(user_forget
);
557 expected_device_map
[device_id
].erase(user_forget
);
560 gallery_prefs()->ForgetGalleryById(auto_forget
);
561 expected_galleries_
[auto_forget
].type
= MediaGalleryPrefInfo::kBlackListed
;
562 expected_galleries_for_all
.erase(auto_forget
);
565 gallery_prefs()->ForgetGalleryById(scan_forget
);
566 expected_galleries_
[scan_forget
].type
= MediaGalleryPrefInfo::kRemovedScan
;
569 gallery_prefs()->EraseGalleryById(user_erase
);
570 device_id
= expected_galleries_
[user_erase
].device_id
;
571 expected_galleries_
.erase(user_erase
);
572 expected_device_map
[device_id
].erase(user_erase
);
575 gallery_prefs()->EraseGalleryById(auto_erase
);
576 device_id
= expected_galleries_
[auto_erase
].device_id
;
577 expected_galleries_
.erase(auto_erase
);
578 expected_device_map
[device_id
].erase(auto_erase
);
579 expected_galleries_for_all
.erase(auto_erase
);
582 gallery_prefs()->EraseGalleryById(scan_erase
);
583 device_id
= expected_galleries_
[scan_erase
].device_id
;
584 expected_galleries_
.erase(scan_erase
);
585 expected_device_map
[device_id
].erase(scan_erase
);
588 // Also erase the previously forgetten ones to check erasing blacklisted ones.
589 gallery_prefs()->EraseGalleryById(auto_forget
);
590 device_id
= expected_galleries_
[auto_forget
].device_id
;
591 expected_galleries_
.erase(auto_forget
);
592 expected_device_map
[device_id
].erase(auto_forget
);
595 gallery_prefs()->EraseGalleryById(scan_forget
);
596 device_id
= expected_galleries_
[scan_forget
].device_id
;
597 expected_galleries_
.erase(scan_forget
);
598 expected_device_map
[device_id
].erase(scan_forget
);
602 TEST_F(MediaGalleriesPreferencesTest
, AddGalleryWithVolumeMetadata
) {
603 MediaGalleryPrefId id
;
606 base::FilePath relative_path
;
607 base::Time now
= base::Time::Now();
610 // Add a new auto detected gallery.
611 path
= MakeMediaGalleriesTestingPath("new_auto");
612 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
613 id
= gallery_prefs()->AddGallery(info
.device_id(), relative_path
,
614 MediaGalleryPrefInfo::kAutoDetected
,
615 ASCIIToUTF16("volume label"),
616 ASCIIToUTF16("vendor name"),
617 ASCIIToUTF16("model name"),
618 1000000ULL, now
, 0, 0, 0);
619 EXPECT_EQ(default_galleries_count() + 1UL, id
);
620 AddGalleryExpectation(id
, base::string16(), info
.device_id(), relative_path
,
621 MediaGalleryPrefInfo::kAutoDetected
);
624 MediaGalleryPrefInfo gallery_info
;
625 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
626 MakeMediaGalleriesTestingPath("new_auto"), &gallery_info
));
627 EXPECT_TRUE(gallery_info
.volume_metadata_valid
);
628 EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info
.volume_label
);
629 EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info
.vendor_name
);
630 EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info
.model_name
);
631 EXPECT_EQ(1000000ULL, gallery_info
.total_size_in_bytes
);
632 // Note: we put the microseconds time into a double, so there'll
633 // be some possible rounding errors. If it's less than 100, we don't
635 EXPECT_LE(std::abs(now
.ToInternalValue() -
636 gallery_info
.last_attach_time
.ToInternalValue()),
640 TEST_F(MediaGalleriesPreferencesTest
, ReplaceGalleryWithVolumeMetadata
) {
641 MediaGalleryPrefId id
, metadata_id
;
644 base::FilePath relative_path
;
645 base::Time now
= base::Time::Now();
648 // Add an auto detected gallery in the prefs version 0 format.
649 path
= MakeMediaGalleriesTestingPath("new_auto");
650 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
651 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
652 id
= AddGalleryWithNameV0(info
.device_id(), gallery_name
, relative_path
,
654 EXPECT_EQ(default_galleries_count() + 1UL, id
);
655 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
656 MediaGalleryPrefInfo::kAutoDetected
);
659 metadata_id
= gallery_prefs()->AddGallery(info
.device_id(),
661 MediaGalleryPrefInfo::kAutoDetected
,
662 ASCIIToUTF16("volume label"),
663 ASCIIToUTF16("vendor name"),
664 ASCIIToUTF16("model name"),
665 1000000ULL, now
, 0, 0, 0);
666 EXPECT_EQ(id
, metadata_id
);
667 AddGalleryExpectation(id
, base::string16(), info
.device_id(), relative_path
,
668 MediaGalleryPrefInfo::kAutoDetected
);
670 // Make sure the display_name is set to empty now, as the metadata
671 // upgrade should set the manual override name empty.
675 // Whenever an "AutoDetected" gallery is removed, it is moved to a black listed
676 // state. When the gallery is added again, the black listed state is updated
677 // back to the "AutoDetected" type.
678 TEST_F(MediaGalleriesPreferencesTest
, AutoAddedBlackListing
) {
679 MediaGalleryPrefId auto_id
, id
;
682 base::FilePath relative_path
;
685 // Add a new auto detect gallery to test with.
686 path
= MakeMediaGalleriesTestingPath("new_auto");
687 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
688 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
689 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
,
690 relative_path
, false /*auto*/);
691 EXPECT_EQ(default_galleries_count() + 1UL, id
);
693 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
694 MediaGalleryPrefInfo::kAutoDetected
);
697 // Remove an auto added gallery (i.e. make it blacklisted).
698 gallery_prefs()->ForgetGalleryById(auto_id
);
699 expected_galleries_
[auto_id
].type
= MediaGalleryPrefInfo::kBlackListed
;
700 expected_galleries_for_all
.erase(auto_id
);
703 // Try adding the gallery again automatically and it should be a no-op.
704 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
706 EXPECT_EQ(auto_id
, id
);
709 // Add the gallery again as a user action.
710 id
= gallery_prefs()->AddGalleryByPath(path
,
711 MediaGalleryPrefInfo::kUserAdded
);
712 EXPECT_EQ(auto_id
, id
);
713 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
714 MediaGalleryPrefInfo::kAutoDetected
);
718 // Whenever a "ScanResult" gallery is removed, it is moved to a black listed
719 // state. When the gallery is added again, the black listed state is updated
720 // back to the "ScanResult" type.
721 TEST_F(MediaGalleriesPreferencesTest
, ScanResultBlackListing
) {
722 MediaGalleryPrefId scan_id
, id
;
725 base::FilePath relative_path
;
728 // Add a new scan result gallery to test with.
729 path
= MakeMediaGalleriesTestingPath("new_scan");
730 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
731 base::string16 gallery_name
= base::ASCIIToUTF16("NewScanGallery");
732 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
733 MediaGalleryPrefInfo::kScanResult
);
734 EXPECT_EQ(default_galleries_count() + 1UL, id
);
736 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
737 MediaGalleryPrefInfo::kScanResult
);
740 // Remove a scan result gallery (i.e. make it blacklisted).
741 gallery_prefs()->ForgetGalleryById(scan_id
);
742 expected_galleries_
[scan_id
].type
= MediaGalleryPrefInfo::kRemovedScan
;
743 expected_galleries_for_all
.erase(scan_id
);
746 // Try adding the gallery again as a scan result it should be a no-op.
747 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
748 MediaGalleryPrefInfo::kScanResult
);
749 EXPECT_EQ(scan_id
, id
);
752 // Add the gallery again as a user action.
753 id
= gallery_prefs()->AddGalleryByPath(path
,
754 MediaGalleryPrefInfo::kUserAdded
);
755 EXPECT_EQ(scan_id
, id
);
756 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
757 MediaGalleryPrefInfo::kUserAdded
);
761 TEST_F(MediaGalleriesPreferencesTest
, UpdateGalleryNameV2
) {
762 // Add a new auto detect gallery to test with.
763 base::FilePath path
= MakeMediaGalleriesTestingPath("new_auto");
765 base::FilePath relative_path
;
766 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
767 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
768 MediaGalleryPrefId id
=
769 AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
770 MediaGalleryPrefInfo::kAutoDetected
);
771 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
772 MediaGalleryPrefInfo::kAutoDetected
);
775 // Won't override the name -- don't change any expectation.
776 gallery_name
= base::string16();
777 AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
778 MediaGalleryPrefInfo::kAutoDetected
);
781 gallery_name
= base::ASCIIToUTF16("NewName");
782 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
783 MediaGalleryPrefInfo::kAutoDetected
);
784 // Note: will really just update the existing expectation.
785 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
786 MediaGalleryPrefInfo::kAutoDetected
);
790 TEST_F(MediaGalleriesPreferencesTest
, GalleryPermissions
) {
791 MediaGalleryPrefId auto_id
, user_added_id
, to_blacklist_id
, scan_id
,
792 to_scan_remove_id
, id
;
795 base::FilePath relative_path
;
798 // Add some galleries to test with.
799 path
= MakeMediaGalleriesTestingPath("new_user");
800 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
801 base::string16 gallery_name
= base::ASCIIToUTF16("NewUserGallery");
802 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
804 EXPECT_EQ(default_galleries_count() + 1UL, id
);
806 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
807 MediaGalleryPrefInfo::kUserAdded
);
810 path
= MakeMediaGalleriesTestingPath("new_auto");
811 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
812 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
813 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
815 EXPECT_EQ(default_galleries_count() + 2UL, id
);
817 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
818 MediaGalleryPrefInfo::kAutoDetected
);
821 path
= MakeMediaGalleriesTestingPath("to_blacklist");
822 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
823 gallery_name
= base::ASCIIToUTF16("ToBlacklistGallery");
824 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
826 EXPECT_EQ(default_galleries_count() + 3UL, id
);
827 to_blacklist_id
= id
;
828 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
829 MediaGalleryPrefInfo::kAutoDetected
);
832 path
= MakeMediaGalleriesTestingPath("new_scan");
833 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
834 gallery_name
= base::ASCIIToUTF16("NewScanGallery");
835 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
836 MediaGalleryPrefInfo::kScanResult
);
837 EXPECT_EQ(default_galleries_count() + 4UL, id
);
839 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
840 MediaGalleryPrefInfo::kScanResult
);
843 path
= MakeMediaGalleriesTestingPath("to_scan_remove");
844 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
845 gallery_name
= base::ASCIIToUTF16("ToScanRemoveGallery");
846 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
847 MediaGalleryPrefInfo::kScanResult
);
848 EXPECT_EQ(default_galleries_count() + 5UL, id
);
849 to_scan_remove_id
= id
;
850 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
851 MediaGalleryPrefInfo::kScanResult
);
854 // Remove permission for all galleries from the all-permission extension.
855 gallery_prefs()->SetGalleryPermissionForExtension(
856 *all_permission_extension
.get(), auto_id
, false);
857 expected_galleries_for_all
.erase(auto_id
);
860 gallery_prefs()->SetGalleryPermissionForExtension(
861 *all_permission_extension
.get(), user_added_id
, false);
862 expected_galleries_for_all
.erase(user_added_id
);
865 gallery_prefs()->SetGalleryPermissionForExtension(
866 *all_permission_extension
.get(), to_blacklist_id
, false);
867 expected_galleries_for_all
.erase(to_blacklist_id
);
870 gallery_prefs()->SetGalleryPermissionForExtension(
871 *all_permission_extension
.get(), scan_id
, false);
872 expected_galleries_for_all
.erase(scan_id
);
875 gallery_prefs()->SetGalleryPermissionForExtension(
876 *all_permission_extension
.get(), to_scan_remove_id
, false);
877 expected_galleries_for_all
.erase(to_scan_remove_id
);
880 // Add permission back for all galleries to the all-permission extension.
881 gallery_prefs()->SetGalleryPermissionForExtension(
882 *all_permission_extension
.get(), auto_id
, true);
883 expected_galleries_for_all
.insert(auto_id
);
886 gallery_prefs()->SetGalleryPermissionForExtension(
887 *all_permission_extension
.get(), user_added_id
, true);
888 expected_galleries_for_all
.insert(user_added_id
);
891 gallery_prefs()->SetGalleryPermissionForExtension(
892 *all_permission_extension
.get(), to_blacklist_id
, true);
893 expected_galleries_for_all
.insert(to_blacklist_id
);
896 gallery_prefs()->SetGalleryPermissionForExtension(
897 *all_permission_extension
.get(), scan_id
, true);
898 expected_galleries_for_all
.insert(scan_id
);
901 gallery_prefs()->SetGalleryPermissionForExtension(
902 *all_permission_extension
.get(), to_scan_remove_id
, true);
903 expected_galleries_for_all
.insert(to_scan_remove_id
);
906 // Add permission for all galleries to the regular permission extension.
907 gallery_prefs()->SetGalleryPermissionForExtension(
908 *regular_permission_extension
.get(), auto_id
, true);
909 expected_galleries_for_regular
.insert(auto_id
);
912 gallery_prefs()->SetGalleryPermissionForExtension(
913 *regular_permission_extension
.get(), user_added_id
, true);
914 expected_galleries_for_regular
.insert(user_added_id
);
917 gallery_prefs()->SetGalleryPermissionForExtension(
918 *regular_permission_extension
.get(), to_blacklist_id
, true);
919 expected_galleries_for_regular
.insert(to_blacklist_id
);
922 gallery_prefs()->SetGalleryPermissionForExtension(
923 *regular_permission_extension
.get(), scan_id
, true);
924 expected_galleries_for_regular
.insert(scan_id
);
927 gallery_prefs()->SetGalleryPermissionForExtension(
928 *regular_permission_extension
.get(), to_scan_remove_id
, true);
929 expected_galleries_for_regular
.insert(to_scan_remove_id
);
932 // Blacklist the to be black listed gallery
933 gallery_prefs()->ForgetGalleryById(to_blacklist_id
);
934 expected_galleries_
[to_blacklist_id
].type
=
935 MediaGalleryPrefInfo::kBlackListed
;
936 expected_galleries_for_all
.erase(to_blacklist_id
);
937 expected_galleries_for_regular
.erase(to_blacklist_id
);
940 gallery_prefs()->ForgetGalleryById(to_scan_remove_id
);
941 expected_galleries_
[to_scan_remove_id
].type
=
942 MediaGalleryPrefInfo::kRemovedScan
;
943 expected_galleries_for_all
.erase(to_scan_remove_id
);
944 expected_galleries_for_regular
.erase(to_scan_remove_id
);
947 // Remove permission for all galleries to the regular permission extension.
948 gallery_prefs()->SetGalleryPermissionForExtension(
949 *regular_permission_extension
.get(), auto_id
, false);
950 expected_galleries_for_regular
.erase(auto_id
);
953 gallery_prefs()->SetGalleryPermissionForExtension(
954 *regular_permission_extension
.get(), user_added_id
, false);
955 expected_galleries_for_regular
.erase(user_added_id
);
958 gallery_prefs()->SetGalleryPermissionForExtension(
959 *regular_permission_extension
.get(), scan_id
, false);
960 expected_galleries_for_regular
.erase(scan_id
);
963 // Add permission for an invalid gallery id.
964 gallery_prefs()->SetGalleryPermissionForExtension(
965 *regular_permission_extension
.get(), 9999L, true);
969 // What an existing gallery is added again, update the gallery information if
971 TEST_F(MediaGalleriesPreferencesTest
, UpdateGalleryDetails
) {
972 MediaGalleryPrefId auto_id
, id
;
975 base::FilePath relative_path
;
978 // Add a new auto detect gallery to test with.
979 path
= MakeMediaGalleriesTestingPath("new_auto");
980 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
981 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
982 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
,
983 relative_path
, false /*auto*/);
984 EXPECT_EQ(default_galleries_count() + 1UL, id
);
986 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
987 MediaGalleryPrefInfo::kAutoDetected
);
990 // Update the device name and add the gallery again.
991 gallery_name
= base::ASCIIToUTF16("AutoGallery2");
992 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
994 EXPECT_EQ(auto_id
, id
);
995 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
996 MediaGalleryPrefInfo::kAutoDetected
);
1000 TEST_F(MediaGalleriesPreferencesTest
, MultipleGalleriesPerDevices
) {
1001 base::FilePath path
;
1003 base::FilePath relative_path
;
1006 // Add a regular gallery
1007 path
= MakeMediaGalleriesTestingPath("new_user");
1008 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
1009 base::string16 gallery_name
= base::ASCIIToUTF16("NewUserGallery");
1010 MediaGalleryPrefId user_added_id
=
1011 AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
1013 EXPECT_EQ(default_galleries_count() + 1UL, user_added_id
);
1014 AddGalleryExpectation(user_added_id
, gallery_name
, info
.device_id(),
1015 relative_path
, MediaGalleryPrefInfo::kUserAdded
);
1018 // Find it by device id and fail to find something related.
1019 MediaGalleryPrefIdSet pref_id_set
;
1020 pref_id_set
= gallery_prefs()->LookUpGalleriesByDeviceId(info
.device_id());
1021 EXPECT_EQ(1U, pref_id_set
.size());
1022 EXPECT_TRUE(pref_id_set
.find(user_added_id
) != pref_id_set
.end());
1024 MediaStorageUtil::GetDeviceInfoFromPath(
1025 MakeMediaGalleriesTestingPath("new_user/foo"), &info
, &relative_path
);
1026 pref_id_set
= gallery_prefs()->LookUpGalleriesByDeviceId(info
.device_id());
1027 EXPECT_EQ(0U, pref_id_set
.size());
1029 // Add some galleries on the same device.
1030 relative_path
= base::FilePath(FILE_PATH_LITERAL("path1/on/device1"));
1031 gallery_name
= base::ASCIIToUTF16("Device1Path1");
1032 std::string device_id
= "path:device1";
1033 MediaGalleryPrefId dev1_path1_id
= AddGalleryWithNameV1(
1034 device_id
, gallery_name
, relative_path
, true /*user*/);
1035 EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id
);
1036 AddGalleryExpectation(dev1_path1_id
, gallery_name
, device_id
, relative_path
,
1037 MediaGalleryPrefInfo::kUserAdded
);
1040 relative_path
= base::FilePath(FILE_PATH_LITERAL("path2/on/device1"));
1041 gallery_name
= base::ASCIIToUTF16("Device1Path2");
1042 MediaGalleryPrefId dev1_path2_id
= AddGalleryWithNameV1(
1043 device_id
, gallery_name
, relative_path
, true /*user*/);
1044 EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id
);
1045 AddGalleryExpectation(dev1_path2_id
, gallery_name
, device_id
, relative_path
,
1046 MediaGalleryPrefInfo::kUserAdded
);
1049 relative_path
= base::FilePath(FILE_PATH_LITERAL("path1/on/device2"));
1050 gallery_name
= base::ASCIIToUTF16("Device2Path1");
1051 device_id
= "path:device2";
1052 MediaGalleryPrefId dev2_path1_id
= AddGalleryWithNameV1(
1053 device_id
, gallery_name
, relative_path
, true /*user*/);
1054 EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id
);
1055 AddGalleryExpectation(dev2_path1_id
, gallery_name
, device_id
, relative_path
,
1056 MediaGalleryPrefInfo::kUserAdded
);
1059 relative_path
= base::FilePath(FILE_PATH_LITERAL("path2/on/device2"));
1060 gallery_name
= base::ASCIIToUTF16("Device2Path2");
1061 MediaGalleryPrefId dev2_path2_id
= AddGalleryWithNameV1(
1062 device_id
, gallery_name
, relative_path
, true /*user*/);
1063 EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id
);
1064 AddGalleryExpectation(dev2_path2_id
, gallery_name
, device_id
, relative_path
,
1065 MediaGalleryPrefInfo::kUserAdded
);
1068 // Check that adding one of them again works as expected.
1069 MediaGalleryPrefId id
= AddGalleryWithNameV1(
1070 device_id
, gallery_name
, relative_path
, true /*user*/);
1071 EXPECT_EQ(dev2_path2_id
, id
);
1075 TEST_F(MediaGalleriesPreferencesTest
, GalleryChangeObserver
) {
1076 // Start with one observer.
1077 MockGalleryChangeObserver
observer1(gallery_prefs());
1078 gallery_prefs()->AddGalleryChangeObserver(&observer1
);
1080 // Add a new auto detected gallery.
1081 base::FilePath path
= MakeMediaGalleriesTestingPath("new_auto");
1083 base::FilePath relative_path
;
1084 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
1085 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
1086 MediaGalleryPrefId auto_id
= AddGalleryWithNameV1(
1087 info
.device_id(), gallery_name
, relative_path
, false /*auto*/);
1088 EXPECT_EQ(default_galleries_count() + 1UL, auto_id
);
1089 AddGalleryExpectation(auto_id
, gallery_name
, info
.device_id(),
1090 relative_path
, MediaGalleryPrefInfo::kAutoDetected
);
1091 EXPECT_EQ(1, observer1
.notifications());
1093 // Add a second observer.
1094 MockGalleryChangeObserver
observer2(gallery_prefs());
1095 gallery_prefs()->AddGalleryChangeObserver(&observer2
);
1097 // Add a new user added gallery.
1098 path
= MakeMediaGalleriesTestingPath("new_user");
1099 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
1100 gallery_name
= base::ASCIIToUTF16("NewUserGallery");
1101 MediaGalleryPrefId user_added_id
=
1102 AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
1104 AddGalleryExpectation(user_added_id
, gallery_name
, info
.device_id(),
1105 relative_path
, MediaGalleryPrefInfo::kUserAdded
);
1106 EXPECT_EQ(default_galleries_count() + 2UL, user_added_id
);
1107 EXPECT_EQ(2, observer1
.notifications());
1108 EXPECT_EQ(1, observer2
.notifications());
1110 // Remove the first observer.
1111 gallery_prefs()->RemoveGalleryChangeObserver(&observer1
);
1113 // Remove an auto added gallery (i.e. make it blacklisted).
1114 gallery_prefs()->ForgetGalleryById(auto_id
);
1115 expected_galleries_
[auto_id
].type
= MediaGalleryPrefInfo::kBlackListed
;
1116 expected_galleries_for_all
.erase(auto_id
);
1118 EXPECT_EQ(2, observer1
.notifications());
1119 EXPECT_EQ(2, observer2
.notifications());
1121 // Remove a user added gallery and it should go away.
1122 gallery_prefs()->ForgetGalleryById(user_added_id
);
1123 expected_galleries_
.erase(user_added_id
);
1124 expected_device_map
[info
.device_id()].erase(user_added_id
);
1126 EXPECT_EQ(2, observer1
.notifications());
1127 EXPECT_EQ(3, observer2
.notifications());
1130 TEST_F(MediaGalleriesPreferencesTest
, UpdateSingletonDeviceIdType
) {
1131 MediaGalleryPrefId id
;
1132 base::FilePath path
;
1135 // Add a new auto detect gallery to test with.
1136 path
= MakeMediaGalleriesTestingPath("new_auto");
1137 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
1138 std::string device_id
= StorageInfo::MakeDeviceId(StorageInfo::ITUNES
,
1139 path
.AsUTF8Unsafe());
1140 id
= AddGalleryWithNameV2(device_id
, gallery_name
, base::FilePath(),
1141 MediaGalleryPrefInfo::kAutoDetected
);
1142 EXPECT_EQ(default_galleries_count() + 1UL, id
);
1143 AddGalleryExpectation(id
, gallery_name
, device_id
, base::FilePath(),
1144 MediaGalleryPrefInfo::kAutoDetected
);
1147 // Update the device id.
1148 MockGalleryChangeObserver
observer(gallery_prefs());
1149 gallery_prefs()->AddGalleryChangeObserver(&observer
);
1151 path
= MakeMediaGalleriesTestingPath("updated_path");
1152 std::string updated_device_id
=
1153 StorageInfo::MakeDeviceId(StorageInfo::ITUNES
, path
.AsUTF8Unsafe());
1154 EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id
));
1155 AddGalleryExpectation(id
, gallery_name
, updated_device_id
, base::FilePath(),
1156 MediaGalleryPrefInfo::kAutoDetected
);
1157 expected_device_map
[device_id
].erase(id
);
1158 expected_device_map
[updated_device_id
].insert(id
);
1160 EXPECT_EQ(1, observer
.notifications());
1162 // No gallery for type.
1163 std::string new_device_id
=
1164 StorageInfo::MakeDeviceId(StorageInfo::PICASA
, path
.AsUTF8Unsafe());
1165 EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id
));
1168 TEST_F(MediaGalleriesPreferencesTest
, LookupImportedGalleryByPath
) {
1169 MediaGalleryPrefId id
;
1170 base::FilePath path
;
1173 // iTunes device path points to an XML file in the library directory.
1174 path
= MakeMediaGalleriesTestingPath("new_auto").AppendASCII("library.xml");
1175 base::string16 gallery_name
= base::ASCIIToUTF16("NewAutoGallery");
1176 std::string device_id
= StorageInfo::MakeDeviceId(StorageInfo::ITUNES
,
1177 path
.AsUTF8Unsafe());
1178 id
= AddGalleryWithNameV2(device_id
, gallery_name
, base::FilePath(),
1179 MediaGalleryPrefInfo::kAutoDetected
);
1180 EXPECT_EQ(default_galleries_count() + 1UL, id
);
1181 AddGalleryExpectation(id
, gallery_name
, device_id
, base::FilePath(),
1182 MediaGalleryPrefInfo::kAutoDetected
);
1185 // Verify we can look up the imported gallery by its path.
1186 MediaGalleryPrefInfo gallery_info
;
1187 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(path
.DirName(),
1189 EXPECT_EQ(id
, gallery_info
.pref_id
);
1192 TEST_F(MediaGalleriesPreferencesTest
, ScanResults
) {
1193 MediaGalleryPrefId id
;
1194 base::FilePath path
;
1196 base::FilePath relative_path
;
1197 base::Time now
= base::Time::Now();
1200 // Add a new scan result gallery to test with.
1201 path
= MakeMediaGalleriesTestingPath("new_scan");
1202 MediaStorageUtil::GetDeviceInfoFromPath(path
, &info
, &relative_path
);
1203 id
= gallery_prefs()->AddGallery(info
.device_id(), relative_path
,
1204 MediaGalleryPrefInfo::kScanResult
,
1205 ASCIIToUTF16("volume label"),
1206 ASCIIToUTF16("vendor name"),
1207 ASCIIToUTF16("model name"),
1208 1000000ULL, now
, 1, 2, 3);
1209 EXPECT_EQ(default_galleries_count() + 1UL, id
);
1210 AddScanResultExpectation(id
, base::string16(), info
.device_id(),
1211 relative_path
, 1, 2, 3);
1214 // Update the found media count.
1215 id
= gallery_prefs()->AddGallery(info
.device_id(), relative_path
,
1216 MediaGalleryPrefInfo::kScanResult
,
1217 ASCIIToUTF16("volume label"),
1218 ASCIIToUTF16("vendor name"),
1219 ASCIIToUTF16("model name"),
1220 1000000ULL, now
, 4, 5, 6);
1221 EXPECT_EQ(default_galleries_count() + 1UL, id
);
1222 AddScanResultExpectation(id
, base::string16(), info
.device_id(),
1223 relative_path
, 4, 5, 6);
1226 // Remove a scan result (i.e. make it blacklisted).
1227 gallery_prefs()->ForgetGalleryById(id
);
1228 expected_galleries_
[id
].type
= MediaGalleryPrefInfo::kRemovedScan
;
1229 expected_galleries_
[id
].audio_count
= 0;
1230 expected_galleries_
[id
].image_count
= 0;
1231 expected_galleries_
[id
].video_count
= 0;
1234 // Try adding the gallery again as a scan result it should be a no-op.
1235 id
= gallery_prefs()->AddGallery(info
.device_id(), relative_path
,
1236 MediaGalleryPrefInfo::kScanResult
,
1237 ASCIIToUTF16("volume label"),
1238 ASCIIToUTF16("vendor name"),
1239 ASCIIToUTF16("model name"),
1240 1000000ULL, now
, 7, 8, 9);
1241 EXPECT_EQ(default_galleries_count() + 1UL, id
);
1244 // Add the gallery again as a user action.
1245 id
= gallery_prefs()->AddGalleryByPath(path
,
1246 MediaGalleryPrefInfo::kUserAdded
);
1247 EXPECT_EQ(default_galleries_count() + 1UL, id
);
1248 AddGalleryExpectation(id
, base::string16(), info
.device_id(), relative_path
,
1249 MediaGalleryPrefInfo::kUserAdded
);
1253 TEST(MediaGalleriesPrefInfoTest
, NameGeneration
) {
1254 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
1256 MediaGalleryPrefInfo info
;
1258 info
.display_name
= ASCIIToUTF16("override");
1259 info
.device_id
= StorageInfo::MakeDeviceId(
1260 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM
, "unique");
1262 EXPECT_EQ(ASCIIToUTF16("override"), info
.GetGalleryDisplayName());
1264 info
.display_name
= ASCIIToUTF16("o2");
1265 EXPECT_EQ(ASCIIToUTF16("o2"), info
.GetGalleryDisplayName());
1267 EXPECT_EQ(l10n_util::GetStringUTF16(
1268 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED
),
1269 info
.GetGalleryAdditionalDetails());
1271 info
.last_attach_time
= base::Time::Now();
1272 EXPECT_NE(l10n_util::GetStringUTF16(
1273 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED
),
1274 info
.GetGalleryAdditionalDetails());
1275 EXPECT_NE(l10n_util::GetStringUTF16(
1276 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED
),
1277 info
.GetGalleryAdditionalDetails());
1279 info
.volume_label
= ASCIIToUTF16("vol");
1280 info
.vendor_name
= ASCIIToUTF16("vendor");
1281 info
.model_name
= ASCIIToUTF16("model");
1282 EXPECT_EQ(ASCIIToUTF16("o2"), info
.GetGalleryDisplayName());
1284 info
.display_name
= base::string16();
1285 EXPECT_EQ(ASCIIToUTF16("vol"), info
.GetGalleryDisplayName());
1286 info
.volume_label
= base::string16();
1287 EXPECT_EQ(ASCIIToUTF16("vendor, model"), info
.GetGalleryDisplayName());
1289 info
.device_id
= StorageInfo::MakeDeviceId(
1290 StorageInfo::FIXED_MASS_STORAGE
, "unique");
1291 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(),
1292 base::UTF16ToUTF8(info
.GetGalleryTooltip()));
1294 TestStorageMonitor::Destroy();
1297 TEST_F(MediaGalleriesPreferencesTest
, SetsDefaultGalleryTypeField
) {
1298 // Tests that default galleries (Music, Pictures, Video) have the correct
1299 // default_gallery field set.
1301 // No default galleries exist on CrOS so this test isn't relevant there.
1302 #if defined(OS_CHROMEOS)
1306 base::FilePath music_path
;
1307 base::FilePath pictures_path
;
1308 base::FilePath videos_path
;
1309 bool got_music_path
= PathService::Get(chrome::DIR_USER_MUSIC
, &music_path
);
1310 bool got_pictures_path
=
1311 PathService::Get(chrome::DIR_USER_PICTURES
, &pictures_path
);
1312 bool got_videos_path
=
1313 PathService::Get(chrome::DIR_USER_VIDEOS
, &videos_path
);
1315 int num_default_galleries
= 0;
1317 const MediaGalleriesPrefInfoMap
& known_galleries
=
1318 gallery_prefs()->known_galleries();
1319 for (MediaGalleriesPrefInfoMap::const_iterator it
=
1320 known_galleries
.begin();
1321 it
!= known_galleries
.end();
1323 if (it
->second
.type
!= MediaGalleryPrefInfo::kAutoDetected
)
1326 std::string unique_id
;
1327 if (!StorageInfo::CrackDeviceId(it
->second
.device_id
, NULL
, &unique_id
))
1330 if (got_music_path
&& unique_id
== music_path
.AsUTF8Unsafe()) {
1331 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault
,
1332 it
->second
.default_gallery_type
);
1333 num_default_galleries
++;
1334 } else if (got_pictures_path
&& unique_id
== pictures_path
.AsUTF8Unsafe()) {
1335 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault
,
1336 it
->second
.default_gallery_type
);
1337 num_default_galleries
++;
1338 } else if (got_videos_path
&& unique_id
== videos_path
.AsUTF8Unsafe()) {
1339 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault
,
1340 it
->second
.default_gallery_type
);
1341 num_default_galleries
++;
1343 EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault
,
1344 it
->second
.default_gallery_type
);
1348 EXPECT_EQ(3, num_default_galleries
);
1351 TEST_F(MediaGalleriesPreferencesTest
, UpdatesDefaultGalleryType
) {
1352 // Tests that if the path of a default gallery changed since last init,
1353 // then when the MediaGalleriesPreferences is initialized, it will
1354 // rewrite the device ID in prefs to include the new path.
1356 // No default galleries exist on CrOS so this test isn't relevant there.
1357 #if defined(OS_CHROMEOS)
1361 base::FilePath old_music_path
;
1362 base::FilePath old_pictures_path
;
1363 base::FilePath old_videos_path
;
1364 bool got_old_music_path
=
1365 PathService::Get(chrome::DIR_USER_MUSIC
, &old_music_path
);
1366 bool got_old_pictures_path
=
1367 PathService::Get(chrome::DIR_USER_PICTURES
, &old_pictures_path
);
1368 bool got_old_videos_path
=
1369 PathService::Get(chrome::DIR_USER_VIDEOS
, &old_videos_path
);
1371 bool found_music
= false;
1372 bool found_pictures
= false;
1373 bool found_videos
= false;
1375 const MediaGalleriesPrefInfoMap
& old_known_galleries
=
1376 gallery_prefs()->known_galleries();
1377 for (MediaGalleriesPrefInfoMap::const_iterator it
=
1378 old_known_galleries
.begin();
1379 it
!= old_known_galleries
.end();
1381 if (it
->second
.type
== MediaGalleryPrefInfo::kAutoDetected
) {
1382 std::string unique_id
;
1383 if (!StorageInfo::CrackDeviceId(it
->second
.device_id
, NULL
, &unique_id
))
1386 if (got_old_music_path
&&
1387 it
->second
.default_gallery_type
==
1388 MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault
) {
1389 EXPECT_EQ(old_music_path
.AsUTF8Unsafe(), unique_id
);
1391 } else if (got_old_pictures_path
&&
1392 it
->second
.default_gallery_type
==
1393 MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault
) {
1394 EXPECT_EQ(old_pictures_path
.AsUTF8Unsafe(), unique_id
);
1395 found_pictures
= true;
1396 } else if (got_old_videos_path
&&
1397 it
->second
.default_gallery_type
==
1398 MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault
) {
1399 EXPECT_EQ(old_videos_path
.AsUTF8Unsafe(), unique_id
);
1400 found_videos
= true;
1405 EXPECT_TRUE(found_music
);
1406 EXPECT_TRUE(found_pictures
);
1407 EXPECT_TRUE(found_videos
);
1409 ChangeMediaPathOverrides();
1410 ReinitPrefsAndExpectations();
1412 base::FilePath new_music_path
;
1413 base::FilePath new_pictures_path
;
1414 base::FilePath new_videos_path
;
1415 bool got_new_music_path
=
1416 PathService::Get(chrome::DIR_USER_MUSIC
, &new_music_path
);
1417 bool got_new_pictures_path
=
1418 PathService::Get(chrome::DIR_USER_PICTURES
, &new_pictures_path
);
1419 bool got_new_videos_path
=
1420 PathService::Get(chrome::DIR_USER_VIDEOS
, &new_videos_path
);
1422 EXPECT_NE(new_music_path
, old_music_path
);
1423 EXPECT_NE(new_pictures_path
, old_pictures_path
);
1424 EXPECT_NE(new_videos_path
, old_videos_path
);
1426 found_music
= false;
1427 found_pictures
= false;
1428 found_videos
= false;
1430 const MediaGalleriesPrefInfoMap
& known_galleries
=
1431 gallery_prefs()->known_galleries();
1432 for (MediaGalleriesPrefInfoMap::const_iterator it
= known_galleries
.begin();
1433 it
!= known_galleries
.end();
1435 if (it
->second
.type
== MediaGalleryPrefInfo::kAutoDetected
) {
1436 std::string unique_id
;
1437 if (!StorageInfo::CrackDeviceId(it
->second
.device_id
, NULL
, &unique_id
))
1440 if (got_new_music_path
&&
1441 it
->second
.default_gallery_type
==
1442 MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault
) {
1443 EXPECT_EQ(new_music_path
.AsUTF8Unsafe(), unique_id
);
1445 } else if (got_new_pictures_path
&&
1446 it
->second
.default_gallery_type
==
1447 MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault
) {
1448 EXPECT_EQ(new_pictures_path
.AsUTF8Unsafe(), unique_id
);
1449 found_pictures
= true;
1450 } else if (got_new_videos_path
&&
1451 it
->second
.default_gallery_type
==
1452 MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault
) {
1453 EXPECT_EQ(new_videos_path
.AsUTF8Unsafe(), unique_id
);
1454 found_videos
= true;
1459 EXPECT_TRUE(found_music
);
1460 EXPECT_TRUE(found_pictures
);
1461 EXPECT_TRUE(found_videos
);
1464 TEST_F(MediaGalleriesPreferencesTest
, UpdateAddsDefaultGalleryTypeIfMissing
) {
1465 // Tests that if no default_gallery_type was specified for an existing prefs
1466 // info object corresponding to a particular gallery, then when the
1467 // MediaGalleriesPreferences is initialized, it assigns the proper one.
1469 // No default galleries exist on CrOS so this test isn't relevant there.
1470 #if defined(OS_CHROMEOS)
1474 // Add a new user added gallery.
1475 AddFixedGalleryWithExepectation("user_added", "UserAdded",
1476 MediaGalleryPrefInfo::kUserAdded
);
1478 // Remove the "default_gallery_type" field completely from the persisted data
1479 // for the prefs info object. This simulates the case where a user updated
1480 // Chrome from a version without that field to one with it.
1481 RemovePersistedDefaultGalleryValues();
1483 // Reinitializing the MediaGalleriesPreferences should populate the
1484 // default_gallery_type field with the correct value for each gallery.
1485 ReinitPrefsAndExpectations();
1487 base::FilePath music_path
;
1488 base::FilePath pictures_path
;
1489 base::FilePath videos_path
;
1490 bool got_music_path
= PathService::Get(chrome::DIR_USER_MUSIC
, &music_path
);
1491 bool got_pictures_path
=
1492 PathService::Get(chrome::DIR_USER_PICTURES
, &pictures_path
);
1493 bool got_videos_path
=
1494 PathService::Get(chrome::DIR_USER_VIDEOS
, &videos_path
);
1496 bool found_music
= false;
1497 bool found_pictures
= false;
1498 bool found_videos
= false;
1499 bool found_user_added
= false;
1501 const MediaGalleriesPrefInfoMap
& known_galleries
=
1502 gallery_prefs()->known_galleries();
1503 for (MediaGalleriesPrefInfoMap::const_iterator it
= known_galleries
.begin();
1504 it
!= known_galleries
.end();
1506 std::string unique_id
;
1507 if (!StorageInfo::CrackDeviceId(it
->second
.device_id
, NULL
, &unique_id
))
1510 if (got_music_path
&&
1511 it
->second
.default_gallery_type
==
1512 MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault
) {
1513 EXPECT_EQ(music_path
.AsUTF8Unsafe(), unique_id
);
1515 } else if (got_pictures_path
&&
1516 it
->second
.default_gallery_type
==
1517 MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault
) {
1518 EXPECT_EQ(pictures_path
.AsUTF8Unsafe(), unique_id
);
1519 found_pictures
= true;
1520 } else if (got_videos_path
&&
1521 it
->second
.default_gallery_type
==
1522 MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault
) {
1523 EXPECT_EQ(videos_path
.AsUTF8Unsafe(), unique_id
);
1524 found_videos
= true;
1525 } else if (it
->second
.default_gallery_type
==
1526 MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault
) {
1527 found_user_added
= true;
1531 EXPECT_TRUE(found_music
);
1532 EXPECT_TRUE(found_pictures
);
1533 EXPECT_TRUE(found_videos
);
1534 EXPECT_TRUE(found_user_added
);