| 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" |
| 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 12 #include "chrome/browser/ui/cocoa/run_loop_testing.h" | 12 #include "chrome/browser/ui/cocoa/run_loop_testing.h" |
| 13 #import "chrome/browser/ui/cocoa/website_settings/permission_bubble_cocoa.h" | 13 #import "chrome/browser/ui/cocoa/website_settings/permission_bubble_cocoa.h" |
| 14 #import "chrome/browser/ui/cocoa/website_settings/split_block_button.h" | 14 #import "chrome/browser/ui/cocoa/website_settings/split_block_button.h" |
| 15 #include "chrome/browser/ui/website_settings/mock_permission_bubble_request.h" | 15 #include "chrome/browser/ui/website_settings/mock_permission_bubble_request.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/l10n/l10n_util_mac.h" | 18 #include "ui/base/l10n/l10n_util_mac.h" |
| 19 #import "ui/events/test/cocoa_test_event_utils.h" | 19 #import "ui/events/test/cocoa_test_event_utils.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 | 21 |
| 22 @class ConstrainedWindowButton; |
| 23 |
| 22 @interface PermissionBubbleController (ExposedForTesting) | 24 @interface PermissionBubbleController (ExposedForTesting) |
| 23 - (void)ok:(id)sender; | 25 - (void)ok:(id)sender; |
| 24 - (void)onAllow:(id)sender; | 26 - (void)onAllow:(id)sender; |
| 25 - (void)onBlock:(id)sender; | 27 - (void)onBlock:(id)sender; |
| 26 - (void)onCustomize:(id)sender; | 28 - (void)onCustomize:(id)sender; |
| 27 - (void)onCheckboxChanged:(id)sender; | 29 - (void)onCheckboxChanged:(id)sender; |
| 28 @end | 30 @end |
| 29 | 31 |
| 30 @interface SplitBlockButton (ExposedForTesting) | 32 @interface SplitBlockButton (ExposedForTesting) |
| 31 - (NSMenu*)menu; | 33 - (NSMenu*)menu; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 68 |
| 67 void AddRequest(const std::string& title) { | 69 void AddRequest(const std::string& title) { |
| 68 MockPermissionBubbleRequest* request = new MockPermissionBubbleRequest( | 70 MockPermissionBubbleRequest* request = new MockPermissionBubbleRequest( |
| 69 title, | 71 title, |
| 70 l10n_util::GetStringUTF8(IDS_PERMISSION_ALLOW), | 72 l10n_util::GetStringUTF8(IDS_PERMISSION_ALLOW), |
| 71 l10n_util::GetStringUTF8(IDS_PERMISSION_DENY)); | 73 l10n_util::GetStringUTF8(IDS_PERMISSION_DENY)); |
| 72 requests_.push_back(request); | 74 requests_.push_back(request); |
| 73 } | 75 } |
| 74 | 76 |
| 75 NSButton* FindButtonWithTitle(const std::string& title) { | 77 NSButton* FindButtonWithTitle(const std::string& title) { |
| 76 return FindButtonWithTitle(base::SysUTF8ToNSString(title)); | 78 return FindButtonWithTitle(base::SysUTF8ToNSString(title), |
| 79 [ConstrainedWindowButton class]); |
| 77 } | 80 } |
| 78 | 81 |
| 79 NSButton* FindButtonWithTitle(int title_id) { | 82 NSButton* FindButtonWithTitle(int title_id) { |
| 80 return FindButtonWithTitle(l10n_util::GetNSString(title_id)); | 83 return FindButtonWithTitle(l10n_util::GetNSString(title_id), |
| 84 [ConstrainedWindowButton class]); |
| 81 } | 85 } |
| 82 | 86 |
| 83 NSButton* FindButtonWithTitle(NSString* title) { | 87 NSButton* FindMenuButtonWithTitle(int title_id) { |
| 84 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); | 88 return FindButtonWithTitle(l10n_util::GetNSString(title_id), |
| 85 for (NSView* child in [parent subviews]) { | 89 [NSPopUpButton class]); |
| 86 NSButton* button = base::mac::ObjCCast<NSButton>(child); | 90 } |
| 87 if ([title isEqualToString:[button title]]) { | 91 |
| 88 return button; | 92 // IDS_PERMISSION_ALLOW and IDS_PERMISSION_DENY are used for two distinct |
| 93 // UI elements, both of which derive from NSButton. So check the expected |
| 94 // class, not just NSButton, as well as the title. |
| 95 NSButton* FindButtonWithTitle(NSString* title, Class button_class) { |
| 96 for (NSButton* view in [[controller_ bubble] subviews]) { |
| 97 if ([view isKindOfClass:button_class] && |
| 98 [title isEqualToString:[view title]]) { |
| 99 return view; |
| 89 } | 100 } |
| 90 } | 101 } |
| 91 return nil; | 102 return nil; |
| 92 } | 103 } |
| 93 | 104 |
| 94 NSTextField* FindTextFieldWithString(const std::string& text) { | 105 NSTextField* FindTextFieldWithString(const std::string& text) { |
| 95 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); | 106 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); |
| 96 return FindTextFieldWithString(parent, base::SysUTF8ToNSString(text)); | 107 return FindTextFieldWithString(parent, base::SysUTF8ToNSString(text)); |
| 97 } | 108 } |
| 98 | 109 |
| 99 NSTextField* FindTextFieldWithString(NSView* view, NSString* text) { | 110 NSTextField* FindTextFieldWithString(NSView* view, NSString* text) { |
| 100 NSTextField* textField = nil; | 111 NSTextField* textField = nil; |
| 101 for (NSView* child in [view subviews]) { | 112 for (NSView* child in [view subviews]) { |
| 102 textField = base::mac::ObjCCast<NSTextField>(child); | 113 textField = base::mac::ObjCCast<NSTextField>(child); |
| 103 if (![[textField stringValue] hasSuffix:text]) { | 114 if (![[textField stringValue] hasSuffix:text]) { |
| 104 textField = FindTextFieldWithString(child, text); | 115 textField = FindTextFieldWithString(child, text); |
| 105 if (textField) | 116 if (textField) |
| 106 break; | 117 break; |
| 107 } | 118 } |
| 108 } | 119 } |
| 109 return textField; | 120 return textField; |
| 110 } | 121 } |
| 111 | 122 |
| 123 void ChangePermissionMenuSelection(NSButton* menu_button, int next_title_id) { |
| 124 NSMenu* menu = [base::mac::ObjCCastStrict<NSPopUpButton>(menu_button) menu]; |
| 125 NSString* next_title = l10n_util::GetNSString(next_title_id); |
| 126 EXPECT_EQ([[menu itemWithTitle:[menu_button title]] state], NSOnState); |
| 127 NSMenuItem* next_item = [menu itemWithTitle:next_title]; |
| 128 EXPECT_EQ([next_item state], NSOffState); |
| 129 [menu performActionForItemAtIndex:[menu indexOfItem:next_item]]; |
| 130 } |
| 131 |
| 112 NSMenuItem* FindCustomizeMenuItem() { | 132 NSMenuItem* FindCustomizeMenuItem() { |
| 113 NSButton* button = FindButtonWithTitle(IDS_PERMISSION_DENY); | 133 NSButton* button = FindButtonWithTitle(IDS_PERMISSION_DENY); |
| 114 if (!button || ![button isKindOfClass:[SplitBlockButton class]]) | 134 if (!button || ![button isKindOfClass:[SplitBlockButton class]]) |
| 115 return nil; | 135 return nil; |
| 116 NSString* customize = l10n_util::GetNSString(IDS_PERMISSION_CUSTOMIZE); | 136 NSString* customize = l10n_util::GetNSString(IDS_PERMISSION_CUSTOMIZE); |
| 117 SplitBlockButton* block_button = | 137 SplitBlockButton* block_button = |
| 118 base::mac::ObjCCast<SplitBlockButton>(button); | 138 base::mac::ObjCCast<SplitBlockButton>(button); |
| 119 for (NSMenuItem* item in [[block_button menu] itemArray]) { | 139 for (NSMenuItem* item in [[block_button menu] itemArray]) { |
| 120 if ([[item title] isEqualToString:customize]) | 140 if ([[item title] isEqualToString:customize]) |
| 121 return item; | 141 return item; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); | 183 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); |
| 164 EXPECT_TRUE(FindTextFieldWithString(kPermissionB)); | 184 EXPECT_TRUE(FindTextFieldWithString(kPermissionB)); |
| 165 EXPECT_TRUE(FindTextFieldWithString(kPermissionC)); | 185 EXPECT_TRUE(FindTextFieldWithString(kPermissionC)); |
| 166 | 186 |
| 167 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); | 187 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); |
| 168 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_DENY)); | 188 EXPECT_TRUE(FindButtonWithTitle(IDS_PERMISSION_DENY)); |
| 169 EXPECT_TRUE(FindCustomizeMenuItem()); | 189 EXPECT_TRUE(FindCustomizeMenuItem()); |
| 170 EXPECT_FALSE(FindButtonWithTitle(IDS_OK)); | 190 EXPECT_FALSE(FindButtonWithTitle(IDS_OK)); |
| 171 } | 191 } |
| 172 | 192 |
| 173 TEST_F(PermissionBubbleControllerTest, ShowCustomizationMode) { | 193 TEST_F(PermissionBubbleControllerTest, ShowCustomizationModeAllow) { |
| 174 AddRequest(kPermissionB); | 194 accept_states_.push_back(true); |
| 195 [controller_ showAtAnchor:NSZeroPoint |
| 196 withDelegate:this |
| 197 forRequests:requests_ |
| 198 acceptStates:accept_states_ |
| 199 customizationMode:YES]; |
| 175 | 200 |
| 176 accept_states_.push_back(true); | 201 // Test that there is one menu, with 'Allow' visible. |
| 202 EXPECT_TRUE(FindMenuButtonWithTitle(IDS_PERMISSION_ALLOW)); |
| 203 EXPECT_FALSE(FindMenuButtonWithTitle(IDS_PERMISSION_DENY)); |
| 204 |
| 205 EXPECT_TRUE(FindButtonWithTitle(IDS_OK)); |
| 206 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); |
| 207 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_DENY)); |
| 208 EXPECT_FALSE(FindCustomizeMenuItem()); |
| 209 } |
| 210 |
| 211 TEST_F(PermissionBubbleControllerTest, ShowCustomizationModeBlock) { |
| 177 accept_states_.push_back(false); | 212 accept_states_.push_back(false); |
| 178 | |
| 179 [controller_ showAtAnchor:NSZeroPoint | 213 [controller_ showAtAnchor:NSZeroPoint |
| 180 withDelegate:this | 214 withDelegate:this |
| 181 forRequests:requests_ | 215 forRequests:requests_ |
| 182 acceptStates:accept_states_ | 216 acceptStates:accept_states_ |
| 183 customizationMode:YES]; | 217 customizationMode:YES]; |
| 184 | 218 |
| 185 // Test that each checkbox is visible and only the first is checked. | 219 // Test that there is one menu, with 'Block' visible. |
| 186 NSButton* checkbox_a = FindButtonWithTitle(kPermissionA); | 220 EXPECT_TRUE(FindMenuButtonWithTitle(IDS_PERMISSION_DENY)); |
| 187 NSButton* checkbox_b = FindButtonWithTitle(kPermissionB); | 221 EXPECT_FALSE(FindMenuButtonWithTitle(IDS_PERMISSION_ALLOW)); |
| 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 | 222 |
| 193 EXPECT_TRUE(FindButtonWithTitle(IDS_OK)); | 223 EXPECT_TRUE(FindButtonWithTitle(IDS_OK)); |
| 194 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); | 224 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_ALLOW)); |
| 195 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_DENY)); | 225 EXPECT_FALSE(FindButtonWithTitle(IDS_PERMISSION_DENY)); |
| 196 EXPECT_FALSE(FindCustomizeMenuItem()); | 226 EXPECT_FALSE(FindCustomizeMenuItem()); |
| 197 } | 227 } |
| 198 | 228 |
| 199 TEST_F(PermissionBubbleControllerTest, OK) { | 229 TEST_F(PermissionBubbleControllerTest, OK) { |
| 200 accept_states_.push_back(true); | 230 accept_states_.push_back(true); |
| 201 [controller_ showAtAnchor:NSZeroPoint | 231 [controller_ showAtAnchor:NSZeroPoint |
| (...skipping 21 matching lines...) Expand all Loading... |
| 223 [controller_ showAtAnchor:NSZeroPoint | 253 [controller_ showAtAnchor:NSZeroPoint |
| 224 withDelegate:this | 254 withDelegate:this |
| 225 forRequests:requests_ | 255 forRequests:requests_ |
| 226 acceptStates:accept_states_ | 256 acceptStates:accept_states_ |
| 227 customizationMode:NO]; | 257 customizationMode:NO]; |
| 228 | 258 |
| 229 EXPECT_CALL(*this, Deny()).Times(1); | 259 EXPECT_CALL(*this, Deny()).Times(1); |
| 230 [FindButtonWithTitle(IDS_PERMISSION_DENY) performClick:nil]; | 260 [FindButtonWithTitle(IDS_PERMISSION_DENY) performClick:nil]; |
| 231 } | 261 } |
| 232 | 262 |
| 233 TEST_F(PermissionBubbleControllerTest, ToggleCheckbox) { | 263 TEST_F(PermissionBubbleControllerTest, ChangePermissionSelection) { |
| 234 AddRequest(kPermissionB); | 264 AddRequest(kPermissionB); |
| 235 | 265 |
| 236 accept_states_.push_back(true); | 266 accept_states_.push_back(true); |
| 237 accept_states_.push_back(false); | 267 accept_states_.push_back(false); |
| 238 | 268 |
| 239 [controller_ showAtAnchor:NSZeroPoint | 269 [controller_ showAtAnchor:NSZeroPoint |
| 240 withDelegate:this | 270 withDelegate:this |
| 241 forRequests:requests_ | 271 forRequests:requests_ |
| 242 acceptStates:accept_states_ | 272 acceptStates:accept_states_ |
| 243 customizationMode:YES]; | 273 customizationMode:YES]; |
| 244 | 274 |
| 245 EXPECT_CALL(*this, ToggleAccept(0, false)).Times(1); | 275 EXPECT_CALL(*this, ToggleAccept(0, false)).Times(1); |
| 246 EXPECT_CALL(*this, ToggleAccept(1, true)).Times(1); | 276 EXPECT_CALL(*this, ToggleAccept(1, true)).Times(1); |
| 247 [FindButtonWithTitle(kPermissionA) performClick:nil]; | 277 NSButton* menu_a = FindMenuButtonWithTitle(IDS_PERMISSION_ALLOW); |
| 248 [FindButtonWithTitle(kPermissionB) performClick:nil]; | 278 NSButton* menu_b = FindMenuButtonWithTitle(IDS_PERMISSION_DENY); |
| 279 ChangePermissionMenuSelection(menu_a, IDS_PERMISSION_DENY); |
| 280 ChangePermissionMenuSelection(menu_b, IDS_PERMISSION_ALLOW); |
| 249 } | 281 } |
| 250 | 282 |
| 251 TEST_F(PermissionBubbleControllerTest, ClickCustomize) { | 283 TEST_F(PermissionBubbleControllerTest, ClickCustomize) { |
| 252 AddRequest(kPermissionB); | 284 AddRequest(kPermissionB); |
| 253 [controller_ showAtAnchor:NSZeroPoint | 285 [controller_ showAtAnchor:NSZeroPoint |
| 254 withDelegate:this | 286 withDelegate:this |
| 255 forRequests:requests_ | 287 forRequests:requests_ |
| 256 acceptStates:accept_states_ | 288 acceptStates:accept_states_ |
| 257 customizationMode:NO]; | 289 customizationMode:NO]; |
| 258 | 290 |
| 259 EXPECT_CALL(*this, SetCustomizationMode()).Times(1); | 291 EXPECT_CALL(*this, SetCustomizationMode()).Times(1); |
| 260 NSMenuItem* customize_item = FindCustomizeMenuItem(); | 292 NSMenuItem* customize_item = FindCustomizeMenuItem(); |
| 261 EXPECT_TRUE(customize_item); | 293 EXPECT_TRUE(customize_item); |
| 262 NSMenu* menu = [customize_item menu]; | 294 NSMenu* menu = [customize_item menu]; |
| 263 [menu performActionForItemAtIndex:[menu indexOfItem:customize_item]]; | 295 [menu performActionForItemAtIndex:[menu indexOfItem:customize_item]]; |
| 264 } | 296 } |
| OLD | NEW |