OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_action_test_util.h" | 10 #include "chrome/browser/extensions/extension_action_test_util.h" |
9 #include "chrome/browser/extensions/extension_browsertest.h" | 11 #include "chrome/browser/extensions/extension_browsertest.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_toolbar_model.h" | 13 #include "chrome/browser/extensions/extension_toolbar_model.h" |
12 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
13 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 15 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
14 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" | 16 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" |
15 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" | 17 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" |
16 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h" | 18 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h" |
17 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" | 19 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
18 #import "chrome/browser/ui/cocoa/wrench_menu/wrench_menu_controller.h" | 20 #import "chrome/browser/ui/cocoa/wrench_menu/wrench_menu_controller.h" |
| 21 #include "chrome/browser/ui/global_error/global_error.h" |
| 22 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 23 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
19 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 24 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
20 #include "chrome/test/base/interactive_test_utils.h" | 25 #include "chrome/test/base/interactive_test_utils.h" |
21 #include "extensions/common/feature_switch.h" | 26 #include "extensions/common/feature_switch.h" |
22 #import "ui/events/test/cocoa_test_event_utils.h" | 27 #import "ui/events/test/cocoa_test_event_utils.h" |
23 | 28 |
24 namespace { | 29 namespace { |
25 | 30 |
| 31 const int kMenuPadding = 26; |
| 32 |
| 33 // A simple error class that has a menu item. |
| 34 class MenuError : public GlobalError { |
| 35 public: |
| 36 MenuError() {} |
| 37 ~MenuError() override {} |
| 38 |
| 39 bool HasMenuItem() override { return true; } |
| 40 int MenuItemCommandID() override { |
| 41 // An arbitrary high id so that it's not taken. |
| 42 return 65536; |
| 43 } |
| 44 base::string16 MenuItemLabel() override { |
| 45 const char kErrorMessage[] = |
| 46 "This is a really long error message that will cause the wrench menu " |
| 47 "to have increased width"; |
| 48 return base::ASCIIToUTF16(kErrorMessage); |
| 49 } |
| 50 void ExecuteMenuItem(Browser* browser) override {} |
| 51 |
| 52 bool HasBubbleView() override { return false; } |
| 53 bool HasShownBubbleView() override { return false; } |
| 54 void ShowBubbleView(Browser* browser) override {} |
| 55 GlobalErrorBubbleViewBase* GetBubbleView() override { return nullptr; } |
| 56 |
| 57 private: |
| 58 DISALLOW_COPY_AND_ASSIGN(MenuError); |
| 59 }; |
| 60 |
26 // Returns the center point for a particular |view|. | 61 // Returns the center point for a particular |view|. |
27 NSPoint GetCenterPoint(NSView* view) { | 62 NSPoint GetCenterPoint(NSView* view) { |
28 NSWindow* window = [view window]; | 63 NSWindow* window = [view window]; |
29 NSScreen* screen = [window screen]; | 64 NSScreen* screen = [window screen]; |
30 DCHECK(screen); | 65 DCHECK(screen); |
31 | 66 |
32 // Converts the center position of the view into the coordinates accepted | 67 // Converts the center position of the view into the coordinates accepted |
33 // by ui_controls methods. | 68 // by ui_controls methods. |
34 NSRect bounds = [view bounds]; | 69 NSRect bounds = [view bounds]; |
35 NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); | 70 NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 @synthesize menuOpened = menuOpened_; | 121 @synthesize menuOpened = menuOpened_; |
87 @synthesize verify = verify_; | 122 @synthesize verify = verify_; |
88 | 123 |
89 @end | 124 @end |
90 | 125 |
91 class BrowserActionButtonUiTest : public ExtensionBrowserTest { | 126 class BrowserActionButtonUiTest : public ExtensionBrowserTest { |
92 protected: | 127 protected: |
93 BrowserActionButtonUiTest() {} | 128 BrowserActionButtonUiTest() {} |
94 ~BrowserActionButtonUiTest() override {} | 129 ~BrowserActionButtonUiTest() override {} |
95 | 130 |
| 131 void SetUpOnMainThread() override { |
| 132 ExtensionBrowserTest::SetUpOnMainThread(); |
| 133 toolbarController_ = |
| 134 [[BrowserWindowController |
| 135 browserWindowControllerForWindow:browser()-> |
| 136 window()->GetNativeWindow()] |
| 137 toolbarController]; |
| 138 ASSERT_TRUE(toolbarController_); |
| 139 wrenchMenuController_ = [toolbarController_ wrenchMenuController]; |
| 140 model_ = extensions::ExtensionToolbarModel::Get(profile()); |
| 141 } |
| 142 |
96 void SetUpCommandLine(base::CommandLine* command_line) override { | 143 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 144 ExtensionBrowserTest::SetUpCommandLine(command_line); |
97 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( | 145 enable_redesign_.reset(new extensions::FeatureSwitch::ScopedOverride( |
98 extensions::FeatureSwitch::extension_action_redesign(), true)); | 146 extensions::FeatureSwitch::extension_action_redesign(), true)); |
99 ToolbarActionsBar::disable_animations_for_testing_ = true; | 147 ToolbarActionsBar::disable_animations_for_testing_ = true; |
100 } | 148 } |
101 | 149 |
102 void TearDownOnMainThread() override { | 150 void TearDownOnMainThread() override { |
103 enable_redesign_.reset(); | 151 enable_redesign_.reset(); |
104 ToolbarActionsBar::disable_animations_for_testing_ = false; | 152 ToolbarActionsBar::disable_animations_for_testing_ = false; |
| 153 ExtensionBrowserTest::TearDownOnMainThread(); |
105 } | 154 } |
106 | 155 |
| 156 ToolbarController* toolbarController() { return toolbarController_; } |
| 157 WrenchMenuController* wrenchMenuController() { return wrenchMenuController_; } |
| 158 extensions::ExtensionToolbarModel* model() { return model_; } |
| 159 NSView* wrenchButton() { return [toolbarController_ wrenchButton]; } |
| 160 |
107 private: | 161 private: |
108 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; | 162 scoped_ptr<extensions::FeatureSwitch::ScopedOverride> enable_redesign_; |
109 | 163 |
| 164 ToolbarController* toolbarController_ = nil; |
| 165 WrenchMenuController* wrenchMenuController_ = nil; |
| 166 extensions::ExtensionToolbarModel* model_ = nullptr; |
| 167 |
110 DISALLOW_COPY_AND_ASSIGN(BrowserActionButtonUiTest); | 168 DISALLOW_COPY_AND_ASSIGN(BrowserActionButtonUiTest); |
111 }; | 169 }; |
112 | 170 |
113 // Simulates a clicks on the action button in the overflow menu, and runs | 171 // Simulates a clicks on the action button in the overflow menu, and runs |
114 // |closure| upon completion. | 172 // |closure| upon completion. |
115 void ClickOnOverflowedAction(ToolbarController* toolbarController, | 173 void ClickOnOverflowedAction(ToolbarController* toolbarController, |
116 const base::Closure& closure) { | 174 const base::Closure& closure) { |
117 WrenchMenuController* wrenchMenuController = | 175 WrenchMenuController* wrenchMenuController = |
118 [toolbarController wrenchMenuController]; | 176 [toolbarController wrenchMenuController]; |
119 // The wrench menu should start as open (since that's where the overflowed | 177 // The wrench menu should start as open (since that's where the overflowed |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // Test that opening a context menu works for both actions on the main bar and | 212 // Test that opening a context menu works for both actions on the main bar and |
155 // actions in the overflow menu. | 213 // actions in the overflow menu. |
156 IN_PROC_BROWSER_TEST_F(BrowserActionButtonUiTest, | 214 IN_PROC_BROWSER_TEST_F(BrowserActionButtonUiTest, |
157 ContextMenusOnMainAndOverflow) { | 215 ContextMenusOnMainAndOverflow) { |
158 // Add an extension with a browser action. | 216 // Add an extension with a browser action. |
159 scoped_refptr<const extensions::Extension> extension = | 217 scoped_refptr<const extensions::Extension> extension = |
160 extensions::extension_action_test_util::CreateActionExtension( | 218 extensions::extension_action_test_util::CreateActionExtension( |
161 "browser_action", | 219 "browser_action", |
162 extensions::extension_action_test_util::BROWSER_ACTION); | 220 extensions::extension_action_test_util::BROWSER_ACTION); |
163 extension_service()->AddExtension(extension.get()); | 221 extension_service()->AddExtension(extension.get()); |
164 extensions::ExtensionToolbarModel* model = | 222 ASSERT_EQ(1u, model()->toolbar_items().size()); |
165 extensions::ExtensionToolbarModel::Get(profile()); | |
166 ASSERT_EQ(1u, model->toolbar_items().size()); | |
167 | |
168 ToolbarController* toolbarController = | |
169 [[BrowserWindowController | |
170 browserWindowControllerForWindow:browser()-> | |
171 window()->GetNativeWindow()] | |
172 toolbarController]; | |
173 ASSERT_TRUE(toolbarController); | |
174 | 223 |
175 BrowserActionsController* actionsController = | 224 BrowserActionsController* actionsController = |
176 [toolbarController browserActionsController]; | 225 [toolbarController() browserActionsController]; |
177 BrowserActionButton* actionButton = [actionsController buttonWithIndex:0]; | 226 BrowserActionButton* actionButton = [actionsController buttonWithIndex:0]; |
178 ASSERT_TRUE(actionButton); | 227 ASSERT_TRUE(actionButton); |
179 | 228 |
180 // Stub out the action button's normal context menu with a fake one so we | 229 // Stub out the action button's normal context menu with a fake one so we |
181 // can track when it opens. | 230 // can track when it opens. |
182 base::scoped_nsobject<NSMenu> testContextMenu( | 231 base::scoped_nsobject<NSMenu> testContextMenu( |
183 [[NSMenu alloc] initWithTitle:@""]); | 232 [[NSMenu alloc] initWithTitle:@""]); |
184 base::scoped_nsobject<MenuHelper> menuHelper([[MenuHelper alloc] init]); | 233 base::scoped_nsobject<MenuHelper> menuHelper([[MenuHelper alloc] init]); |
185 [testContextMenu setDelegate:menuHelper.get()]; | 234 [testContextMenu setDelegate:menuHelper.get()]; |
186 [actionButton setTestContextMenu:testContextMenu.get()]; | 235 [actionButton setTestContextMenu:testContextMenu.get()]; |
(...skipping 16 matching lines...) Expand all Loading... |
203 // The menu should have opened from the click. | 252 // The menu should have opened from the click. |
204 EXPECT_TRUE([menuHelper menuOpened]); | 253 EXPECT_TRUE([menuHelper menuOpened]); |
205 } | 254 } |
206 | 255 |
207 // Reset the menu helper so we can use it again. | 256 // Reset the menu helper so we can use it again. |
208 [menuHelper setMenuOpened:NO]; | 257 [menuHelper setMenuOpened:NO]; |
209 [menuHelper setVerify:base::Bind( | 258 [menuHelper setVerify:base::Bind( |
210 CheckActionIsPoppedOut, actionsController, actionButton)]; | 259 CheckActionIsPoppedOut, actionsController, actionButton)]; |
211 | 260 |
212 // Shrink the visible count to be 0. This should hide the action button. | 261 // Shrink the visible count to be 0. This should hide the action button. |
213 model->SetVisibleIconCount(0); | 262 model()->SetVisibleIconCount(0); |
214 EXPECT_EQ(nil, [actionButton superview]); | 263 EXPECT_EQ(nil, [actionButton superview]); |
215 | 264 |
216 // Move the mouse over the wrench button. | 265 // Move the mouse over the wrench button. |
217 NSView* wrenchButton = [toolbarController wrenchButton]; | 266 MoveMouseToCenter(wrenchButton()); |
218 ASSERT_TRUE(wrenchButton); | |
219 MoveMouseToCenter(wrenchButton); | |
220 | 267 |
221 { | 268 { |
222 // No menu yet (on the browser action). | 269 // No menu yet (on the browser action). |
223 EXPECT_FALSE([menuHelper menuOpened]); | 270 EXPECT_FALSE([menuHelper menuOpened]); |
224 base::RunLoop runLoop; | 271 base::RunLoop runLoop; |
225 // Click on the wrench menu, and pass in a callback to continue the test | 272 // Click on the wrench menu, and pass in a callback to continue the test |
226 // in ClickOnOverflowedAction (Due to the blocking nature of Cocoa menus, | 273 // in ClickOnOverflowedAction (Due to the blocking nature of Cocoa menus, |
227 // passing in runLoop.QuitClosure() is not sufficient here.) | 274 // passing in runLoop.QuitClosure() is not sufficient here.) |
228 ui_controls::SendMouseEventsNotifyWhenDone( | 275 ui_controls::SendMouseEventsNotifyWhenDone( |
229 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP, | 276 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP, |
230 base::Bind(&ClickOnOverflowedAction, | 277 base::Bind(&ClickOnOverflowedAction, |
231 base::Unretained(toolbarController), | 278 base::Unretained(toolbarController()), |
232 runLoop.QuitClosure())); | 279 runLoop.QuitClosure())); |
233 runLoop.Run(); | 280 runLoop.Run(); |
234 // The menu should have opened. Note that the menu opened on the main bar's | 281 // The menu should have opened. Note that the menu opened on the main bar's |
235 // action button, not the overflow's. Since Cocoa doesn't support running | 282 // action button, not the overflow's. Since Cocoa doesn't support running |
236 // a menu-within-a-menu, this is what has to happen. | 283 // a menu-within-a-menu, this is what has to happen. |
237 EXPECT_TRUE([menuHelper menuOpened]); | 284 EXPECT_TRUE([menuHelper menuOpened]); |
238 } | 285 } |
239 } | 286 } |
| 287 |
| 288 // Checks the layout of the overflow bar in the wrench menu. |
| 289 void CheckWrenchMenuLayout(ToolbarController* toolbarController, |
| 290 int overflowStartIndex, |
| 291 const std::string& error_message, |
| 292 const base::Closure& closure) { |
| 293 WrenchMenuController* wrenchMenuController = |
| 294 [toolbarController wrenchMenuController]; |
| 295 // The wrench menu should start as open (since that's where the overflowed |
| 296 // actions are). |
| 297 EXPECT_TRUE([wrenchMenuController isMenuOpen]) << error_message; |
| 298 BrowserActionsController* overflowController = |
| 299 [wrenchMenuController browserActionsController]; |
| 300 ASSERT_TRUE(overflowController) << error_message; |
| 301 |
| 302 ToolbarActionsBar* overflowBar = [overflowController toolbarActionsBar]; |
| 303 BrowserActionsContainerView* overflowContainer = |
| 304 [overflowController containerView]; |
| 305 NSMenu* menu = [wrenchMenuController menu]; |
| 306 |
| 307 // The overflow container should be within the bounds of the wrench menu, as |
| 308 // should its parents. |
| 309 int menu_width = [menu size].width; |
| 310 NSRect frame = [overflowContainer frame]; |
| 311 // The container itself should be indented in the menu. |
| 312 EXPECT_GT(NSMinX(frame), 0) << error_message; |
| 313 // Hierarchy: The overflow container is owned by two different views in the |
| 314 // wrench menu. Each superview should start at 0 in the x-axis.. |
| 315 EXPECT_EQ(0, NSMinX([[overflowContainer superview] frame])) << error_message; |
| 316 EXPECT_EQ(0, NSMinX([[[overflowContainer superview] superview] frame])) << |
| 317 error_message; |
| 318 // The overflow container should fully fit in the wrench menu, including the |
| 319 // space taken away for padding, and should have its desired size. |
| 320 EXPECT_LE(NSWidth(frame), menu_width - kMenuPadding) << error_message; |
| 321 EXPECT_EQ(NSWidth(frame), overflowBar->GetPreferredSize().width()) << |
| 322 error_message; |
| 323 |
| 324 // Every button that has an index lower than the overflow start index (i.e., |
| 325 // every button on the main toolbar) should not be attached to a view. |
| 326 for (int i = 0; i < overflowStartIndex; ++i) { |
| 327 BrowserActionButton* button = [overflowController buttonWithIndex:i]; |
| 328 EXPECT_FALSE([button superview]) << error_message; |
| 329 EXPECT_GE(0, NSMaxX([button frame])) << error_message; |
| 330 } |
| 331 |
| 332 // Every other button should be attached to a view, and should be at the |
| 333 // proper bounds. Calculating each button's proper bounds here would just be |
| 334 // a duplication of the logic in the method, but we can test that each button |
| 335 // a) is within the overflow container's bounds, and |
| 336 // b) doesn't intersect with another button. |
| 337 // If both of those are true, then we're probably good. |
| 338 for (NSUInteger i = overflowStartIndex; |
| 339 i < [overflowController buttonCount]; ++i) { |
| 340 BrowserActionButton* button = [overflowController buttonWithIndex:i]; |
| 341 EXPECT_TRUE([button superview]) << error_message; |
| 342 EXPECT_TRUE(NSContainsRect([overflowContainer bounds], [button frame])) << |
| 343 error_message; |
| 344 for (NSUInteger j = 0; j < i; ++j) { |
| 345 EXPECT_FALSE( |
| 346 NSContainsRect([[overflowController buttonWithIndex:j] frame], |
| 347 [button frame])) << error_message; |
| 348 } |
| 349 } |
| 350 |
| 351 // Close the wrench menu. |
| 352 [wrenchMenuController cancel]; |
| 353 EXPECT_FALSE([wrenchMenuController isMenuOpen]) << error_message; |
| 354 base::MessageLoop::current()->PostTask(FROM_HERE, closure); |
| 355 } |
| 356 |
| 357 // Tests the layout of the overflow container in the wrench menu. |
| 358 IN_PROC_BROWSER_TEST_F(BrowserActionButtonUiTest, TestOverflowContainerLayout) { |
| 359 // Add a bunch of extensions - enough to trigger multiple rows in the overflow |
| 360 // menu. |
| 361 const int kNumExtensions = 12; |
| 362 for (int i = 0; i < kNumExtensions; ++i) { |
| 363 scoped_refptr<const extensions::Extension> extension = |
| 364 extensions::extension_action_test_util::CreateActionExtension( |
| 365 base::StringPrintf("extension%d", i), |
| 366 extensions::extension_action_test_util::BROWSER_ACTION); |
| 367 extension_service()->AddExtension(extension.get()); |
| 368 } |
| 369 ASSERT_EQ(kNumExtensions, static_cast<int>(model()->toolbar_items().size())); |
| 370 |
| 371 // A helper function to open the wrench menu and call the check function. |
| 372 auto resizeAndActivateWrench = [this](int visible_count, |
| 373 const std::string& error_message) { |
| 374 model()->SetVisibleIconCount(kNumExtensions - visible_count); |
| 375 MoveMouseToCenter(wrenchButton()); |
| 376 |
| 377 { |
| 378 base::RunLoop runLoop; |
| 379 // Click on the wrench menu, and pass in a callback to continue the test |
| 380 // in CheckWrenchMenuLayout (due to the blocking nature of Cocoa menus, |
| 381 // passing in runLoop.QuitClosure() is not sufficient here.) |
| 382 ui_controls::SendMouseEventsNotifyWhenDone( |
| 383 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP, |
| 384 base::Bind(&CheckWrenchMenuLayout, |
| 385 base::Unretained(toolbarController()), |
| 386 kNumExtensions - visible_count, |
| 387 error_message, |
| 388 runLoop.QuitClosure())); |
| 389 runLoop.Run(); |
| 390 } |
| 391 }; |
| 392 |
| 393 // Test the layout with gradually more extensions hidden. |
| 394 for (int i = 1; i <= kNumExtensions; ++i) |
| 395 resizeAndActivateWrench(i, base::StringPrintf("Normal: %d", i)); |
| 396 |
| 397 // Adding a global error adjusts the wrench menu size, and has been known |
| 398 // to mess up the overflow container's bounds (crbug.com/511326). |
| 399 GlobalErrorService* error_service = |
| 400 GlobalErrorServiceFactory::GetForProfile(profile()); |
| 401 error_service->AddGlobalError(new MenuError()); |
| 402 |
| 403 // It's probably excessive to test every level of the overflow here. Test |
| 404 // having all actions overflowed, some actions overflowed, and one action |
| 405 // overflowed. |
| 406 resizeAndActivateWrench(kNumExtensions, "GlobalError Full"); |
| 407 resizeAndActivateWrench(kNumExtensions / 2, "GlobalError Half"); |
| 408 resizeAndActivateWrench(1, "GlobalError One"); |
| 409 } |
OLD | NEW |