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_request.h" |
| 15 #import "ui/events/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 MOCK_METHOD1(SetView, void(PermissionBubbleView*)); |
| 45 |
| 46 virtual void SetUp() OVERRIDE { |
| 47 CocoaTest::SetUp(); |
| 48 bridge_.reset(new PermissionBubbleCocoa(nil)); |
| 49 controller_ = [[PermissionBubbleController alloc] |
| 50 initWithParentWindow:test_window() |
| 51 bridge:bridge_.get()]; |
| 52 } |
| 53 |
| 54 virtual void TearDown() OVERRIDE { |
| 55 [controller_ close]; |
| 56 chrome::testing::NSRunLoopRunAllPending(); |
| 57 STLDeleteElements(&requests_); |
| 58 CocoaTest::TearDown(); |
| 59 } |
| 60 |
| 61 void AddRequest(const std::string& title) { |
| 62 MockPermissionBubbleRequest* request = |
| 63 new MockPermissionBubbleRequest(title); |
| 64 requests_.push_back(request); |
| 65 EXPECT_CALL(*request, GetMessageTextFragment()).Times(1); |
| 66 } |
| 67 |
| 68 NSButton* FindButtonWithTitle(const std::string& text) { |
| 69 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); |
| 70 NSString* title = base::SysUTF8ToNSString(text); |
| 71 for (NSView* child in [parent subviews]) { |
| 72 NSButton* button = base::mac::ObjCCast<NSButton>(child); |
| 73 if ([title isEqualToString:[button title]]) { |
| 74 return button; |
| 75 } |
| 76 } |
| 77 return nil; |
| 78 } |
| 79 |
| 80 NSTextField* FindTextFieldWithString(const std::string& text) { |
| 81 NSView* parent = base::mac::ObjCCastStrict<NSView>([controller_ bubble]); |
| 82 NSString* title = base::SysUTF8ToNSString(text); |
| 83 for (NSView* child in [parent subviews]) { |
| 84 NSTextField* textField = base::mac::ObjCCast<NSTextField>(child); |
| 85 if ([[textField stringValue] hasSuffix:title]) { |
| 86 return textField; |
| 87 } |
| 88 } |
| 89 return nil; |
| 90 } |
| 91 |
| 92 protected: |
| 93 PermissionBubbleController* controller_; // Weak; it deletes itself. |
| 94 scoped_ptr<PermissionBubbleCocoa> bridge_; |
| 95 std::vector<PermissionBubbleRequest*> requests_; |
| 96 std::vector<bool> accept_states_; |
| 97 }; |
| 98 |
| 99 TEST_F(PermissionBubbleControllerTest, ShowAndClose) { |
| 100 EXPECT_FALSE([[controller_ window] isVisible]); |
| 101 [controller_ showWindow:nil]; |
| 102 EXPECT_TRUE([[controller_ window] isVisible]); |
| 103 } |
| 104 |
| 105 TEST_F(PermissionBubbleControllerTest, ShowSinglePermission) { |
| 106 AddRequest(kPermissionA); |
| 107 |
| 108 [controller_ showAtAnchor:NSZeroPoint |
| 109 withDelegate:this |
| 110 forRequests:requests_ |
| 111 acceptStates:accept_states_ |
| 112 customizationMode:NO]; |
| 113 |
| 114 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); |
| 115 EXPECT_TRUE(FindButtonWithTitle(kAllowButtonLabel)); |
| 116 EXPECT_TRUE(FindButtonWithTitle(kBlockButtonLabel)); |
| 117 EXPECT_FALSE(FindButtonWithTitle(kOKButtonLabel)); |
| 118 EXPECT_FALSE(FindButtonWithTitle(kCustomizeLabel)); |
| 119 } |
| 120 |
| 121 TEST_F(PermissionBubbleControllerTest, ShowMultiplePermissions) { |
| 122 AddRequest(kPermissionA); |
| 123 AddRequest(kPermissionB); |
| 124 AddRequest(kPermissionC); |
| 125 |
| 126 [controller_ showAtAnchor:NSZeroPoint |
| 127 withDelegate:this |
| 128 forRequests:requests_ |
| 129 acceptStates:accept_states_ |
| 130 customizationMode:NO]; |
| 131 |
| 132 EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); |
| 133 EXPECT_TRUE(FindTextFieldWithString(kPermissionB)); |
| 134 EXPECT_TRUE(FindTextFieldWithString(kPermissionC)); |
| 135 |
| 136 EXPECT_TRUE(FindButtonWithTitle(kAllowButtonLabel)); |
| 137 EXPECT_TRUE(FindButtonWithTitle(kBlockButtonLabel)); |
| 138 EXPECT_TRUE(FindButtonWithTitle(kCustomizeLabel)); |
| 139 EXPECT_FALSE(FindButtonWithTitle(kOKButtonLabel)); |
| 140 } |
| 141 |
| 142 TEST_F(PermissionBubbleControllerTest, ShowCustomizationMode) { |
| 143 AddRequest(kPermissionA); |
| 144 AddRequest(kPermissionB); |
| 145 |
| 146 accept_states_.push_back(true); |
| 147 accept_states_.push_back(false); |
| 148 |
| 149 [controller_ showAtAnchor:NSZeroPoint |
| 150 withDelegate:this |
| 151 forRequests:requests_ |
| 152 acceptStates:accept_states_ |
| 153 customizationMode:YES]; |
| 154 |
| 155 // Test that each checkbox is visible and only the first is checked. |
| 156 NSButton* checkbox_a = FindButtonWithTitle(kPermissionA); |
| 157 NSButton* checkbox_b = FindButtonWithTitle(kPermissionB); |
| 158 EXPECT_TRUE(checkbox_a); |
| 159 EXPECT_TRUE(checkbox_b); |
| 160 EXPECT_EQ(NSOnState, [checkbox_a state]); |
| 161 EXPECT_EQ(NSOffState, [checkbox_b state]); |
| 162 |
| 163 EXPECT_TRUE(FindButtonWithTitle(kOKButtonLabel)); |
| 164 EXPECT_FALSE(FindButtonWithTitle(kAllowButtonLabel)); |
| 165 EXPECT_FALSE(FindButtonWithTitle(kBlockButtonLabel)); |
| 166 EXPECT_FALSE(FindButtonWithTitle(kCustomizeLabel)); |
| 167 } |
| 168 |
| 169 TEST_F(PermissionBubbleControllerTest, OK) { |
| 170 [controller_ showAtAnchor:NSZeroPoint |
| 171 withDelegate:this |
| 172 forRequests:requests_ |
| 173 acceptStates:accept_states_ |
| 174 customizationMode:YES]; |
| 175 |
| 176 EXPECT_CALL(*this, Closing()).Times(1); |
| 177 [FindButtonWithTitle(kOKButtonLabel) performClick:nil]; |
| 178 } |
| 179 |
| 180 TEST_F(PermissionBubbleControllerTest, Allow) { |
| 181 [controller_ showAtAnchor:NSZeroPoint |
| 182 withDelegate:this |
| 183 forRequests:requests_ |
| 184 acceptStates:accept_states_ |
| 185 customizationMode:NO]; |
| 186 |
| 187 EXPECT_CALL(*this, Accept()).Times(1); |
| 188 [FindButtonWithTitle(kAllowButtonLabel) performClick:nil]; |
| 189 } |
| 190 |
| 191 TEST_F(PermissionBubbleControllerTest, Deny) { |
| 192 [controller_ showAtAnchor:NSZeroPoint |
| 193 withDelegate:this |
| 194 forRequests:requests_ |
| 195 acceptStates:accept_states_ |
| 196 customizationMode:NO]; |
| 197 |
| 198 EXPECT_CALL(*this, Deny()).Times(1); |
| 199 [FindButtonWithTitle(kBlockButtonLabel) performClick:nil]; |
| 200 } |
| 201 |
| 202 TEST_F(PermissionBubbleControllerTest, ToggleCheckbox) { |
| 203 AddRequest(kPermissionA); |
| 204 AddRequest(kPermissionB); |
| 205 |
| 206 accept_states_.push_back(true); |
| 207 accept_states_.push_back(false); |
| 208 |
| 209 [controller_ showAtAnchor:NSZeroPoint |
| 210 withDelegate:this |
| 211 forRequests:requests_ |
| 212 acceptStates:accept_states_ |
| 213 customizationMode:YES]; |
| 214 |
| 215 EXPECT_CALL(*this, ToggleAccept(0, false)).Times(1); |
| 216 EXPECT_CALL(*this, ToggleAccept(1, true)).Times(1); |
| 217 [FindButtonWithTitle(kPermissionA) performClick:nil]; |
| 218 [FindButtonWithTitle(kPermissionB) performClick:nil]; |
| 219 } |
| 220 |
| 221 TEST_F(PermissionBubbleControllerTest, ClickCustomize) { |
| 222 [controller_ showAtAnchor:NSZeroPoint |
| 223 withDelegate:this |
| 224 forRequests:requests_ |
| 225 acceptStates:accept_states_ |
| 226 customizationMode:NO]; |
| 227 |
| 228 EXPECT_CALL(*this, SetCustomizationMode()).Times(1); |
| 229 [FindButtonWithTitle(kCustomizeLabel) performClick:nil]; |
| 230 } |
OLD | NEW |