OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #import "chrome/browser/ui/cocoa/website_settings/permission_bubble_controller.h " | 5 #import "chrome/browser/ui/cocoa/website_settings/permission_bubble_controller.h " |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 | 66 |
67 void AddRequest(const std::string& title) { | 67 void AddRequest(const std::string& title) { |
68 MockPermissionBubbleRequest* request = new MockPermissionBubbleRequest( | 68 MockPermissionBubbleRequest* request = new MockPermissionBubbleRequest( |
69 title, | 69 title, |
70 l10n_util::GetStringUTF8(IDS_PERMISSION_ALLOW), | 70 l10n_util::GetStringUTF8(IDS_PERMISSION_ALLOW), |
71 l10n_util::GetStringUTF8(IDS_PERMISSION_DENY)); | 71 l10n_util::GetStringUTF8(IDS_PERMISSION_DENY)); |
72 requests_.push_back(request); | 72 requests_.push_back(request); |
73 } | 73 } |
74 | 74 |
75 NSButton* FindButtonWithTitle(const std::string& title) { | 75 NSButton* FindButtonWithTitle(const std::string& title) { |
76 return FindButtonWithTitle(base::SysUTF8ToNSString(title)); | 76 return FindButtonWithTitle(base::SysUTF8ToNSString(title), false); |
77 } | 77 } |
78 | 78 |
79 NSButton* FindButtonWithTitle(int title_id) { | 79 NSButton* FindButtonWithTitle(int title_id) { |
80 return FindButtonWithTitle(l10n_util::GetNSString(title_id)); | 80 return FindButtonWithTitle(l10n_util::GetNSString(title_id), false); |
81 } | 81 } |
82 | 82 |
83 NSButton* FindButtonWithTitle(NSString* title) { | 83 NSButton* FindMenuButtonWithTitle(int title_id) { |
84 return FindButtonWithTitle(l10n_util::GetNSString(title_id), true); | |
85 } | |
86 | |
87 // IDS_PERMISSION_ALLOW and IDS_PERMISSION_DENY are used for two distinct | |
88 // UI elements, both of which derive from NSButton. In order to test for | |
89 // each kind of element, check the derived class of one - NSPopUpButton - | |
90 // to distinguish between the two. | |
91 NSButton* FindButtonWithTitle(NSString* title, bool want_nspopup) { | |
84 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); | 92 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); |
groby-ooo-7-16
2014/04/18 18:18:18
No need to cast - the bubble is always an NSView
leng
2014/04/18 22:48:46
Made NSButton to avoid a cast.
| |
85 for (NSView* child in [parent subviews]) { | 93 for (NSView* child in [parent subviews]) { |
86 NSButton* button = base::mac::ObjCCast<NSButton>(child); | 94 NSButton* button = base::mac::ObjCCast<NSButton>(child); |
groby-ooo-7-16
2014/04/18 18:18:18
Shorter & less casts:
Class button_class = want_n
leng
2014/04/18 22:48:46
I like it. I need to use ConstrainedWindowButton
| |
87 if ([title isEqualToString:[button title]]) { | 95 if ([title isEqualToString:[button title]]) { |
88 return button; | 96 NSPopUpButton* popup = base::mac::ObjCCast<NSPopUpButton>(button); |
97 if ((!want_nspopup && !popup) || (want_nspopup && popup)) | |
98 return button; | |
89 } | 99 } |
90 } | 100 } |
91 return nil; | 101 return nil; |
92 } | 102 } |
93 | 103 |
94 NSTextField* FindTextFieldWithString(const std::string& text) { | 104 NSTextField* FindTextFieldWithString(const std::string& text) { |
95 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); | 105 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); |
96 return FindTextFieldWithString(parent, base::SysUTF8ToNSString(text)); | 106 return FindTextFieldWithString(parent, base::SysUTF8ToNSString(text)); |
97 } | 107 } |
98 | 108 |
99 NSTextField* FindTextFieldWithString(NSView* view, NSString* text) { | 109 NSTextField* FindTextFieldWithString(NSView* view, NSString* text) { |
100 NSTextField* textField = nil; | 110 NSTextField* textField = nil; |
101 for (NSView* child in [view subviews]) { | 111 for (NSView* child in [view subviews]) { |
102 textField = base::mac::ObjCCast<NSTextField>(child); | 112 textField = base::mac::ObjCCast<NSTextField>(child); |
103 if (![[textField stringValue] hasSuffix:text]) { | 113 if (![[textField stringValue] hasSuffix:text]) { |
104 textField = FindTextFieldWithString(child, text); | 114 textField = FindTextFieldWithString(child, text); |
105 if (textField) | 115 if (textField) |
106 break; | 116 break; |
107 } | 117 } |
108 } | 118 } |
109 return textField; | 119 return textField; |
110 } | 120 } |
111 | 121 |
122 void ChangePermissionMenuSelection(NSButton* menu_button, int next_title_id) { | |
123 NSMenu* menu = [base::mac::ObjCCast<NSPopUpButton>(menu_button) menu]; | |
groby-ooo-7-16
2014/04/18 18:18:18
I think you want strict here, or even pass in an N
leng
2014/04/18 22:48:46
Done.
| |
124 NSString* next_title = l10n_util::GetNSString(next_title_id); | |
125 EXPECT_EQ([[menu itemWithTitle:[menu_button title]] state], NSOnState); | |
126 NSMenuItem* next_item = [menu itemWithTitle:next_title]; | |
127 EXPECT_EQ([next_item state], NSOffState); | |
128 [menu performActionForItemAtIndex:[menu indexOfItem:next_item]]; | |
129 } | |
130 | |
112 NSMenuItem* FindCustomizeMenuItem() { | 131 NSMenuItem* FindCustomizeMenuItem() { |
113 NSButton* button = FindButtonWithTitle(IDS_PERMISSION_DENY); | 132 NSButton* button = FindButtonWithTitle(IDS_PERMISSION_DENY); |
114 if (!button || ![button isKindOfClass:[SplitBlockButton class]]) | 133 if (!button || ![button isKindOfClass:[SplitBlockButton class]]) |
115 return nil; | 134 return nil; |
116 NSString* customize = l10n_util::GetNSString(IDS_PERMISSION_CUSTOMIZE); | 135 NSString* customize = l10n_util::GetNSString(IDS_PERMISSION_CUSTOMIZE); |
117 SplitBlockButton* block_button = | 136 SplitBlockButton* block_button = |
118 base::mac::ObjCCast<SplitBlockButton>(button); | 137 base::mac::ObjCCast<SplitBlockButton>(button); |
119 for (NSMenuItem* item in [[block_button menu] itemArray]) { | 138 for (NSMenuItem* item in [[block_button menu] itemArray]) { |
120 if ([[item title] isEqualToString:customize]) | 139 if ([[item title] isEqualToString:customize]) |
121 return item; | 140 return item; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); | 182 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); |
164 EXPECT_TRUE(FindTextFieldWithString(kPermissionB)); | 183 EXPECT_TRUE(FindTextFieldWithString(kPermissionB)); |
165 EXPECT_TRUE(FindTextFieldWithString(kPermissionC)); | 184 EXPECT_TRUE(FindTextFieldWithString(kPermissionC)); |
166 | 185 |
167 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); | 186 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); |
168 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_DENY)); | 187 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_DENY)); |
169 EXPECT_TRUE(FindCustomizeMenuItem()); | 188 EXPECT_TRUE(FindCustomizeMenuItem()); |
170 EXPECT_FALSE(FindButtonWithTitle(IDS_OK)); | 189 EXPECT_FALSE(FindButtonWithTitle(IDS_OK)); |
171 } | 190 } |
172 | 191 |
173 TEST_F(PermissionBubbleControllerTest, ShowCustomizationMode) { | 192 TEST_F(PermissionBubbleControllerTest, ShowCustomizationModeAllow) { |
174 AddRequest(kPermissionB); | |
175 | |
176 accept_states_.push_back(true); | 193 accept_states_.push_back(true); |
177 accept_states_.push_back(false); | |
178 | |
179 [controller_ showAtAnchor:NSZeroPoint | 194 [controller_ showAtAnchor:NSZeroPoint |
180 withDelegate:this | 195 withDelegate:this |
181 forRequests:requests_ | 196 forRequests:requests_ |
182 acceptStates:accept_states_ | 197 acceptStates:accept_states_ |
183 customizationMode:YES]; | 198 customizationMode:YES]; |
184 | 199 |
185 // Test that each checkbox is visible and only the first is checked. | 200 // Test that there is one menu, with 'Allow' visible. |
186 NSButton* checkbox_a = FindButtonWithTitle(kPermissionA); | 201 EXPECT_TRUE(FindMenuButtonWithTitle(IDS_PERMISSION_ALLOW)); |
187 NSButton* checkbox_b = FindButtonWithTitle(kPermissionB); | 202 EXPECT_FALSE(FindMenuButtonWithTitle(IDS_PERMISSION_DENY)); |
188 EXPECT_TRUE(checkbox_a); | |
189 EXPECT_TRUE(checkbox_b); | |
190 EXPECT_EQ(NSOnState, [checkbox_a state]); | |
191 EXPECT_EQ(NSOffState, [checkbox_b state]); | |
192 | 203 |
193 EXPECT_TRUE(FindButtonWithTitle(IDS_OK)); | 204 EXPECT_TRUE(FindButtonWithTitle(IDS_OK)); |
194 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); | 205 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); |
206 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_DENY)); | |
207 EXPECT_FALSE(FindCustomizeMenuItem()); | |
208 } | |
209 | |
210 TEST_F(PermissionBubbleControllerTest, ShowCustomizationModeBlock) { | |
211 accept_states_.push_back(false); | |
212 [controller_ showAtAnchor:NSZeroPoint | |
213 withDelegate:this | |
214 forRequests:requests_ | |
215 acceptStates:accept_states_ | |
216 customizationMode:YES]; | |
217 | |
218 // Test that there is one menu, with 'Block' visible. | |
219 EXPECT_TRUE(FindMenuButtonWithTitle(IDS_PERMISSION_DENY)); | |
220 EXPECT_FALSE(FindMenuButtonWithTitle(IDS_PERMISSION_ALLOW)); | |
221 | |
222 EXPECT_TRUE(FindButtonWithTitle(IDS_OK)); | |
223 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); | |
195 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_DENY)); | 224 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_DENY)); |
196 EXPECT_FALSE(FindCustomizeMenuItem()); | 225 EXPECT_FALSE(FindCustomizeMenuItem()); |
197 } | 226 } |
198 | 227 |
199 TEST_F(PermissionBubbleControllerTest, OK) { | 228 TEST_F(PermissionBubbleControllerTest, OK) { |
200 accept_states_.push_back(true); | 229 accept_states_.push_back(true); |
201 [controller_ showAtAnchor:NSZeroPoint | 230 [controller_ showAtAnchor:NSZeroPoint |
202 withDelegate:this | 231 withDelegate:this |
203 forRequests:requests_ | 232 forRequests:requests_ |
204 acceptStates:accept_states_ | 233 acceptStates:accept_states_ |
(...skipping 18 matching lines...) Expand all Loading... | |
223 [controller_ showAtAnchor:NSZeroPoint | 252 [controller_ showAtAnchor:NSZeroPoint |
224 withDelegate:this | 253 withDelegate:this |
225 forRequests:requests_ | 254 forRequests:requests_ |
226 acceptStates:accept_states_ | 255 acceptStates:accept_states_ |
227 customizationMode:NO]; | 256 customizationMode:NO]; |
228 | 257 |
229 EXPECT_CALL(*this, Deny()).Times(1); | 258 EXPECT_CALL(*this, Deny()).Times(1); |
230 [FindButtonWithTitle(IDS_PERMISSION_DENY) performClick:nil]; | 259 [FindButtonWithTitle(IDS_PERMISSION_DENY) performClick:nil]; |
231 } | 260 } |
232 | 261 |
233 TEST_F(PermissionBubbleControllerTest, ToggleCheckbox) { | 262 TEST_F(PermissionBubbleControllerTest, ChangePermissionSelection) { |
234 AddRequest(kPermissionB); | 263 AddRequest(kPermissionB); |
235 | 264 |
236 accept_states_.push_back(true); | 265 accept_states_.push_back(true); |
237 accept_states_.push_back(false); | 266 accept_states_.push_back(false); |
238 | 267 |
239 [controller_ showAtAnchor:NSZeroPoint | 268 [controller_ showAtAnchor:NSZeroPoint |
240 withDelegate:this | 269 withDelegate:this |
241 forRequests:requests_ | 270 forRequests:requests_ |
242 acceptStates:accept_states_ | 271 acceptStates:accept_states_ |
243 customizationMode:YES]; | 272 customizationMode:YES]; |
244 | 273 |
245 EXPECT_CALL(*this, ToggleAccept(0, false)).Times(1); | 274 EXPECT_CALL(*this, ToggleAccept(0, false)).Times(1); |
246 EXPECT_CALL(*this, ToggleAccept(1, true)).Times(1); | 275 EXPECT_CALL(*this, ToggleAccept(1, true)).Times(1); |
247 [FindButtonWithTitle(kPermissionA) performClick:nil]; | 276 NSButton* menu_a = FindMenuButtonWithTitle(IDS_PERMISSION_ALLOW); |
248 [FindButtonWithTitle(kPermissionB) performClick:nil]; | 277 NSButton* menu_b = FindMenuButtonWithTitle(IDS_PERMISSION_DENY); |
278 ChangePermissionMenuSelection(menu_a, IDS_PERMISSION_DENY); | |
279 ChangePermissionMenuSelection(menu_b, IDS_PERMISSION_ALLOW); | |
249 } | 280 } |
250 | 281 |
251 TEST_F(PermissionBubbleControllerTest, ClickCustomize) { | 282 TEST_F(PermissionBubbleControllerTest, ClickCustomize) { |
252 AddRequest(kPermissionB); | 283 AddRequest(kPermissionB); |
253 [controller_ showAtAnchor:NSZeroPoint | 284 [controller_ showAtAnchor:NSZeroPoint |
254 withDelegate:this | 285 withDelegate:this |
255 forRequests:requests_ | 286 forRequests:requests_ |
256 acceptStates:accept_states_ | 287 acceptStates:accept_states_ |
257 customizationMode:NO]; | 288 customizationMode:NO]; |
258 | 289 |
259 EXPECT_CALL(*this, SetCustomizationMode()).Times(1); | 290 EXPECT_CALL(*this, SetCustomizationMode()).Times(1); |
260 NSMenuItem* customize_item = FindCustomizeMenuItem(); | 291 NSMenuItem* customize_item = FindCustomizeMenuItem(); |
261 EXPECT_TRUE(customize_item); | 292 EXPECT_TRUE(customize_item); |
262 NSMenu* menu = [customize_item menu]; | 293 NSMenu* menu = [customize_item menu]; |
263 [menu performActionForItemAtIndex:[menu indexOfItem:customize_item]]; | 294 [menu performActionForItemAtIndex:[menu indexOfItem:customize_item]]; |
264 } | 295 } |
OLD | NEW |