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"
38 using base::ASCIIToUTF16
;
39 using storage_monitor::MediaStorageUtil
;
40 using storage_monitor::StorageInfo
;
41 using storage_monitor::TestStorageMonitor
;
45 class MockGalleryChangeObserver
46 : public MediaGalleriesPreferences::GalleryChangeObserver
{
48 explicit MockGalleryChangeObserver(MediaGalleriesPreferences
* pref
)
51 virtual ~MockGalleryChangeObserver() {}
53 int notifications() const { return notifications_
;}
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
);
64 virtual void OnPermissionRemoved(MediaGalleriesPreferences
* pref
,
65 const std::string
& extension_id
,
66 MediaGalleryPrefId pref_id
) OVERRIDE
{
67 EXPECT_EQ(pref_
, pref
);
71 virtual void OnGalleryAdded(MediaGalleriesPreferences
* pref
,
72 MediaGalleryPrefId pref_id
) OVERRIDE
{
73 EXPECT_EQ(pref_
, pref
);
77 virtual void OnGalleryRemoved(MediaGalleriesPreferences
* pref
,
78 MediaGalleryPrefId pref_id
) OVERRIDE
{
79 EXPECT_EQ(pref_
, pref
);
83 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences
* pref
,
84 MediaGalleryPrefId pref_id
) OVERRIDE
{
85 EXPECT_EQ(pref_
, pref
);
89 MediaGalleriesPreferences
* pref_
;
92 DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver
);
97 class MediaGalleriesPreferencesTest
: public testing::Test
{
99 typedef std::map
<std::string
/*device id*/, MediaGalleryPrefIdSet
>
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()));
121 gallery_prefs_
->EnsureInitialized(loop
.QuitClosure());
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
{
157 TestStorageMonitor::Destroy();
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();
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
));
174 EXPECT_TRUE(gallery_prefs_
->NonAutoGalleryHasPermission(it
->first
));
179 for (DeviceIdPrefIdsMap::const_iterator it
= expected_device_map
.begin();
180 it
!= expected_device_map
.end();
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
,
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
,
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(),
267 MediaGalleryPrefId
AddGalleryWithNameV1(const std::string
& device_id
,
268 const base::string16
& display_name
,
269 const base::FilePath
& relative_path
,
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(),
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(),
290 MediaGalleryPrefId
AddFixedGalleryWithExepectation(
291 const std::string
& path_name
, const std::string
& name
,
292 MediaGalleryPrefInfo::Type type
) {
293 base::FilePath path
= MakeMediaGalleriesTestingPath(path_name
);
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
,
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_
;
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_
;
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
;
345 base::FilePath relative_path
;
348 // Add a new auto detected gallery.
349 path
= MakeMediaGalleriesTestingPath("new_auto");
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
);
357 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
358 MediaGalleryPrefInfo::kAutoDetected
);
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
);
366 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
367 MediaGalleryPrefInfo::kAutoDetected
);
368 EXPECT_EQ(auto_id
, id
);
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
);
382 const std::string user_added_device_id
= info
.device_id();
383 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
384 MediaGalleryPrefInfo::kUserAdded
);
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
);
392 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
393 MediaGalleryPrefInfo::kAutoDetected
);
394 EXPECT_EQ(user_added_id
, id
);
396 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
397 MediaGalleryPrefInfo::kScanResult
);
398 EXPECT_EQ(user_added_id
, id
);
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
);
409 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
410 MediaGalleryPrefInfo::kScanResult
);
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
);
418 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
419 MediaGalleryPrefInfo::kAutoDetected
);
420 EXPECT_EQ(scan_id
, id
);
422 id
= AddGalleryWithNameV2(info
.device_id(), gallery_name
, relative_path
,
423 MediaGalleryPrefInfo::kScanResult
);
424 EXPECT_EQ(scan_id
, id
);
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
);
467 // Remove a scan result (i.e. make it blacklisted).
468 gallery_prefs()->ForgetGalleryById(scan_id
);
469 expected_galleries_
[scan_id
].type
= MediaGalleryPrefInfo::kRemovedScan
;
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
);
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
);
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
);
516 gallery_prefs()->ForgetGalleryById(auto_forget
);
517 expected_galleries_
[auto_forget
].type
= MediaGalleryPrefInfo::kBlackListed
;
518 expected_galleries_for_all
.erase(auto_forget
);
521 gallery_prefs()->ForgetGalleryById(scan_forget
);
522 expected_galleries_
[scan_forget
].type
= MediaGalleryPrefInfo::kRemovedScan
;
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
);
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
);
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
);
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
);
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
);
558 TEST_F(MediaGalleriesPreferencesTest
, AddGalleryWithVolumeMetadata
) {
559 MediaGalleryPrefId id
;
562 base::FilePath relative_path
;
563 base::Time now
= base::Time::Now();
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
);
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
591 EXPECT_LE(std::abs(now
.ToInternalValue() -
592 gallery_info
.last_attach_time
.ToInternalValue()),
596 TEST_F(MediaGalleriesPreferencesTest
, ReplaceGalleryWithVolumeMetadata
) {
597 MediaGalleryPrefId id
, metadata_id
;
600 base::FilePath relative_path
;
601 base::Time now
= base::Time::Now();
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
,
610 EXPECT_EQ(default_galleries_count() + 1UL, id
);
611 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
612 MediaGalleryPrefInfo::kAutoDetected
);
615 metadata_id
= gallery_prefs()->AddGallery(info
.device_id(),
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.
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
;
638 base::FilePath relative_path
;
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
);
649 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
650 MediaGalleryPrefInfo::kAutoDetected
);
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
);
659 // Try adding the gallery again automatically and it should be a no-op.
660 id
= AddGalleryWithNameV1(info
.device_id(), gallery_name
, relative_path
,
662 EXPECT_EQ(auto_id
, id
);
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
);
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
;
681 base::FilePath relative_path
;
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
);
692 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
693 MediaGalleryPrefInfo::kScanResult
);
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
);
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
);
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
);
717 TEST_F(MediaGalleriesPreferencesTest
, UpdateGalleryNameV2
) {
718 // Add a new auto detect gallery to test with.
719 base::FilePath path
= MakeMediaGalleriesTestingPath("new_auto");
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
);
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
);
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
);
746 TEST_F(MediaGalleriesPreferencesTest
, GalleryPermissions
) {
747 MediaGalleryPrefId auto_id
, user_added_id
, to_blacklist_id
, scan_id
,
748 to_scan_remove_id
, id
;
751 base::FilePath relative_path
;
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
,
760 EXPECT_EQ(default_galleries_count() + 1UL, id
);
762 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
763 MediaGalleryPrefInfo::kUserAdded
);
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
,
771 EXPECT_EQ(default_galleries_count() + 2UL, id
);
773 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
774 MediaGalleryPrefInfo::kAutoDetected
);
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
,
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
);
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
);
795 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
796 MediaGalleryPrefInfo::kScanResult
);
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
);
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
);
816 gallery_prefs()->SetGalleryPermissionForExtension(
817 *all_permission_extension
.get(), user_added_id
, false);
818 expected_galleries_for_all
.erase(user_added_id
);
821 gallery_prefs()->SetGalleryPermissionForExtension(
822 *all_permission_extension
.get(), to_blacklist_id
, false);
823 expected_galleries_for_all
.erase(to_blacklist_id
);
826 gallery_prefs()->SetGalleryPermissionForExtension(
827 *all_permission_extension
.get(), scan_id
, false);
828 expected_galleries_for_all
.erase(scan_id
);
831 gallery_prefs()->SetGalleryPermissionForExtension(
832 *all_permission_extension
.get(), to_scan_remove_id
, false);
833 expected_galleries_for_all
.erase(to_scan_remove_id
);
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
);
842 gallery_prefs()->SetGalleryPermissionForExtension(
843 *all_permission_extension
.get(), user_added_id
, true);
844 expected_galleries_for_all
.insert(user_added_id
);
847 gallery_prefs()->SetGalleryPermissionForExtension(
848 *all_permission_extension
.get(), to_blacklist_id
, true);
849 expected_galleries_for_all
.insert(to_blacklist_id
);
852 gallery_prefs()->SetGalleryPermissionForExtension(
853 *all_permission_extension
.get(), scan_id
, true);
854 expected_galleries_for_all
.insert(scan_id
);
857 gallery_prefs()->SetGalleryPermissionForExtension(
858 *all_permission_extension
.get(), to_scan_remove_id
, true);
859 expected_galleries_for_all
.insert(to_scan_remove_id
);
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
);
868 gallery_prefs()->SetGalleryPermissionForExtension(
869 *regular_permission_extension
.get(), user_added_id
, true);
870 expected_galleries_for_regular
.insert(user_added_id
);
873 gallery_prefs()->SetGalleryPermissionForExtension(
874 *regular_permission_extension
.get(), to_blacklist_id
, true);
875 expected_galleries_for_regular
.insert(to_blacklist_id
);
878 gallery_prefs()->SetGalleryPermissionForExtension(
879 *regular_permission_extension
.get(), scan_id
, true);
880 expected_galleries_for_regular
.insert(scan_id
);
883 gallery_prefs()->SetGalleryPermissionForExtension(
884 *regular_permission_extension
.get(), to_scan_remove_id
, true);
885 expected_galleries_for_regular
.insert(to_scan_remove_id
);
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
);
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
);
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
);
909 gallery_prefs()->SetGalleryPermissionForExtension(
910 *regular_permission_extension
.get(), user_added_id
, false);
911 expected_galleries_for_regular
.erase(user_added_id
);
914 gallery_prefs()->SetGalleryPermissionForExtension(
915 *regular_permission_extension
.get(), scan_id
, false);
916 expected_galleries_for_regular
.erase(scan_id
);
919 // Add permission for an invalid gallery id.
920 gallery_prefs()->SetGalleryPermissionForExtension(
921 *regular_permission_extension
.get(), 9999L, true);
925 // What an existing gallery is added again, update the gallery information if
927 TEST_F(MediaGalleriesPreferencesTest
, UpdateGalleryDetails
) {
928 MediaGalleryPrefId auto_id
, id
;
931 base::FilePath relative_path
;
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
);
942 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
943 MediaGalleryPrefInfo::kAutoDetected
);
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
,
950 EXPECT_EQ(auto_id
, id
);
951 AddGalleryExpectation(id
, gallery_name
, info
.device_id(), relative_path
,
952 MediaGalleryPrefInfo::kAutoDetected
);
956 TEST_F(MediaGalleriesPreferencesTest
, MultipleGalleriesPerDevices
) {
959 base::FilePath relative_path
;
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
,
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
);
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
);
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
);
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
);
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
);
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
);
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");
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
,
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
;
1090 base::FilePath relative_path
;
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
);
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
);
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
;
1131 base::FilePath relative_path
;
1132 base::Time now
= base::Time::Now();
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);
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);
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;
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
);
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
);
1188 TEST(MediaGalleriesPrefInfoTest
, NameGeneration
) {
1189 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
1191 MediaGalleryPrefInfo info
;
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();