Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_permission_controller_unittest.cc
blob3bdbfac9fe961e2537879dc61d72b0ef9bbad66d
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/files/file_path.h"
8 #include "base/run_loop.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/extensions/test_extension_system.h"
13 #include "chrome/browser/media_galleries/media_galleries_dialog_controller_test_util.h"
14 #include "chrome/browser/media_galleries/media_galleries_permission_controller.h"
15 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
16 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/storage_monitor/storage_info.h"
19 #include "components/storage_monitor/test_storage_monitor.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "extensions/common/permissions/media_galleries_permission.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
26 #include "chrome/browser/chromeos/settings/cros_settings.h"
27 #include "chrome/browser/chromeos/settings/device_settings_service.h"
28 #endif
30 using storage_monitor::StorageInfo;
31 using storage_monitor::TestStorageMonitor;
33 namespace {
35 std::string GalleryName(const MediaGalleryPrefInfo& gallery) {
36 base::string16 name = gallery.GetGalleryDisplayName();
37 return base::UTF16ToASCII(name);
40 } // namespace
42 class MediaGalleriesPermissionControllerTest : public ::testing::Test {
43 public:
44 MediaGalleriesPermissionControllerTest()
45 : dialog_(NULL),
46 dialog_update_count_at_destruction_(0),
47 controller_(NULL),
48 profile_(new TestingProfile()),
49 weak_factory_(this) {
52 ~MediaGalleriesPermissionControllerTest() override {
53 EXPECT_FALSE(controller_);
54 EXPECT_FALSE(dialog_);
57 void SetUp() override {
58 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
60 extensions::TestExtensionSystem* extension_system(
61 static_cast<extensions::TestExtensionSystem*>(
62 extensions::ExtensionSystem::Get(profile_.get())));
63 extension_system->CreateExtensionService(
64 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
66 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
67 base::RunLoop loop;
68 gallery_prefs_->EnsureInitialized(loop.QuitClosure());
69 loop.Run();
71 std::vector<std::string> read_permissions;
72 read_permissions.push_back(
73 extensions::MediaGalleriesPermission::kReadPermission);
74 extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get());
77 void TearDown() override { TestStorageMonitor::Destroy(); }
79 void StartDialog() {
80 ASSERT_FALSE(controller_);
81 controller_ = new MediaGalleriesPermissionController(
82 *extension_.get(),
83 gallery_prefs_.get(),
84 base::Bind(&MediaGalleriesPermissionControllerTest::CreateMockDialog,
85 base::Unretained(this)),
86 base::Bind(
87 &MediaGalleriesPermissionControllerTest::OnControllerDone,
88 base::Unretained(this)));
91 MediaGalleriesPermissionController* controller() {
92 return controller_;
95 MockMediaGalleriesDialog* dialog() {
96 return dialog_;
99 int dialog_update_count_at_destruction() {
100 EXPECT_FALSE(dialog_);
101 return dialog_update_count_at_destruction_;
104 extensions::Extension* extension() {
105 return extension_.get();
108 MediaGalleriesPreferences* gallery_prefs() {
109 return gallery_prefs_.get();
112 GalleryDialogId GetDialogIdFromPrefId(MediaGalleryPrefId pref_id);
114 void TestForgottenType(MediaGalleryPrefInfo::Type type,
115 bool forget_preserves_pref_id);
117 protected:
118 EnsureMediaDirectoriesExists mock_gallery_locations_;
120 private:
121 MediaGalleriesDialog* CreateMockDialog(
122 MediaGalleriesDialogController* controller) {
123 EXPECT_FALSE(dialog_);
124 dialog_update_count_at_destruction_ = 0;
125 dialog_ = new MockMediaGalleriesDialog(base::Bind(
126 &MediaGalleriesPermissionControllerTest::OnDialogDestroyed,
127 weak_factory_.GetWeakPtr()));
128 return dialog_;
131 void OnDialogDestroyed(int update_count) {
132 EXPECT_TRUE(dialog_);
133 dialog_update_count_at_destruction_ = update_count;
134 dialog_ = NULL;
137 void OnControllerDone() {
138 controller_ = NULL;
141 // Needed for extension service & friends to work.
142 content::TestBrowserThreadBundle thread_bundle_;
144 // The dialog is owned by the controller, but this pointer should only be
145 // valid while the dialog is live within the controller.
146 MockMediaGalleriesDialog* dialog_;
147 int dialog_update_count_at_destruction_;
149 // The controller owns itself.
150 MediaGalleriesPermissionController* controller_;
152 scoped_refptr<extensions::Extension> extension_;
154 #if defined OS_CHROMEOS
155 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
156 chromeos::ScopedTestCrosSettings test_cros_settings_;
157 chromeos::ScopedTestUserManager test_user_manager_;
158 #endif
160 TestStorageMonitor monitor_;
161 scoped_ptr<TestingProfile> profile_;
162 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
164 base::WeakPtrFactory<MediaGalleriesPermissionControllerTest>
165 weak_factory_;
167 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPermissionControllerTest);
170 GalleryDialogId
171 MediaGalleriesPermissionControllerTest::GetDialogIdFromPrefId(
172 MediaGalleryPrefId pref_id) {
173 return controller_->GetDialogId(pref_id);
176 void MediaGalleriesPermissionControllerTest::TestForgottenType(
177 MediaGalleryPrefInfo::Type type, bool forget_preserves_pref_id) {
178 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());
180 MediaGalleryPrefId forgotten1 = gallery_prefs()->AddGalleryByPath(
181 MakeMediaGalleriesTestingPath("forgotten1"), type);
182 MediaGalleryPrefId forgotten2 = gallery_prefs()->AddGalleryByPath(
183 MakeMediaGalleriesTestingPath("forgotten2"), type);
184 // Show dialog and accept to verify 2 entries
185 StartDialog();
186 EXPECT_EQ(0U, controller()->GetSectionEntries(0).size());
187 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 2U,
188 controller()->GetSectionEntries(1).size());
189 controller()->DidToggleEntry(GetDialogIdFromPrefId(forgotten1), true);
190 controller()->DidToggleEntry(GetDialogIdFromPrefId(forgotten2), true);
191 controller()->DialogFinished(true);
192 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
194 // Forget one and cancel to see that it's still there.
195 StartDialog();
196 EXPECT_EQ(2U, controller()->GetSectionEntries(0).size());
197 controller()->DidForgetEntry(GetDialogIdFromPrefId(forgotten1));
198 EXPECT_EQ(1U, controller()->GetSectionEntries(0).size());
199 controller()->DialogFinished(false);
200 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
202 // Forget one and confirm to see that it's gone.
203 StartDialog();
204 EXPECT_EQ(2U, controller()->GetSectionEntries(0).size());
205 controller()->DidForgetEntry(GetDialogIdFromPrefId(forgotten1));
206 EXPECT_EQ(1U, controller()->GetSectionEntries(0).size());
207 controller()->DialogFinished(true);
208 EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size());
210 // Add back and test whether the same pref id is preserved.
211 StartDialog();
212 controller()->FileSelected(
213 MakeMediaGalleriesTestingPath("forgotten1"), 0, NULL);
214 controller()->DialogFinished(true);
215 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
216 MediaGalleryPrefInfo retrieved_info;
217 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(
218 MakeMediaGalleriesTestingPath("forgotten1"), &retrieved_info));
219 EXPECT_EQ(forget_preserves_pref_id, retrieved_info.pref_id == forgotten1);
221 // Add a new one and forget it & see that it's gone.
222 MediaGalleryPrefId forgotten3 = gallery_prefs()->AddGalleryByPath(
223 MakeMediaGalleriesTestingPath("forgotten3"), type);
224 StartDialog();
225 EXPECT_EQ(2U, controller()->GetSectionEntries(0).size());
226 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U,
227 controller()->GetSectionEntries(1).size());
228 controller()->DidToggleEntry(GetDialogIdFromPrefId(forgotten3), true);
229 controller()->DidForgetEntry(GetDialogIdFromPrefId(forgotten3));
230 EXPECT_EQ(2U, controller()->GetSectionEntries(0).size());
231 EXPECT_EQ(static_cast<unsigned long>(mock_gallery_locations_.num_galleries()),
232 controller()->GetSectionEntries(1).size());
233 controller()->DialogFinished(true);
234 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
237 TEST_F(MediaGalleriesPermissionControllerTest, TestForgottenUserAdded) {
238 TestForgottenType(MediaGalleryPrefInfo::kUserAdded,
239 false /* forget_preserves_pref_id */);
242 TEST_F(MediaGalleriesPermissionControllerTest, TestForgottenAutoDetected) {
243 TestForgottenType(MediaGalleryPrefInfo::kAutoDetected,
244 true /* forget_preserves_pref_id */);
247 TEST_F(MediaGalleriesPermissionControllerTest, TestForgottenScanResult) {
248 TestForgottenType(MediaGalleryPrefInfo::kScanResult,
249 true /* forget_preserves_pref_id */);
252 TEST_F(MediaGalleriesPermissionControllerTest, TestNameGeneration) {
253 MediaGalleryPrefInfo gallery;
254 gallery.pref_id = 1;
255 gallery.device_id = StorageInfo::MakeDeviceId(
256 StorageInfo::FIXED_MASS_STORAGE, "/path/to/gallery");
257 gallery.type = MediaGalleryPrefInfo::kAutoDetected;
258 std::string galleryName("/path/to/gallery");
259 #if defined(OS_CHROMEOS)
260 galleryName = "gallery";
261 #endif
262 EXPECT_EQ(galleryName, GalleryName(gallery));
264 gallery.display_name = base::ASCIIToUTF16("override");
265 EXPECT_EQ("override", GalleryName(gallery));
267 gallery.display_name = base::string16();
268 gallery.volume_label = base::ASCIIToUTF16("label");
269 EXPECT_EQ(galleryName, GalleryName(gallery));
271 gallery.path = base::FilePath(FILE_PATH_LITERAL("sub/gallery2"));
272 galleryName = "/path/to/gallery/sub/gallery2";
273 #if defined(OS_CHROMEOS)
274 galleryName = "gallery2";
275 #endif
276 #if defined(OS_WIN)
277 galleryName = base::FilePath(FILE_PATH_LITERAL("/path/to/gallery"))
278 .Append(gallery.path).MaybeAsASCII();
279 #endif
280 EXPECT_EQ(galleryName, GalleryName(gallery));
282 gallery.path = base::FilePath();
283 gallery.device_id = StorageInfo::MakeDeviceId(
284 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM,
285 "/path/to/dcim");
286 gallery.display_name = base::ASCIIToUTF16("override");
287 EXPECT_EQ("override", GalleryName(gallery));
289 gallery.volume_label = base::ASCIIToUTF16("volume");
290 gallery.vendor_name = base::ASCIIToUTF16("vendor");
291 gallery.model_name = base::ASCIIToUTF16("model");
292 EXPECT_EQ("override", GalleryName(gallery));
294 gallery.display_name = base::string16();
295 EXPECT_EQ("volume", GalleryName(gallery));
297 gallery.volume_label = base::string16();
298 EXPECT_EQ("vendor, model", GalleryName(gallery));
300 gallery.total_size_in_bytes = 1000000;
301 EXPECT_EQ("977 KB vendor, model", GalleryName(gallery));
303 gallery.path = base::FilePath(FILE_PATH_LITERAL("sub/path"));
304 EXPECT_EQ("path - 977 KB vendor, model", GalleryName(gallery));