1 // Copyright 2013 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.
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.h"
14 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
15 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/storage_monitor/storage_info.h"
18 #include "components/storage_monitor/test_storage_monitor.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "extensions/common/permissions/media_galleries_permission.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/login/user_manager.h"
25 #include "chrome/browser/chromeos/settings/cros_settings.h"
26 #include "chrome/browser/chromeos/settings/device_settings_service.h"
29 using storage_monitor::StorageInfo
;
30 using storage_monitor::TestStorageMonitor
;
34 std::string
GalleryName(const MediaGalleryPrefInfo
& gallery
) {
35 base::string16 name
= gallery
.GetGalleryDisplayName();
36 return base::UTF16ToASCII(name
);
39 class MockMediaGalleriesDialog
40 : public MediaGalleriesDialog
{
42 typedef base::Callback
<void(int update_count
)> DialogDestroyedCallback
;
44 explicit MockMediaGalleriesDialog(const DialogDestroyedCallback
& callback
)
46 dialog_destroyed_callback_(callback
) {
49 virtual ~MockMediaGalleriesDialog() {
50 dialog_destroyed_callback_
.Run(update_count_
);
53 // MockMediaGalleriesDialog implementation.
54 virtual void UpdateGalleries() OVERRIDE
{
58 // Number of times UpdateResults has been called.
66 DialogDestroyedCallback dialog_destroyed_callback_
;
68 DISALLOW_COPY_AND_ASSIGN(MockMediaGalleriesDialog
);
73 class MediaGalleriesDialogControllerTest
: public ::testing::Test
{
75 MediaGalleriesDialogControllerTest()
77 dialog_update_count_at_destruction_(0),
79 profile_(new TestingProfile()),
83 virtual ~MediaGalleriesDialogControllerTest() {
84 EXPECT_FALSE(controller_
);
85 EXPECT_FALSE(dialog_
);
88 virtual void SetUp() OVERRIDE
{
89 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
91 extensions::TestExtensionSystem
* extension_system(
92 static_cast<extensions::TestExtensionSystem
*>(
93 extensions::ExtensionSystem::Get(profile_
.get())));
94 extension_system
->CreateExtensionService(
95 CommandLine::ForCurrentProcess(), base::FilePath(), false);
97 gallery_prefs_
.reset(new MediaGalleriesPreferences(profile_
.get()));
99 gallery_prefs_
->EnsureInitialized(loop
.QuitClosure());
102 std::vector
<std::string
> read_permissions
;
103 read_permissions
.push_back(
104 extensions::MediaGalleriesPermission::kReadPermission
);
105 extension_
= AddMediaGalleriesApp("read", read_permissions
, profile_
.get());
108 virtual void TearDown() OVERRIDE
{
109 TestStorageMonitor::Destroy();
113 ASSERT_FALSE(controller_
);
114 controller_
= new MediaGalleriesDialogController(
116 gallery_prefs_
.get(),
117 base::Bind(&MediaGalleriesDialogControllerTest::CreateMockDialog
,
118 base::Unretained(this)),
120 &MediaGalleriesDialogControllerTest::OnControllerDone
,
121 base::Unretained(this)));
124 MediaGalleriesDialogController
* controller() {
128 MockMediaGalleriesDialog
* dialog() {
132 int dialog_update_count_at_destruction() {
133 EXPECT_FALSE(dialog_
);
134 return dialog_update_count_at_destruction_
;
137 extensions::Extension
* extension() {
138 return extension_
.get();
141 MediaGalleriesPreferences
* gallery_prefs() {
142 return gallery_prefs_
.get();
145 GalleryDialogId
GetDialogIdFromPrefId(MediaGalleryPrefId pref_id
);
147 void TestForgottenType(MediaGalleryPrefInfo::Type type
);
150 EnsureMediaDirectoriesExists mock_gallery_locations_
;
153 MediaGalleriesDialog
* CreateMockDialog(
154 MediaGalleriesDialogController
* controller
) {
155 EXPECT_FALSE(dialog_
);
156 dialog_update_count_at_destruction_
= 0;
157 dialog_
= new MockMediaGalleriesDialog(base::Bind(
158 &MediaGalleriesDialogControllerTest::OnDialogDestroyed
,
159 weak_factory_
.GetWeakPtr()));
163 void OnDialogDestroyed(int update_count
) {
164 EXPECT_TRUE(dialog_
);
165 dialog_update_count_at_destruction_
= update_count
;
169 void OnControllerDone() {
173 // Needed for extension service & friends to work.
174 content::TestBrowserThreadBundle thread_bundle_
;
176 // The dialog is owned by the controller, but this pointer should only be
177 // valid while the dialog is live within the controller.
178 MockMediaGalleriesDialog
* dialog_
;
179 int dialog_update_count_at_destruction_
;
181 // The controller owns itself.
182 MediaGalleriesDialogController
* controller_
;
184 scoped_refptr
<extensions::Extension
> extension_
;
186 #if defined OS_CHROMEOS
187 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
188 chromeos::ScopedTestCrosSettings test_cros_settings_
;
189 chromeos::ScopedTestUserManager test_user_manager_
;
192 TestStorageMonitor monitor_
;
193 scoped_ptr
<TestingProfile
> profile_
;
194 scoped_ptr
<MediaGalleriesPreferences
> gallery_prefs_
;
196 base::WeakPtrFactory
<MediaGalleriesDialogControllerTest
>
199 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogControllerTest
);
203 MediaGalleriesDialogControllerTest::GetDialogIdFromPrefId(
204 MediaGalleryPrefId pref_id
) {
205 return controller_
->GetDialogId(pref_id
);
208 void MediaGalleriesDialogControllerTest::TestForgottenType(
209 MediaGalleryPrefInfo::Type type
) {
210 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());
212 MediaGalleryPrefId forgotten1
= gallery_prefs()->AddGalleryByPath(
213 MakeMediaGalleriesTestingPath("forgotten1"), type
);
214 MediaGalleryPrefId forgotten2
= gallery_prefs()->AddGalleryByPath(
215 MakeMediaGalleriesTestingPath("forgotten2"), type
);
216 // Show dialog and accept to verify 2 entries
218 EXPECT_EQ(mock_gallery_locations_
.num_galleries() + 2U,
219 controller()->AttachedPermissions().size());
220 EXPECT_EQ(0U, controller()->UnattachedPermissions().size());
221 controller()->DidToggleGallery(GetDialogIdFromPrefId(forgotten1
), true);
222 controller()->DidToggleGallery(GetDialogIdFromPrefId(forgotten2
), true);
223 controller()->DialogFinished(true);
224 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
226 // Forget one and cancel to see that it's still there.
228 controller()->DidForgetGallery(GetDialogIdFromPrefId(forgotten1
));
229 EXPECT_EQ(mock_gallery_locations_
.num_galleries() + 1U,
230 controller()->AttachedPermissions().size());
231 controller()->DialogFinished(false);
232 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
234 // Forget one and confirm to see that it's gone.
236 controller()->DidForgetGallery(GetDialogIdFromPrefId(forgotten1
));
237 EXPECT_EQ(mock_gallery_locations_
.num_galleries() + 1U,
238 controller()->AttachedPermissions().size());
239 controller()->DialogFinished(true);
240 EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size());
242 // Add a new one and forget it & see that it's gone.
243 MediaGalleryPrefId forgotten3
= gallery_prefs()->AddGalleryByPath(
244 MakeMediaGalleriesTestingPath("forgotten3"), type
);
246 EXPECT_EQ(mock_gallery_locations_
.num_galleries() + 2U,
247 controller()->AttachedPermissions().size());
248 EXPECT_EQ(0U, controller()->UnattachedPermissions().size());
249 controller()->DidToggleGallery(GetDialogIdFromPrefId(forgotten3
), true);
250 controller()->DidForgetGallery(GetDialogIdFromPrefId(forgotten3
));
251 EXPECT_EQ(mock_gallery_locations_
.num_galleries() + 1U,
252 controller()->AttachedPermissions().size());
253 controller()->DialogFinished(true);
254 EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size());
257 TEST_F(MediaGalleriesDialogControllerTest
, TestForgottenUserAdded
) {
258 TestForgottenType(MediaGalleryPrefInfo::kUserAdded
);
261 TEST_F(MediaGalleriesDialogControllerTest
, TestForgottenAutoDetected
) {
262 TestForgottenType(MediaGalleryPrefInfo::kAutoDetected
);
265 TEST_F(MediaGalleriesDialogControllerTest
, TestForgottenScanResult
) {
266 TestForgottenType(MediaGalleryPrefInfo::kScanResult
);
269 TEST_F(MediaGalleriesDialogControllerTest
, TestNameGeneration
) {
270 MediaGalleryPrefInfo gallery
;
272 gallery
.device_id
= StorageInfo::MakeDeviceId(
273 StorageInfo::FIXED_MASS_STORAGE
, "/path/to/gallery");
274 gallery
.type
= MediaGalleryPrefInfo::kAutoDetected
;
275 std::string
galleryName("/path/to/gallery");
276 #if defined(OS_CHROMEOS)
277 galleryName
= "gallery";
279 EXPECT_EQ(galleryName
, GalleryName(gallery
));
281 gallery
.display_name
= base::ASCIIToUTF16("override");
282 EXPECT_EQ("override", GalleryName(gallery
));
284 gallery
.display_name
= base::string16();
285 gallery
.volume_label
= base::ASCIIToUTF16("label");
286 EXPECT_EQ(galleryName
, GalleryName(gallery
));
288 gallery
.path
= base::FilePath(FILE_PATH_LITERAL("sub/gallery2"));
289 galleryName
= "/path/to/gallery/sub/gallery2";
290 #if defined(OS_CHROMEOS)
291 galleryName
= "gallery2";
294 galleryName
= base::FilePath(FILE_PATH_LITERAL("/path/to/gallery"))
295 .Append(gallery
.path
).MaybeAsASCII();
297 EXPECT_EQ(galleryName
, GalleryName(gallery
));
299 gallery
.path
= base::FilePath();
300 gallery
.device_id
= StorageInfo::MakeDeviceId(
301 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM
,
303 gallery
.display_name
= base::ASCIIToUTF16("override");
304 EXPECT_EQ("override", GalleryName(gallery
));
306 gallery
.volume_label
= base::ASCIIToUTF16("volume");
307 gallery
.vendor_name
= base::ASCIIToUTF16("vendor");
308 gallery
.model_name
= base::ASCIIToUTF16("model");
309 EXPECT_EQ("override", GalleryName(gallery
));
311 gallery
.display_name
= base::string16();
312 EXPECT_EQ("volume", GalleryName(gallery
));
314 gallery
.volume_label
= base::string16();
315 EXPECT_EQ("vendor, model", GalleryName(gallery
));
317 gallery
.total_size_in_bytes
= 1000000;
318 EXPECT_EQ("977 KB vendor, model", GalleryName(gallery
));
320 gallery
.path
= base::FilePath(FILE_PATH_LITERAL("sub/path"));
321 EXPECT_EQ("path - 977 KB vendor, model", GalleryName(gallery
));