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