Chromium Code Reviews| 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. | |
|
Avi (use Gerrit)
2015/07/30 02:50:35
I'm sure one day you will trace down some weird bu
Devlin
2015/07/31 19:48:30
This should only be used when the menu item is use
| |
| 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"; | |
|
Avi (use Gerrit)
2015/07/30 02:50:35
Did you intend that double space? Drop the space b
Devlin
2015/07/31 19:48:30
Done.
| |
| 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_ = nil; | |
|
Avi (use Gerrit)
2015/07/30 02:50:35
nullptr for C++ objects, nil for ObjC ones.
Devlin
2015/07/31 19:48:30
Heh, did that at first, and thought it looked so w
| |
| 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 base::Closure& closure) { | |
| 292 WrenchMenuController* wrenchMenuController = | |
| 293 [toolbarController wrenchMenuController]; | |
| 294 // The wrench menu should start as open (since that's where the overflowed | |
| 295 // actions are). | |
| 296 EXPECT_TRUE([wrenchMenuController isMenuOpen]); | |
| 297 BrowserActionsController* overflowController = | |
| 298 [wrenchMenuController browserActionsController]; | |
| 299 ASSERT_TRUE(overflowController); | |
| 300 | |
| 301 ToolbarActionsBar* overflowBar = [overflowController toolbarActionsBar]; | |
| 302 BrowserActionsContainerView* overflowContainer = | |
| 303 [overflowController containerView]; | |
| 304 NSMenu* menu = [wrenchMenuController menu]; | |
| 305 | |
| 306 // The overflow container should be within the bounds of the wrench menu, as | |
| 307 // should its parents. | |
| 308 int menu_width = [menu size].width; | |
| 309 NSRect frame = [overflowContainer frame]; | |
| 310 EXPECT_GE(NSMinX(frame), 0); | |
| 311 // Hierarchy: The overflow container is owned by two different views in the | |
| 312 // wrench menu. Each should have a min x of >= 0. | |
| 313 EXPECT_GE(NSMinX([[overflowContainer superview] frame]), 0); | |
| 314 EXPECT_GE(NSMinX([[[overflowContainer superview] superview] frame]), 0); | |
| 315 // The overflow container should fully fit in the wrench menu, including the | |
| 316 // space taken away for padding, and should have its desired size. | |
| 317 EXPECT_LE(NSWidth(frame), menu_width - kMenuPadding); | |
| 318 EXPECT_EQ(NSWidth(frame), overflowBar->GetPreferredSize().width()); | |
| 319 | |
| 320 // Every button that has an index lower than the overflow start index (i.e., | |
| 321 // every button on the main toolbar) should not be attached to a view. | |
| 322 for (int i = 0; i < overflowStartIndex; ++i) { | |
| 323 BrowserActionButton* button = [overflowController buttonWithIndex:i]; | |
| 324 EXPECT_FALSE([button superview]); | |
| 325 EXPECT_GE(0, NSMaxX([button frame])); | |
| 326 } | |
| 327 | |
| 328 // Every other button should be attached to a view, and should be at the | |
| 329 // proper bounds. Calculating each button's proper bounds here would just be | |
| 330 // a duplication of the logic in the method, but we can test that each button | |
| 331 // a) is within the overflow container's bounds, and | |
| 332 // b) doesn't intersect with another button. | |
| 333 // If both of those are true, then we're probably good. | |
| 334 for (NSUInteger i = overflowStartIndex; | |
| 335 i < [overflowController buttonCount]; ++i) { | |
| 336 BrowserActionButton* button = [overflowController buttonWithIndex:i]; | |
| 337 EXPECT_TRUE([button superview]); | |
| 338 EXPECT_TRUE(NSContainsRect([overflowContainer bounds], [button frame])); | |
| 339 for (NSUInteger j = 0; j < i; ++j) { | |
| 340 EXPECT_FALSE( | |
| 341 NSContainsRect([[overflowController buttonWithIndex:j] frame], | |
| 342 [button frame])); | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 // Close the wrench menu. | |
| 347 [wrenchMenuController cancel]; | |
| 348 EXPECT_FALSE([wrenchMenuController isMenuOpen]); | |
| 349 base::MessageLoop::current()->PostTask(FROM_HERE, closure); | |
| 350 } | |
| 351 | |
| 352 // Tests the layout of the overflow container in the wrench menu. | |
| 353 IN_PROC_BROWSER_TEST_F(BrowserActionButtonUiTest, TestOverflowContainerLayout) { | |
| 354 // Add a bunch of extensions - enough to trigger multiple rows in the overflow | |
| 355 // menu. | |
| 356 const int kNumExtensions = 12; | |
| 357 for (int i = 0; i < kNumExtensions; ++i) { | |
| 358 scoped_refptr<const extensions::Extension> extension = | |
| 359 extensions::extension_action_test_util::CreateActionExtension( | |
| 360 base::StringPrintf("extension%d", i), | |
| 361 extensions::extension_action_test_util::BROWSER_ACTION); | |
| 362 extension_service()->AddExtension(extension.get()); | |
| 363 } | |
| 364 ASSERT_EQ(kNumExtensions, static_cast<int>(model()->toolbar_items().size())); | |
| 365 | |
| 366 // A helper function to open the wrench menu and call the check function. | |
| 367 auto resizeAndActivateWrench = [this](int visible_count) { | |
| 368 model()->SetVisibleIconCount(kNumExtensions - visible_count); | |
| 369 MoveMouseToCenter(wrenchButton()); | |
| 370 | |
| 371 { | |
| 372 base::RunLoop runLoop; | |
| 373 // Click on the wrench menu, and pass in a callback to continue the test | |
| 374 // in CheckWrenchMenuLayout (due to the blocking nature of Cocoa menus, | |
| 375 // passing in runLoop.QuitClosure() is not sufficient here.) | |
| 376 ui_controls::SendMouseEventsNotifyWhenDone( | |
| 377 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP, | |
| 378 base::Bind(&CheckWrenchMenuLayout, | |
| 379 base::Unretained(toolbarController()), | |
| 380 kNumExtensions - visible_count, | |
| 381 runLoop.QuitClosure())); | |
| 382 runLoop.Run(); | |
| 383 } | |
| 384 }; | |
| 385 | |
| 386 // Test the layout with gradually more extensions hidden. | |
| 387 for (int i = 1; i <= kNumExtensions; ++i) | |
| 388 resizeAndActivateWrench(i); | |
| 389 | |
| 390 // Adding a global error adjusts the wrench menu size, and has been known | |
| 391 // to mess up the overflow container's bounds (crbug.com/511326). | |
| 392 GlobalErrorService* error_service = | |
| 393 GlobalErrorServiceFactory::GetForProfile(profile()); | |
| 394 error_service->AddGlobalError(new MenuError()); | |
| 395 | |
| 396 // It's probably excessive to test every level of the overflow here. Test | |
| 397 // having all actions overflowed, some actions overflowed, and one action | |
| 398 // overflowed. | |
| 399 resizeAndActivateWrench(kNumExtensions); | |
| 400 resizeAndActivateWrench(kNumExtensions / 2); | |
| 401 resizeAndActivateWrench(1); | |
| 402 } | |
| OLD | NEW |