OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/permission_bubble_controller.h" | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 #include "base/strings/sys_string_conversions.h" | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
11 #import "chrome/browser/ui/cocoa/permission_bubble_cocoa.h" | |
12 #include "chrome/browser/ui/cocoa/run_loop_testing.h" | |
13 #include "chrome/browser/ui/website_settings/mock_permission_bubble_delegate.h" | |
14 #import "ui/base/test/cocoa_test_event_utils.h" | |
15 | |
16 @interface PermissionBubbleController (ExposedForTesting) | |
17 - (void)ok:(id)sender; | |
18 - (void)onAllow:(id)sender; | |
19 - (void)onBlock:(id)sender; | |
20 - (void)onCustomize:(id)sender; | |
21 - (void)onCheckboxChanged:(id)sender; | |
22 @end | |
23 | |
24 namespace { | |
25 const char* const kOKButtonLabel = "OK"; | |
26 const char* const kAllowButtonLabel = "Allow"; | |
27 const char* const kBlockButtonLabel = "Block"; | |
28 const char* const kCustomizeLabel = "Customize"; | |
29 const char* const kPermissionA = "Permission A"; | |
30 const char* const kPermissionB = "Permission B"; | |
31 const char* const kPermissionC = "Permission C"; | |
32 } | |
33 | |
34 class PermissionBubbleControllerTest : public CocoaTest, | |
35 public PermissionBubbleView::Delegate { | |
36 public: | |
37 | |
38 MOCK_METHOD2(ToggleAccept, void(int, bool)); | |
39 MOCK_METHOD0(SetCustomizationMode, void()); | |
40 MOCK_METHOD0(Accept, void()); | |
41 MOCK_METHOD0(Deny, void()); | |
42 MOCK_METHOD0(Closing, void()); | |
43 | |
44 virtual void SetUp() OVERRIDE { | |
45 CocoaTest::SetUp(); | |
46 bridge_.reset(new PermissionBubbleCocoa(nil)); | |
47 controller_ = [[PermissionBubbleController alloc] | |
48 initWithParentWindow:test_window() | |
49 bridge:bridge_.get()]; | |
50 } | |
51 | |
52 virtual void TearDown() OVERRIDE { | |
53 [controller_ close]; | |
54 chrome::testing::NSRunLoopRunAllPending(); | |
55 CocoaTest::TearDown(); | |
56 } | |
57 | |
58 NSButton* FindButtonWithTitle(const std::string& text) { | |
59 NSView* parent = base::mac::ObjCCast<NSView>([controller_ bubble]); | |
groby-ooo-7-16
2014/02/04 02:39:30
Since parent _must_ be an NSView, ObjCCastStrict i
leng
2014/02/04 21:48:22
Done.
| |
60 NSString* title = base::SysUTF8ToNSString(text); | |
61 for (NSView* child in [parent subviews]) { | |
62 if ([child isKindOfClass:[NSButton class]]) { | |
63 NSButton* button = base::mac::ObjCCast<NSButton>(child); | |
groby-ooo-7-16
2014/02/04 02:39:30
You can skip the isKindOfClass - ObjCCast will ret
leng
2014/02/04 21:48:22
Cool! Done.
| |
64 if ([title isEqualToString:[button title]]) { | |
65 return button; | |
66 } | |
67 } | |
68 } | |
69 return nil; | |
70 } | |
71 | |
72 NSTextField* FindTextFieldWithString(const std::string& text) { | |
73 NSView* parent = base::mac::ObjCCast<NSView>([controller_ bubble]); | |
groby-ooo-7-16
2014/02/04 02:39:30
See above on strict
leng
2014/02/04 21:48:22
Done.
| |
74 NSString* title = base::SysUTF8ToNSString(text); | |
75 for (NSView* child in [parent subviews]) { | |
76 if ([child isKindOfClass:[NSTextField class]]) { | |
77 NSTextField* textField = base::mac::ObjCCast<NSTextField>(child); | |
groby-ooo-7-16
2014/02/04 02:39:30
See above on isKindOfClass
leng
2014/02/04 21:48:22
Done.
| |
78 if ([[textField stringValue] hasSuffix:title]) { | |
79 return textField; | |
80 } | |
81 } | |
82 } | |
83 return nil; | |
84 } | |
85 | |
86 protected: | |
87 PermissionBubbleController* controller_; // Weak; it deletes itself. | |
88 scoped_ptr<PermissionBubbleCocoa> bridge_; | |
89 }; | |
90 | |
91 TEST_F(PermissionBubbleControllerTest, ShowAndClose) { | |
92 EXPECT_FALSE([[controller_ window] isVisible]); | |
93 [controller_ showWindow:nil]; | |
94 EXPECT_TRUE([[controller_ window] isVisible]); | |
95 } | |
96 | |
97 TEST_F(PermissionBubbleControllerTest, ShowSinglePermission) { | |
98 MockPermissionBubbleDelegate delegate_a(kPermissionA); | |
99 EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); | |
100 | |
101 std::vector<PermissionBubbleDelegate*> requests; | |
102 requests.push_back(&delegate_a); | |
103 std::vector<bool> acceptStates; | |
104 | |
105 [controller_ showAtAnchor:NSZeroPoint | |
106 withDelegate:this | |
107 forRequests:requests | |
108 acceptStates:acceptStates | |
109 customizationMode:NO]; | |
110 | |
111 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); | |
112 EXPECT_TRUE(FindButtonWithTitle(kAllowButtonLabel)); | |
113 EXPECT_TRUE(FindButtonWithTitle(kBlockButtonLabel)); | |
114 EXPECT_FALSE(FindButtonWithTitle(kOKButtonLabel)); | |
115 EXPECT_FALSE(FindButtonWithTitle(kCustomizeLabel)); | |
116 } | |
117 | |
118 TEST_F(PermissionBubbleControllerTest, ShowMultiplePermissions) { | |
119 MockPermissionBubbleDelegate delegate_a(kPermissionA); | |
groby-ooo-7-16
2014/02/04 02:39:30
You _could_ factor out the MockDelegate stuff into
leng
2014/02/04 21:48:22
Another good suggestion. I did the same with acce
| |
120 MockPermissionBubbleDelegate delegate_b(kPermissionB); | |
121 MockPermissionBubbleDelegate delegate_c(kPermissionC); | |
122 EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); | |
123 EXPECT_CALL(delegate_b, GetMessageTextFragment()).Times(1); | |
124 EXPECT_CALL(delegate_c, GetMessageTextFragment()).Times(1); | |
125 | |
126 std::vector<PermissionBubbleDelegate*> requests; | |
127 requests.push_back(&delegate_a); | |
128 requests.push_back(&delegate_b); | |
129 requests.push_back(&delegate_c); | |
130 std::vector<bool> acceptStates; | |
131 | |
132 [controller_ showAtAnchor:NSZeroPoint | |
133 withDelegate:this | |
134 forRequests:requests | |
135 acceptStates:acceptStates | |
136 customizationMode:NO]; | |
137 | |
138 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); | |
139 EXPECT_TRUE(FindTextFieldWithString(kPermissionB)); | |
140 EXPECT_TRUE(FindTextFieldWithString(kPermissionC)); | |
141 | |
142 EXPECT_TRUE(FindButtonWithTitle(kAllowButtonLabel)); | |
143 EXPECT_TRUE(FindButtonWithTitle(kBlockButtonLabel)); | |
144 EXPECT_TRUE(FindButtonWithTitle(kCustomizeLabel)); | |
145 EXPECT_FALSE(FindButtonWithTitle(kOKButtonLabel)); | |
146 } | |
147 | |
148 TEST_F(PermissionBubbleControllerTest, ShowCustomizationMode) { | |
149 MockPermissionBubbleDelegate delegate_a(kPermissionA); | |
150 MockPermissionBubbleDelegate delegate_b(kPermissionB); | |
151 EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); | |
152 EXPECT_CALL(delegate_b, GetMessageTextFragment()).Times(1); | |
153 | |
154 std::vector<PermissionBubbleDelegate*> requests; | |
155 requests.push_back(&delegate_a); | |
156 requests.push_back(&delegate_b); | |
157 | |
158 std::vector<bool> acceptStates; | |
159 acceptStates.push_back(true); | |
160 acceptStates.push_back(false); | |
161 | |
162 [controller_ showAtAnchor:NSZeroPoint | |
163 withDelegate:this | |
164 forRequests:requests | |
165 acceptStates:acceptStates | |
166 customizationMode:YES]; | |
167 | |
168 // Test that each checkbox is visible and only the first is checked. | |
169 NSButton* checkbox_a = FindButtonWithTitle(kPermissionA); | |
170 NSButton* checkbox_b = FindButtonWithTitle(kPermissionB); | |
171 EXPECT_TRUE(checkbox_a); | |
172 EXPECT_TRUE(checkbox_b); | |
173 EXPECT_EQ(NSOnState, [checkbox_a state]); | |
174 EXPECT_EQ(NSOffState, [checkbox_b state]); | |
175 | |
176 EXPECT_TRUE(FindButtonWithTitle(kOKButtonLabel)); | |
177 EXPECT_FALSE(FindButtonWithTitle(kAllowButtonLabel)); | |
178 EXPECT_FALSE(FindButtonWithTitle(kBlockButtonLabel)); | |
179 EXPECT_FALSE(FindButtonWithTitle(kCustomizeLabel)); | |
180 } | |
181 | |
182 TEST_F(PermissionBubbleControllerTest, OK) { | |
183 std::vector<PermissionBubbleDelegate*> requests; | |
184 std::vector<bool> acceptStates; | |
185 [controller_ showAtAnchor:NSZeroPoint | |
186 withDelegate:this | |
187 forRequests:requests | |
188 acceptStates:acceptStates | |
189 customizationMode:NO]; | |
190 | |
191 EXPECT_CALL(*this, Closing()).Times(1); | |
192 [FindButtonWithTitle(kOKButtonLabel) performClick:nil]; | |
193 } | |
194 | |
195 TEST_F(PermissionBubbleControllerTest, Allow) { | |
196 std::vector<PermissionBubbleDelegate*> requests; | |
197 std::vector<bool> acceptStates; | |
198 [controller_ showAtAnchor:NSZeroPoint | |
199 withDelegate:this | |
200 forRequests:requests | |
201 acceptStates:acceptStates | |
202 customizationMode:NO]; | |
203 | |
204 EXPECT_CALL(*this, Accept()).Times(1); | |
205 [FindButtonWithTitle(kAllowButtonLabel) performClick:nil]; | |
206 } | |
207 | |
208 TEST_F(PermissionBubbleControllerTest, Deny) { | |
209 std::vector<PermissionBubbleDelegate*> requests; | |
210 std::vector<bool> acceptStates; | |
211 [controller_ showAtAnchor:NSZeroPoint | |
212 withDelegate:this | |
213 forRequests:requests | |
214 acceptStates:acceptStates | |
215 customizationMode:NO]; | |
216 | |
217 EXPECT_CALL(*this, Deny()).Times(1); | |
218 [FindButtonWithTitle(kBlockButtonLabel) performClick:nil]; | |
219 } | |
220 | |
221 TEST_F(PermissionBubbleControllerTest, ToggleCheckbox) { | |
222 MockPermissionBubbleDelegate delegate_a(kPermissionA); | |
223 MockPermissionBubbleDelegate delegate_b(kPermissionB); | |
224 EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); | |
225 EXPECT_CALL(delegate_b, GetMessageTextFragment()).Times(1); | |
226 | |
227 std::vector<PermissionBubbleDelegate*> requests; | |
228 requests.push_back(&delegate_a); | |
229 requests.push_back(&delegate_b); | |
230 | |
231 std::vector<bool> acceptStates; | |
232 acceptStates.push_back(true); | |
233 acceptStates.push_back(false); | |
234 | |
235 [controller_ showAtAnchor:NSZeroPoint | |
236 withDelegate:this | |
237 forRequests:requests | |
238 acceptStates:acceptStates | |
239 customizationMode:YES]; | |
240 | |
241 EXPECT_CALL(*this, ToggleAccept(0, false)).Times(1); | |
242 EXPECT_CALL(*this, ToggleAccept(1, true)).Times(1); | |
243 [FindButtonWithTitle(kPermissionA) performClick:nil]; | |
244 [FindButtonWithTitle(kPermissionB) performClick:nil]; | |
245 } | |
246 | |
247 TEST_F(PermissionBubbleControllerTest, ClickCustomize) { | |
248 std::vector<PermissionBubbleDelegate*> requests; | |
249 std::vector<bool> acceptStates; | |
250 [controller_ showAtAnchor:NSZeroPoint | |
251 withDelegate:this | |
252 forRequests:requests | |
253 acceptStates:acceptStates | |
254 customizationMode:NO]; | |
255 | |
256 EXPECT_CALL(*this, SetCustomizationMode()).Times(1); | |
257 [FindButtonWithTitle(kCustomizeLabel) performClick:nil]; | |
258 } | |
OLD | NEW |