Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / constrained_window / constrained_window_mac_browsertest.mm
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.
4
5 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
6
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_tabstrip.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_view.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "url/gurl.h"
19
20 using ::testing::NiceMock;
21
22 namespace {
23
24 class ConstrainedWindowDelegateMock : public ConstrainedWindowMacDelegate {
25  public:
26   MOCK_METHOD1(OnConstrainedWindowClosed, void(ConstrainedWindowMac*));
27 };
28
29 }  // namespace
30
31 class ConstrainedWindowMacTest : public InProcessBrowserTest {
32  public:
33   ConstrainedWindowMacTest()
34       : InProcessBrowserTest(),
35         tab0_(NULL),
36         tab1_(NULL),
37         controller_(NULL),
38         tab_view0_(NULL),
39         tab_view1_(NULL) {
40     sheet_window_.reset([[NSWindow alloc]
41         initWithContentRect:NSMakeRect(0, 0, 30, 30)
42                   styleMask:NSTitledWindowMask
43                     backing:NSBackingStoreBuffered
44                       defer:NO]);
45     [sheet_window_ setReleasedWhenClosed:NO];
46     sheet_.reset([[CustomConstrainedWindowSheet alloc]
47         initWithCustomWindow:sheet_window_]);
48     [sheet_ hideSheet];
49   }
50
51   virtual void SetUpOnMainThread() OVERRIDE {
52     AddTabAtIndex(1, GURL("about:blank"), content::PAGE_TRANSITION_LINK);
53     tab0_ = browser()->tab_strip_model()->GetWebContentsAt(0);
54     tab1_ = browser()->tab_strip_model()->GetWebContentsAt(1);
55     EXPECT_EQ(tab1_, browser()->tab_strip_model()->GetActiveWebContents());
56
57     controller_ = [BrowserWindowController browserWindowControllerForWindow:
58         browser()->window()->GetNativeWindow()];
59     EXPECT_TRUE(controller_);
60     tab_view0_ = [[controller_ tabStripController] viewAtIndex:0];
61     EXPECT_TRUE(tab_view0_);
62     tab_view1_ = [[controller_ tabStripController] viewAtIndex:1];
63     EXPECT_TRUE(tab_view1_);
64   }
65
66  protected:
67   base::scoped_nsobject<CustomConstrainedWindowSheet> sheet_;
68   base::scoped_nsobject<NSWindow> sheet_window_;
69   content::WebContents* tab0_;
70   content::WebContents* tab1_;
71   BrowserWindowController* controller_;
72   NSView* tab_view0_;
73   NSView* tab_view1_;
74 };
75
76 // Test that a sheet added to a inactive tab is not shown until the
77 // tab is activated.
78 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, ShowInInactiveTab) {
79   // Show dialog in non active tab.
80   NiceMock<ConstrainedWindowDelegateMock> delegate;
81   ConstrainedWindowMac dialog(&delegate, tab0_, sheet_);
82   EXPECT_EQ(0.0, [sheet_window_ alphaValue]);
83
84   // Switch to inactive tab.
85   browser()->tab_strip_model()->ActivateTabAt(0, true);
86   EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
87
88   dialog.CloseWebContentsModalDialog();
89 }
90
91 // If a tab has never been shown then the associated tab view for the web
92 // content will not be created. Verify that adding a constrained window to such
93 // a tab works correctly.
94 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, ShowInUninitializedTab) {
95   scoped_ptr<content::WebContents> web_contents(content::WebContents::Create(
96       content::WebContents::CreateParams(browser()->profile())));
97   bool was_blocked = false;
98   chrome::AddWebContents(browser(), NULL, web_contents.release(),
99                          NEW_BACKGROUND_TAB, gfx::Rect(), false, &was_blocked);
100   content::WebContents* tab2 =
101       browser()->tab_strip_model()->GetWebContentsAt(2);
102   ASSERT_TRUE(tab2);
103   EXPECT_FALSE([tab2->GetView()->GetNativeView() superview]);
104
105   // Show dialog and verify that it's not visible yet.
106   NiceMock<ConstrainedWindowDelegateMock> delegate;
107   ConstrainedWindowMac dialog(&delegate, tab2, sheet_);
108   EXPECT_FALSE([sheet_window_ isVisible]);
109
110   // Activate the tab and verify that the constrained window is shown.
111   browser()->tab_strip_model()->ActivateTabAt(2, true);
112   EXPECT_TRUE([tab2->GetView()->GetNativeView() superview]);
113   EXPECT_TRUE([sheet_window_ isVisible]);
114   EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
115
116   dialog.CloseWebContentsModalDialog();
117 }
118
119 // Test that adding a sheet disables tab dragging.
120 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, TabDragging) {
121   NiceMock<ConstrainedWindowDelegateMock> delegate;
122   ConstrainedWindowMac dialog(&delegate, tab1_, sheet_);
123
124   // Verify that the dialog disables dragging.
125   EXPECT_TRUE([controller_ isTabDraggable:tab_view0_]);
126   EXPECT_FALSE([controller_ isTabDraggable:tab_view1_]);
127
128   dialog.CloseWebContentsModalDialog();
129 }
130
131 // Test that closing a browser window with a sheet works.
132 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, BrowserWindowClose) {
133   NiceMock<ConstrainedWindowDelegateMock> delegate;
134   ConstrainedWindowMac dialog(&delegate, tab1_, sheet_);
135   EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
136
137   // Close the browser window.
138   base::scoped_nsobject<NSWindow> browser_window(
139       [browser()->window()->GetNativeWindow() retain]);
140   EXPECT_TRUE([browser_window isVisible]);
141   [browser()->window()->GetNativeWindow() performClose:nil];
142   EXPECT_FALSE([browser_window isVisible]);
143 }
144
145 // Test that closing a tab with a sheet works.
146 IN_PROC_BROWSER_TEST_F(ConstrainedWindowMacTest, TabClose) {
147   NiceMock<ConstrainedWindowDelegateMock> delegate;
148   ConstrainedWindowMac dialog(&delegate, tab1_, sheet_);
149   EXPECT_EQ(1.0, [sheet_window_ alphaValue]);
150
151   // Close the tab.
152   TabStripModel* tab_strip = browser()->tab_strip_model();
153   EXPECT_EQ(2, tab_strip->count());
154   EXPECT_TRUE(tab_strip->CloseWebContentsAt(1,
155                                             TabStripModel::CLOSE_USER_GESTURE));
156   EXPECT_EQ(1, tab_strip->count());
157 }