OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_bubble_controller.h" | 5 #import "chrome/browser/ui/cocoa/website_settings_bubble_controller.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 8 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
9 #include "testing/gtest_mac.h" | 9 #include "testing/gtest_mac.h" |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 - (NSTextField*)firstVisitDescriptionField { | 55 - (NSTextField*)firstVisitDescriptionField { |
56 return firstVisitDescriptionField_; | 56 return firstVisitDescriptionField_; |
57 } | 57 } |
58 | 58 |
59 - (NSButton*)helpButton { | 59 - (NSButton*)helpButton { |
60 return helpButton_; | 60 return helpButton_; |
61 } | 61 } |
62 @end | 62 @end |
63 | 63 |
| 64 @interface WebsiteSettingsBubbleControllerForTesting |
| 65 : WebsiteSettingsBubbleController { |
| 66 @private |
| 67 CGFloat defaultWindowWidth_; |
| 68 } |
| 69 @end |
| 70 |
| 71 @implementation WebsiteSettingsBubbleControllerForTesting |
| 72 - (void)setDefaultWindowWidth:(CGFloat)width { |
| 73 defaultWindowWidth_ = width; |
| 74 } |
| 75 - (CGFloat)defaultWindowWidth { |
| 76 // If |defaultWindowWidth_| is 0, use the superclass implementation. |
| 77 return defaultWindowWidth_ ? |
| 78 defaultWindowWidth_ : [super defaultWindowWidth]; |
| 79 } |
| 80 @end |
| 81 |
64 namespace { | 82 namespace { |
65 | 83 |
66 // Indices of the menu items in the permission menu. | 84 // Indices of the menu items in the permission menu. |
67 enum PermissionMenuIndices { | 85 enum PermissionMenuIndices { |
68 kMenuIndexContentSettingAllow = 0, | 86 kMenuIndexContentSettingAllow = 0, |
69 kMenuIndexContentSettingBlock, | 87 kMenuIndexContentSettingBlock, |
70 kMenuIndexContentSettingDefault | 88 kMenuIndexContentSettingDefault |
71 }; | 89 }; |
72 | 90 |
| 91 const ContentSettingsType kTestPermissionTypes[] = { |
| 92 // NOTE: FULLSCREEN does not support "Always block", so it must appear as |
| 93 // one of the first three permissions. |
| 94 CONTENT_SETTINGS_TYPE_FULLSCREEN, |
| 95 CONTENT_SETTINGS_TYPE_IMAGES, |
| 96 CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 97 CONTENT_SETTINGS_TYPE_PLUGINS, |
| 98 CONTENT_SETTINGS_TYPE_POPUPS, |
| 99 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 100 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 101 CONTENT_SETTINGS_TYPE_MOUSELOCK, |
| 102 CONTENT_SETTINGS_TYPE_MEDIASTREAM, |
| 103 }; |
| 104 |
| 105 const ContentSetting kTestSettings[] = { |
| 106 CONTENT_SETTING_DEFAULT, |
| 107 CONTENT_SETTING_DEFAULT, |
| 108 CONTENT_SETTING_DEFAULT, |
| 109 CONTENT_SETTING_ALLOW, |
| 110 CONTENT_SETTING_BLOCK, |
| 111 CONTENT_SETTING_ALLOW, |
| 112 CONTENT_SETTING_BLOCK, |
| 113 CONTENT_SETTING_ALLOW, |
| 114 CONTENT_SETTING_BLOCK, |
| 115 }; |
| 116 |
| 117 const ContentSetting kTestDefaultSettings[] = { |
| 118 CONTENT_SETTING_ALLOW, |
| 119 CONTENT_SETTING_BLOCK, |
| 120 CONTENT_SETTING_ASK |
| 121 }; |
| 122 |
| 123 const content_settings::SettingSource kTestSettingSources[] = { |
| 124 content_settings::SETTING_SOURCE_USER, |
| 125 content_settings::SETTING_SOURCE_USER, |
| 126 content_settings::SETTING_SOURCE_USER, |
| 127 content_settings::SETTING_SOURCE_USER, |
| 128 content_settings::SETTING_SOURCE_USER, |
| 129 content_settings::SETTING_SOURCE_POLICY, |
| 130 content_settings::SETTING_SOURCE_POLICY, |
| 131 content_settings::SETTING_SOURCE_EXTENSION, |
| 132 content_settings::SETTING_SOURCE_EXTENSION, |
| 133 }; |
| 134 |
73 class WebsiteSettingsBubbleControllerTest : public CocoaTest { | 135 class WebsiteSettingsBubbleControllerTest : public CocoaTest { |
74 public: | 136 public: |
75 WebsiteSettingsBubbleControllerTest() { | 137 WebsiteSettingsBubbleControllerTest() { |
76 controller_ = nil; | 138 controller_ = nil; |
77 } | 139 } |
78 | 140 |
79 virtual void TearDown() { | 141 virtual void TearDown() { |
80 [controller_ close]; | 142 [controller_ close]; |
81 CocoaTest::TearDown(); | 143 CocoaTest::TearDown(); |
82 } | 144 } |
83 | 145 |
84 protected: | 146 protected: |
85 WebsiteSettingsUIBridge* bridge_; // Weak, owned by controller. | 147 WebsiteSettingsUIBridge* bridge_; // Weak, owned by controller. |
86 | 148 |
87 enum MatchType { | 149 enum MatchType { |
88 TEXT_EQUAL = 0, | 150 TEXT_EQUAL = 0, |
89 TEXT_NOT_EQUAL | 151 TEXT_NOT_EQUAL |
90 }; | 152 }; |
91 | 153 |
92 void CreateBubble() { | 154 // Creates a new website settings bubble, with the given default width. |
| 155 // If |default_width| is 0, the *default* default width will be used. |
| 156 void CreateBubbleWithWidth(CGFloat default_width) { |
93 bridge_ = new WebsiteSettingsUIBridge(); | 157 bridge_ = new WebsiteSettingsUIBridge(); |
94 | 158 |
95 // The controller cleans up after itself when the window closes. | 159 // The controller cleans up after itself when the window closes. |
96 controller_ = | 160 controller_ = [WebsiteSettingsBubbleControllerForTesting alloc]; |
97 [[WebsiteSettingsBubbleController alloc] | 161 [controller_ setDefaultWindowWidth:default_width]; |
98 initWithParentWindow:test_window() | 162 [controller_ initWithParentWindow:test_window() |
99 websiteSettingsUIBridge:bridge_ | 163 websiteSettingsUIBridge:bridge_ |
100 tabContents:nil | 164 tabContents:nil |
101 isInternalPage:NO]; | 165 isInternalPage:NO]; |
102 window_ = [controller_ window]; | 166 window_ = [controller_ window]; |
103 [controller_ showWindow:nil]; | 167 [controller_ showWindow:nil]; |
104 } | 168 } |
105 | 169 |
| 170 void CreateBubble() { |
| 171 CreateBubbleWithWidth(0.0); |
| 172 } |
| 173 |
106 // Return a pointer to the first NSTextField found that either matches, or | 174 // Return a pointer to the first NSTextField found that either matches, or |
107 // doesn't match, the given text. | 175 // doesn't match, the given text. |
108 NSTextField* FindTextField(MatchType match_type, NSString* text) { | 176 NSTextField* FindTextField(MatchType match_type, NSString* text) { |
109 // The window's only immediate child is an invisible view that has a flipped | 177 // The window's only immediate child is an invisible view that has a flipped |
110 // coordinate origin. It is into this that all views get placed. | 178 // coordinate origin. It is into this that all views get placed. |
111 NSArray* window_subviews = [[window_ contentView] subviews]; | 179 NSArray* window_subviews = [[window_ contentView] subviews]; |
112 EXPECT_EQ(1U, [window_subviews count]); | 180 EXPECT_EQ(1U, [window_subviews count]); |
113 NSArray* subviews = [[window_subviews lastObject] subviews]; | 181 NSArray* subviews = [[window_subviews lastObject] subviews]; |
114 | 182 |
115 // Expect 4 views: the identity, identity status, the segmented control | 183 // Expect 4 views: the identity, identity status, the segmented control |
(...skipping 14 matching lines...) Expand all Loading... |
130 | 198 |
131 NSMutableArray* FindAllSubviewsOfClass(NSView* parent_view, Class a_class) { | 199 NSMutableArray* FindAllSubviewsOfClass(NSView* parent_view, Class a_class) { |
132 NSMutableArray* views = [NSMutableArray array]; | 200 NSMutableArray* views = [NSMutableArray array]; |
133 for (NSView* view in [parent_view subviews]) { | 201 for (NSView* view in [parent_view subviews]) { |
134 if ([view isKindOfClass:a_class]) | 202 if ([view isKindOfClass:a_class]) |
135 [views addObject:view]; | 203 [views addObject:view]; |
136 } | 204 } |
137 return views; | 205 return views; |
138 } | 206 } |
139 | 207 |
140 WebsiteSettingsBubbleController* controller_; // Weak, owns self. | 208 // Sets up the dialog with some test permission settings. |
| 209 void SetTestPermissions() { |
| 210 // Create a list of 5 different permissions, corresponding to all the |
| 211 // possible settings: |
| 212 // - [allow, block, ask] by default |
| 213 // - [block, allow] * [by user, by policy, by extension] |
| 214 PermissionInfoList list; |
| 215 WebsiteSettingsUI::PermissionInfo info; |
| 216 for (size_t i = 0; i < arraysize(kTestPermissionTypes); ++i) { |
| 217 info.type = kTestPermissionTypes[i]; |
| 218 info.setting = kTestSettings[i]; |
| 219 if (info.setting == CONTENT_SETTING_DEFAULT) |
| 220 info.default_setting = kTestDefaultSettings[i]; |
| 221 info.source = kTestSettingSources[i]; |
| 222 list.push_back(info); |
| 223 } |
| 224 bridge_->SetPermissionInfo(list); |
| 225 } |
| 226 |
| 227 WebsiteSettingsBubbleControllerForTesting* controller_; // Weak, owns self. |
141 NSWindow* window_; // Weak, owned by controller. | 228 NSWindow* window_; // Weak, owned by controller. |
142 }; | 229 }; |
143 | 230 |
144 TEST_F(WebsiteSettingsBubbleControllerTest, BasicIdentity) { | 231 TEST_F(WebsiteSettingsBubbleControllerTest, BasicIdentity) { |
145 WebsiteSettingsUI::IdentityInfo info; | 232 WebsiteSettingsUI::IdentityInfo info; |
146 info.site_identity = std::string("nhl.com"); | 233 info.site_identity = std::string("nhl.com"); |
147 info.identity_status = WebsiteSettings::SITE_IDENTITY_STATUS_UNKNOWN; | 234 info.identity_status = WebsiteSettings::SITE_IDENTITY_STATUS_UNKNOWN; |
148 | 235 |
149 CreateBubble(); | 236 CreateBubble(); |
150 | 237 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 | 318 |
232 TEST_F(WebsiteSettingsBubbleControllerTest, SetFirstVisit) { | 319 TEST_F(WebsiteSettingsBubbleControllerTest, SetFirstVisit) { |
233 CreateBubble(); | 320 CreateBubble(); |
234 bridge_->SetFirstVisit(ASCIIToUTF16("Yesterday")); | 321 bridge_->SetFirstVisit(ASCIIToUTF16("Yesterday")); |
235 EXPECT_NSEQ(@"Yesterday", | 322 EXPECT_NSEQ(@"Yesterday", |
236 [[controller_ firstVisitDescriptionField] stringValue]); | 323 [[controller_ firstVisitDescriptionField] stringValue]); |
237 } | 324 } |
238 | 325 |
239 TEST_F(WebsiteSettingsBubbleControllerTest, SetPermissionInfo) { | 326 TEST_F(WebsiteSettingsBubbleControllerTest, SetPermissionInfo) { |
240 CreateBubble(); | 327 CreateBubble(); |
241 | 328 SetTestPermissions(); |
242 const ContentSettingsType kTestPermissionTypes[] = { | |
243 // NOTE: FULLSCREEN does not support "Always block", so it must appear as | |
244 // one of the first three permissions. | |
245 CONTENT_SETTINGS_TYPE_FULLSCREEN, | |
246 CONTENT_SETTINGS_TYPE_IMAGES, | |
247 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
248 CONTENT_SETTINGS_TYPE_PLUGINS, | |
249 CONTENT_SETTINGS_TYPE_POPUPS, | |
250 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
251 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
252 CONTENT_SETTINGS_TYPE_MOUSELOCK, | |
253 CONTENT_SETTINGS_TYPE_MEDIASTREAM, | |
254 }; | |
255 | |
256 const ContentSetting kTestSettings[] = { | |
257 CONTENT_SETTING_DEFAULT, | |
258 CONTENT_SETTING_DEFAULT, | |
259 CONTENT_SETTING_DEFAULT, | |
260 CONTENT_SETTING_ALLOW, | |
261 CONTENT_SETTING_BLOCK, | |
262 CONTENT_SETTING_ALLOW, | |
263 CONTENT_SETTING_BLOCK, | |
264 CONTENT_SETTING_ALLOW, | |
265 CONTENT_SETTING_BLOCK, | |
266 }; | |
267 | |
268 const ContentSetting kTestDefaultSettings[] = { | |
269 CONTENT_SETTING_ALLOW, | |
270 CONTENT_SETTING_BLOCK, | |
271 CONTENT_SETTING_ASK | |
272 }; | |
273 | |
274 const content_settings::SettingSource kTestSettingSources[] = { | |
275 content_settings::SETTING_SOURCE_USER, | |
276 content_settings::SETTING_SOURCE_USER, | |
277 content_settings::SETTING_SOURCE_USER, | |
278 content_settings::SETTING_SOURCE_USER, | |
279 content_settings::SETTING_SOURCE_USER, | |
280 content_settings::SETTING_SOURCE_POLICY, | |
281 content_settings::SETTING_SOURCE_POLICY, | |
282 content_settings::SETTING_SOURCE_EXTENSION, | |
283 content_settings::SETTING_SOURCE_EXTENSION, | |
284 }; | |
285 | |
286 // Create a list of 5 different permissions, corresponding to all the | |
287 // possible settings: | |
288 // - [allow, block, ask] by default | |
289 // - [block, allow] * [by user, by policy, by extension] | |
290 PermissionInfoList list; | |
291 WebsiteSettingsUI::PermissionInfo info; | |
292 for (size_t i = 0; i < arraysize(kTestPermissionTypes); ++i) { | |
293 info.type = kTestPermissionTypes[i]; | |
294 info.setting = kTestSettings[i]; | |
295 if (info.setting == CONTENT_SETTING_DEFAULT) | |
296 info.default_setting = kTestDefaultSettings[i]; | |
297 info.source = kTestSettingSources[i]; | |
298 list.push_back(info); | |
299 } | |
300 bridge_->SetPermissionInfo(list); | |
301 | 329 |
302 // There should be three subviews per permission (an icon, a label and a | 330 // There should be three subviews per permission (an icon, a label and a |
303 // select box), plus a text label for the Permission section. | 331 // select box), plus a text label for the Permission section. |
304 NSArray* subviews = [[controller_ permissionsView] subviews]; | 332 NSArray* subviews = [[controller_ permissionsView] subviews]; |
305 EXPECT_EQ(arraysize(kTestPermissionTypes) * 3 + 1, [subviews count]); | 333 EXPECT_EQ(arraysize(kTestPermissionTypes) * 3 + 1, [subviews count]); |
306 | 334 |
307 // Ensure that there is a distinct label for each permission. | 335 // Ensure that there is a distinct label for each permission. |
308 NSMutableSet* labels = [NSMutableSet set]; | 336 NSMutableSet* labels = [NSMutableSet set]; |
309 for (NSView* view in subviews) { | 337 for (NSView* view in subviews) { |
310 if ([view isKindOfClass:[NSTextField class]]) | 338 if ([view isKindOfClass:[NSTextField class]]) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 // (which implements the tabs) as well as the visible tab contents. | 370 // (which implements the tabs) as well as the visible tab contents. |
343 // NOTE: This implicitly (and deliberately) tests that the tabs appear in a | 371 // NOTE: This implicitly (and deliberately) tests that the tabs appear in a |
344 // specific order: Permissions, Connection. | 372 // specific order: Permissions, Connection. |
345 EXPECT_EQ(0, [segmentedControl selectedSegment]); | 373 EXPECT_EQ(0, [segmentedControl selectedSegment]); |
346 EXPECT_EQ(0, [tabView indexOfTabViewItem:[tabView selectedTabViewItem]]); | 374 EXPECT_EQ(0, [tabView indexOfTabViewItem:[tabView selectedTabViewItem]]); |
347 bridge_->SetSelectedTab(WebsiteSettingsUI::TAB_ID_CONNECTION); | 375 bridge_->SetSelectedTab(WebsiteSettingsUI::TAB_ID_CONNECTION); |
348 EXPECT_EQ(1, [segmentedControl selectedSegment]); | 376 EXPECT_EQ(1, [segmentedControl selectedSegment]); |
349 EXPECT_EQ(1, [tabView indexOfTabViewItem:[tabView selectedTabViewItem]]); | 377 EXPECT_EQ(1, [tabView indexOfTabViewItem:[tabView selectedTabViewItem]]); |
350 } | 378 } |
351 | 379 |
| 380 TEST_F(WebsiteSettingsBubbleControllerTest, WindowWidth) { |
| 381 // Try creating a window that is obviously too small. |
| 382 CreateBubbleWithWidth(30.0); |
| 383 SetTestPermissions(); |
| 384 |
| 385 CGFloat window_width = NSWidth([[controller_ window] frame]); |
| 386 |
| 387 // Check the window was made bigger to fit the content. |
| 388 EXPECT_LT(30.0, window_width); |
| 389 |
| 390 // Check that the window is wider than the right edge of all the permission |
| 391 // popup buttons. |
| 392 for (NSView* view in [[controller_ permissionsView] subviews]) { |
| 393 if ([view isKindOfClass:[NSPopUpButton class]]) { |
| 394 NSPopUpButton* button = static_cast<NSPopUpButton*>(view); |
| 395 EXPECT_LT(NSMaxX([button frame]), window_width); |
| 396 } |
| 397 } |
| 398 } |
| 399 |
352 } // namespace | 400 } // namespace |
OLD | NEW |