1 // Copyright (c) 2012 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/strings/string_number_conversions.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/media_galleries/media_galleries_dialog_controller_mock.h"
8 #include "chrome/browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h"
9 #include "components/storage_monitor/storage_info.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::NiceMock;
16 using ::testing::Return;
17 using ::testing::ReturnPointee;
18 using ::testing::ReturnRef;
20 @interface MediaGalleryListEntry (testing)
22 - (void)performClick:(id)sender;
25 @implementation MediaGalleryListEntry (testing)
28 return [checkbox_ state];
31 - (void)performClick:(id)sender {
32 [checkbox_ performClick:sender];
38 MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId pref_id) {
39 MediaGalleryPrefInfo gallery;
40 gallery.pref_id = pref_id;
41 gallery.device_id = storage_monitor::StorageInfo::MakeDeviceId(
42 storage_monitor::StorageInfo::FIXED_MASS_STORAGE,
43 base::Int64ToString(pref_id));
44 gallery.display_name = base::ASCIIToUTF16("name");
48 class MediaGalleriesDialogTest : public testing::Test {
50 MediaGalleriesDialogTest() {}
51 virtual ~MediaGalleriesDialogTest() {}
52 virtual void SetUp() OVERRIDE {
53 dummy_extension_ = extensions::test_util::CreateExtensionWithID("dummy");
55 virtual void TearDown() OVERRIDE {
56 dummy_extension_ = NULL;
59 const extensions::Extension& dummy_extension() const {
60 return *dummy_extension_;
64 scoped_refptr<extensions::Extension> dummy_extension_;
66 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogTest);
69 // Tests that checkboxes are initialized according to the contents of
71 TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
72 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
74 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
75 attached_permissions.push_back(
76 MediaGalleriesDialogController::GalleryPermission(
77 1, MakePrefInfoForTesting(10), true));
78 attached_permissions.push_back(
79 MediaGalleriesDialogController::GalleryPermission(
80 2, MakePrefInfoForTesting(20), false));
81 EXPECT_CALL(controller, AttachedPermissions()).
82 WillRepeatedly(Return(attached_permissions));
84 MediaGalleriesDialogController::GalleryPermissionsVector
85 unattached_permissions;
86 EXPECT_CALL(controller, UnattachedPermissions()).
87 WillRepeatedly(Return(unattached_permissions));
89 // Initializing checkboxes should not cause them to be toggled.
90 EXPECT_CALL(controller, DidToggleGallery(_, _)).
93 scoped_ptr<MediaGalleriesDialogCocoa> dialog(
94 static_cast<MediaGalleriesDialogCocoa*>(
95 MediaGalleriesDialog::Create(&controller)));
96 EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
99 [[dialog->checkbox_container_ subviews] objectAtIndex:0];
100 EXPECT_EQ([checkbox1 state], NSOnState);
102 NSButton* checkbox2 =
103 [[dialog->checkbox_container_ subviews] objectAtIndex:1];
104 EXPECT_EQ([checkbox2 state], NSOffState);
107 // Tests that toggling checkboxes updates the controller.
108 TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) {
109 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
111 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
112 attached_permissions.push_back(
113 MediaGalleriesDialogController::GalleryPermission(
114 1, MakePrefInfoForTesting(10), true));
115 EXPECT_CALL(controller, AttachedPermissions()).
116 WillRepeatedly(Return(attached_permissions));
118 MediaGalleriesDialogController::GalleryPermissionsVector
119 unattached_permissions;
120 EXPECT_CALL(controller, UnattachedPermissions()).
121 WillRepeatedly(Return(unattached_permissions));
123 scoped_ptr<MediaGalleriesDialogCocoa> dialog(
124 static_cast<MediaGalleriesDialogCocoa*>(
125 MediaGalleriesDialog::Create(&controller)));
126 EXPECT_EQ(1U, [[dialog->checkbox_container_ subviews] count]);
128 NSButton* checkbox = [[dialog->checkbox_container_ subviews] objectAtIndex:0];
129 EXPECT_EQ([checkbox state], NSOnState);
131 EXPECT_CALL(controller, DidToggleGallery(1, false));
132 [checkbox performClick:nil];
133 EXPECT_EQ([checkbox state], NSOffState);
135 EXPECT_CALL(controller, DidToggleGallery(1, true));
136 [checkbox performClick:nil];
137 EXPECT_EQ([checkbox state], NSOnState);
140 // Tests that UpdateGalleries will add a new checkbox, but only if it refers to
141 // a gallery that the dialog hasn't seen before.
142 TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
143 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
145 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
146 EXPECT_CALL(controller, AttachedPermissions()).
147 WillRepeatedly(ReturnPointee(&attached_permissions));
149 MediaGalleriesDialogController::GalleryPermissionsVector
150 unattached_permissions;
151 EXPECT_CALL(controller, UnattachedPermissions()).
152 WillRepeatedly(Return(unattached_permissions));
154 scoped_ptr<MediaGalleriesDialogCocoa> dialog(
155 static_cast<MediaGalleriesDialogCocoa*>(
156 MediaGalleriesDialog::Create(&controller)));
158 EXPECT_EQ(0U, [[dialog->checkbox_container_ subviews] count]);
159 CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]);
161 attached_permissions.push_back(
162 MediaGalleriesDialogController::GalleryPermission(
163 1, MakePrefInfoForTesting(10), true));
164 dialog->UpdateGalleries();
165 EXPECT_EQ(1U, [[dialog->checkbox_container_ subviews] count]);
167 // The checkbox container should be taller.
168 CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]);
169 EXPECT_GT(new_container_height, old_container_height);
170 old_container_height = new_container_height;
172 attached_permissions.push_back(
173 MediaGalleriesDialogController::GalleryPermission(
174 2, MakePrefInfoForTesting(20), true));
175 dialog->UpdateGalleries();
176 EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
178 // The checkbox container should be taller.
179 new_container_height = NSHeight([dialog->checkbox_container_ frame]);
180 EXPECT_GT(new_container_height, old_container_height);
181 old_container_height = new_container_height;
183 attached_permissions[1].allowed = false;
184 dialog->UpdateGalleries();
185 EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
187 // The checkbox container height should not have changed.
188 new_container_height = NSHeight([dialog->checkbox_container_ frame]);
189 EXPECT_EQ(new_container_height, old_container_height);
192 TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
193 NiceMock<MediaGalleriesDialogControllerMock> controller(dummy_extension());
195 MediaGalleriesDialogController::GalleryPermissionsVector attached_permissions;
196 EXPECT_CALL(controller, AttachedPermissions()).
197 WillRepeatedly(ReturnPointee(&attached_permissions));
199 MediaGalleriesDialogController::GalleryPermissionsVector
200 unattached_permissions;
201 EXPECT_CALL(controller, UnattachedPermissions()).
202 WillRepeatedly(Return(unattached_permissions));
204 scoped_ptr<MediaGalleriesDialogCocoa> dialog(
205 static_cast<MediaGalleriesDialogCocoa*>(
206 MediaGalleriesDialog::Create(&controller)));
208 // Add a couple of galleries.
209 attached_permissions.push_back(
210 MediaGalleriesDialogController::GalleryPermission(
211 1, MakePrefInfoForTesting(10), true));
212 dialog->UpdateGalleries();
213 attached_permissions.push_back(
214 MediaGalleriesDialogController::GalleryPermission(
215 2, MakePrefInfoForTesting(20), true));
216 dialog->UpdateGalleries();
217 EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
218 CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]);
221 attached_permissions.erase(attached_permissions.begin());
222 dialog->UpdateGalleries();
223 EXPECT_EQ(1U, [[dialog->checkbox_container_ subviews] count]);
225 // The checkbox container should be shorter.
226 CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]);
227 EXPECT_LT(new_container_height, old_container_height);