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