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 <stdint.h>
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "base/command_line.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/path_service.h"
19 #include "base/run_loop.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/values.h"
22 #include "build/build_config.h"
23 #include "chrome/browser/extensions/test_extension_system.h"
24 #include "chrome/browser/media_galleries/media_file_system_registry.h"
25 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
26 #include "chrome/common/apps/platform_apps/media_galleries_permission.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/pref_names.h"
29 #include "chrome/grit/generated_resources.h"
30 #include "chrome/test/base/testing_profile.h"
31 #include "components/prefs/scoped_user_pref_update.h"
32 #include "components/storage_monitor/media_storage_util.h"
33 #include "components/storage_monitor/storage_monitor.h"
34 #include "components/storage_monitor/test_storage_monitor.h"
35 #include "components/sync/model/string_ordinal.h"
36 #include "content/public/test/browser_task_environment.h"
37 #include "extensions/browser/extension_system.h"
38 #include "extensions/common/extension.h"
39 #include "extensions/common/manifest_handlers/background_info.h"
40 #include "testing/gtest/include/gtest/gtest.h"
41 #include "ui/base/l10n/l10n_util.h"
42 
43 #if defined(OS_CHROMEOS)
44 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
45 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
46 #endif
47 
48 using base::ASCIIToUTF16;
49 using storage_monitor::MediaStorageUtil;
50 using storage_monitor::StorageInfo;
51 using storage_monitor::TestStorageMonitor;
52 
53 namespace {
54 
55 class MockGalleryChangeObserver
56     : public MediaGalleriesPreferences::GalleryChangeObserver {
57  public:
MockGalleryChangeObserver(MediaGalleriesPreferences * pref)58   explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref)
59       : pref_(pref),
60         notifications_(0) {}
~MockGalleryChangeObserver()61   ~MockGalleryChangeObserver() override {}
62 
notifications() const63   int notifications() const { return notifications_;}
64 
65  private:
66   // MediaGalleriesPreferences::GalleryChangeObserver implementation.
OnPermissionAdded(MediaGalleriesPreferences * pref,const std::string & extension_id,MediaGalleryPrefId pref_id)67   void OnPermissionAdded(MediaGalleriesPreferences* pref,
68                          const std::string& extension_id,
69                          MediaGalleryPrefId pref_id) override {
70     EXPECT_EQ(pref_, pref);
71     ++notifications_;
72   }
73 
OnPermissionRemoved(MediaGalleriesPreferences * pref,const std::string & extension_id,MediaGalleryPrefId pref_id)74   void OnPermissionRemoved(MediaGalleriesPreferences* pref,
75                            const std::string& extension_id,
76                            MediaGalleryPrefId pref_id) override {
77     EXPECT_EQ(pref_, pref);
78     ++notifications_;
79   }
80 
OnGalleryAdded(MediaGalleriesPreferences * pref,MediaGalleryPrefId pref_id)81   void OnGalleryAdded(MediaGalleriesPreferences* pref,
82                       MediaGalleryPrefId pref_id) override {
83     EXPECT_EQ(pref_, pref);
84     ++notifications_;
85   }
86 
OnGalleryRemoved(MediaGalleriesPreferences * pref,MediaGalleryPrefId pref_id)87   void OnGalleryRemoved(MediaGalleriesPreferences* pref,
88                         MediaGalleryPrefId pref_id) override {
89     EXPECT_EQ(pref_, pref);
90     ++notifications_;
91   }
92 
OnGalleryInfoUpdated(MediaGalleriesPreferences * pref,MediaGalleryPrefId pref_id)93   void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
94                             MediaGalleryPrefId pref_id) override {
95     EXPECT_EQ(pref_, pref);
96     ++notifications_;
97   }
98 
99   MediaGalleriesPreferences* pref_;
100   int notifications_;
101 
102   DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver);
103 };
104 
105 }  // namespace
106 
107 class MediaGalleriesPreferencesTest : public testing::Test {
108  public:
109   typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
110       DeviceIdPrefIdsMap;
111 
MediaGalleriesPreferencesTest()112   MediaGalleriesPreferencesTest()
113       : profile_(new TestingProfile()), default_galleries_count_(0) {}
114 
~MediaGalleriesPreferencesTest()115   ~MediaGalleriesPreferencesTest() override {}
116 
SetUp()117   void SetUp() override {
118     ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
119 
120     extensions::TestExtensionSystem* extension_system(
121         static_cast<extensions::TestExtensionSystem*>(
122             extensions::ExtensionSystem::Get(profile_.get())));
123     extension_system->CreateExtensionService(
124         base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
125 
126     ReinitPrefsAndExpectations();
127 
128     const MediaGalleriesPrefInfoMap& known_galleries =
129         gallery_prefs_->known_galleries();
130     if (!known_galleries.empty()) {
131       ASSERT_EQ(3U, known_galleries.size());
132     }
133 
134     std::vector<std::string> all_permissions;
135     all_permissions.push_back(
136         chrome_apps::MediaGalleriesPermission::kReadPermission);
137     all_permissions.push_back(
138         chrome_apps::MediaGalleriesPermission::kAllAutoDetectedPermission);
139     std::vector<std::string> read_permissions;
140     read_permissions.push_back(
141         chrome_apps::MediaGalleriesPermission::kReadPermission);
142 
143     all_permission_extension =
144         AddMediaGalleriesApp("all", all_permissions, profile_.get());
145     regular_permission_extension =
146         AddMediaGalleriesApp("regular", read_permissions, profile_.get());
147     no_permissions_extension =
148         AddMediaGalleriesApp("no", read_permissions, profile_.get());
149   }
150 
TearDown()151   void TearDown() override {
152     Verify();
153     TestStorageMonitor::Destroy();
154   }
155 
ChangeMediaPathOverrides()156   void ChangeMediaPathOverrides() {
157     mock_gallery_locations_.ChangeMediaPathOverrides();
158   }
159 
ReinitPrefsAndExpectations()160   void ReinitPrefsAndExpectations() {
161     gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
162     base::RunLoop loop;
163     gallery_prefs_->EnsureInitialized(loop.QuitClosure());
164     loop.Run();
165 
166     // Load the default galleries into the expectations.
167     const MediaGalleriesPrefInfoMap& known_galleries =
168         gallery_prefs_->known_galleries();
169     if (!known_galleries.empty()) {
170       default_galleries_count_ = 3;
171       MediaGalleriesPrefInfoMap::const_iterator it;
172       for (it = known_galleries.begin(); it != known_galleries.end(); ++it) {
173         expected_galleries_[it->first] = it->second;
174         if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
175           expected_galleries_for_all.insert(it->first);
176       }
177     }
178   }
179 
RemovePersistedDefaultGalleryValues()180   void RemovePersistedDefaultGalleryValues() {
181     PrefService* prefs = profile_->GetPrefs();
182     std::unique_ptr<ListPrefUpdate> update(
183         new ListPrefUpdate(prefs, prefs::kMediaGalleriesRememberedGalleries));
184     base::ListValue* list = update->Get();
185 
186     for (auto iter = list->begin(); iter != list->end(); ++iter) {
187       base::DictionaryValue* dict;
188 
189       if (iter->GetAsDictionary(&dict)) {
190         // Setting the prefs version to 2 which is the version before
191         // default_gallery_type was added.
192         dict->SetInteger(kMediaGalleriesPrefsVersionKey, 2);
193         dict->Remove(kMediaGalleriesDefaultGalleryTypeKey, NULL);
194       }
195     }
196     update.reset();
197   }
198 
Verify()199   void Verify() {
200     const MediaGalleriesPrefInfoMap& known_galleries =
201         gallery_prefs_->known_galleries();
202     EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
203     for (auto it = known_galleries.begin(); it != known_galleries.end(); ++it) {
204       VerifyGalleryInfo(it->second, it->first);
205       if (it->second.type != MediaGalleryPrefInfo::kAutoDetected &&
206           it->second.type != MediaGalleryPrefInfo::kBlackListed) {
207         if (!base::Contains(expected_galleries_for_all, it->first) &&
208             !base::Contains(expected_galleries_for_regular, it->first)) {
209           EXPECT_FALSE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
210         } else {
211           EXPECT_TRUE(gallery_prefs_->NonAutoGalleryHasPermission(it->first));
212         }
213       }
214     }
215 
216     for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin();
217          it != expected_device_map.end();
218          ++it) {
219       MediaGalleryPrefIdSet actual_id_set =
220           gallery_prefs_->LookUpGalleriesByDeviceId(it->first);
221       EXPECT_EQ(it->second, actual_id_set);
222     }
223 
224     std::set<MediaGalleryPrefId> galleries_for_all =
225         gallery_prefs_->GalleriesForExtension(*all_permission_extension.get());
226     EXPECT_EQ(expected_galleries_for_all, galleries_for_all);
227 
228     std::set<MediaGalleryPrefId> galleries_for_regular =
229         gallery_prefs_->GalleriesForExtension(
230             *regular_permission_extension.get());
231     EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular);
232 
233     std::set<MediaGalleryPrefId> galleries_for_no =
234         gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get());
235     EXPECT_EQ(0U, galleries_for_no.size());
236   }
237 
VerifyGalleryInfo(const MediaGalleryPrefInfo & actual,MediaGalleryPrefId expected_id) const238   void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual,
239                          MediaGalleryPrefId expected_id) const {
240     auto in_expectation = expected_galleries_.find(expected_id);
241     ASSERT_FALSE(in_expectation == expected_galleries_.end())  << expected_id;
242     EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id);
243     EXPECT_EQ(in_expectation->second.display_name, actual.display_name);
244     EXPECT_EQ(in_expectation->second.device_id, actual.device_id);
245     EXPECT_EQ(in_expectation->second.path.value(), actual.path.value());
246     EXPECT_EQ(in_expectation->second.type, actual.type);
247     EXPECT_EQ(in_expectation->second.audio_count, actual.audio_count);
248     EXPECT_EQ(in_expectation->second.image_count, actual.image_count);
249     EXPECT_EQ(in_expectation->second.video_count, actual.video_count);
250     EXPECT_EQ(
251         in_expectation->second.default_gallery_type,
252         actual.default_gallery_type);
253   }
254 
gallery_prefs()255   MediaGalleriesPreferences* gallery_prefs() {
256     return gallery_prefs_.get();
257   }
258 
default_galleries_count()259   uint64_t default_galleries_count() { return default_galleries_count_; }
260 
AddGalleryExpectation(MediaGalleryPrefId id,base::string16 display_name,std::string device_id,base::FilePath relative_path,MediaGalleryPrefInfo::Type type)261   void AddGalleryExpectation(MediaGalleryPrefId id, base::string16 display_name,
262                              std::string device_id,
263                              base::FilePath relative_path,
264                              MediaGalleryPrefInfo::Type type) {
265     expected_galleries_[id].pref_id = id;
266     expected_galleries_[id].display_name = display_name;
267     expected_galleries_[id].device_id = device_id;
268     expected_galleries_[id].path = relative_path.NormalizePathSeparators();
269     expected_galleries_[id].type = type;
270 
271     if (type == MediaGalleryPrefInfo::kAutoDetected)
272       expected_galleries_for_all.insert(id);
273 
274     expected_device_map[device_id].insert(id);
275   }
276 
AddScanResultExpectation(MediaGalleryPrefId id,base::string16 display_name,std::string device_id,base::FilePath relative_path,int audio_count,int image_count,int video_count)277   void AddScanResultExpectation(MediaGalleryPrefId id,
278                                 base::string16 display_name,
279                                 std::string device_id,
280                                 base::FilePath relative_path,
281                                 int audio_count,
282                                 int image_count,
283                                 int video_count) {
284     AddGalleryExpectation(id, display_name, device_id, relative_path,
285                           MediaGalleryPrefInfo::kScanResult);
286     expected_galleries_[id].audio_count = audio_count;
287     expected_galleries_[id].image_count = image_count;
288     expected_galleries_[id].video_count = video_count;
289   }
290 
AddGalleryWithNameV0(const std::string & device_id,const base::string16 & display_name,const base::FilePath & relative_path,bool user_added)291   MediaGalleryPrefId AddGalleryWithNameV0(const std::string& device_id,
292                                           const base::string16& display_name,
293                                           const base::FilePath& relative_path,
294                                           bool user_added) {
295     MediaGalleryPrefInfo::Type type =
296         user_added ? MediaGalleryPrefInfo::kUserAdded
297                    : MediaGalleryPrefInfo::kAutoDetected;
298     return gallery_prefs()->AddOrUpdateGalleryInternal(
299         device_id, display_name, relative_path, type,
300         base::string16(), base::string16(), base::string16(), 0, base::Time(),
301         false, 0, 0, 0, 0, MediaGalleryPrefInfo::kNotDefault);
302   }
303 
AddGalleryWithNameV1(const std::string & device_id,const base::string16 & display_name,const base::FilePath & relative_path,bool user_added)304   MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id,
305                                           const base::string16& display_name,
306                                           const base::FilePath& relative_path,
307                                           bool user_added) {
308     MediaGalleryPrefInfo::Type type =
309         user_added ? MediaGalleryPrefInfo::kUserAdded
310                    : MediaGalleryPrefInfo::kAutoDetected;
311     return gallery_prefs()->AddOrUpdateGalleryInternal(
312         device_id, display_name, relative_path, type,
313         base::string16(), base::string16(), base::string16(), 0, base::Time(),
314         false, 0, 0, 0, 1, MediaGalleryPrefInfo::kNotDefault);
315   }
316 
AddGalleryWithNameV2(const std::string & device_id,const base::string16 & display_name,const base::FilePath & relative_path,MediaGalleryPrefInfo::Type type)317   MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id,
318                                           const base::string16& display_name,
319                                           const base::FilePath& relative_path,
320                                           MediaGalleryPrefInfo::Type type) {
321     return gallery_prefs()->AddOrUpdateGalleryInternal(
322         device_id, display_name, relative_path, type,
323         base::string16(), base::string16(), base::string16(), 0, base::Time(),
324         false, 0, 0, 0, 2, MediaGalleryPrefInfo::kNotDefault);
325   }
326 
AddFixedGalleryWithExpectation(const std::string & path_name,const std::string & name,MediaGalleryPrefInfo::Type type)327   MediaGalleryPrefId AddFixedGalleryWithExpectation(
328       const std::string& path_name,
329       const std::string& name,
330       MediaGalleryPrefInfo::Type type) {
331     base::FilePath path = MakeMediaGalleriesTestingPath(path_name);
332     StorageInfo info;
333     base::FilePath relative_path;
334     MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
335     base::string16 gallery_name = base::ASCIIToUTF16(name);
336     MediaGalleryPrefId id = AddGalleryWithNameV2(info.device_id(), gallery_name,
337                                                 relative_path, type);
338     AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
339                           type);
340     Verify();
341     return id;
342   }
343 
344   scoped_refptr<extensions::Extension> all_permission_extension;
345   scoped_refptr<extensions::Extension> regular_permission_extension;
346   scoped_refptr<extensions::Extension> no_permissions_extension;
347 
348   std::set<MediaGalleryPrefId> expected_galleries_for_all;
349   std::set<MediaGalleryPrefId> expected_galleries_for_regular;
350 
351   DeviceIdPrefIdsMap expected_device_map;
352 
353   MediaGalleriesPrefInfoMap expected_galleries_;
354 
355  private:
356   // Needed for extension service & friends to work.
357   content::BrowserTaskEnvironment task_environment_;
358 
359   EnsureMediaDirectoriesExists mock_gallery_locations_;
360 
361 #if defined(OS_CHROMEOS)
362   chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
363   chromeos::ScopedTestUserManager test_user_manager_;
364 #endif
365 
366   TestStorageMonitor monitor_;
367   std::unique_ptr<TestingProfile> profile_;
368   std::unique_ptr<MediaGalleriesPreferences> gallery_prefs_;
369 
370   uint64_t default_galleries_count_;
371 
372   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest);
373 };
374 
TEST_F(MediaGalleriesPreferencesTest,GalleryManagement)375 TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) {
376   MediaGalleryPrefId auto_id, user_added_id, scan_id, id;
377   base::FilePath path;
378   base::FilePath relative_path;
379   Verify();
380 
381   // Add a new auto detected gallery.
382   path = MakeMediaGalleriesTestingPath("new_auto");
383   StorageInfo info;
384   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
385   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
386   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
387                             MediaGalleryPrefInfo::kAutoDetected);
388   EXPECT_EQ(default_galleries_count() + 1UL, id);
389   auto_id = id;
390   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
391                         MediaGalleryPrefInfo::kAutoDetected);
392   Verify();
393 
394   // Add it as other types, nothing should happen.
395   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
396                             MediaGalleryPrefInfo::kUserAdded);
397   EXPECT_EQ(auto_id, id);
398   Verify();
399   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
400                             MediaGalleryPrefInfo::kAutoDetected);
401   EXPECT_EQ(auto_id, id);
402   Verify();
403   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
404                             MediaGalleryPrefInfo::kScanResult);
405   EXPECT_EQ(auto_id, id);
406 
407   // Add a new user added gallery.
408   path = MakeMediaGalleriesTestingPath("new_user");
409   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
410   gallery_name = base::ASCIIToUTF16("NewUserGallery");
411   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
412                             MediaGalleryPrefInfo::kUserAdded);
413   EXPECT_EQ(default_galleries_count() + 2UL, id);
414   user_added_id = id;
415   const std::string user_added_device_id = info.device_id();
416   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
417                         MediaGalleryPrefInfo::kUserAdded);
418   Verify();
419 
420   // Add it as other types, nothing should happen.
421   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
422                             MediaGalleryPrefInfo::kUserAdded);
423   EXPECT_EQ(user_added_id, id);
424   Verify();
425   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
426                             MediaGalleryPrefInfo::kAutoDetected);
427   EXPECT_EQ(user_added_id, id);
428   Verify();
429   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
430                             MediaGalleryPrefInfo::kScanResult);
431   EXPECT_EQ(user_added_id, id);
432   Verify();
433 
434   // Add a new scan result gallery.
435   path = MakeMediaGalleriesTestingPath("new_scan");
436   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
437   gallery_name = base::ASCIIToUTF16("NewScanGallery");
438   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
439                             MediaGalleryPrefInfo::kScanResult);
440   EXPECT_EQ(default_galleries_count() + 3UL, id);
441   scan_id = id;
442   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
443                         MediaGalleryPrefInfo::kScanResult);
444   Verify();
445 
446   // Add it as other types, nothing should happen.
447   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
448                             MediaGalleryPrefInfo::kUserAdded);
449   EXPECT_EQ(scan_id, id);
450   Verify();
451   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
452                             MediaGalleryPrefInfo::kAutoDetected);
453   EXPECT_EQ(scan_id, id);
454   Verify();
455   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
456                             MediaGalleryPrefInfo::kScanResult);
457   EXPECT_EQ(scan_id, id);
458   Verify();
459 
460   // Lookup some galleries.
461   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
462       MakeMediaGalleriesTestingPath("new_auto"), NULL));
463   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
464       MakeMediaGalleriesTestingPath("new_user"), NULL));
465   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
466       MakeMediaGalleriesTestingPath("new_scan"), NULL));
467   EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(
468       MakeMediaGalleriesTestingPath("other"), NULL));
469 
470   // Check that we always get the gallery info.
471   MediaGalleryPrefInfo gallery_info;
472   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
473       MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
474   VerifyGalleryInfo(gallery_info, auto_id);
475   EXPECT_FALSE(gallery_info.volume_metadata_valid);
476   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
477       MakeMediaGalleriesTestingPath("new_user"), &gallery_info));
478   VerifyGalleryInfo(gallery_info, user_added_id);
479   EXPECT_FALSE(gallery_info.volume_metadata_valid);
480   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
481       MakeMediaGalleriesTestingPath("new_scan"), &gallery_info));
482   VerifyGalleryInfo(gallery_info, scan_id);
483   EXPECT_FALSE(gallery_info.volume_metadata_valid);
484 
485   path = MakeMediaGalleriesTestingPath("other");
486   EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info));
487   EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id);
488 
489   StorageInfo other_info;
490   MediaStorageUtil::GetDeviceInfoFromPath(path, &other_info, &relative_path);
491   EXPECT_EQ(other_info.device_id(), gallery_info.device_id);
492   EXPECT_EQ(relative_path.value(), gallery_info.path.value());
493 
494   // Remove an auto added gallery (i.e. make it blacklisted).
495   gallery_prefs()->ForgetGalleryById(auto_id);
496   expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
497   expected_galleries_for_all.erase(auto_id);
498   Verify();
499 
500   // Remove a scan result (i.e. make it blacklisted).
501   gallery_prefs()->ForgetGalleryById(scan_id);
502   expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
503   Verify();
504 
505   // Remove a user added gallery and it should go away.
506   gallery_prefs()->ForgetGalleryById(user_added_id);
507   expected_galleries_.erase(user_added_id);
508   expected_device_map[user_added_device_id].erase(user_added_id);
509   Verify();
510 }
511 
TEST_F(MediaGalleriesPreferencesTest,ForgetAndErase)512 TEST_F(MediaGalleriesPreferencesTest, ForgetAndErase) {
513   MediaGalleryPrefId user_erase = AddFixedGalleryWithExpectation(
514       "user_erase", "UserErase", MediaGalleryPrefInfo::kUserAdded);
515   EXPECT_EQ(default_galleries_count() + 1UL, user_erase);
516   MediaGalleryPrefId user_forget = AddFixedGalleryWithExpectation(
517       "user_forget", "UserForget", MediaGalleryPrefInfo::kUserAdded);
518   EXPECT_EQ(default_galleries_count() + 2UL, user_forget);
519 
520   MediaGalleryPrefId auto_erase = AddFixedGalleryWithExpectation(
521       "auto_erase", "AutoErase", MediaGalleryPrefInfo::kAutoDetected);
522   EXPECT_EQ(default_galleries_count() + 3UL, auto_erase);
523   MediaGalleryPrefId auto_forget = AddFixedGalleryWithExpectation(
524       "auto_forget", "AutoForget", MediaGalleryPrefInfo::kAutoDetected);
525   EXPECT_EQ(default_galleries_count() + 4UL, auto_forget);
526 
527   MediaGalleryPrefId scan_erase = AddFixedGalleryWithExpectation(
528       "scan_erase", "ScanErase", MediaGalleryPrefInfo::kScanResult);
529   EXPECT_EQ(default_galleries_count() + 5UL, scan_erase);
530   MediaGalleryPrefId scan_forget = AddFixedGalleryWithExpectation(
531       "scan_forget", "ScanForget", MediaGalleryPrefInfo::kScanResult);
532   EXPECT_EQ(default_galleries_count() + 6UL, scan_forget);
533 
534   Verify();
535   std::string device_id;
536 
537   gallery_prefs()->ForgetGalleryById(user_forget);
538   device_id = expected_galleries_[user_forget].device_id;
539   expected_galleries_.erase(user_forget);
540   expected_device_map[device_id].erase(user_forget);
541   Verify();
542 
543   gallery_prefs()->ForgetGalleryById(auto_forget);
544   expected_galleries_[auto_forget].type = MediaGalleryPrefInfo::kBlackListed;
545   expected_galleries_for_all.erase(auto_forget);
546   Verify();
547 
548   gallery_prefs()->ForgetGalleryById(scan_forget);
549   expected_galleries_[scan_forget].type = MediaGalleryPrefInfo::kRemovedScan;
550   Verify();
551 
552   gallery_prefs()->EraseGalleryById(user_erase);
553   device_id = expected_galleries_[user_erase].device_id;
554   expected_galleries_.erase(user_erase);
555   expected_device_map[device_id].erase(user_erase);
556   Verify();
557 
558   gallery_prefs()->EraseGalleryById(auto_erase);
559   device_id = expected_galleries_[auto_erase].device_id;
560   expected_galleries_.erase(auto_erase);
561   expected_device_map[device_id].erase(auto_erase);
562   expected_galleries_for_all.erase(auto_erase);
563   Verify();
564 
565   gallery_prefs()->EraseGalleryById(scan_erase);
566   device_id = expected_galleries_[scan_erase].device_id;
567   expected_galleries_.erase(scan_erase);
568   expected_device_map[device_id].erase(scan_erase);
569   Verify();
570 
571   // Also erase the previously forgetten ones to check erasing blacklisted ones.
572   gallery_prefs()->EraseGalleryById(auto_forget);
573   device_id = expected_galleries_[auto_forget].device_id;
574   expected_galleries_.erase(auto_forget);
575   expected_device_map[device_id].erase(auto_forget);
576   Verify();
577 
578   gallery_prefs()->EraseGalleryById(scan_forget);
579   device_id = expected_galleries_[scan_forget].device_id;
580   expected_galleries_.erase(scan_forget);
581   expected_device_map[device_id].erase(scan_forget);
582   Verify();
583 }
584 
TEST_F(MediaGalleriesPreferencesTest,AddGalleryWithVolumeMetadata)585 TEST_F(MediaGalleriesPreferencesTest, AddGalleryWithVolumeMetadata) {
586   MediaGalleryPrefId id;
587   StorageInfo info;
588   base::FilePath path;
589   base::FilePath relative_path;
590   base::Time now = base::Time::Now();
591   Verify();
592 
593   // Add a new auto detected gallery.
594   path = MakeMediaGalleriesTestingPath("new_auto");
595   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
596   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
597                                    MediaGalleryPrefInfo::kAutoDetected,
598                                    ASCIIToUTF16("volume label"),
599                                    ASCIIToUTF16("vendor name"),
600                                    ASCIIToUTF16("model name"),
601                                    1000000ULL, now, 0, 0, 0);
602   EXPECT_EQ(default_galleries_count() + 1UL, id);
603   AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
604                         MediaGalleryPrefInfo::kAutoDetected);
605   Verify();
606 
607   MediaGalleryPrefInfo gallery_info;
608   EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
609       MakeMediaGalleriesTestingPath("new_auto"), &gallery_info));
610   EXPECT_TRUE(gallery_info.volume_metadata_valid);
611   EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info.volume_label);
612   EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info.vendor_name);
613   EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info.model_name);
614   EXPECT_EQ(1000000ULL, gallery_info.total_size_in_bytes);
615   // Note: we put the microseconds time into a double, so there'll
616   // be some possible rounding errors. If it's less than 100, we don't
617   // care.
618   EXPECT_LE(std::abs(now.ToInternalValue() -
619                      gallery_info.last_attach_time.ToInternalValue()),
620             100);
621 }
622 
TEST_F(MediaGalleriesPreferencesTest,ReplaceGalleryWithVolumeMetadata)623 TEST_F(MediaGalleriesPreferencesTest, ReplaceGalleryWithVolumeMetadata) {
624   MediaGalleryPrefId id, metadata_id;
625   base::FilePath path;
626   StorageInfo info;
627   base::FilePath relative_path;
628   base::Time now = base::Time::Now();
629   Verify();
630 
631   // Add an auto detected gallery in the prefs version 0 format.
632   path = MakeMediaGalleriesTestingPath("new_auto");
633   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
634   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
635   id = AddGalleryWithNameV0(info.device_id(), gallery_name, relative_path,
636                             false /*auto*/);
637   EXPECT_EQ(default_galleries_count() + 1UL, id);
638   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
639                         MediaGalleryPrefInfo::kAutoDetected);
640   Verify();
641 
642   metadata_id = gallery_prefs()->AddGallery(info.device_id(),
643                                             relative_path,
644                                             MediaGalleryPrefInfo::kAutoDetected,
645                                             ASCIIToUTF16("volume label"),
646                                             ASCIIToUTF16("vendor name"),
647                                             ASCIIToUTF16("model name"),
648                                             1000000ULL, now, 0, 0, 0);
649   EXPECT_EQ(id, metadata_id);
650   AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
651                         MediaGalleryPrefInfo::kAutoDetected);
652 
653   // Make sure the display_name is set to empty now, as the metadata
654   // upgrade should set the manual override name empty.
655   Verify();
656 }
657 
658 // Whenever an "AutoDetected" gallery is removed, it is moved to a black listed
659 // state.  When the gallery is added again, the black listed state is updated
660 // back to the "AutoDetected" type.
TEST_F(MediaGalleriesPreferencesTest,AutoAddedBlackListing)661 TEST_F(MediaGalleriesPreferencesTest, AutoAddedBlackListing) {
662   MediaGalleryPrefId auto_id, id;
663   base::FilePath path;
664   StorageInfo info;
665   base::FilePath relative_path;
666   Verify();
667 
668   // Add a new auto detect gallery to test with.
669   path = MakeMediaGalleriesTestingPath("new_auto");
670   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
671   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
672   id = AddGalleryWithNameV1(info.device_id(), gallery_name,
673                             relative_path, false /*auto*/);
674   EXPECT_EQ(default_galleries_count() + 1UL, id);
675   auto_id = id;
676   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
677                         MediaGalleryPrefInfo::kAutoDetected);
678   Verify();
679 
680   // Remove an auto added gallery (i.e. make it blacklisted).
681   gallery_prefs()->ForgetGalleryById(auto_id);
682   expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
683   expected_galleries_for_all.erase(auto_id);
684   Verify();
685 
686   // Try adding the gallery again automatically and it should be a no-op.
687   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
688                             false /*auto*/);
689   EXPECT_EQ(auto_id, id);
690   Verify();
691 
692   // Add the gallery again as a user action.
693   id = gallery_prefs()->AddGalleryByPath(path,
694                                          MediaGalleryPrefInfo::kUserAdded);
695   EXPECT_EQ(auto_id, id);
696   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
697                         MediaGalleryPrefInfo::kAutoDetected);
698   Verify();
699 }
700 
701 // Whenever a "ScanResult" gallery is removed, it is moved to a black listed
702 // state.  When the gallery is added again, the black listed state is updated
703 // back to the "ScanResult" type.
TEST_F(MediaGalleriesPreferencesTest,ScanResultBlackListing)704 TEST_F(MediaGalleriesPreferencesTest, ScanResultBlackListing) {
705   MediaGalleryPrefId scan_id, id;
706   base::FilePath path;
707   StorageInfo info;
708   base::FilePath relative_path;
709   Verify();
710 
711   // Add a new scan result gallery to test with.
712   path = MakeMediaGalleriesTestingPath("new_scan");
713   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
714   base::string16 gallery_name = base::ASCIIToUTF16("NewScanGallery");
715   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
716                             MediaGalleryPrefInfo::kScanResult);
717   EXPECT_EQ(default_galleries_count() + 1UL, id);
718   scan_id = id;
719   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
720                         MediaGalleryPrefInfo::kScanResult);
721   Verify();
722 
723   // Remove a scan result gallery (i.e. make it blacklisted).
724   gallery_prefs()->ForgetGalleryById(scan_id);
725   expected_galleries_[scan_id].type = MediaGalleryPrefInfo::kRemovedScan;
726   expected_galleries_for_all.erase(scan_id);
727   Verify();
728 
729   // Try adding the gallery again as a scan result it should be a no-op.
730   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
731                             MediaGalleryPrefInfo::kScanResult);
732   EXPECT_EQ(scan_id, id);
733   Verify();
734 
735   // Add the gallery again as a user action.
736   id = gallery_prefs()->AddGalleryByPath(path,
737                                          MediaGalleryPrefInfo::kUserAdded);
738   EXPECT_EQ(scan_id, id);
739   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
740                         MediaGalleryPrefInfo::kUserAdded);
741   Verify();
742 }
743 
TEST_F(MediaGalleriesPreferencesTest,UpdateGalleryNameV2)744 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryNameV2) {
745   // Add a new auto detect gallery to test with.
746   base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
747   StorageInfo info;
748   base::FilePath relative_path;
749   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
750   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
751   MediaGalleryPrefId id =
752       AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
753                            MediaGalleryPrefInfo::kAutoDetected);
754   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
755                         MediaGalleryPrefInfo::kAutoDetected);
756   Verify();
757 
758   // Won't override the name -- don't change any expectation.
759   gallery_name = base::string16();
760   AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
761                        MediaGalleryPrefInfo::kAutoDetected);
762   Verify();
763 
764   gallery_name = base::ASCIIToUTF16("NewName");
765   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
766                             MediaGalleryPrefInfo::kAutoDetected);
767   // Note: will really just update the existing expectation.
768   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
769                         MediaGalleryPrefInfo::kAutoDetected);
770   Verify();
771 }
772 
TEST_F(MediaGalleriesPreferencesTest,GalleryPermissions)773 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
774   MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, scan_id,
775                      to_scan_remove_id, id;
776   base::FilePath path;
777   StorageInfo info;
778   base::FilePath relative_path;
779   Verify();
780 
781   // Add some galleries to test with.
782   path = MakeMediaGalleriesTestingPath("new_user");
783   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
784   base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
785   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
786                             true /*user*/);
787   EXPECT_EQ(default_galleries_count() + 1UL, id);
788   user_added_id = id;
789   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
790                         MediaGalleryPrefInfo::kUserAdded);
791   Verify();
792 
793   path = MakeMediaGalleriesTestingPath("new_auto");
794   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
795   gallery_name = base::ASCIIToUTF16("NewAutoGallery");
796   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
797                             false /*auto*/);
798   EXPECT_EQ(default_galleries_count() + 2UL, id);
799   auto_id = id;
800   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
801                         MediaGalleryPrefInfo::kAutoDetected);
802   Verify();
803 
804   path = MakeMediaGalleriesTestingPath("to_blacklist");
805   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
806   gallery_name = base::ASCIIToUTF16("ToBlacklistGallery");
807   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
808                             false /*auto*/);
809   EXPECT_EQ(default_galleries_count() + 3UL, id);
810   to_blacklist_id = id;
811   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
812                         MediaGalleryPrefInfo::kAutoDetected);
813   Verify();
814 
815   path = MakeMediaGalleriesTestingPath("new_scan");
816   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
817   gallery_name = base::ASCIIToUTF16("NewScanGallery");
818   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
819                             MediaGalleryPrefInfo::kScanResult);
820   EXPECT_EQ(default_galleries_count() + 4UL, id);
821   scan_id = id;
822   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
823                         MediaGalleryPrefInfo::kScanResult);
824   Verify();
825 
826   path = MakeMediaGalleriesTestingPath("to_scan_remove");
827   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
828   gallery_name = base::ASCIIToUTF16("ToScanRemoveGallery");
829   id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
830                             MediaGalleryPrefInfo::kScanResult);
831   EXPECT_EQ(default_galleries_count() + 5UL, id);
832   to_scan_remove_id = id;
833   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
834                         MediaGalleryPrefInfo::kScanResult);
835   Verify();
836 
837   // Remove permission for all galleries from the all-permission extension.
838   gallery_prefs()->SetGalleryPermissionForExtension(
839       *all_permission_extension.get(), auto_id, false);
840   expected_galleries_for_all.erase(auto_id);
841   Verify();
842 
843   gallery_prefs()->SetGalleryPermissionForExtension(
844       *all_permission_extension.get(), user_added_id, false);
845   expected_galleries_for_all.erase(user_added_id);
846   Verify();
847 
848   gallery_prefs()->SetGalleryPermissionForExtension(
849       *all_permission_extension.get(), to_blacklist_id, false);
850   expected_galleries_for_all.erase(to_blacklist_id);
851   Verify();
852 
853   gallery_prefs()->SetGalleryPermissionForExtension(
854       *all_permission_extension.get(), scan_id, false);
855   expected_galleries_for_all.erase(scan_id);
856   Verify();
857 
858   gallery_prefs()->SetGalleryPermissionForExtension(
859       *all_permission_extension.get(), to_scan_remove_id, false);
860   expected_galleries_for_all.erase(to_scan_remove_id);
861   Verify();
862 
863   // Add permission back for all galleries to the all-permission extension.
864   gallery_prefs()->SetGalleryPermissionForExtension(
865       *all_permission_extension.get(), auto_id, true);
866   expected_galleries_for_all.insert(auto_id);
867   Verify();
868 
869   gallery_prefs()->SetGalleryPermissionForExtension(
870       *all_permission_extension.get(), user_added_id, true);
871   expected_galleries_for_all.insert(user_added_id);
872   Verify();
873 
874   gallery_prefs()->SetGalleryPermissionForExtension(
875       *all_permission_extension.get(), to_blacklist_id, true);
876   expected_galleries_for_all.insert(to_blacklist_id);
877   Verify();
878 
879   gallery_prefs()->SetGalleryPermissionForExtension(
880       *all_permission_extension.get(), scan_id, true);
881   expected_galleries_for_all.insert(scan_id);
882   Verify();
883 
884   gallery_prefs()->SetGalleryPermissionForExtension(
885       *all_permission_extension.get(), to_scan_remove_id, true);
886   expected_galleries_for_all.insert(to_scan_remove_id);
887   Verify();
888 
889   // Add permission for all galleries to the regular permission extension.
890   gallery_prefs()->SetGalleryPermissionForExtension(
891       *regular_permission_extension.get(), auto_id, true);
892   expected_galleries_for_regular.insert(auto_id);
893   Verify();
894 
895   gallery_prefs()->SetGalleryPermissionForExtension(
896       *regular_permission_extension.get(), user_added_id, true);
897   expected_galleries_for_regular.insert(user_added_id);
898   Verify();
899 
900   gallery_prefs()->SetGalleryPermissionForExtension(
901       *regular_permission_extension.get(), to_blacklist_id, true);
902   expected_galleries_for_regular.insert(to_blacklist_id);
903   Verify();
904 
905   gallery_prefs()->SetGalleryPermissionForExtension(
906       *regular_permission_extension.get(), scan_id, true);
907   expected_galleries_for_regular.insert(scan_id);
908   Verify();
909 
910   gallery_prefs()->SetGalleryPermissionForExtension(
911       *regular_permission_extension.get(), to_scan_remove_id, true);
912   expected_galleries_for_regular.insert(to_scan_remove_id);
913   Verify();
914 
915   // Blacklist the to be black listed gallery
916   gallery_prefs()->ForgetGalleryById(to_blacklist_id);
917   expected_galleries_[to_blacklist_id].type =
918       MediaGalleryPrefInfo::kBlackListed;
919   expected_galleries_for_all.erase(to_blacklist_id);
920   expected_galleries_for_regular.erase(to_blacklist_id);
921   Verify();
922 
923   gallery_prefs()->ForgetGalleryById(to_scan_remove_id);
924   expected_galleries_[to_scan_remove_id].type =
925       MediaGalleryPrefInfo::kRemovedScan;
926   expected_galleries_for_all.erase(to_scan_remove_id);
927   expected_galleries_for_regular.erase(to_scan_remove_id);
928   Verify();
929 
930   // Remove permission for all galleries to the regular permission extension.
931   gallery_prefs()->SetGalleryPermissionForExtension(
932       *regular_permission_extension.get(), auto_id, false);
933   expected_galleries_for_regular.erase(auto_id);
934   Verify();
935 
936   gallery_prefs()->SetGalleryPermissionForExtension(
937       *regular_permission_extension.get(), user_added_id, false);
938   expected_galleries_for_regular.erase(user_added_id);
939   Verify();
940 
941   gallery_prefs()->SetGalleryPermissionForExtension(
942       *regular_permission_extension.get(), scan_id, false);
943   expected_galleries_for_regular.erase(scan_id);
944   Verify();
945 
946   // Add permission for an invalid gallery id.
947   gallery_prefs()->SetGalleryPermissionForExtension(
948       *regular_permission_extension.get(), 9999L, true);
949   Verify();
950 }
951 
952 // What an existing gallery is added again, update the gallery information if
953 // needed.
TEST_F(MediaGalleriesPreferencesTest,UpdateGalleryDetails)954 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryDetails) {
955   MediaGalleryPrefId auto_id, id;
956   base::FilePath path;
957   StorageInfo info;
958   base::FilePath relative_path;
959   Verify();
960 
961   // Add a new auto detect gallery to test with.
962   path = MakeMediaGalleriesTestingPath("new_auto");
963   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
964   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
965   id = AddGalleryWithNameV1(info.device_id(), gallery_name,
966                             relative_path, false /*auto*/);
967   EXPECT_EQ(default_galleries_count() + 1UL, id);
968   auto_id = id;
969   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
970                         MediaGalleryPrefInfo::kAutoDetected);
971   Verify();
972 
973   // Update the device name and add the gallery again.
974   gallery_name = base::ASCIIToUTF16("AutoGallery2");
975   id = AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
976                             false /*auto*/);
977   EXPECT_EQ(auto_id, id);
978   AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
979                         MediaGalleryPrefInfo::kAutoDetected);
980   Verify();
981 }
982 
TEST_F(MediaGalleriesPreferencesTest,MultipleGalleriesPerDevices)983 TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) {
984   base::FilePath path;
985   StorageInfo info;
986   base::FilePath relative_path;
987   Verify();
988 
989   // Add a regular gallery
990   path = MakeMediaGalleriesTestingPath("new_user");
991   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
992   base::string16 gallery_name = base::ASCIIToUTF16("NewUserGallery");
993   MediaGalleryPrefId user_added_id =
994       AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
995                            true /*user*/);
996   EXPECT_EQ(default_galleries_count() + 1UL, user_added_id);
997   AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
998                         relative_path, MediaGalleryPrefInfo::kUserAdded);
999   Verify();
1000 
1001   // Find it by device id and fail to find something related.
1002   MediaGalleryPrefIdSet pref_id_set;
1003   pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
1004   EXPECT_EQ(1U, pref_id_set.size());
1005   EXPECT_TRUE(pref_id_set.find(user_added_id) != pref_id_set.end());
1006 
1007   MediaStorageUtil::GetDeviceInfoFromPath(
1008       MakeMediaGalleriesTestingPath("new_user/foo"), &info, &relative_path);
1009   pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
1010   EXPECT_EQ(0U, pref_id_set.size());
1011 
1012   // Add some galleries on the same device.
1013   relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device1"));
1014   gallery_name = base::ASCIIToUTF16("Device1Path1");
1015   std::string device_id = "path:device1";
1016   MediaGalleryPrefId dev1_path1_id = AddGalleryWithNameV1(
1017       device_id, gallery_name, relative_path, true /*user*/);
1018   EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id);
1019   AddGalleryExpectation(dev1_path1_id, gallery_name, device_id, relative_path,
1020                         MediaGalleryPrefInfo::kUserAdded);
1021   Verify();
1022 
1023   relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device1"));
1024   gallery_name = base::ASCIIToUTF16("Device1Path2");
1025   MediaGalleryPrefId dev1_path2_id = AddGalleryWithNameV1(
1026       device_id, gallery_name, relative_path, true /*user*/);
1027   EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id);
1028   AddGalleryExpectation(dev1_path2_id, gallery_name, device_id, relative_path,
1029                         MediaGalleryPrefInfo::kUserAdded);
1030   Verify();
1031 
1032   relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device2"));
1033   gallery_name = base::ASCIIToUTF16("Device2Path1");
1034   device_id = "path:device2";
1035   MediaGalleryPrefId dev2_path1_id = AddGalleryWithNameV1(
1036       device_id, gallery_name, relative_path, true /*user*/);
1037   EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id);
1038   AddGalleryExpectation(dev2_path1_id, gallery_name, device_id, relative_path,
1039                         MediaGalleryPrefInfo::kUserAdded);
1040   Verify();
1041 
1042   relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device2"));
1043   gallery_name = base::ASCIIToUTF16("Device2Path2");
1044   MediaGalleryPrefId dev2_path2_id = AddGalleryWithNameV1(
1045       device_id, gallery_name, relative_path, true /*user*/);
1046   EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id);
1047   AddGalleryExpectation(dev2_path2_id, gallery_name, device_id, relative_path,
1048                         MediaGalleryPrefInfo::kUserAdded);
1049   Verify();
1050 
1051   // Check that adding one of them again works as expected.
1052   MediaGalleryPrefId id = AddGalleryWithNameV1(
1053       device_id, gallery_name, relative_path, true /*user*/);
1054   EXPECT_EQ(dev2_path2_id, id);
1055   Verify();
1056 }
1057 
TEST_F(MediaGalleriesPreferencesTest,GalleryChangeObserver)1058 TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) {
1059   // Start with one observer.
1060   MockGalleryChangeObserver observer1(gallery_prefs());
1061   gallery_prefs()->AddGalleryChangeObserver(&observer1);
1062 
1063   // Add a new auto detected gallery.
1064   base::FilePath path = MakeMediaGalleriesTestingPath("new_auto");
1065   StorageInfo info;
1066   base::FilePath relative_path;
1067   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1068   base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
1069   MediaGalleryPrefId auto_id = AddGalleryWithNameV1(
1070       info.device_id(), gallery_name, relative_path, false /*auto*/);
1071   EXPECT_EQ(default_galleries_count() + 1UL, auto_id);
1072   AddGalleryExpectation(auto_id, gallery_name, info.device_id(),
1073                         relative_path, MediaGalleryPrefInfo::kAutoDetected);
1074   EXPECT_EQ(1, observer1.notifications());
1075 
1076   // Add a second observer.
1077   MockGalleryChangeObserver observer2(gallery_prefs());
1078   gallery_prefs()->AddGalleryChangeObserver(&observer2);
1079 
1080   // Add a new user added gallery.
1081   path = MakeMediaGalleriesTestingPath("new_user");
1082   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1083   gallery_name = base::ASCIIToUTF16("NewUserGallery");
1084   MediaGalleryPrefId user_added_id =
1085       AddGalleryWithNameV1(info.device_id(), gallery_name, relative_path,
1086                            true /*user*/);
1087   AddGalleryExpectation(user_added_id, gallery_name, info.device_id(),
1088                         relative_path, MediaGalleryPrefInfo::kUserAdded);
1089   EXPECT_EQ(default_galleries_count() + 2UL, user_added_id);
1090   EXPECT_EQ(2, observer1.notifications());
1091   EXPECT_EQ(1, observer2.notifications());
1092 
1093   // Remove the first observer.
1094   gallery_prefs()->RemoveGalleryChangeObserver(&observer1);
1095 
1096   // Remove an auto added gallery (i.e. make it blacklisted).
1097   gallery_prefs()->ForgetGalleryById(auto_id);
1098   expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
1099   expected_galleries_for_all.erase(auto_id);
1100 
1101   EXPECT_EQ(2, observer1.notifications());
1102   EXPECT_EQ(2, observer2.notifications());
1103 
1104   // Remove a user added gallery and it should go away.
1105   gallery_prefs()->ForgetGalleryById(user_added_id);
1106   expected_galleries_.erase(user_added_id);
1107   expected_device_map[info.device_id()].erase(user_added_id);
1108 
1109   EXPECT_EQ(2, observer1.notifications());
1110   EXPECT_EQ(3, observer2.notifications());
1111 }
1112 
TEST_F(MediaGalleriesPreferencesTest,ScanResults)1113 TEST_F(MediaGalleriesPreferencesTest, ScanResults) {
1114   MediaGalleryPrefId id;
1115   base::FilePath path;
1116   StorageInfo info;
1117   base::FilePath relative_path;
1118   base::Time now = base::Time::Now();
1119   Verify();
1120 
1121   // Add a new scan result gallery to test with.
1122   path = MakeMediaGalleriesTestingPath("new_scan");
1123   MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
1124   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1125                                    MediaGalleryPrefInfo::kScanResult,
1126                                    ASCIIToUTF16("volume label"),
1127                                    ASCIIToUTF16("vendor name"),
1128                                    ASCIIToUTF16("model name"),
1129                                    1000000ULL, now, 1, 2, 3);
1130   EXPECT_EQ(default_galleries_count() + 1UL, id);
1131   AddScanResultExpectation(id, base::string16(), info.device_id(),
1132                            relative_path, 1, 2, 3);
1133   Verify();
1134 
1135   // Update the found media count.
1136   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1137                                    MediaGalleryPrefInfo::kScanResult,
1138                                    ASCIIToUTF16("volume label"),
1139                                    ASCIIToUTF16("vendor name"),
1140                                    ASCIIToUTF16("model name"),
1141                                    1000000ULL, now, 4, 5, 6);
1142   EXPECT_EQ(default_galleries_count() + 1UL, id);
1143   AddScanResultExpectation(id, base::string16(), info.device_id(),
1144                            relative_path, 4, 5, 6);
1145   Verify();
1146 
1147   // Remove a scan result (i.e. make it blacklisted).
1148   gallery_prefs()->ForgetGalleryById(id);
1149   expected_galleries_[id].type = MediaGalleryPrefInfo::kRemovedScan;
1150   expected_galleries_[id].audio_count = 0;
1151   expected_galleries_[id].image_count = 0;
1152   expected_galleries_[id].video_count = 0;
1153   Verify();
1154 
1155   // Try adding the gallery again as a scan result it should be a no-op.
1156   id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
1157                                    MediaGalleryPrefInfo::kScanResult,
1158                                    ASCIIToUTF16("volume label"),
1159                                    ASCIIToUTF16("vendor name"),
1160                                    ASCIIToUTF16("model name"),
1161                                    1000000ULL, now, 7, 8, 9);
1162   EXPECT_EQ(default_galleries_count() + 1UL, id);
1163   Verify();
1164 
1165   // Add the gallery again as a user action.
1166   id = gallery_prefs()->AddGalleryByPath(path,
1167                                          MediaGalleryPrefInfo::kUserAdded);
1168   EXPECT_EQ(default_galleries_count() + 1UL, id);
1169   AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path,
1170                         MediaGalleryPrefInfo::kUserAdded);
1171   Verify();
1172 }
1173 
TEST(MediaGalleriesPrefInfoTest,NameGeneration)1174 TEST(MediaGalleriesPrefInfoTest, NameGeneration) {
1175   base::test::TaskEnvironment task_environment(
1176       base::test::TaskEnvironment::MainThreadType::UI);
1177 
1178   ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
1179 
1180   MediaGalleryPrefInfo info;
1181   info.pref_id = 1;
1182   info.display_name = ASCIIToUTF16("override");
1183   info.device_id = StorageInfo::MakeDeviceId(
1184       StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, "unique");
1185 
1186   EXPECT_EQ(ASCIIToUTF16("override"), info.GetGalleryDisplayName());
1187 
1188   info.display_name = ASCIIToUTF16("o2");
1189   EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1190 
1191   EXPECT_EQ(l10n_util::GetStringUTF16(
1192                 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1193             info.GetGalleryAdditionalDetails());
1194 
1195   info.last_attach_time = base::Time::Now();
1196   EXPECT_NE(l10n_util::GetStringUTF16(
1197                 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
1198             info.GetGalleryAdditionalDetails());
1199   EXPECT_NE(l10n_util::GetStringUTF16(
1200                 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED),
1201             info.GetGalleryAdditionalDetails());
1202 
1203   info.volume_label = ASCIIToUTF16("vol");
1204   info.vendor_name = ASCIIToUTF16("vendor");
1205   info.model_name = ASCIIToUTF16("model");
1206   EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
1207 
1208   info.display_name = base::string16();
1209   EXPECT_EQ(ASCIIToUTF16("vol"), info.GetGalleryDisplayName());
1210   info.volume_label = base::string16();
1211   EXPECT_EQ(ASCIIToUTF16("vendor, model"), info.GetGalleryDisplayName());
1212 
1213   info.device_id = StorageInfo::MakeDeviceId(
1214       StorageInfo::FIXED_MASS_STORAGE, "unique");
1215   EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(),
1216             base::UTF16ToUTF8(info.GetGalleryTooltip()));
1217 
1218   TestStorageMonitor::Destroy();
1219 }
1220 
TEST_F(MediaGalleriesPreferencesTest,SetsDefaultGalleryTypeField)1221 TEST_F(MediaGalleriesPreferencesTest, SetsDefaultGalleryTypeField) {
1222   // Tests that default galleries (Music, Pictures, Video) have the correct
1223   // default_gallery field set.
1224 
1225   // No default galleries exist on CrOS so this test isn't relevant there.
1226 #if !defined(OS_CHROMEOS)
1227   base::FilePath music_path;
1228   base::FilePath pictures_path;
1229   base::FilePath videos_path;
1230   bool got_music_path =
1231       base::PathService::Get(chrome::DIR_USER_MUSIC, &music_path);
1232   bool got_pictures_path =
1233       base::PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path);
1234   bool got_videos_path =
1235       base::PathService::Get(chrome::DIR_USER_VIDEOS, &videos_path);
1236 
1237   int num_default_galleries = 0;
1238 
1239   const MediaGalleriesPrefInfoMap& known_galleries =
1240       gallery_prefs()->known_galleries();
1241   for (auto it = known_galleries.begin(); it != known_galleries.end(); ++it) {
1242     if (it->second.type != MediaGalleryPrefInfo::kAutoDetected)
1243       continue;
1244 
1245     std::string unique_id;
1246     if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1247       continue;
1248 
1249     if (got_music_path && unique_id == music_path.AsUTF8Unsafe()) {
1250       EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault,
1251                 it->second.default_gallery_type);
1252       num_default_galleries++;
1253     } else if (got_pictures_path && unique_id == pictures_path.AsUTF8Unsafe()) {
1254       EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault,
1255                 it->second.default_gallery_type);
1256       num_default_galleries++;
1257     } else if (got_videos_path && unique_id == videos_path.AsUTF8Unsafe()) {
1258       EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault,
1259                 it->second.default_gallery_type);
1260       num_default_galleries++;
1261     } else {
1262       EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault,
1263                 it->second.default_gallery_type);
1264     }
1265   }
1266 
1267   EXPECT_EQ(3, num_default_galleries);
1268 #endif
1269 }
1270 
TEST_F(MediaGalleriesPreferencesTest,UpdatesDefaultGalleryType)1271 TEST_F(MediaGalleriesPreferencesTest, UpdatesDefaultGalleryType) {
1272   // Tests that if the path of a default gallery changed since last init,
1273   // then when the MediaGalleriesPreferences is initialized, it will
1274   // rewrite the device ID in prefs to include the new path.
1275 
1276   // No default galleries exist on CrOS so this test isn't relevant there.
1277 #if !defined(OS_CHROMEOS)
1278   base::FilePath old_music_path;
1279   base::FilePath old_pictures_path;
1280   base::FilePath old_videos_path;
1281   bool got_old_music_path =
1282       base::PathService::Get(chrome::DIR_USER_MUSIC, &old_music_path);
1283   bool got_old_pictures_path =
1284       base::PathService::Get(chrome::DIR_USER_PICTURES, &old_pictures_path);
1285   bool got_old_videos_path =
1286       base::PathService::Get(chrome::DIR_USER_VIDEOS, &old_videos_path);
1287 
1288   bool found_music = false;
1289   bool found_pictures = false;
1290   bool found_videos = false;
1291 
1292   const MediaGalleriesPrefInfoMap& old_known_galleries =
1293       gallery_prefs()->known_galleries();
1294   for (auto it = old_known_galleries.begin(); it != old_known_galleries.end();
1295        ++it) {
1296     if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) {
1297       std::string unique_id;
1298       if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1299         continue;
1300 
1301       if (got_old_music_path &&
1302           it->second.default_gallery_type ==
1303           MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
1304         EXPECT_EQ(old_music_path.AsUTF8Unsafe(), unique_id);
1305         found_music = true;
1306       } else if (got_old_pictures_path &&
1307                  it->second.default_gallery_type ==
1308                  MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
1309         EXPECT_EQ(old_pictures_path.AsUTF8Unsafe(), unique_id);
1310         found_pictures = true;
1311       } else if (got_old_videos_path &&
1312                  it->second.default_gallery_type ==
1313                  MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
1314         EXPECT_EQ(old_videos_path.AsUTF8Unsafe(), unique_id);
1315         found_videos = true;
1316       }
1317     }
1318   }
1319 
1320   EXPECT_TRUE(found_music);
1321   EXPECT_TRUE(found_pictures);
1322   EXPECT_TRUE(found_videos);
1323 
1324   ChangeMediaPathOverrides();
1325   ReinitPrefsAndExpectations();
1326 
1327   base::FilePath new_music_path;
1328   base::FilePath new_pictures_path;
1329   base::FilePath new_videos_path;
1330   bool got_new_music_path =
1331       base::PathService::Get(chrome::DIR_USER_MUSIC, &new_music_path);
1332   bool got_new_pictures_path =
1333       base::PathService::Get(chrome::DIR_USER_PICTURES, &new_pictures_path);
1334   bool got_new_videos_path =
1335       base::PathService::Get(chrome::DIR_USER_VIDEOS, &new_videos_path);
1336 
1337   EXPECT_NE(new_music_path, old_music_path);
1338   EXPECT_NE(new_pictures_path, old_pictures_path);
1339   EXPECT_NE(new_videos_path, old_videos_path);
1340 
1341   found_music = false;
1342   found_pictures = false;
1343   found_videos = false;
1344 
1345   const MediaGalleriesPrefInfoMap& known_galleries =
1346       gallery_prefs()->known_galleries();
1347   for (auto it = known_galleries.begin(); it != known_galleries.end(); ++it) {
1348     if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) {
1349       std::string unique_id;
1350       if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1351         continue;
1352 
1353       if (got_new_music_path &&
1354           it->second.default_gallery_type ==
1355           MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
1356         EXPECT_EQ(new_music_path.AsUTF8Unsafe(), unique_id);
1357         found_music = true;
1358       } else if (got_new_pictures_path &&
1359                  it->second.default_gallery_type ==
1360                  MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
1361         EXPECT_EQ(new_pictures_path.AsUTF8Unsafe(), unique_id);
1362         found_pictures = true;
1363       } else if (got_new_videos_path &&
1364                  it->second.default_gallery_type ==
1365                  MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
1366         EXPECT_EQ(new_videos_path.AsUTF8Unsafe(), unique_id);
1367         found_videos = true;
1368       }
1369     }
1370   }
1371 
1372   EXPECT_TRUE(found_music);
1373   EXPECT_TRUE(found_pictures);
1374   EXPECT_TRUE(found_videos);
1375 #endif
1376 }
1377 
TEST_F(MediaGalleriesPreferencesTest,UpdateAddsDefaultGalleryTypeIfMissing)1378 TEST_F(MediaGalleriesPreferencesTest, UpdateAddsDefaultGalleryTypeIfMissing) {
1379   // Tests that if no default_gallery_type was specified for an existing prefs
1380   // info object corresponding to a particular gallery, then when the
1381   // MediaGalleriesPreferences is initialized, it assigns the proper one.
1382 
1383   // No default galleries exist on CrOS so this test isn't relevant there.
1384 #if !defined(OS_CHROMEOS)
1385   // Add a new user added gallery.
1386   AddFixedGalleryWithExpectation("user_added", "UserAdded",
1387                                  MediaGalleryPrefInfo::kUserAdded);
1388 
1389   // Remove the "default_gallery_type" field completely from the persisted data
1390   // for the prefs info object. This simulates the case where a user updated
1391   // Chrome from a version without that field to one with it.
1392   RemovePersistedDefaultGalleryValues();
1393 
1394   // Reinitializing the MediaGalleriesPreferences should populate the
1395   // default_gallery_type field with the correct value for each gallery.
1396   ReinitPrefsAndExpectations();
1397 
1398   base::FilePath music_path;
1399   base::FilePath pictures_path;
1400   base::FilePath videos_path;
1401   bool got_music_path =
1402       base::PathService::Get(chrome::DIR_USER_MUSIC, &music_path);
1403   bool got_pictures_path =
1404       base::PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path);
1405   bool got_videos_path =
1406       base::PathService::Get(chrome::DIR_USER_VIDEOS, &videos_path);
1407 
1408   bool found_music = false;
1409   bool found_pictures = false;
1410   bool found_videos = false;
1411   bool found_user_added = false;
1412 
1413   const MediaGalleriesPrefInfoMap& known_galleries =
1414       gallery_prefs()->known_galleries();
1415   for (auto it = known_galleries.begin(); it != known_galleries.end(); ++it) {
1416     std::string unique_id;
1417     if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
1418       continue;
1419 
1420     if (got_music_path &&
1421         it->second.default_gallery_type ==
1422         MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
1423       EXPECT_EQ(music_path.AsUTF8Unsafe(), unique_id);
1424       found_music = true;
1425     } else if (got_pictures_path &&
1426                it->second.default_gallery_type ==
1427                MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
1428       EXPECT_EQ(pictures_path.AsUTF8Unsafe(), unique_id);
1429       found_pictures = true;
1430     } else if (got_videos_path &&
1431                it->second.default_gallery_type ==
1432                MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
1433       EXPECT_EQ(videos_path.AsUTF8Unsafe(), unique_id);
1434       found_videos = true;
1435     } else if (it->second.default_gallery_type ==
1436                MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault) {
1437       found_user_added = true;
1438     }
1439   }
1440 
1441   EXPECT_TRUE(found_music);
1442   EXPECT_TRUE(found_pictures);
1443   EXPECT_TRUE(found_videos);
1444   EXPECT_TRUE(found_user_added);
1445 #endif
1446 }
1447