• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // MediaGalleriesPreferences unit tests.
6 
7 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
8 
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"
31 
32 #if defined(OS_CHROMEOS)
33 #include "chrome/browser/chromeos/login/users/user_manager.h"
34 #include "chrome/browser/chromeos/settings/cros_settings.h"
35 #include "chrome/browser/chromeos/settings/device_settings_service.h"
36 #endif
37 
38 using base::ASCIIToUTF16;
39 using storage_monitor::MediaStorageUtil;
40 using storage_monitor::StorageInfo;
41 using storage_monitor::TestStorageMonitor;
42 
43 namespace {
44 
45 class MockGalleryChangeObserver
46     : public MediaGalleriesPreferences::GalleryChangeObserver {
47  public:
MockGalleryChangeObserver(MediaGalleriesPreferences * pref)48   explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref)
49       : pref_(pref),
50         notifications_(0) {}
~MockGalleryChangeObserver()51   virtual ~MockGalleryChangeObserver() {}
52 
notifications() const53   int notifications() const { return notifications_;}
54 
55  private:
56   // MediaGalleriesPreferences::GalleryChangeObserver implementation.
OnPermissionAdded(MediaGalleriesPreferences * pref,const std::string & extension_id,MediaGalleryPrefId pref_id)57   virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
58                                  const std::string& extension_id,
59                                  MediaGalleryPrefId pref_id) OVERRIDE {
60     EXPECT_EQ(pref_, pref);
61     ++notifications_;
62   }
63 
OnPermissionRemoved(MediaGalleriesPreferences * pref,const std::string & extension_id,MediaGalleryPrefId pref_id)64   virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
65                                    const std::string& extension_id,
66                                    MediaGalleryPrefId pref_id) OVERRIDE {
67     EXPECT_EQ(pref_, pref);
68     ++notifications_;
69   }
70 
OnGalleryAdded(MediaGalleriesPreferences * pref,MediaGalleryPrefId pref_id)71   virtual void OnGalleryAdded(MediaGalleriesPreferences* pref,
72                               MediaGalleryPrefId pref_id) OVERRIDE {
73     EXPECT_EQ(pref_, pref);
74     ++notifications_;
75   }
76 
OnGalleryRemoved(MediaGalleriesPreferences * pref,MediaGalleryPrefId pref_id)77   virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
78                                 MediaGalleryPrefId pref_id) OVERRIDE {
79     EXPECT_EQ(pref_, pref);
80     ++notifications_;
81   }
82 
OnGalleryInfoUpdated(MediaGalleriesPreferences * pref,MediaGalleryPrefId pref_id)83   virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
84                                     MediaGalleryPrefId pref_id) OVERRIDE {
85     EXPECT_EQ(pref_, pref);
86     ++notifications_;
87   }
88 
89   MediaGalleriesPreferences* pref_;
90   int notifications_;
91 
92   DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver);
93 };
94 
95 }  // namespace
96 
97 class MediaGalleriesPreferencesTest : public testing::Test {
98  public:
99   typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
100       DeviceIdPrefIdsMap;
101 
MediaGalleriesPreferencesTest()102   MediaGalleriesPreferencesTest()
103       : profile_(new TestingProfile()),
104         default_galleries_count_(0) {
105   }
106 
~MediaGalleriesPreferencesTest()107   virtual ~MediaGalleriesPreferencesTest() {
108   }
109 
SetUp()110   virtual void SetUp() OVERRIDE {
111     ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
112 
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);
118 
119     gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
120     base::RunLoop loop;
121     gallery_prefs_->EnsureInitialized(loop.QuitClosure());
122     loop.Run();
123 
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);
135       }
136     }
137 
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);
146 
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());
153   }
154 
TearDown()155   virtual void TearDown() OVERRIDE {
156     Verify();
157     TestStorageMonitor::Destroy();
158   }
159 
Verify()160   void Verify() {
161     const MediaGalleriesPrefInfoMap& known_galleries =
162         gallery_prefs_->known_galleries();
163     EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
164     for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
165          it != known_galleries.end();
166          ++it) {
167       VerifyGalleryInfo(it->second, it->first);
168       if (it->second.type != MediaGalleryPrefInfo::kAutoDetected &&
169           it->second.type != MediaGalleryPrefInfo::kBlackListed) {
170         if (!ContainsKey(expected_galleries_for_all, it->first) &&
171             !ContainsKey(expected_galleries_for_regular, it->first)) {
172           EXPECT_FALSE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
173         } else {
174           EXPECT_TRUE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
175         }
176       }
177     }
178 
179     for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin();
180          it != expected_device_map.end();
181          ++it) {
182       MediaGalleryPrefIdSet actual_id_set =
183           gallery_prefs_->LookUpGalleriesByDeviceId(it->first);
184       EXPECT_EQ(it->second, actual_id_set);
185     }
186 
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);
190 
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);
195 
196     std::set<MediaGalleryPrefId> galleries_for_no =
197         gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get());
198     EXPECT_EQ(0U, galleries_for_no.size());
199   }
200 
VerifyGalleryInfo(const MediaGalleryPrefInfo & actual,MediaGalleryPrefId expected_id) const201   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);
214   }
215 
gallery_prefs()216   MediaGalleriesPreferences* gallery_prefs() {
217     return gallery_prefs_.get();
218   }
219 
default_galleries_count()220   uint64 default_galleries_count() {
221     return default_galleries_count_;
222   }
223 
AddGalleryExpectation(MediaGalleryPrefId id,base::string16 display_name,std::string device_id,base::FilePath relative_path,MediaGalleryPrefInfo::Type type)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;
233 
234     if (type == MediaGalleryPrefInfo::kAutoDetected)
235       expected_galleries_for_all.insert(id);
236 
237     expected_device_map[device_id].insert(id);
238   }
239 
AddScanResultExpectation(MediaGalleryPrefId id,base::string16 display_name,std::string device_id,base::FilePath relative_path,int audio_count,int image_count,int video_count)240   void AddScanResultExpectation(MediaGalleryPrefId id,
241                                 base::string16 display_name,
242                                 std::string device_id,
243                                 base::FilePath relative_path,
244                                 int audio_count,
245                                 int image_count,
246                                 int video_count) {
247     AddGalleryExpectation(id, display_name, device_id, relative_path,
248                           MediaGalleryPrefInfo::kScanResult);
249     expected_galleries_[id].audio_count = audio_count;
250     expected_galleries_[id].image_count = image_count;
251     expected_galleries_[id].video_count = video_count;
252   }
253 
AddGalleryWithNameV0(const std::string & device_id,const base::string16 & display_name,const base::FilePath & relative_path,bool user_added)254   MediaGalleryPrefId AddGalleryWithNameV0(const std::string& device_id,
255                                           const base::string16& display_name,
256                                           const base::FilePath& relative_path,
257                                           bool user_added) {
258     MediaGalleryPrefInfo::Type type =
259         user_added ? MediaGalleryPrefInfo::kUserAdded
260                    : MediaGalleryPrefInfo::kAutoDetected;
261     return gallery_prefs()->AddGalleryInternal(
262         device_id, display_name, relative_path, type,
263         base::string16(), base::string16(), base::string16(), 0, base::Time(),
264         false, 0, 0, 0, 0);
265   }
266 
AddGalleryWithNameV1(const std::string & device_id,const base::string16 & display_name,const base::FilePath & relative_path,bool user_added)267   MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id,
268                                           const base::string16& display_name,
269                                           const base::FilePath& relative_path,
270                                           bool user_added) {
271     MediaGalleryPrefInfo::Type type =
272         user_added ? MediaGalleryPrefInfo::kUserAdded
273                    : MediaGalleryPrefInfo::kAutoDetected;
274     return gallery_prefs()->AddGalleryInternal(
275         device_id, display_name, relative_path, type,
276         base::string16(), base::string16(), base::string16(), 0, base::Time(),
277         false, 0, 0, 0, 1);
278   }
279 
AddGalleryWithNameV2(const std::string & device_id,const base::string16 & display_name,const base::FilePath & relative_path,MediaGalleryPrefInfo::Type type)280   MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id,
281                                           const base::string16& display_name,
282                                           const base::FilePath& relative_path,
283                                           MediaGalleryPrefInfo::Type type) {
284     return gallery_prefs()->AddGalleryInternal(
285         device_id, display_name, relative_path, type,
286         base::string16(), base::string16(), base::string16(), 0, base::Time(),
287         false, 0, 0, 0, 2);
288   }
289 
AddFixedGalleryWithExepectation(const std::string & path_name,const std::string & name,MediaGalleryPrefInfo::Type type)290   MediaGalleryPrefId AddFixedGalleryWithExepectation(
291       const std::string& path_name, const std::string& name,
292       MediaGalleryPrefInfo::Type type) {
293     base::FilePath path = MakeMediaGalleriesTestingPath(path_name);
294     StorageInfo info;
295     base::FilePath relative_path;
296     MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
297     base::string16 gallery_name = base::ASCIIToUTF16(name);
298     MediaGalleryPrefId id = AddGalleryWithNameV2(info.device_id(), gallery_name,
299                                                 relative_path, type);
300     AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
301                           type);
302     Verify();
303     return id;
304   }
305 
UpdateDeviceIDForSingletonType(const std::string & device_id)306   bool UpdateDeviceIDForSingletonType(const std::string& device_id) {
307     return gallery_prefs()->UpdateDeviceIDForSingletonType(device_id);
308   }
309 
310   scoped_refptr<extensions::Extension> all_permission_extension;
311   scoped_refptr<extensions::Extension> regular_permission_extension;
312   scoped_refptr<extensions::Extension> no_permissions_extension;
313 
314   std::set<MediaGalleryPrefId> expected_galleries_for_all;
315   std::set<MediaGalleryPrefId> expected_galleries_for_regular;
316 
317   DeviceIdPrefIdsMap expected_device_map;
318 
319   MediaGalleriesPrefInfoMap expected_galleries_;
320 
321  private:
322   // Needed for extension service & friends to work.
323   content::TestBrowserThreadBundle thread_bundle_;
324 
325   EnsureMediaDirectoriesExists mock_gallery_locations_;
326 
327 #if defined(OS_CHROMEOS)
328   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
329   chromeos::ScopedTestCrosSettings test_cros_settings_;
330   chromeos::ScopedTestUserManager test_user_manager_;
331 #endif
332 
333   TestStorageMonitor monitor_;
334   scoped_ptr<TestingProfile> profile_;
335   scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
336 
337   uint64 default_galleries_count_;
338 
339   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest);
340 };
341 
TEST_F(MediaGalleriesPreferencesTest,GalleryManagement)342 TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) {
343   MediaGalleryPrefId auto_id, user_added_id, scan_id, id;
344   base::FilePath path;
345   base::FilePath relative_path;
346   Verify();
347 
348   // Add a new auto detected gallery.
349   path = MakeMediaGalleriesTestingPath("new_auto");
350   StorageInfo info;
351   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
352   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
353   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
354                             MediaGalleryPrefInfo::kAutoDetected);
355   EXPECT_EQ(default_galleries_count() + 1UL, id);
356   auto_id = id;
357   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
358                         MediaGalleryPrefInfo::kAutoDetected);
359   Verify();
360 
361   // Add it as other types, nothing should happen.
362   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
363                             MediaGalleryPrefInfo::kUserAdded);
364   EXPECT_EQ(auto_id, id);
365   Verify();
366   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
367                             MediaGalleryPrefInfo::kAutoDetected);
368   EXPECT_EQ(auto_id, id);
369   Verify();
370   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
371                             MediaGalleryPrefInfo::kScanResult);
372   EXPECT_EQ(auto_id, id);
373 
374   // Add a new user added gallery.
375   path = MakeMediaGalleriesTestingPath("new_user");
376   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
377   gallery_name = base::ASCIIToUTF16("NewUserGallery");
378   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
379                             MediaGalleryPrefInfo::kUserAdded);
380   EXPECT_EQ(default_galleries_count() + 2UL, id);
381   user_added_id = id;
382   const std::string user_added_device_id = info.device_id();
383   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
384                         MediaGalleryPrefInfo::kUserAdded);
385   Verify();
386 
387   // Add it as other types, nothing should happen.
388   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
389                             MediaGalleryPrefInfo::kUserAdded);
390   EXPECT_EQ(user_added_id, id);
391   Verify();
392   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
393                             MediaGalleryPrefInfo::kAutoDetected);
394   EXPECT_EQ(user_added_id, id);
395   Verify();
396   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
397                             MediaGalleryPrefInfo::kScanResult);
398   EXPECT_EQ(user_added_id, id);
399   Verify();
400 
401   // Add a new scan result gallery.
402   path = MakeMediaGalleriesTestingPath("new_scan");
403   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
404   gallery_name = base::ASCIIToUTF16("NewScanGallery");
405   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
406                             MediaGalleryPrefInfo::kScanResult);
407   EXPECT_EQ(default_galleries_count() + 3UL, id);
408   scan_id = id;
409   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
410                         MediaGalleryPrefInfo::kScanResult);
411   Verify();
412 
413   // Add it as other types, nothing should happen.
414   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
415                             MediaGalleryPrefInfo::kUserAdded);
416   EXPECT_EQ(scan_id, id);
417   Verify();
418   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
419                             MediaGalleryPrefInfo::kAutoDetected);
420   EXPECT_EQ(scan_id, id);
421   Verify();
422   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
423                             MediaGalleryPrefInfo::kScanResult);
424   EXPECT_EQ(scan_id, id);
425   Verify();
426 
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));
436 
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);
451 
452   path = MakeMediaGalleriesTestingPath("other");
453   EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info));
454   EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id);
455 
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());
460 
461   // Remove an auto added gallery (i.e. make it blacklisted).
462   gallery_prefs()->ForgetGalleryById(auto_id);
463   expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
464   expected_galleries_for_all.erase(auto_id);
465   Verify();
466 
467   // Remove a scan result (i.e. make it blacklisted).
468   gallery_prefs()->ForgetGalleryById(scan_id);
469   expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
470   Verify();
471 
472   // Remove a user added gallery and it should go away.
473   gallery_prefs()->ForgetGalleryById(user_added_id);
474   expected_galleries_.erase(user_added_id);
475   expected_device_map[user_added_device_id].erase(user_added_id);
476   Verify();
477 }
478 
TEST_F(MediaGalleriesPreferencesTest,ForgetAndErase)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);
488 
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);
497 
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);
506 
507   Verify();
508   std::string device_id;
509 
510   gallery_prefs()->ForgetGalleryById(user_forget);
511   device_id = expected_galleries_[user_forget].device_id;
512   expected_galleries_.erase(user_forget);
513   expected_device_map[device_id].erase(user_forget);
514   Verify();
515 
516   gallery_prefs()->ForgetGalleryById(auto_forget);
517   expected_galleries_[auto_forget].type = MediaGalleryPrefInfo::kBlackListed;
518   expected_galleries_for_all.erase(auto_forget);
519   Verify();
520 
521   gallery_prefs()->ForgetGalleryById(scan_forget);
522   expected_galleries_[scan_forget].type = MediaGalleryPrefInfo::kRemovedScan;
523   Verify();
524 
525   gallery_prefs()->EraseGalleryById(user_erase);
526   device_id = expected_galleries_[user_erase].device_id;
527   expected_galleries_.erase(user_erase);
528   expected_device_map[device_id].erase(user_erase);
529   Verify();
530 
531   gallery_prefs()->EraseGalleryById(auto_erase);
532   device_id = expected_galleries_[auto_erase].device_id;
533   expected_galleries_.erase(auto_erase);
534   expected_device_map[device_id].erase(auto_erase);
535   expected_galleries_for_all.erase(auto_erase);
536   Verify();
537 
538   gallery_prefs()->EraseGalleryById(scan_erase);
539   device_id = expected_galleries_[scan_erase].device_id;
540   expected_galleries_.erase(scan_erase);
541   expected_device_map[device_id].erase(scan_erase);
542   Verify();
543 
544   // Also erase the previously forgetten ones to check erasing blacklisted ones.
545   gallery_prefs()->EraseGalleryById(auto_forget);
546   device_id = expected_galleries_[auto_forget].device_id;
547   expected_galleries_.erase(auto_forget);
548   expected_device_map[device_id].erase(auto_forget);
549   Verify();
550 
551   gallery_prefs()->EraseGalleryById(scan_forget);
552   device_id = expected_galleries_[scan_forget].device_id;
553   expected_galleries_.erase(scan_forget);
554   expected_device_map[device_id].erase(scan_forget);
555   Verify();
556 }
557 
TEST_F(MediaGalleriesPreferencesTest,AddGalleryWithVolumeMetadata)558 TEST_F(MediaGalleriesPreferencesTest, AddGalleryWithVolumeMetadata) {
559   MediaGalleryPrefId id;
560   StorageInfo info;
561   base::FilePath path;
562   base::FilePath relative_path;
563   base::Time now = base::Time::Now();
564   Verify();
565 
566   // Add a new auto detected gallery.
567   path = MakeMediaGalleriesTestingPath("new_auto");
568   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
569   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
570                                    MediaGalleryPrefInfo::kAutoDetected,
571                                    ASCIIToUTF16("volume label"),
572                                    ASCIIToUTF16("vendor name"),
573                                    ASCIIToUTF16("model name"),
574                                    1000000ULL, now, 0, 0, 0);
575   EXPECT_EQ(default_galleries_count() + 1UL, id);
576   AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
577                         MediaGalleryPrefInfo::kAutoDetected);
578   Verify();
579 
580   MediaGalleryPrefInfo gallery_info;
581   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
582       MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
583   EXPECT_TRUE(gallery_info.volume_metadata_valid);
584   EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info.volume_label);
585   EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info.vendor_name);
586   EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info.model_name);
587   EXPECT_EQ(1000000ULL, gallery_info.total_size_in_bytes);
588   // Note: we put the microseconds time into a double, so there'll
589   // be some possible rounding errors. If it's less than 100, we don't
590   // care.
591   EXPECT_LE(std::abs(now.ToInternalValue() -
592                      gallery_info.last_attach_time.ToInternalValue()),
593             100);
594 }
595 
TEST_F(MediaGalleriesPreferencesTest,ReplaceGalleryWithVolumeMetadata)596 TEST_F(MediaGalleriesPreferencesTest, ReplaceGalleryWithVolumeMetadata) {
597   MediaGalleryPrefId id, metadata_id;
598   base::FilePath path;
599   StorageInfo info;
600   base::FilePath relative_path;
601   base::Time now = base::Time::Now();
602   Verify();
603 
604   // Add an auto detected gallery in the prefs version 0 format.
605   path = MakeMediaGalleriesTestingPath("new_auto");
606   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
607   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
608   id = AddGalleryWithNameV0(info.device_id(), gallery_name, relative_path,
609                             false /*auto*/);
610   EXPECT_EQ(default_galleries_count() + 1UL, id);
611   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
612                         MediaGalleryPrefInfo::kAutoDetected);
613   Verify();
614 
615   metadata_id = gallery_prefs()->AddGallery(info.device_id(),
616                                             relative_path,
617                                             MediaGalleryPrefInfo::kAutoDetected,
618                                             ASCIIToUTF16("volume label"),
619                                             ASCIIToUTF16("vendor name"),
620                                             ASCIIToUTF16("model name"),
621                                             1000000ULL, now, 0, 0, 0);
622   EXPECT_EQ(id, metadata_id);
623   AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
624                         MediaGalleryPrefInfo::kAutoDetected);
625 
626   // Make sure the display_name is set to empty now, as the metadata
627   // upgrade should set the manual override name empty.
628   Verify();
629 }
630 
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.
TEST_F(MediaGalleriesPreferencesTest,AutoAddedBlackListing)634 TEST_F(MediaGalleriesPreferencesTest, AutoAddedBlackListing) {
635   MediaGalleryPrefId auto_id, id;
636   base::FilePath path;
637   StorageInfo info;
638   base::FilePath relative_path;
639   Verify();
640 
641   // Add a new auto detect gallery to test with.
642   path = MakeMediaGalleriesTestingPath("new_auto");
643   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
644   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
645   id = AddGalleryWithNameV1(info.device_id(), gallery_name,
646                             relative_path, false /*auto*/);
647   EXPECT_EQ(default_galleries_count() + 1UL, id);
648   auto_id = id;
649   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
650                         MediaGalleryPrefInfo::kAutoDetected);
651   Verify();
652 
653   // Remove an auto added gallery (i.e. make it blacklisted).
654   gallery_prefs()->ForgetGalleryById(auto_id);
655   expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
656   expected_galleries_for_all.erase(auto_id);
657   Verify();
658 
659   // Try adding the gallery again automatically and it should be a no-op.
660   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
661                             false /*auto*/);
662   EXPECT_EQ(auto_id, id);
663   Verify();
664 
665   // Add the gallery again as a user action.
666   id = gallery_prefs()->AddGalleryByPath(path,
667                                          MediaGalleryPrefInfo::kUserAdded);
668   EXPECT_EQ(auto_id, id);
669   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
670                         MediaGalleryPrefInfo::kAutoDetected);
671   Verify();
672 }
673 
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.
TEST_F(MediaGalleriesPreferencesTest,ScanResultBlackListing)677 TEST_F(MediaGalleriesPreferencesTest, ScanResultBlackListing) {
678   MediaGalleryPrefId scan_id, id;
679   base::FilePath path;
680   StorageInfo info;
681   base::FilePath relative_path;
682   Verify();
683 
684   // Add a new scan result gallery to test with.
685   path = MakeMediaGalleriesTestingPath("new_scan");
686   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
687   base::string16 gallery_name = base::ASCIIToUTF16("NewScanGallery");
688   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
689                             MediaGalleryPrefInfo::kScanResult);
690   EXPECT_EQ(default_galleries_count() + 1UL, id);
691   scan_id = id;
692   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
693                         MediaGalleryPrefInfo::kScanResult);
694   Verify();
695 
696   // Remove a scan result gallery (i.e. make it blacklisted).
697   gallery_prefs()->ForgetGalleryById(scan_id);
698   expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
699   expected_galleries_for_all.erase(scan_id);
700   Verify();
701 
702   // Try adding the gallery again as a scan result it should be a no-op.
703   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
704                             MediaGalleryPrefInfo::kScanResult);
705   EXPECT_EQ(scan_id, id);
706   Verify();
707 
708   // Add the gallery again as a user action.
709   id = gallery_prefs()->AddGalleryByPath(path,
710                                          MediaGalleryPrefInfo::kUserAdded);
711   EXPECT_EQ(scan_id, id);
712   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
713                         MediaGalleryPrefInfo::kUserAdded);
714   Verify();
715 }
716 
TEST_F(MediaGalleriesPreferencesTest,UpdateGalleryNameV2)717 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryNameV2) {
718   // Add a new auto detect gallery to test with.
719   base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
720   StorageInfo info;
721   base::FilePath relative_path;
722   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
723   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
724   MediaGalleryPrefId id =
725       AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
726                            MediaGalleryPrefInfo::kAutoDetected);
727   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
728                         MediaGalleryPrefInfo::kAutoDetected);
729   Verify();
730 
731   // Won't override the name -- don't change any expectation.
732   gallery_name = base::string16();
733   AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
734                        MediaGalleryPrefInfo::kAutoDetected);
735   Verify();
736 
737   gallery_name = base::ASCIIToUTF16("NewName");
738   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
739                             MediaGalleryPrefInfo::kAutoDetected);
740   // Note: will really just update the existing expectation.
741   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
742                         MediaGalleryPrefInfo::kAutoDetected);
743   Verify();
744 }
745 
TEST_F(MediaGalleriesPreferencesTest,GalleryPermissions)746 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
747   MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, scan_id,
748                      to_scan_remove_id, id;
749   base::FilePath path;
750   StorageInfo info;
751   base::FilePath relative_path;
752   Verify();
753 
754   // Add some galleries to test with.
755   path = MakeMediaGalleriesTestingPath("new_user");
756   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
757   base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
758   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
759                             true /*user*/);
760   EXPECT_EQ(default_galleries_count() + 1UL, id);
761   user_added_id = id;
762   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
763                         MediaGalleryPrefInfo::kUserAdded);
764   Verify();
765 
766   path = MakeMediaGalleriesTestingPath("new_auto");
767   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
768   gallery_name = base::ASCIIToUTF16("NewAutoGallery");
769   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
770                             false /*auto*/);
771   EXPECT_EQ(default_galleries_count() + 2UL, id);
772   auto_id = id;
773   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
774                         MediaGalleryPrefInfo::kAutoDetected);
775   Verify();
776 
777   path = MakeMediaGalleriesTestingPath("to_blacklist");
778   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
779   gallery_name = base::ASCIIToUTF16("ToBlacklistGallery");
780   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
781                             false /*auto*/);
782   EXPECT_EQ(default_galleries_count() + 3UL, id);
783   to_blacklist_id = id;
784   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
785                         MediaGalleryPrefInfo::kAutoDetected);
786   Verify();
787 
788   path = MakeMediaGalleriesTestingPath("new_scan");
789   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
790   gallery_name = base::ASCIIToUTF16("NewScanGallery");
791   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
792                             MediaGalleryPrefInfo::kScanResult);
793   EXPECT_EQ(default_galleries_count() + 4UL, id);
794   scan_id = id;
795   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
796                         MediaGalleryPrefInfo::kScanResult);
797   Verify();
798 
799   path = MakeMediaGalleriesTestingPath("to_scan_remove");
800   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
801   gallery_name = base::ASCIIToUTF16("ToScanRemoveGallery");
802   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
803                             MediaGalleryPrefInfo::kScanResult);
804   EXPECT_EQ(default_galleries_count() + 5UL, id);
805   to_scan_remove_id = id;
806   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
807                         MediaGalleryPrefInfo::kScanResult);
808   Verify();
809 
810   // Remove permission for all galleries from the all-permission extension.
811   gallery_prefs()->SetGalleryPermissionForExtension(
812       *all_permission_extension.get(), auto_id, false);
813   expected_galleries_for_all.erase(auto_id);
814   Verify();
815 
816   gallery_prefs()->SetGalleryPermissionForExtension(
817       *all_permission_extension.get(), user_added_id, false);
818   expected_galleries_for_all.erase(user_added_id);
819   Verify();
820 
821   gallery_prefs()->SetGalleryPermissionForExtension(
822       *all_permission_extension.get(), to_blacklist_id, false);
823   expected_galleries_for_all.erase(to_blacklist_id);
824   Verify();
825 
826   gallery_prefs()->SetGalleryPermissionForExtension(
827       *all_permission_extension.get(), scan_id, false);
828   expected_galleries_for_all.erase(scan_id);
829   Verify();
830 
831   gallery_prefs()->SetGalleryPermissionForExtension(
832       *all_permission_extension.get(), to_scan_remove_id, false);
833   expected_galleries_for_all.erase(to_scan_remove_id);
834   Verify();
835 
836   // Add permission back for all galleries to the all-permission extension.
837   gallery_prefs()->SetGalleryPermissionForExtension(
838       *all_permission_extension.get(), auto_id, true);
839   expected_galleries_for_all.insert(auto_id);
840   Verify();
841 
842   gallery_prefs()->SetGalleryPermissionForExtension(
843       *all_permission_extension.get(), user_added_id, true);
844   expected_galleries_for_all.insert(user_added_id);
845   Verify();
846 
847   gallery_prefs()->SetGalleryPermissionForExtension(
848       *all_permission_extension.get(), to_blacklist_id, true);
849   expected_galleries_for_all.insert(to_blacklist_id);
850   Verify();
851 
852   gallery_prefs()->SetGalleryPermissionForExtension(
853       *all_permission_extension.get(), scan_id, true);
854   expected_galleries_for_all.insert(scan_id);
855   Verify();
856 
857   gallery_prefs()->SetGalleryPermissionForExtension(
858       *all_permission_extension.get(), to_scan_remove_id, true);
859   expected_galleries_for_all.insert(to_scan_remove_id);
860   Verify();
861 
862   // Add permission for all galleries to the regular permission extension.
863   gallery_prefs()->SetGalleryPermissionForExtension(
864       *regular_permission_extension.get(), auto_id, true);
865   expected_galleries_for_regular.insert(auto_id);
866   Verify();
867 
868   gallery_prefs()->SetGalleryPermissionForExtension(
869       *regular_permission_extension.get(), user_added_id, true);
870   expected_galleries_for_regular.insert(user_added_id);
871   Verify();
872 
873   gallery_prefs()->SetGalleryPermissionForExtension(
874       *regular_permission_extension.get(), to_blacklist_id, true);
875   expected_galleries_for_regular.insert(to_blacklist_id);
876   Verify();
877 
878   gallery_prefs()->SetGalleryPermissionForExtension(
879       *regular_permission_extension.get(), scan_id, true);
880   expected_galleries_for_regular.insert(scan_id);
881   Verify();
882 
883   gallery_prefs()->SetGalleryPermissionForExtension(
884       *regular_permission_extension.get(), to_scan_remove_id, true);
885   expected_galleries_for_regular.insert(to_scan_remove_id);
886   Verify();
887 
888   // Blacklist the to be black listed gallery
889   gallery_prefs()->ForgetGalleryById(to_blacklist_id);
890   expected_galleries_[to_blacklist_id].type =
891       MediaGalleryPrefInfo::kBlackListed;
892   expected_galleries_for_all.erase(to_blacklist_id);
893   expected_galleries_for_regular.erase(to_blacklist_id);
894   Verify();
895 
896   gallery_prefs()->ForgetGalleryById(to_scan_remove_id);
897   expected_galleries_[to_scan_remove_id].type =
898       MediaGalleryPrefInfo::kRemovedScan;
899   expected_galleries_for_all.erase(to_scan_remove_id);
900   expected_galleries_for_regular.erase(to_scan_remove_id);
901   Verify();
902 
903   // Remove permission for all galleries to the regular permission extension.
904   gallery_prefs()->SetGalleryPermissionForExtension(
905       *regular_permission_extension.get(), auto_id, false);
906   expected_galleries_for_regular.erase(auto_id);
907   Verify();
908 
909   gallery_prefs()->SetGalleryPermissionForExtension(
910       *regular_permission_extension.get(), user_added_id, false);
911   expected_galleries_for_regular.erase(user_added_id);
912   Verify();
913 
914   gallery_prefs()->SetGalleryPermissionForExtension(
915       *regular_permission_extension.get(), scan_id, false);
916   expected_galleries_for_regular.erase(scan_id);
917   Verify();
918 
919   // Add permission for an invalid gallery id.
920   gallery_prefs()->SetGalleryPermissionForExtension(
921       *regular_permission_extension.get(), 9999L, true);
922   Verify();
923 }
924 
925 // What an existing gallery is added again, update the gallery information if
926 // needed.
TEST_F(MediaGalleriesPreferencesTest,UpdateGalleryDetails)927 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryDetails) {
928   MediaGalleryPrefId auto_id, id;
929   base::FilePath path;
930   StorageInfo info;
931   base::FilePath relative_path;
932   Verify();
933 
934   // Add a new auto detect gallery to test with.
935   path = MakeMediaGalleriesTestingPath("new_auto");
936   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
937   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
938   id = AddGalleryWithNameV1(info.device_id(), gallery_name,
939                             relative_path, false /*auto*/);
940   EXPECT_EQ(default_galleries_count() + 1UL, id);
941   auto_id = id;
942   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
943                         MediaGalleryPrefInfo::kAutoDetected);
944   Verify();
945 
946   // Update the device name and add the gallery again.
947   gallery_name = base::ASCIIToUTF16("AutoGallery2");
948   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
949                             false /*auto*/);
950   EXPECT_EQ(auto_id, id);
951   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
952                         MediaGalleryPrefInfo::kAutoDetected);
953   Verify();
954 }
955 
TEST_F(MediaGalleriesPreferencesTest,MultipleGalleriesPerDevices)956 TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) {
957   base::FilePath path;
958   StorageInfo info;
959   base::FilePath relative_path;
960   Verify();
961 
962   // Add a regular gallery
963   path = MakeMediaGalleriesTestingPath("new_user");
964   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
965   base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
966   MediaGalleryPrefId user_added_id =
967       AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
968                            true /*user*/);
969   EXPECT_EQ(default_galleries_count() + 1UL, user_added_id);
970   AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
971                         relative_path, MediaGalleryPrefInfo::kUserAdded);
972   Verify();
973 
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());
979 
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());
984 
985   // Add some galleries on the same device.
986   relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device1"));
987   gallery_name = base::ASCIIToUTF16("Device1Path1");
988   std::string device_id = "path:device1";
989   MediaGalleryPrefId dev1_path1_id = AddGalleryWithNameV1(
990       device_id, gallery_name, relative_path, true /*user*/);
991   EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id);
992   AddGalleryExpectation(dev1_path1_id, gallery_name, device_id, relative_path,
993                         MediaGalleryPrefInfo::kUserAdded);
994   Verify();
995 
996   relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device1"));
997   gallery_name = base::ASCIIToUTF16("Device1Path2");
998   MediaGalleryPrefId dev1_path2_id = AddGalleryWithNameV1(
999       device_id, gallery_name, relative_path, true /*user*/);
1000   EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id);
1001   AddGalleryExpectation(dev1_path2_id, gallery_name, device_id, relative_path,
1002                         MediaGalleryPrefInfo::kUserAdded);
1003   Verify();
1004 
1005   relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device2"));
1006   gallery_name = base::ASCIIToUTF16("Device2Path1");
1007   device_id = "path:device2";
1008   MediaGalleryPrefId dev2_path1_id = AddGalleryWithNameV1(
1009       device_id, gallery_name, relative_path, true /*user*/);
1010   EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id);
1011   AddGalleryExpectation(dev2_path1_id, gallery_name, device_id, relative_path,
1012                         MediaGalleryPrefInfo::kUserAdded);
1013   Verify();
1014 
1015   relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device2"));
1016   gallery_name = base::ASCIIToUTF16("Device2Path2");
1017   MediaGalleryPrefId dev2_path2_id = AddGalleryWithNameV1(
1018       device_id, gallery_name, relative_path, true /*user*/);
1019   EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id);
1020   AddGalleryExpectation(dev2_path2_id, gallery_name, device_id, relative_path,
1021                         MediaGalleryPrefInfo::kUserAdded);
1022   Verify();
1023 
1024   // Check that adding one of them again works as expected.
1025   MediaGalleryPrefId id = AddGalleryWithNameV1(
1026       device_id, gallery_name, relative_path, true /*user*/);
1027   EXPECT_EQ(dev2_path2_id, id);
1028   Verify();
1029 }
1030 
TEST_F(MediaGalleriesPreferencesTest,GalleryChangeObserver)1031 TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) {
1032   // Start with one observer.
1033   MockGalleryChangeObserver observer1(gallery_prefs());
1034   gallery_prefs()->AddGalleryChangeObserver(&observer1);
1035 
1036   // Add a new auto detected gallery.
1037   base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
1038   StorageInfo info;
1039   base::FilePath relative_path;
1040   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1041   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1042   MediaGalleryPrefId auto_id = AddGalleryWithNameV1(
1043       info.device_id(), gallery_name, relative_path, false /*auto*/);
1044   EXPECT_EQ(default_galleries_count() + 1UL, auto_id);
1045   AddGalleryExpectation(auto_id, gallery_name, info.device_id(),
1046                         relative_path, MediaGalleryPrefInfo::kAutoDetected);
1047   EXPECT_EQ(1, observer1.notifications());
1048 
1049   // Add a second observer.
1050   MockGalleryChangeObserver observer2(gallery_prefs());
1051   gallery_prefs()->AddGalleryChangeObserver(&observer2);
1052 
1053   // Add a new user added gallery.
1054   path = MakeMediaGalleriesTestingPath("new_user");
1055   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1056   gallery_name = base::ASCIIToUTF16("NewUserGallery");
1057   MediaGalleryPrefId user_added_id =
1058       AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
1059                            true /*user*/);
1060   AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
1061                         relative_path, MediaGalleryPrefInfo::kUserAdded);
1062   EXPECT_EQ(default_galleries_count() + 2UL, user_added_id);
1063   EXPECT_EQ(2, observer1.notifications());
1064   EXPECT_EQ(1, observer2.notifications());
1065 
1066   // Remove the first observer.
1067   gallery_prefs()->RemoveGalleryChangeObserver(&observer1);
1068 
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);
1073 
1074   EXPECT_EQ(2, observer1.notifications());
1075   EXPECT_EQ(2, observer2.notifications());
1076 
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);
1081 
1082   EXPECT_EQ(2, observer1.notifications());
1083   EXPECT_EQ(3, observer2.notifications());
1084 }
1085 
TEST_F(MediaGalleriesPreferencesTest,UpdateSingletonDeviceIdType)1086 TEST_F(MediaGalleriesPreferencesTest, UpdateSingletonDeviceIdType) {
1087   MediaGalleryPrefId id;
1088   base::FilePath path;
1089   Verify();
1090 
1091   // Add a new auto detect gallery to test with.
1092   path = MakeMediaGalleriesTestingPath("new_auto");
1093   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1094   std::string device_id = StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
1095                                                     path.AsUTF8Unsafe());
1096   id = AddGalleryWithNameV2(device_id, gallery_name, base::FilePath(),
1097                             MediaGalleryPrefInfo::kAutoDetected);
1098   EXPECT_EQ(default_galleries_count() + 1UL, id);
1099   AddGalleryExpectation(id, gallery_name, device_id, base::FilePath(),
1100                         MediaGalleryPrefInfo::kAutoDetected);
1101   Verify();
1102 
1103   // Update the device id.
1104   MockGalleryChangeObserver observer(gallery_prefs());
1105   gallery_prefs()->AddGalleryChangeObserver(&observer);
1106 
1107   path = MakeMediaGalleriesTestingPath("updated_path");
1108   std::string updated_device_id =
1109       StorageInfo::MakeDeviceId(StorageInfo::ITUNES, path.AsUTF8Unsafe());
1110   EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id));
1111   AddGalleryExpectation(id, gallery_name, updated_device_id, base::FilePath(),
1112                         MediaGalleryPrefInfo::kAutoDetected);
1113   expected_device_map[device_id].erase(id);
1114   expected_device_map[updated_device_id].insert(id);
1115   Verify();
1116   EXPECT_EQ(1, observer.notifications());
1117 
1118   // No gallery for type.
1119   std::string new_device_id =
1120       StorageInfo::MakeDeviceId(StorageInfo::PICASA, path.AsUTF8Unsafe());
1121   EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id));
1122 }
1123 
TEST_F(MediaGalleriesPreferencesTest,LookupImportedGalleryByPath)1124 TEST_F(MediaGalleriesPreferencesTest, LookupImportedGalleryByPath) {
1125   MediaGalleryPrefId id;
1126   base::FilePath path;
1127   Verify();
1128 
1129   // iTunes device path points to an XML file in the library directory.
1130   path = MakeMediaGalleriesTestingPath("new_auto").AppendASCII("library.xml");
1131   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1132   std::string device_id = StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
1133                                                     path.AsUTF8Unsafe());
1134   id = AddGalleryWithNameV2(device_id, gallery_name, base::FilePath(),
1135                             MediaGalleryPrefInfo::kAutoDetected);
1136   EXPECT_EQ(default_galleries_count() + 1UL, id);
1137   AddGalleryExpectation(id, gallery_name, device_id, base::FilePath(),
1138                         MediaGalleryPrefInfo::kAutoDetected);
1139   Verify();
1140 
1141   // Verify we can look up the imported gallery by its path.
1142   MediaGalleryPrefInfo gallery_info;
1143   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(path.DirName(),
1144                                                    &gallery_info));
1145   EXPECT_EQ(id, gallery_info.pref_id);
1146 }
1147 
TEST_F(MediaGalleriesPreferencesTest,ScanResults)1148 TEST_F(MediaGalleriesPreferencesTest, ScanResults) {
1149   MediaGalleryPrefId id;
1150   base::FilePath path;
1151   StorageInfo info;
1152   base::FilePath relative_path;
1153   base::Time now = base::Time::Now();
1154   Verify();
1155 
1156   // Add a new scan result gallery to test with.
1157   path = MakeMediaGalleriesTestingPath("new_scan");
1158   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1159   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1160                                    MediaGalleryPrefInfo::kScanResult,
1161                                    ASCIIToUTF16("volume label"),
1162                                    ASCIIToUTF16("vendor name"),
1163                                    ASCIIToUTF16("model name"),
1164                                    1000000ULL, now, 1, 2, 3);
1165   EXPECT_EQ(default_galleries_count() + 1UL, id);
1166   AddScanResultExpectation(id, base::string16(), info.device_id(),
1167                            relative_path, 1, 2, 3);
1168   Verify();
1169 
1170   // Update the found media count.
1171   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1172                                    MediaGalleryPrefInfo::kScanResult,
1173                                    ASCIIToUTF16("volume label"),
1174                                    ASCIIToUTF16("vendor name"),
1175                                    ASCIIToUTF16("model name"),
1176                                    1000000ULL, now, 4, 5, 6);
1177   EXPECT_EQ(default_galleries_count() + 1UL, id);
1178   AddScanResultExpectation(id, base::string16(), info.device_id(),
1179                            relative_path, 4, 5, 6);
1180   Verify();
1181 
1182   // Remove a scan result (i.e. make it blacklisted).
1183   gallery_prefs()->ForgetGalleryById(id);
1184   expected_galleries_[id].type = MediaGalleryPrefInfo::kRemovedScan;
1185   expected_galleries_[id].audio_count = 0;
1186   expected_galleries_[id].image_count = 0;
1187   expected_galleries_[id].video_count = 0;
1188   Verify();
1189 
1190   // Try adding the gallery again as a scan result it should be a no-op.
1191   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1192                                    MediaGalleryPrefInfo::kScanResult,
1193                                    ASCIIToUTF16("volume label"),
1194                                    ASCIIToUTF16("vendor name"),
1195                                    ASCIIToUTF16("model name"),
1196                                    1000000ULL, now, 7, 8, 9);
1197   EXPECT_EQ(default_galleries_count() + 1UL, id);
1198   Verify();
1199 
1200   // Add the gallery again as a user action.
1201   id = gallery_prefs()->AddGalleryByPath(path,
1202                                          MediaGalleryPrefInfo::kUserAdded);
1203   EXPECT_EQ(default_galleries_count() + 1UL, id);
1204   AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
1205                         MediaGalleryPrefInfo::kUserAdded);
1206   Verify();
1207 }
1208 
TEST(MediaGalleriesPrefInfoTest,NameGeneration)1209 TEST(MediaGalleriesPrefInfoTest, NameGeneration) {
1210   ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
1211 
1212   MediaGalleryPrefInfo info;
1213   info.pref_id = 1;
1214   info.display_name = ASCIIToUTF16("override");
1215   info.device_id = StorageInfo::MakeDeviceId(
1216       StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, "unique");
1217 
1218   EXPECT_EQ(ASCIIToUTF16("override"), info.GetGalleryDisplayName());
1219 
1220   info.display_name = ASCIIToUTF16("o2");
1221   EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1222 
1223   EXPECT_EQ(l10n_util::GetStringUTF16(
1224                 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1225             info.GetGalleryAdditionalDetails());
1226 
1227   info.last_attach_time = base::Time::Now();
1228   EXPECT_NE(l10n_util::GetStringUTF16(
1229                 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1230             info.GetGalleryAdditionalDetails());
1231   EXPECT_NE(l10n_util::GetStringUTF16(
1232                 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED),
1233             info.GetGalleryAdditionalDetails());
1234 
1235   info.volume_label = ASCIIToUTF16("vol");
1236   info.vendor_name = ASCIIToUTF16("vendor");
1237   info.model_name = ASCIIToUTF16("model");
1238   EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1239 
1240   info.display_name = base::string16();
1241   EXPECT_EQ(ASCIIToUTF16("vol"), info.GetGalleryDisplayName());
1242   info.volume_label = base::string16();
1243   EXPECT_EQ(ASCIIToUTF16("vendor, model"), info.GetGalleryDisplayName());
1244 
1245   info.device_id = StorageInfo::MakeDeviceId(
1246       StorageInfo::FIXED_MASS_STORAGE, "unique");
1247   EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(),
1248             base::UTF16ToUTF8(info.GetGalleryTooltip()));
1249 
1250   TestStorageMonitor::Destroy();
1251 }
1252