OLD | NEW |
| (Empty) |
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. | |
4 | |
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.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" | |
22 | |
23 #if defined(OS_CHROMEOS) | |
24 #include "chrome/browser/chromeos/login/users/user_manager.h" | |
25 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
26 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
27 #endif | |
28 | |
29 using storage_monitor::StorageInfo; | |
30 using storage_monitor::TestStorageMonitor; | |
31 | |
32 namespace { | |
33 | |
34 std::string GalleryName(const MediaGalleryPrefInfo& gallery) { | |
35 base::string16 name = gallery.GetGalleryDisplayName(); | |
36 return base::UTF16ToASCII(name); | |
37 } | |
38 | |
39 class MockMediaGalleriesDialog | |
40 : public MediaGalleriesDialog { | |
41 public: | |
42 typedef base::Callback<void(int update_count)> DialogDestroyedCallback; | |
43 | |
44 explicit MockMediaGalleriesDialog(const DialogDestroyedCallback& callback) | |
45 : update_count_(0), | |
46 dialog_destroyed_callback_(callback) { | |
47 } | |
48 | |
49 virtual ~MockMediaGalleriesDialog() { | |
50 dialog_destroyed_callback_.Run(update_count_); | |
51 } | |
52 | |
53 // MockMediaGalleriesDialog implementation. | |
54 virtual void UpdateGalleries() OVERRIDE { | |
55 update_count_++; | |
56 } | |
57 | |
58 // Number of times UpdateResults has been called. | |
59 int update_count() { | |
60 return update_count_; | |
61 } | |
62 | |
63 private: | |
64 int update_count_; | |
65 | |
66 DialogDestroyedCallback dialog_destroyed_callback_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(MockMediaGalleriesDialog); | |
69 }; | |
70 | |
71 } // namespace | |
72 | |
73 class MediaGalleriesDialogControllerTest : public ::testing::Test { | |
74 public: | |
75 MediaGalleriesDialogControllerTest() | |
76 : dialog_(NULL), | |
77 dialog_update_count_at_destruction_(0), | |
78 controller_(NULL), | |
79 profile_(new TestingProfile()), | |
80 weak_factory_(this) { | |
81 } | |
82 | |
83 virtual ~MediaGalleriesDialogControllerTest() { | |
84 EXPECT_FALSE(controller_); | |
85 EXPECT_FALSE(dialog_); | |
86 } | |
87 | |
88 virtual void SetUp() OVERRIDE { | |
89 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall()); | |
90 | |
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); | |
96 | |
97 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get())); | |
98 base::RunLoop loop; | |
99 gallery_prefs_->EnsureInitialized(loop.QuitClosure()); | |
100 loop.Run(); | |
101 | |
102 std::vector<std::string> read_permissions; | |
103 read_permissions.push_back( | |
104 extensions::MediaGalleriesPermission::kReadPermission); | |
105 extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get()); | |
106 } | |
107 | |
108 virtual void TearDown() OVERRIDE { | |
109 TestStorageMonitor::Destroy(); | |
110 } | |
111 | |
112 void StartDialog() { | |
113 ASSERT_FALSE(controller_); | |
114 controller_ = new MediaGalleriesDialogController( | |
115 *extension_.get(), | |
116 gallery_prefs_.get(), | |
117 base::Bind(&MediaGalleriesDialogControllerTest::CreateMockDialog, | |
118 base::Unretained(this)), | |
119 base::Bind( | |
120 &MediaGalleriesDialogControllerTest::OnControllerDone, | |
121 base::Unretained(this))); | |
122 } | |
123 | |
124 MediaGalleriesDialogController* controller() { | |
125 return controller_; | |
126 } | |
127 | |
128 MockMediaGalleriesDialog* dialog() { | |
129 return dialog_; | |
130 } | |
131 | |
132 int dialog_update_count_at_destruction() { | |
133 EXPECT_FALSE(dialog_); | |
134 return dialog_update_count_at_destruction_; | |
135 } | |
136 | |
137 extensions::Extension* extension() { | |
138 return extension_.get(); | |
139 } | |
140 | |
141 MediaGalleriesPreferences* gallery_prefs() { | |
142 return gallery_prefs_.get(); | |
143 } | |
144 | |
145 GalleryDialogId GetDialogIdFromPrefId(MediaGalleryPrefId pref_id); | |
146 | |
147 void TestForgottenType(MediaGalleryPrefInfo::Type type); | |
148 | |
149 protected: | |
150 EnsureMediaDirectoriesExists mock_gallery_locations_; | |
151 | |
152 private: | |
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())); | |
160 return dialog_; | |
161 } | |
162 | |
163 void OnDialogDestroyed(int update_count) { | |
164 EXPECT_TRUE(dialog_); | |
165 dialog_update_count_at_destruction_ = update_count; | |
166 dialog_ = NULL; | |
167 } | |
168 | |
169 void OnControllerDone() { | |
170 controller_ = NULL; | |
171 } | |
172 | |
173 // Needed for extension service & friends to work. | |
174 content::TestBrowserThreadBundle thread_bundle_; | |
175 | |
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_; | |
180 | |
181 // The controller owns itself. | |
182 MediaGalleriesDialogController* controller_; | |
183 | |
184 scoped_refptr<extensions::Extension> extension_; | |
185 | |
186 #if defined OS_CHROMEOS | |
187 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | |
188 chromeos::ScopedTestCrosSettings test_cros_settings_; | |
189 chromeos::ScopedTestUserManager test_user_manager_; | |
190 #endif | |
191 | |
192 TestStorageMonitor monitor_; | |
193 scoped_ptr<TestingProfile> profile_; | |
194 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_; | |
195 | |
196 base::WeakPtrFactory<MediaGalleriesDialogControllerTest> | |
197 weak_factory_; | |
198 | |
199 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogControllerTest); | |
200 }; | |
201 | |
202 GalleryDialogId | |
203 MediaGalleriesDialogControllerTest::GetDialogIdFromPrefId( | |
204 MediaGalleryPrefId pref_id) { | |
205 return controller_->GetDialogId(pref_id); | |
206 } | |
207 | |
208 void MediaGalleriesDialogControllerTest::TestForgottenType( | |
209 MediaGalleryPrefInfo::Type type) { | |
210 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); | |
211 | |
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 | |
217 StartDialog(); | |
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()); | |
225 | |
226 // Forget one and cancel to see that it's still there. | |
227 StartDialog(); | |
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()); | |
233 | |
234 // Forget one and confirm to see that it's gone. | |
235 StartDialog(); | |
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()); | |
241 | |
242 // Add a new one and forget it & see that it's gone. | |
243 MediaGalleryPrefId forgotten3 = gallery_prefs()->AddGalleryByPath( | |
244 MakeMediaGalleriesTestingPath("forgotten3"), type); | |
245 StartDialog(); | |
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()); | |
255 } | |
256 | |
257 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenUserAdded) { | |
258 TestForgottenType(MediaGalleryPrefInfo::kUserAdded); | |
259 } | |
260 | |
261 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenAutoDetected) { | |
262 TestForgottenType(MediaGalleryPrefInfo::kAutoDetected); | |
263 } | |
264 | |
265 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenScanResult) { | |
266 TestForgottenType(MediaGalleryPrefInfo::kScanResult); | |
267 } | |
268 | |
269 TEST_F(MediaGalleriesDialogControllerTest, TestNameGeneration) { | |
270 MediaGalleryPrefInfo gallery; | |
271 gallery.pref_id = 1; | |
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"; | |
278 #endif | |
279 EXPECT_EQ(galleryName, GalleryName(gallery)); | |
280 | |
281 gallery.display_name = base::ASCIIToUTF16("override"); | |
282 EXPECT_EQ("override", GalleryName(gallery)); | |
283 | |
284 gallery.display_name = base::string16(); | |
285 gallery.volume_label = base::ASCIIToUTF16("label"); | |
286 EXPECT_EQ(galleryName, GalleryName(gallery)); | |
287 | |
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"; | |
292 #endif | |
293 #if defined(OS_WIN) | |
294 galleryName = base::FilePath(FILE_PATH_LITERAL("/path/to/gallery")) | |
295 .Append(gallery.path).MaybeAsASCII(); | |
296 #endif | |
297 EXPECT_EQ(galleryName, GalleryName(gallery)); | |
298 | |
299 gallery.path = base::FilePath(); | |
300 gallery.device_id = StorageInfo::MakeDeviceId( | |
301 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, | |
302 "/path/to/dcim"); | |
303 gallery.display_name = base::ASCIIToUTF16("override"); | |
304 EXPECT_EQ("override", GalleryName(gallery)); | |
305 | |
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)); | |
310 | |
311 gallery.display_name = base::string16(); | |
312 EXPECT_EQ("volume", GalleryName(gallery)); | |
313 | |
314 gallery.volume_label = base::string16(); | |
315 EXPECT_EQ("vendor, model", GalleryName(gallery)); | |
316 | |
317 gallery.total_size_in_bytes = 1000000; | |
318 EXPECT_EQ("977 KB vendor, model", GalleryName(gallery)); | |
319 | |
320 gallery.path = base::FilePath(FILE_PATH_LITERAL("sub/path")); | |
321 EXPECT_EQ("path - 977 KB vendor, model", GalleryName(gallery)); | |
322 } | |
OLD | NEW |