Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: chrome/browser/media_galleries/media_galleries_permission_controller_unittest.cc

Issue 310383004: Make a controller interface for media galleries dialogs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/string16.h" 9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/extensions/test_extension_system.h" 12 #include "chrome/browser/extensions/test_extension_system.h"
13 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" 13 #include "chrome/browser/media_galleries/media_galleries_permission_controller.h "
14 #include "chrome/browser/media_galleries/media_galleries_preferences.h" 14 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
15 #include "chrome/browser/media_galleries/media_galleries_test_util.h" 15 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
16 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "components/storage_monitor/storage_info.h" 17 #include "components/storage_monitor/storage_info.h"
18 #include "components/storage_monitor/test_storage_monitor.h" 18 #include "components/storage_monitor/test_storage_monitor.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "extensions/common/permissions/media_galleries_permission.h" 20 #include "extensions/common/permissions/media_galleries_permission.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 #if defined(OS_CHROMEOS) 23 #if defined(OS_CHROMEOS)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 private: 63 private:
64 int update_count_; 64 int update_count_;
65 65
66 DialogDestroyedCallback dialog_destroyed_callback_; 66 DialogDestroyedCallback dialog_destroyed_callback_;
67 67
68 DISALLOW_COPY_AND_ASSIGN(MockMediaGalleriesDialog); 68 DISALLOW_COPY_AND_ASSIGN(MockMediaGalleriesDialog);
69 }; 69 };
70 70
71 } // namespace 71 } // namespace
72 72
73 class MediaGalleriesDialogControllerTest : public ::testing::Test { 73 class MediaGalleriesPermissionControllerTest : public ::testing::Test {
74 public: 74 public:
75 MediaGalleriesDialogControllerTest() 75 MediaGalleriesPermissionControllerTest()
76 : dialog_(NULL), 76 : dialog_(NULL),
77 dialog_update_count_at_destruction_(0), 77 dialog_update_count_at_destruction_(0),
78 controller_(NULL), 78 controller_(NULL),
79 profile_(new TestingProfile()), 79 profile_(new TestingProfile()),
80 weak_factory_(this) { 80 weak_factory_(this) {
81 } 81 }
82 82
83 virtual ~MediaGalleriesDialogControllerTest() { 83 virtual ~MediaGalleriesPermissionControllerTest() {
84 EXPECT_FALSE(controller_); 84 EXPECT_FALSE(controller_);
85 EXPECT_FALSE(dialog_); 85 EXPECT_FALSE(dialog_);
86 } 86 }
87 87
88 virtual void SetUp() OVERRIDE { 88 virtual void SetUp() OVERRIDE {
89 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall()); 89 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
90 90
91 extensions::TestExtensionSystem* extension_system( 91 extensions::TestExtensionSystem* extension_system(
92 static_cast<extensions::TestExtensionSystem*>( 92 static_cast<extensions::TestExtensionSystem*>(
93 extensions::ExtensionSystem::Get(profile_.get()))); 93 extensions::ExtensionSystem::Get(profile_.get())));
(...skipping 10 matching lines...) Expand all
104 extensions::MediaGalleriesPermission::kReadPermission); 104 extensions::MediaGalleriesPermission::kReadPermission);
105 extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get()); 105 extension_ = AddMediaGalleriesApp("read", read_permissions, profile_.get());
106 } 106 }
107 107
108 virtual void TearDown() OVERRIDE { 108 virtual void TearDown() OVERRIDE {
109 TestStorageMonitor::Destroy(); 109 TestStorageMonitor::Destroy();
110 } 110 }
111 111
112 void StartDialog() { 112 void StartDialog() {
113 ASSERT_FALSE(controller_); 113 ASSERT_FALSE(controller_);
114 controller_ = new MediaGalleriesDialogController( 114 controller_ = new MediaGalleriesPermissionController(
115 *extension_.get(), 115 *extension_.get(),
116 gallery_prefs_.get(), 116 gallery_prefs_.get(),
117 base::Bind(&MediaGalleriesDialogControllerTest::CreateMockDialog, 117 base::Bind(&MediaGalleriesPermissionControllerTest::CreateMockDialog,
118 base::Unretained(this)), 118 base::Unretained(this)),
119 base::Bind( 119 base::Bind(
120 &MediaGalleriesDialogControllerTest::OnControllerDone, 120 &MediaGalleriesPermissionControllerTest::OnControllerDone,
121 base::Unretained(this))); 121 base::Unretained(this)));
122 } 122 }
123 123
124 MediaGalleriesDialogController* controller() { 124 MediaGalleriesPermissionController* controller() {
125 return controller_; 125 return controller_;
126 } 126 }
127 127
128 MockMediaGalleriesDialog* dialog() { 128 MockMediaGalleriesDialog* dialog() {
129 return dialog_; 129 return dialog_;
130 } 130 }
131 131
132 int dialog_update_count_at_destruction() { 132 int dialog_update_count_at_destruction() {
133 EXPECT_FALSE(dialog_); 133 EXPECT_FALSE(dialog_);
134 return dialog_update_count_at_destruction_; 134 return dialog_update_count_at_destruction_;
(...skipping 13 matching lines...) Expand all
148 148
149 protected: 149 protected:
150 EnsureMediaDirectoriesExists mock_gallery_locations_; 150 EnsureMediaDirectoriesExists mock_gallery_locations_;
151 151
152 private: 152 private:
153 MediaGalleriesDialog* CreateMockDialog( 153 MediaGalleriesDialog* CreateMockDialog(
154 MediaGalleriesDialogController* controller) { 154 MediaGalleriesDialogController* controller) {
155 EXPECT_FALSE(dialog_); 155 EXPECT_FALSE(dialog_);
156 dialog_update_count_at_destruction_ = 0; 156 dialog_update_count_at_destruction_ = 0;
157 dialog_ = new MockMediaGalleriesDialog(base::Bind( 157 dialog_ = new MockMediaGalleriesDialog(base::Bind(
158 &MediaGalleriesDialogControllerTest::OnDialogDestroyed, 158 &MediaGalleriesPermissionControllerTest::OnDialogDestroyed,
159 weak_factory_.GetWeakPtr())); 159 weak_factory_.GetWeakPtr()));
160 return dialog_; 160 return dialog_;
161 } 161 }
162 162
163 void OnDialogDestroyed(int update_count) { 163 void OnDialogDestroyed(int update_count) {
164 EXPECT_TRUE(dialog_); 164 EXPECT_TRUE(dialog_);
165 dialog_update_count_at_destruction_ = update_count; 165 dialog_update_count_at_destruction_ = update_count;
166 dialog_ = NULL; 166 dialog_ = NULL;
167 } 167 }
168 168
169 void OnControllerDone() { 169 void OnControllerDone() {
170 controller_ = NULL; 170 controller_ = NULL;
171 } 171 }
172 172
173 // Needed for extension service & friends to work. 173 // Needed for extension service & friends to work.
174 content::TestBrowserThreadBundle thread_bundle_; 174 content::TestBrowserThreadBundle thread_bundle_;
175 175
176 // The dialog is owned by the controller, but this pointer should only be 176 // The dialog is owned by the controller, but this pointer should only be
177 // valid while the dialog is live within the controller. 177 // valid while the dialog is live within the controller.
178 MockMediaGalleriesDialog* dialog_; 178 MockMediaGalleriesDialog* dialog_;
179 int dialog_update_count_at_destruction_; 179 int dialog_update_count_at_destruction_;
180 180
181 // The controller owns itself. 181 // The controller owns itself.
182 MediaGalleriesDialogController* controller_; 182 MediaGalleriesPermissionController* controller_;
183 183
184 scoped_refptr<extensions::Extension> extension_; 184 scoped_refptr<extensions::Extension> extension_;
185 185
186 #if defined OS_CHROMEOS 186 #if defined OS_CHROMEOS
187 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 187 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
188 chromeos::ScopedTestCrosSettings test_cros_settings_; 188 chromeos::ScopedTestCrosSettings test_cros_settings_;
189 chromeos::ScopedTestUserManager test_user_manager_; 189 chromeos::ScopedTestUserManager test_user_manager_;
190 #endif 190 #endif
191 191
192 TestStorageMonitor monitor_; 192 TestStorageMonitor monitor_;
193 scoped_ptr<TestingProfile> profile_; 193 scoped_ptr<TestingProfile> profile_;
194 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_; 194 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
195 195
196 base::WeakPtrFactory<MediaGalleriesDialogControllerTest> 196 base::WeakPtrFactory<MediaGalleriesPermissionControllerTest>
197 weak_factory_; 197 weak_factory_;
198 198
199 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogControllerTest); 199 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPermissionControllerTest);
200 }; 200 };
201 201
202 GalleryDialogId 202 GalleryDialogId
203 MediaGalleriesDialogControllerTest::GetDialogIdFromPrefId( 203 MediaGalleriesPermissionControllerTest::GetDialogIdFromPrefId(
204 MediaGalleryPrefId pref_id) { 204 MediaGalleryPrefId pref_id) {
205 return controller_->GetDialogId(pref_id); 205 return controller_->GetDialogId(pref_id);
206 } 206 }
207 207
208 void MediaGalleriesDialogControllerTest::TestForgottenType( 208 void MediaGalleriesPermissionControllerTest::TestForgottenType(
209 MediaGalleryPrefInfo::Type type) { 209 MediaGalleryPrefInfo::Type type) {
210 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size()); 210 EXPECT_EQ(0U, gallery_prefs()->GalleriesForExtension(*extension()).size());
211 211
212 MediaGalleryPrefId forgotten1 = gallery_prefs()->AddGalleryByPath( 212 MediaGalleryPrefId forgotten1 = gallery_prefs()->AddGalleryByPath(
213 MakeMediaGalleriesTestingPath("forgotten1"), type); 213 MakeMediaGalleriesTestingPath("forgotten1"), type);
214 MediaGalleryPrefId forgotten2 = gallery_prefs()->AddGalleryByPath( 214 MediaGalleryPrefId forgotten2 = gallery_prefs()->AddGalleryByPath(
215 MakeMediaGalleriesTestingPath("forgotten2"), type); 215 MakeMediaGalleriesTestingPath("forgotten2"), type);
216 // Show dialog and accept to verify 2 entries 216 // Show dialog and accept to verify 2 entries
217 StartDialog(); 217 StartDialog();
218 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 2U, 218 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 2U,
219 controller()->AttachedPermissions().size()); 219 controller()->GetSectionEntries(0).size());
220 EXPECT_EQ(0U, controller()->UnattachedPermissions().size()); 220 EXPECT_EQ(0U, controller()->GetSectionEntries(1).size());
221 controller()->DidToggleGallery(GetDialogIdFromPrefId(forgotten1), true); 221 controller()->DidToggleEntry(GetDialogIdFromPrefId(forgotten1), true);
222 controller()->DidToggleGallery(GetDialogIdFromPrefId(forgotten2), true); 222 controller()->DidToggleEntry(GetDialogIdFromPrefId(forgotten2), true);
223 controller()->DialogFinished(true); 223 controller()->DialogFinished(true);
224 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size()); 224 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
225 225
226 // Forget one and cancel to see that it's still there. 226 // Forget one and cancel to see that it's still there.
227 StartDialog(); 227 StartDialog();
228 controller()->DidForgetGallery(GetDialogIdFromPrefId(forgotten1)); 228 controller()->DidForgetEntry(GetDialogIdFromPrefId(forgotten1));
229 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U, 229 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U,
230 controller()->AttachedPermissions().size()); 230 controller()->GetSectionEntries(0).size());
231 controller()->DialogFinished(false); 231 controller()->DialogFinished(false);
232 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size()); 232 EXPECT_EQ(2U, gallery_prefs()->GalleriesForExtension(*extension()).size());
233 233
234 // Forget one and confirm to see that it's gone. 234 // Forget one and confirm to see that it's gone.
235 StartDialog(); 235 StartDialog();
236 controller()->DidForgetGallery(GetDialogIdFromPrefId(forgotten1)); 236 controller()->DidForgetEntry(GetDialogIdFromPrefId(forgotten1));
237 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U, 237 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U,
238 controller()->AttachedPermissions().size()); 238 controller()->GetSectionEntries(0).size());
239 controller()->DialogFinished(true); 239 controller()->DialogFinished(true);
240 EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size()); 240 EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size());
241 241
242 // Add a new one and forget it & see that it's gone. 242 // Add a new one and forget it & see that it's gone.
243 MediaGalleryPrefId forgotten3 = gallery_prefs()->AddGalleryByPath( 243 MediaGalleryPrefId forgotten3 = gallery_prefs()->AddGalleryByPath(
244 MakeMediaGalleriesTestingPath("forgotten3"), type); 244 MakeMediaGalleriesTestingPath("forgotten3"), type);
245 StartDialog(); 245 StartDialog();
246 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 2U, 246 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 2U,
247 controller()->AttachedPermissions().size()); 247 controller()->GetSectionEntries(0).size());
248 EXPECT_EQ(0U, controller()->UnattachedPermissions().size()); 248 EXPECT_EQ(0U, controller()->GetSectionEntries(1).size());
249 controller()->DidToggleGallery(GetDialogIdFromPrefId(forgotten3), true); 249 controller()->DidToggleEntry(GetDialogIdFromPrefId(forgotten3), true);
250 controller()->DidForgetGallery(GetDialogIdFromPrefId(forgotten3)); 250 controller()->DidForgetEntry(GetDialogIdFromPrefId(forgotten3));
251 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U, 251 EXPECT_EQ(mock_gallery_locations_.num_galleries() + 1U,
252 controller()->AttachedPermissions().size()); 252 controller()->GetSectionEntries(0).size());
253 controller()->DialogFinished(true); 253 controller()->DialogFinished(true);
254 EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size()); 254 EXPECT_EQ(1U, gallery_prefs()->GalleriesForExtension(*extension()).size());
255 } 255 }
256 256
257 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenUserAdded) { 257 TEST_F(MediaGalleriesPermissionControllerTest, TestForgottenUserAdded) {
258 TestForgottenType(MediaGalleryPrefInfo::kUserAdded); 258 TestForgottenType(MediaGalleryPrefInfo::kUserAdded);
259 } 259 }
260 260
261 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenAutoDetected) { 261 TEST_F(MediaGalleriesPermissionControllerTest, TestForgottenAutoDetected) {
262 TestForgottenType(MediaGalleryPrefInfo::kAutoDetected); 262 TestForgottenType(MediaGalleryPrefInfo::kAutoDetected);
263 } 263 }
264 264
265 TEST_F(MediaGalleriesDialogControllerTest, TestForgottenScanResult) { 265 TEST_F(MediaGalleriesPermissionControllerTest, TestForgottenScanResult) {
266 TestForgottenType(MediaGalleryPrefInfo::kScanResult); 266 TestForgottenType(MediaGalleryPrefInfo::kScanResult);
267 } 267 }
268 268
269 TEST_F(MediaGalleriesDialogControllerTest, TestNameGeneration) { 269 TEST_F(MediaGalleriesPermissionControllerTest, TestNameGeneration) {
270 MediaGalleryPrefInfo gallery; 270 MediaGalleryPrefInfo gallery;
271 gallery.pref_id = 1; 271 gallery.pref_id = 1;
272 gallery.device_id = StorageInfo::MakeDeviceId( 272 gallery.device_id = StorageInfo::MakeDeviceId(
273 StorageInfo::FIXED_MASS_STORAGE, "/path/to/gallery"); 273 StorageInfo::FIXED_MASS_STORAGE, "/path/to/gallery");
274 gallery.type = MediaGalleryPrefInfo::kAutoDetected; 274 gallery.type = MediaGalleryPrefInfo::kAutoDetected;
275 std::string galleryName("/path/to/gallery"); 275 std::string galleryName("/path/to/gallery");
276 #if defined(OS_CHROMEOS) 276 #if defined(OS_CHROMEOS)
277 galleryName = "gallery"; 277 galleryName = "gallery";
278 #endif 278 #endif
279 EXPECT_EQ(galleryName, GalleryName(gallery)); 279 EXPECT_EQ(galleryName, GalleryName(gallery));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 313
314 gallery.volume_label = base::string16(); 314 gallery.volume_label = base::string16();
315 EXPECT_EQ("vendor, model", GalleryName(gallery)); 315 EXPECT_EQ("vendor, model", GalleryName(gallery));
316 316
317 gallery.total_size_in_bytes = 1000000; 317 gallery.total_size_in_bytes = 1000000;
318 EXPECT_EQ("977 KB vendor, model", GalleryName(gallery)); 318 EXPECT_EQ("977 KB vendor, model", GalleryName(gallery));
319 319
320 gallery.path = base::FilePath(FILE_PATH_LITERAL("sub/path")); 320 gallery.path = base::FilePath(FILE_PATH_LITERAL("sub/path"));
321 EXPECT_EQ("path - 977 KB vendor, model", GalleryName(gallery)); 321 EXPECT_EQ("path - 977 KB vendor, model", GalleryName(gallery));
322 } 322 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698