1 // Copyright 2013 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/views/frame/browser_view_layout.h"
7 #include "chrome/browser/ui/views/frame/browser_view.h"
8 #include "chrome/browser/ui/views/frame/browser_view_layout_delegate.h"
9 #include "chrome/browser/ui/views/frame/contents_layout_manager.h"
10 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
11 #include "chrome/browser/ui/views/infobars/infobar_container_view.h"
12 #include "chrome/browser/ui/views/tabs/tab_strip.h"
13 #include "chrome/test/base/browser_with_test_window_test.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 class MockBrowserViewLayoutDelegate
: public BrowserViewLayoutDelegate
{
18 explicit MockBrowserViewLayoutDelegate(views::View
* contents_web_view
)
19 : contents_web_view_(contents_web_view
),
20 tab_strip_visible_(true),
21 toolbar_visible_(true),
22 bookmark_bar_visible_(true),
23 download_shelf_needs_layout_(false) {
25 virtual ~MockBrowserViewLayoutDelegate() {}
27 void set_download_shelf_needs_layout(bool layout
) {
28 download_shelf_needs_layout_
= layout
;
30 void set_tab_strip_visible(bool visible
) {
31 tab_strip_visible_
= visible
;
33 void set_toolbar_visible(bool visible
) {
34 toolbar_visible_
= visible
;
36 void set_bookmark_bar_visible(bool visible
) {
37 bookmark_bar_visible_
= visible
;
40 // BrowserViewLayout::Delegate overrides:
41 virtual views::View
* GetContentsWebView() const OVERRIDE
{
42 return contents_web_view_
;
44 virtual views::View
* GetWindowSwitcherButton() const OVERRIDE
{
45 // TODO(jamescook): Add a test for Windows that exercises the layout for
49 virtual bool IsTabStripVisible() const OVERRIDE
{
50 return tab_strip_visible_
;
52 virtual gfx::Rect
GetBoundsForTabStripInBrowserView() const OVERRIDE
{
55 virtual int GetTopInsetInBrowserView() const OVERRIDE
{
58 virtual int GetThemeBackgroundXInset() const OVERRIDE
{
61 virtual bool IsToolbarVisible() const OVERRIDE
{
62 return toolbar_visible_
;
64 virtual bool IsBookmarkBarVisible() const OVERRIDE
{
65 return bookmark_bar_visible_
;
67 virtual bool DownloadShelfNeedsLayout() const OVERRIDE
{
68 return download_shelf_needs_layout_
;
71 virtual FullscreenExitBubbleViews
* GetFullscreenExitBubble() const OVERRIDE
{
76 views::View
* contents_web_view_
;
77 bool tab_strip_visible_
;
78 bool toolbar_visible_
;
79 bool bookmark_bar_visible_
;
80 bool download_shelf_needs_layout_
;
82 DISALLOW_COPY_AND_ASSIGN(MockBrowserViewLayoutDelegate
);
85 ///////////////////////////////////////////////////////////////////////////////
87 // A simple view that prefers an initial size.
88 class MockView
: public views::View
{
90 explicit MockView(gfx::Size initial_size
)
91 : size_(initial_size
) {
92 SetBoundsRect(gfx::Rect(gfx::Point(), size_
));
94 virtual ~MockView() {}
96 // views::View overrides:
97 virtual gfx::Size
GetPreferredSize() OVERRIDE
{
104 DISALLOW_COPY_AND_ASSIGN(MockView
);
107 ///////////////////////////////////////////////////////////////////////////////
109 class MockImmersiveModeController
: public ImmersiveModeController
{
111 MockImmersiveModeController() {}
112 virtual ~MockImmersiveModeController() {}
114 // ImmersiveModeController overrides:
115 virtual void Init(BrowserView
* browser_view
) OVERRIDE
{}
116 virtual void SetEnabled(bool enabled
) OVERRIDE
{}
117 virtual bool IsEnabled() const OVERRIDE
{ return false; }
118 virtual bool ShouldHideTabIndicators() const OVERRIDE
{ return false; }
119 virtual bool ShouldHideTopViews() const OVERRIDE
{ return false; }
120 virtual bool IsRevealed() const OVERRIDE
{ return false; }
121 virtual int GetTopContainerVerticalOffset(
122 const gfx::Size
& top_container_size
) const OVERRIDE
{ return 0; }
123 virtual ImmersiveRevealedLock
* GetRevealedLock(
124 AnimateReveal animate_reveal
) OVERRIDE WARN_UNUSED_RESULT
{ return NULL
; }
125 virtual void OnFindBarVisibleBoundsChanged(
126 const gfx::Rect
& new_visible_bounds
) OVERRIDE
{}
127 virtual void SetupForTest() OVERRIDE
{}
130 DISALLOW_COPY_AND_ASSIGN(MockImmersiveModeController
);
133 ///////////////////////////////////////////////////////////////////////////////
134 // Tests of BrowserViewLayout. Runs tests without constructing a BrowserView.
135 class BrowserViewLayoutTest
: public BrowserWithTestWindowTest
{
137 BrowserViewLayoutTest()
139 top_container_(NULL
),
142 infobar_container_(NULL
),
143 contents_container_(NULL
),
144 contents_web_view_(NULL
),
145 devtools_web_view_(NULL
) {}
146 virtual ~BrowserViewLayoutTest() {}
148 BrowserViewLayout
* layout() { return layout_
.get(); }
149 MockBrowserViewLayoutDelegate
* delegate() { return delegate_
; }
150 MockView
* root_view() { return root_view_
.get(); }
151 MockView
* top_container() { return top_container_
; }
152 TabStrip
* tab_strip() { return tab_strip_
; }
153 MockView
* toolbar() { return toolbar_
; }
154 InfoBarContainerView
* infobar_container() { return infobar_container_
; }
155 MockView
* contents_container() { return contents_container_
; }
157 // BrowserWithTestWindowTest overrides:
158 virtual void SetUp() OVERRIDE
{
159 BrowserWithTestWindowTest::SetUp();
161 root_view_
.reset(new MockView(gfx::Size(800, 600)));
163 immersive_mode_controller_
.reset(new MockImmersiveModeController
);
165 top_container_
= new MockView(gfx::Size(800, 60));
166 tab_strip_
= new TabStrip(NULL
);
167 top_container_
->AddChildView(tab_strip_
);
168 toolbar_
= new MockView(gfx::Size(800, 30));
169 top_container_
->AddChildView(toolbar_
);
170 root_view_
->AddChildView(top_container_
);
172 infobar_container_
= new InfoBarContainerView(NULL
);
173 root_view_
->AddChildView(infobar_container_
);
175 contents_web_view_
= new MockView(gfx::Size(800, 600));
176 devtools_web_view_
= new MockView(gfx::Size(800, 600));
177 devtools_web_view_
->SetVisible(false);
179 contents_container_
= new MockView(gfx::Size(800, 600));
180 contents_container_
->AddChildView(devtools_web_view_
);
181 contents_container_
->AddChildView(contents_web_view_
);
182 ContentsLayoutManager
* contents_layout_manager
=
183 new ContentsLayoutManager(devtools_web_view_
, contents_web_view_
);
184 contents_container_
->SetLayoutManager(contents_layout_manager
);
186 root_view_
->AddChildView(contents_container_
);
188 // TODO(jamescook): Attach |layout_| to |root_view_|?
189 layout_
.reset(new BrowserViewLayout
);
190 delegate_
= new MockBrowserViewLayoutDelegate(contents_web_view_
);
191 layout_
->Init(delegate_
,
193 NULL
, // BrowserView.
199 contents_layout_manager
,
200 immersive_mode_controller_
.get());
204 scoped_ptr
<BrowserViewLayout
> layout_
;
205 MockBrowserViewLayoutDelegate
* delegate_
; // Owned by |layout_|.
206 scoped_ptr
<MockView
> root_view_
;
208 // Views owned by |root_view_|.
209 MockView
* top_container_
;
210 TabStrip
* tab_strip_
;
212 InfoBarContainerView
* infobar_container_
;
213 MockView
* contents_container_
;
214 MockView
* contents_web_view_
;
215 MockView
* devtools_web_view_
;
217 scoped_ptr
<MockImmersiveModeController
> immersive_mode_controller_
;
219 DISALLOW_COPY_AND_ASSIGN(BrowserViewLayoutTest
);
222 // Test basic construction and initialization.
223 TEST_F(BrowserViewLayoutTest
, BrowserViewLayout
) {
224 EXPECT_TRUE(layout()->browser());
225 EXPECT_TRUE(layout()->GetWebContentsModalDialogHost());
226 EXPECT_FALSE(layout()->InfobarVisible());
229 // Test the core layout functions.
230 TEST_F(BrowserViewLayoutTest
, Layout
) {
231 // Simulate a window with no interesting UI.
232 delegate()->set_tab_strip_visible(false);
233 delegate()->set_toolbar_visible(false);
234 delegate()->set_bookmark_bar_visible(false);
235 layout()->Layout(root_view());
237 // Top views are zero-height.
238 EXPECT_EQ("0,0 0x0", tab_strip()->bounds().ToString());
239 EXPECT_EQ("0,0 800x0", toolbar()->bounds().ToString());
240 EXPECT_EQ("0,0 800x0", infobar_container()->bounds().ToString());
241 // Contents split fills the window.
242 EXPECT_EQ("0,0 800x600", contents_container()->bounds().ToString());
244 // Turn on the toolbar, like in a pop-up window.
245 delegate()->set_toolbar_visible(true);
246 layout()->Layout(root_view());
248 // Now the toolbar has bounds and other views shift down.
249 EXPECT_EQ("0,0 0x0", tab_strip()->bounds().ToString());
250 EXPECT_EQ("0,0 800x30", toolbar()->bounds().ToString());
251 EXPECT_EQ("0,30 800x0", infobar_container()->bounds().ToString());
252 EXPECT_EQ("0,30 800x570", contents_container()->bounds().ToString());
254 // TODO(jamescook): Tab strip and bookmark bar.
257 TEST_F(BrowserViewLayoutTest
, LayoutDownloadShelf
) {
258 scoped_ptr
<MockView
> download_shelf(new MockView(gfx::Size(800, 50)));
259 layout()->set_download_shelf(download_shelf
.get());
261 // If download shelf doesn't need layout, it doesn't move the bottom edge.
262 delegate()->set_download_shelf_needs_layout(false);
263 const int kBottom
= 500;
264 EXPECT_EQ(kBottom
, layout()->LayoutDownloadShelf(kBottom
));
266 // Download shelf layout moves up the bottom edge and sets visibility.
267 delegate()->set_download_shelf_needs_layout(true);
268 download_shelf
->SetVisible(false);
269 EXPECT_EQ(450, layout()->LayoutDownloadShelf(kBottom
));
270 EXPECT_TRUE(download_shelf
->visible());
271 EXPECT_EQ("0,450 0x50", download_shelf
->bounds().ToString());