media_galleries_preferences_unittest.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MediaGalleriesPreferences unit tests.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/media_galleries/media_galleries_preferences.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_system.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/test_extension_system.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/media_galleries/media_file_system_registry.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/media_galleries/media_galleries_test_util.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/storage_monitor/media_storage_util.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/storage_monitor/storage_monitor.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/storage_monitor/test_storage_monitor.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/extensions/background_info.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/extensions/extension.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
24ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "content/public/test/test_browser_thread_bundle.h"
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "grit/generated_resources.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/api/string_ordinal.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/base/l10n/l10n_util.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_CHROMEOS)
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/login/user_manager.h"
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/settings/cros_settings.h"
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/settings/device_settings_service.h"
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class MockGalleryChangeObserver
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : public MediaGalleriesPreferences::GalleryChangeObserver {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : pref_(pref),
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        notifications_(0) {}
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~MockGalleryChangeObserver() {}
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int notifications() const { return notifications_;}
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // MediaGalleriesPreferences::GalleryChangeObserver implementation.
50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                 const std::string& extension_id,
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                 MediaGalleryPrefId pref_id) OVERRIDE {
53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    EXPECT_EQ(pref_, pref);
54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ++notifications_;
55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
56d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                   const std::string& extension_id,
59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                   MediaGalleryPrefId pref_id) OVERRIDE {
60d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    EXPECT_EQ(pref_, pref);
61d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ++notifications_;
62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
63d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
64d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void OnGalleryAdded(MediaGalleriesPreferences* pref,
65d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                              MediaGalleryPrefId pref_id) OVERRIDE {
66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    EXPECT_EQ(pref_, pref);
67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ++notifications_;
68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
69d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
70d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
71d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                MediaGalleryPrefId pref_id) OVERRIDE {
72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    EXPECT_EQ(pref_, pref);
73d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ++notifications_;
74d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
75d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
76d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                    MediaGalleryPrefId pref_id) OVERRIDE {
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(pref_, pref);
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ++notifications_;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaGalleriesPreferences* pref_;
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int notifications_;
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MediaGalleriesPreferencesTest : public testing::Test {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DeviceIdPrefIdsMap;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleriesPreferencesTest()
96ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      : profile_(new TestingProfile()),
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        default_galleries_count_(0) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~MediaGalleriesPreferencesTest() {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
105558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions::TestExtensionSystem* extension_system(
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        static_cast<extensions::TestExtensionSystem*>(
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            extensions::ExtensionSystem::Get(profile_.get())));
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    extension_system->CreateExtensionService(
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        CommandLine::ForCurrentProcess(), base::FilePath(), false);
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Load the default galleries into the expectations.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const MediaGalleriesPrefInfoMap& known_galleries =
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        gallery_prefs_->known_galleries();
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (known_galleries.size()) {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ASSERT_EQ(3U, known_galleries.size());
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      default_galleries_count_ = 3;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MediaGalleriesPrefInfoMap::const_iterator it;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (it = known_galleries.begin(); it != known_galleries.end(); ++it) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        expected_galleries_[it->first] = it->second;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          expected_galleries_for_all.insert(it->first);
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<std::string> all_permissions;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    all_permissions.push_back("allAutoDetected");
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    all_permissions.push_back("read");
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<std::string> read_permissions;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    read_permissions.push_back("read");
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    all_permission_extension =
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AddMediaGalleriesApp("all", all_permissions, profile_.get());
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    regular_permission_extension =
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AddMediaGalleriesApp("regular", read_permissions, profile_.get());
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    no_permissions_extension =
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AddMediaGalleriesApp("no", read_permissions, profile_.get());
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Verify();
144d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    TestStorageMonitor::RemoveSingleton();
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Verify() {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const MediaGalleriesPrefInfoMap& known_galleries =
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        gallery_prefs_->known_galleries();
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         it != known_galleries.end();
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         ++it) {
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      VerifyGalleryInfo(it->second, it->first);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin();
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         it != expected_device_map.end();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         ++it) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MediaGalleryPrefIdSet actual_id_set =
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          gallery_prefs_->LookUpGalleriesByDeviceId(it->first);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      EXPECT_EQ(it->second, actual_id_set);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::set<MediaGalleryPrefId> galleries_for_all =
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        gallery_prefs_->GalleriesForExtension(*all_permission_extension.get());
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_galleries_for_all, galleries_for_all);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::set<MediaGalleryPrefId> galleries_for_regular =
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        gallery_prefs_->GalleriesForExtension(
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            *regular_permission_extension.get());
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular);
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::set<MediaGalleryPrefId> galleries_for_no =
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get());
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(0U, galleries_for_no.size());
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual,
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         MediaGalleryPrefId expected_id) const {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MediaGalleriesPrefInfoMap::const_iterator in_expectation =
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      expected_galleries_.find(expected_id);
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ASSERT_FALSE(in_expectation == expected_galleries_.end())  << expected_id;
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id);
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(in_expectation->second.display_name, actual.display_name);
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(in_expectation->second.device_id, actual.device_id);
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(in_expectation->second.path.value(), actual.path.value());
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_EQ(in_expectation->second.type, actual.type);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleriesPreferences* gallery_prefs() {
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return gallery_prefs_.get();
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint64 default_galleries_count() {
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return default_galleries_count_;
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddGalleryExpectation(MediaGalleryPrefId id, string16 display_name,
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             std::string device_id,
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             base::FilePath relative_path,
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             MediaGalleryPrefInfo::Type type) {
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    expected_galleries_[id].pref_id = id;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    expected_galleries_[id].display_name = display_name;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    expected_galleries_[id].device_id = device_id;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    expected_galleries_[id].path = relative_path.NormalizePathSeparators();
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    expected_galleries_[id].type = type;
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (type == MediaGalleryPrefInfo::kAutoDetected)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      expected_galleries_for_all.insert(id);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    expected_device_map[device_id].insert(id);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaGalleryPrefId AddGalleryWithNameV0(const std::string& device_id,
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          const string16& display_name,
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          const base::FilePath& relative_path,
218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          bool user_added) {
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gallery_prefs()->AddGalleryInternal(
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        device_id, display_name, relative_path, user_added,
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        string16(), string16(), string16(), 0, base::Time(), false, 0);
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id,
2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                          const string16& display_name,
2267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                          const base::FilePath& relative_path,
2277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                          bool user_added) {
2287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return gallery_prefs()->AddGalleryInternal(
2297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        device_id, display_name, relative_path, user_added,
2307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        string16(), string16(), string16(), 0, base::Time(), false, 1);
2317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id,
2347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                          const string16& display_name,
2357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                          const base::FilePath& relative_path,
2367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                          bool user_added) {
2377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return gallery_prefs()->AddGalleryInternal(
2387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        device_id, display_name, relative_path, user_added,
2397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        string16(), string16(), string16(), 0, base::Time(), false, 2);
2407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  bool UpdateDeviceIDForSingletonType(const std::string& device_id) {
2437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return gallery_prefs()->UpdateDeviceIDForSingletonType(device_id);
2447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<extensions::Extension> all_permission_extension;
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<extensions::Extension> regular_permission_extension;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<extensions::Extension> no_permissions_extension;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::set<MediaGalleryPrefId> expected_galleries_for_all;
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::set<MediaGalleryPrefId> expected_galleries_for_regular;
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DeviceIdPrefIdsMap expected_device_map;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleriesPrefInfoMap expected_galleries_;
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Needed for extension service & friends to work.
259ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  content::TestBrowserThreadBundle thread_bundle_;
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined OS_CHROMEOS
262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chromeos::ScopedTestCrosSettings test_cros_settings_;
264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  chromeos::ScopedTestUserManager test_user_manager_;
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
267d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  TestStorageMonitor monitor_;
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<TestingProfile> profile_;
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint64 default_galleries_count_;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath MakePath(std::string dir) {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::FilePath(FILE_PATH_LITERAL("C:\\")).Append(UTF8ToWide(dir));
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_POSIX)
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::FilePath(FILE_PATH_LITERAL("/")).Append(dir);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTREACHED();
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) {
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleryPrefId auto_id, user_added_id, id;
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add a new auto detected gallery.
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("new_auto");
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
2977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
2987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false /*auto*/);
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, id);
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  auto_id = id;
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add it again (as user), nothing should happen.
3067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
3077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, true /*auto*/);
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(auto_id, id);
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add a new user added gallery.
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("new_user");
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewUserGallery"));
3157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
3167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, true /*user*/);
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 2UL, id);
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  user_added_id = id;
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kUserAdded);
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Lookup some galleries.
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), NULL));
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), NULL));
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(MakePath("other"), NULL));
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check that we always get the gallery info.
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleryPrefInfo gallery_info;
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"),
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                   &gallery_info));
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  VerifyGalleryInfo(gallery_info, auto_id);
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(gallery_info.volume_metadata_valid);
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"),
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                   &gallery_info));
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  VerifyGalleryInfo(gallery_info, user_added_id);
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(gallery_info.volume_metadata_valid);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("other");
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info));
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id);
3427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo other_info;
34490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &other_info, &relative_path);
34590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(other_info.device_id(), gallery_info.device_id);
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(relative_path.value(), gallery_info.path.value());
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove an auto added gallery (i.e. make it blacklisted).
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->ForgetGalleryById(auto_id);
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.erase(auto_id);
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove a user added gallery and it should go away.
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->ForgetGalleryById(user_added_id);
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_.erase(user_added_id);
35790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  expected_device_map[info.device_id()].erase(user_added_id);
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Verify();
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, AddGalleryWithVolumeMetadata) {
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaGalleryPrefId id;
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Time now = base::Time::Now();
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Verify();
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add a new auto detected gallery.
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  path = MakePath("new_auto");
37190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
37290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  id = gallery_prefs()->AddGallery(info.device_id(), relative_path,
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   false /*auto*/,
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   ASCIIToUTF16("volume label"),
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   ASCIIToUTF16("vendor name"),
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   ASCIIToUTF16("model name"),
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   1000000ULL, now);
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, id);
37990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, string16(), info.device_id(), relative_path,
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Verify();
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaGalleryPrefInfo gallery_info;
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"),
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                   &gallery_info));
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(gallery_info.volume_metadata_valid);
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info.volume_label);
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info.vendor_name);
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info.model_name);
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(1000000ULL, gallery_info.total_size_in_bytes);
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note: we put the microseconds time into a double, so there'll
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // be some possible rounding errors. If it's less than 100, we don't
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // care.
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_LE(abs(now.ToInternalValue() -
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                gallery_info.last_attach_time.ToInternalValue()), 100);
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, ReplaceGalleryWithVolumeMetadata) {
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaGalleryPrefId id, metadata_id;
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Time now = base::Time::Now();
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Verify();
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add an auto detected gallery in the prefs version 0 format.
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  path = MakePath("new_auto");
40890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
40990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
41090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  id = AddGalleryWithNameV0(info.device_id(), info.name(),
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            relative_path, false /*auto*/);
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, id);
41390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Verify();
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
41790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  metadata_id = gallery_prefs()->AddGallery(info.device_id(),
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            relative_path,
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            false /*auto*/,
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            ASCIIToUTF16("volume label"),
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            ASCIIToUTF16("vendor name"),
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            ASCIIToUTF16("model name"),
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            1000000ULL, now);
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(id, metadata_id);
42590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, string16(), info.device_id(), relative_path,
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Make sure the display_name is set to empty now, as the metadata
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // upgrade should set the manual override name empty.
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Whenever a gallery is added, its type is either set to "AutoDetected" or
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "UserAdded". When the gallery is removed, user added galleries are actually
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deleted and the auto detected galleries are moved to black listed state.
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When the gallery is added again, the black listed state is updated back to
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AutoDetected" type.
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryType) {
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleryPrefId auto_id, id;
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add a new auto detect gallery to test with.
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("new_auto");
44890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
44990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
4507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
4517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false /*auto*/);
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, id);
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  auto_id = id;
45490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove an auto added gallery (i.e. make it blacklisted).
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->ForgetGalleryById(auto_id);
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.erase(auto_id);
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add the gallery again as a user action.
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  id = gallery_prefs()->AddGalleryByPath(path);
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(auto_id, id);
46790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Remove an auto added gallery (i.e. make it blacklisted).
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gallery_prefs()->ForgetGalleryById(auto_id);
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  expected_galleries_for_all.erase(auto_id);
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Verify();
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Try adding the gallery again automatically and it should be a no-op.
4787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
4797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false /*auto*/);
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(auto_id, id);
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Verify();
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryNameV2) {
4857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Add a new auto detect gallery to test with.
4867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath path = MakePath("new_auto");
4877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  StorageInfo info;
4887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath relative_path;
4897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
4907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
4917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId id =
4927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      AddGalleryWithNameV2(info.device_id(), info.name(),
4937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                           relative_path, false /*auto*/);
4947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
4957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
4967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Verify();
4977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Won't override the name -- don't change any expectation.
4997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  info.set_name(string16());
5007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AddGalleryWithNameV2(info.device_id(), info.name(), relative_path, false);
5017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Verify();
5027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  info.set_name(ASCIIToUTF16("NewName"));
5047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV2(info.device_id(), info.name(),
5057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false);
5067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Note: will really just update the existing expectation.
5077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
5087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
5097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Verify();
5107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
5117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, id;
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add some galleries to test with.
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("new_user");
52190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
52290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewUserGallery"));
5237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
5247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, true /*user*/);
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, id);
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  user_added_id = id;
52790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kUserAdded);
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("new_auto");
53290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
53390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
5347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
5357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false /*auto*/);
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 2UL, id);
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  auto_id = id;
53890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("to_blacklist");
54390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
54490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("ToBlacklistGallery"));
5457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
5467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false /*auto*/);
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 3UL, id);
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  to_blacklist_id = id;
54990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove permission for all galleries from the all-permission extension.
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *all_permission_extension.get(), auto_id, false);
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.erase(auto_id);
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *all_permission_extension.get(), user_added_id, false);
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.erase(user_added_id);
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *all_permission_extension.get(), to_blacklist_id, false);
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.erase(to_blacklist_id);
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add permission back for all galleries to the all-permission extension.
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *all_permission_extension.get(), auto_id, true);
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.insert(auto_id);
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *all_permission_extension.get(), user_added_id, true);
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.insert(user_added_id);
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *all_permission_extension.get(), to_blacklist_id, true);
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.insert(to_blacklist_id);
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add permission for all galleries to the regular permission extension.
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *regular_permission_extension.get(), auto_id, true);
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_regular.insert(auto_id);
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *regular_permission_extension.get(), user_added_id, true);
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_regular.insert(user_added_id);
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *regular_permission_extension.get(), to_blacklist_id, true);
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_regular.insert(to_blacklist_id);
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Blacklist the to be black listed gallery
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->ForgetGalleryById(to_blacklist_id);
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_[to_blacklist_id].type =
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MediaGalleryPrefInfo::kBlackListed;
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_all.erase(to_blacklist_id);
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_regular.erase(to_blacklist_id);
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove permission for all galleries to the regular permission extension.
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *regular_permission_extension.get(), auto_id, false);
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_regular.erase(auto_id);
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *regular_permission_extension.get(), user_added_id, false);
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_galleries_for_regular.erase(user_added_id);
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add permission for an invalid gallery id.
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gallery_prefs()->SetGalleryPermissionForExtension(
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *regular_permission_extension.get(), 9999L, true);
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Whenever a gallery is added, check to see if there is any gallery info exists
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for the added gallery. If so, verify the existing gallery information with
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the new details. If there is a mismatch, update the gallery information
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// accordingly.
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryDetails) {
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleryPrefId auto_id, id;
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add a new auto detect gallery to test with.
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("new_auto");
63990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
64090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
6417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
6427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false /*auto*/);
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, id);
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  auto_id = id;
64590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the device name and add the gallery again.
65090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("AutoGallery2"));
6517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV1(info.device_id(), info.name(),
6527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            relative_path, false /*auto*/);
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(auto_id, id);
65490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) {
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add a regular gallery
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  path = MakePath("new_user");
66790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
66890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewUserGallery"));
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleryPrefId user_added_id =
6707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      AddGalleryWithNameV1(info.device_id(), info.name(),
6717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                           relative_path, true /*user*/);
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, user_added_id);
67390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(user_added_id, info.name(), info.device_id(),
6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        relative_path, MediaGalleryPrefInfo::kUserAdded);
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Find it by device id and fail to find something related.
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MediaGalleryPrefIdSet pref_id_set;
67990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1U, pref_id_set.size());
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(pref_id_set.find(user_added_id) != pref_id_set.end());
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(MakePath("new_user/foo"), &info,
68490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                          &relative_path);
68590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id());
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0U, pref_id_set.size());
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add some galleries on the same device.
6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device1"));
69090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("Device1Path1"));
691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string device_id = "path:device1";
6927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId dev1_path1_id = AddGalleryWithNameV1(
69390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      device_id, info.name(), relative_path, true /*user*/);
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id);
69590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(dev1_path1_id, info.name(), device_id, relative_path,
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kUserAdded);
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device1"));
70090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("Device1Path2"));
7017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId dev1_path2_id = AddGalleryWithNameV1(
70290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      device_id, info.name(), relative_path, true /*user*/);
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id);
70490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(dev1_path2_id, info.name(), device_id, relative_path,
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kUserAdded);
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device2"));
70990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("Device2Path1"));
710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  device_id = "path:device2";
7117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId dev2_path1_id = AddGalleryWithNameV1(
71290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      device_id, info.name(), relative_path, true /*user*/);
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id);
71490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(dev2_path1_id, info.name(), device_id, relative_path,
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kUserAdded);
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device2"));
71990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("Device2Path2"));
7207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId dev2_path2_id = AddGalleryWithNameV1(
72190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      device_id, info.name(), relative_path, true /*user*/);
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id);
72390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(dev2_path2_id, info.name(), device_id, relative_path,
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        MediaGalleryPrefInfo::kUserAdded);
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check that adding one of them again works as expected.
7287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId id = AddGalleryWithNameV1(
72990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      device_id, info.name(), relative_path, true /*user*/);
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(dev2_path2_id, id);
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Verify();
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) {
7352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Start with one observer.
7362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockGalleryChangeObserver observer1(gallery_prefs());
7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gallery_prefs()->AddGalleryChangeObserver(&observer1);
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add a new auto detected gallery.
7402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path = MakePath("new_auto");
7412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StorageInfo info;
7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath relative_path;
74390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
74490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
7457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId auto_id = AddGalleryWithNameV1(
7467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      info.device_id(), info.name(), relative_path, false /*auto*/);
7472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, auto_id);
74890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(auto_id, info.name(), info.device_id(),
7492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        relative_path, MediaGalleryPrefInfo::kAutoDetected);
7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(1, observer1.notifications());
7512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add a second observer.
7532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockGalleryChangeObserver observer2(gallery_prefs());
7542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gallery_prefs()->AddGalleryChangeObserver(&observer2);
7552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add a new user added gallery.
7572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  path = MakePath("new_user");
75890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
75990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  info.set_name(ASCIIToUTF16("NewUserGallery"));
7602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MediaGalleryPrefId user_added_id =
7617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      AddGalleryWithNameV1(info.device_id(), info.name(),
7627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                           relative_path, true /*user*/);
76390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AddGalleryExpectation(user_added_id, info.name(), info.device_id(),
7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        relative_path, MediaGalleryPrefInfo::kUserAdded);
7652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 2UL, user_added_id);
7662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, observer1.notifications());
7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(1, observer2.notifications());
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Remove the first observer.
7702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gallery_prefs()->RemoveGalleryChangeObserver(&observer1);
7712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Remove an auto added gallery (i.e. make it blacklisted).
7732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gallery_prefs()->ForgetGalleryById(auto_id);
7742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
7752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  expected_galleries_for_all.erase(auto_id);
7762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, observer1.notifications());
7782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, observer2.notifications());
7792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Remove a user added gallery and it should go away.
7812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gallery_prefs()->ForgetGalleryById(user_added_id);
7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  expected_galleries_.erase(user_added_id);
78390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  expected_device_map[info.device_id()].erase(user_added_id);
7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(2, observer1.notifications());
7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(3, observer2.notifications());
7872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(MediaGalleriesPreferencesTest, UpdateSingletonDeviceIdType) {
7907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaGalleryPrefId id;
7917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath path;
7927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  StorageInfo info;
7937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath relative_path;
7947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Verify();
7957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
7967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Add a new auto detect gallery to test with.
7977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  path = MakePath("new_auto");
7987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
7997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  info.set_name(ASCIIToUTF16("NewAutoGallery"));
8007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  info.set_device_id(StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
8017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                               path.AsUTF8Unsafe()));
8027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  id = AddGalleryWithNameV2(info.device_id(), info.name(), relative_path,
8037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                            false /*auto*/);
8047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EXPECT_EQ(default_galleries_count() + 1UL, id);
8057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AddGalleryExpectation(id, info.name(), info.device_id(), relative_path,
8067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
8077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Verify();
8087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Update the device id.
8107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MockGalleryChangeObserver observer(gallery_prefs());
8117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  gallery_prefs()->AddGalleryChangeObserver(&observer);
8127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  path = MakePath("updated_path");
8147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::string updated_device_id =
8157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      StorageInfo::MakeDeviceId(StorageInfo::ITUNES, path.AsUTF8Unsafe());
8167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id));
8177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AddGalleryExpectation(id, info.name(), updated_device_id, relative_path,
8187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                        MediaGalleryPrefInfo::kAutoDetected);
8197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  expected_device_map[info.device_id()].erase(id);
8207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  expected_device_map[updated_device_id].insert(id);
8217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Verify();
8227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EXPECT_EQ(1, observer.notifications());
8237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // No gallery for type.
8257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::string new_device_id =
8267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      StorageInfo::MakeDeviceId(StorageInfo::PICASA, path.AsUTF8Unsafe());
8277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id));
8287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
8297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
830eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST(MediaGalleryPrefInfoTest, NameGeneration) {
831d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
832eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
833eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  MediaGalleryPrefInfo info;
834eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.pref_id = 1;
835eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.display_name = ASCIIToUTF16("override");
836eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.device_id = StorageInfo::MakeDeviceId(
837eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, "unique");
838eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
839eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(ASCIIToUTF16("override"), info.GetGalleryDisplayName());
840eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
841eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.display_name = ASCIIToUTF16("o2");
842eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
843eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
844eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(l10n_util::GetStringUTF16(
845eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
846eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            info.GetGalleryAdditionalDetails());
847eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
848eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.last_attach_time = base::Time::Now();
849eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_NE(l10n_util::GetStringUTF16(
850eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED),
851eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            info.GetGalleryAdditionalDetails());
852eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_NE(l10n_util::GetStringUTF16(
853eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED),
854eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            info.GetGalleryAdditionalDetails());
855eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
856eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.volume_label = ASCIIToUTF16("vol");
857eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.vendor_name = ASCIIToUTF16("vendor");
858eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.model_name = ASCIIToUTF16("model");
859eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName());
860eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
861eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.display_name = string16();
862eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(ASCIIToUTF16("vol"), info.GetGalleryDisplayName());
863eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.volume_label = string16();
864eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(ASCIIToUTF16("vendor, model"), info.GetGalleryDisplayName());
865eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
866eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  info.device_id = StorageInfo::MakeDeviceId(
867eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      StorageInfo::FIXED_MASS_STORAGE, "unique");
868eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(),
869eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            UTF16ToUTF8(info.GetGalleryTooltip()));
8703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
871d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  TestStorageMonitor::RemoveSingleton();
872eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
873