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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/browser_window.h"
6 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
7 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h"
8 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
9 #import "chrome/browser/ui/cocoa/extensions/toolbar_actions_bar_bubble_mac.h"
10 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
11 #include "chrome/browser/ui/extensions/extension_message_bubble_browsertest.h"
15 // Returns the ToolbarController for the given browser.
16 ToolbarController* ToolbarControllerForBrowser(Browser* browser) {
17 return [[BrowserWindowController browserWindowControllerForWindow:
18 browser->window()->GetNativeWindow()] toolbarController];
21 // Checks that the |bubble| is using the |expectedReferenceView|, and is in
22 // approximately the correct position.
23 void CheckBubbleAndReferenceView(ToolbarActionsBarBubbleMac* bubble,
24 NSView* expectedReferenceView) {
26 ASSERT_TRUE(expectedReferenceView);
28 // Do a rough check that the bubble is in the right place.
29 // A window's frame (like the bubble's) is already in screen coordinates.
30 NSRect bubbleFrame = [[bubble window] frame];
32 // Unfortunately, it's more tedious to get the reference view's screen
33 // coordinates (since -[NSWindow convertRectToScreen is in OSX 10.7).
34 NSRect referenceFrame = [expectedReferenceView bounds];
36 [expectedReferenceView convertRect:referenceFrame toView:nil];
37 NSWindow* window = [expectedReferenceView window];
38 CGFloat refLowY = [expectedReferenceView isFlipped] ?
39 NSMaxY(referenceFrame) : NSMinY(referenceFrame);
40 NSPoint refLowerLeft = NSMakePoint(NSMinX(referenceFrame), refLowY);
41 NSPoint refLowerLeftInScreen = [window convertBaseToScreen:refLowerLeft];
42 NSPoint refLowerRight = NSMakePoint(NSMaxX(referenceFrame), refLowY);
43 NSPoint refLowerRightInScreen = [window convertBaseToScreen:refLowerRight];
45 // The bubble should be below the reference view, but not too far below.
46 EXPECT_LE(NSMaxY(bubbleFrame), refLowerLeftInScreen.y);
47 // "Too far below" is kind of ambiguous. The exact logic of where a bubble
48 // is positioned with respect to its anchor view should be tested as part of
49 // the bubble logic, but we still want to make sure we didn't accidentally
50 // place it somewhere crazy (which can happen if we draw it, and then
51 // animate or reposition the reference view).
52 const int kFudgeFactor = 50;
53 EXPECT_LE(NSMaxY(bubbleFrame), refLowerLeftInScreen.y + kFudgeFactor);
55 // The bubble should intersect the reference view somewhere along the x-axis.
56 EXPECT_LE(refLowerLeftInScreen.x, NSMaxX(bubbleFrame));
57 EXPECT_LE(NSMinX(bubbleFrame), refLowerRightInScreen.x);
59 // And, of course, the bubble should be visible.
60 EXPECT_TRUE([[bubble window] isVisible]);
65 class ExtensionMessageBubbleBrowserTestMac
66 : public ExtensionMessageBubbleBrowserTest {
68 ExtensionMessageBubbleBrowserTestMac() {}
69 ~ExtensionMessageBubbleBrowserTestMac() override {}
72 void SetUpCommandLine(base::CommandLine* command_line) override;
73 void CheckBubble(Browser* browser, AnchorPosition anchor) override;
74 void CloseBubble(Browser* browser) override;
76 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageBubbleBrowserTestMac);
79 void ExtensionMessageBubbleBrowserTestMac::SetUpCommandLine(
80 base::CommandLine* command_line) {
81 ExtensionMessageBubbleBrowserTest::SetUpCommandLine(command_line);
82 [ToolbarActionsBarBubbleMac setAnimationEnabledForTesting:NO];
85 void ExtensionMessageBubbleBrowserTestMac::CheckBubble(
87 AnchorPosition anchor) {
88 ToolbarController* toolbarController = ToolbarControllerForBrowser(browser);
89 BrowserActionsController* actionsController =
90 [toolbarController browserActionsController];
91 NSView* anchorView = nil;
92 ToolbarActionsBarBubbleMac* bubble = [actionsController activeBubble];
94 case ANCHOR_BROWSER_ACTION:
95 anchorView = [actionsController buttonWithIndex:0];
97 case ANCHOR_WRENCH_MENU:
98 anchorView = [toolbarController wrenchButton];
101 CheckBubbleAndReferenceView(bubble, anchorView);
104 void ExtensionMessageBubbleBrowserTestMac::CloseBubble(Browser* browser) {
105 BrowserActionsController* controller =
106 [ToolbarControllerForBrowser(browser) browserActionsController];
107 ToolbarActionsBarBubbleMac* bubble = [controller activeBubble];
110 EXPECT_EQ(nil, [controller activeBubble]);
113 IN_PROC_BROWSER_TEST_F(ExtensionMessageBubbleBrowserTestMac,
114 ExtensionBubbleAnchoredToExtensionAction) {
115 TestBubbleAnchoredToExtensionAction();
118 IN_PROC_BROWSER_TEST_F(ExtensionMessageBubbleBrowserTestMac,
119 ExtensionBubbleAnchoredToWrenchMenu) {
120 TestBubbleAnchoredToWrenchMenu();
123 IN_PROC_BROWSER_TEST_F(ExtensionMessageBubbleBrowserTestMac,
124 ExtensionBubbleAnchoredToWrenchMenuWithOtherAction) {
125 TestBubbleAnchoredToWrenchMenuWithOtherAction();
128 IN_PROC_BROWSER_TEST_F(ExtensionMessageBubbleBrowserTestMac,
129 PRE_ExtensionBubbleShowsOnStartup) {
130 PreBubbleShowsOnStartup();
133 IN_PROC_BROWSER_TEST_F(ExtensionMessageBubbleBrowserTestMac,
134 ExtensionBubbleShowsOnStartup) {
135 TestBubbleShowsOnStartup();