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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/browser_tabstrip.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
14 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_view.h"
19 #include "testing/gmock/include/gmock/gmock.h"
22 using ::testing::NiceMock;
26 class ConstrainedWindowDelegateMock : public ConstrainedWindowMacDelegate {
28 MOCK_METHOD1(OnConstrainedWindowClosed, void(ConstrainedWindowMac*));
33 class ConstrainedWindowMacTest : public InProcessBrowserTest {
35 ConstrainedWindowMacTest()
36 : InProcessBrowserTest(),
42 sheet_window_.reset([[NSWindow alloc]
43 initWithContentRect:NSMakeRect(0, 0, 30, 30)
44 styleMask:NSTitledWindowMask
45 backing:NSBackingStoreBuffered
47 [sheet_window_ setReleasedWhenClosed:NO];
48 sheet_.reset([[CustomConstrainedWindowSheet alloc]
49 initWithCustomWindow:sheet_window_]);
53 virtual void SetUpOnMainThread() OVERRIDE {
54 AddTabAtIndex(1, GURL("about:blank"), content::PAGE_TRANSITION_LINK);
55 tab0_ = browser()->tab_strip_model()->GetWebContentsAt(0);
56 tab1_ = browser()->tab_strip_model()->GetWebContentsAt(1);
57 EXPECT_EQ(tab1_, browser()->tab_strip_model()->GetActiveWebContents());
59 controller_ = [BrowserWindowController browserWindowControllerForWindow:
60 browser()->window()->GetNativeWindow()];
61 EXPECT_TRUE(controller_);
62 tab_view0_ = [[controller_ tabStripController] viewAtIndex:0];
63 EXPECT_TRUE(tab_view0_);
64 tab_view1_ = [[controller_ tabStripController] viewAtIndex:1];
65 EXPECT_TRUE(tab_view1_);
69 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet_;
70 base::scoped_nsobject<NSWindow> sheet_window_;
71 content::WebContents* tab0_;
72 content::WebContents* tab1_;
73 BrowserWindowController* controller_;
78 // Test that a sheet added to a inactive tab is not shown until the
80 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, ShowInInactiveTab) {
81 // Show dialog in non active tab.
82 NiceMock<ConstrainedWindowDelegateMock> delegate;
83 ConstrainedWindowMac dialog(&delegate, tab0_, sheet_);
84 EXPECT_EQ(0.0, [sheet_window_ alphaValue]);
86 // Switch to inactive tab.
87 browser()->tab_strip_model()->ActivateTabAt(0, true);
88 EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
90 dialog.CloseWebContentsModalDialog();
93 // If a tab has never been shown then the associated tab view for the web
94 // content will not be created. Verify that adding a constrained window to such
95 // a tab works correctly.
96 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, ShowInUninitializedTab) {
97 scoped_ptr<content::WebContents> web_contents(content::WebContents::Create(
98 content::WebContents::CreateParams(browser()->profile())));
99 bool was_blocked = false;
100 chrome::AddWebContents(browser(), NULL, web_contents.release(),
101 NEW_BACKGROUND_TAB, gfx::Rect(), false, &was_blocked);
102 content::WebContents* tab2 =
103 browser()->tab_strip_model()->GetWebContentsAt(2);
105 EXPECT_FALSE([tab2->GetView()->GetNativeView() superview]);
107 // Show dialog and verify that it's not visible yet.
108 NiceMock<ConstrainedWindowDelegateMock> delegate;
109 ConstrainedWindowMac dialog(&delegate, tab2, sheet_);
110 EXPECT_FALSE([sheet_window_ isVisible]);
112 // Activate the tab and verify that the constrained window is shown.
113 browser()->tab_strip_model()->ActivateTabAt(2, true);
114 EXPECT_TRUE([tab2->GetView()->GetNativeView() superview]);
115 EXPECT_TRUE([sheet_window_ isVisible]);
116 EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
118 dialog.CloseWebContentsModalDialog();
121 // Test that adding a sheet disables tab dragging.
122 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, TabDragging) {
123 NiceMock<ConstrainedWindowDelegateMock> delegate;
124 ConstrainedWindowMac dialog(&delegate, tab1_, sheet_);
126 // Verify that the dialog disables dragging.
127 EXPECT_TRUE([controller_ isTabDraggable:tab_view0_]);
128 EXPECT_FALSE([controller_ isTabDraggable:tab_view1_]);
130 dialog.CloseWebContentsModalDialog();
133 // Test that closing a browser window with a sheet works.
134 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, BrowserWindowClose) {
135 NiceMock<ConstrainedWindowDelegateMock> delegate;
136 ConstrainedWindowMac dialog(&delegate, tab1_, sheet_);
137 EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
139 // Close the browser window.
140 base::scoped_nsobject<NSWindow> browser_window(
141 [browser()->window()->GetNativeWindow() retain]);
142 EXPECT_TRUE([browser_window isVisible]);
143 [browser()->window()->GetNativeWindow() performClose:nil];
144 EXPECT_FALSE([browser_window isVisible]);
147 // Test that closing a tab with a sheet works.
148 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, TabClose) {
149 NiceMock<ConstrainedWindowDelegateMock> delegate;
150 ConstrainedWindowMac dialog(&delegate, tab1_, sheet_);
151 EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
154 TabStripModel* tab_strip = browser()->tab_strip_model();
155 EXPECT_EQ(2, tab_strip->count());
156 EXPECT_TRUE(tab_strip->CloseWebContentsAt(1,
157 TabStripModel::CLOSE_USER_GESTURE));
158 EXPECT_EQ(1, tab_strip->count());
161 // Test that adding a sheet disables fullscreen.
162 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, Fullscreen) {
163 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
165 // Dialog will delete it self when closed.
166 NiceMock<ConstrainedWindowDelegateMock> delegate;
167 ConstrainedWindowMac dialog(&delegate, tab1_, sheet_);
169 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
171 dialog.CloseWebContentsModalDialog();