Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_view_layout_unittest.cc
blob1bf455154a8b739781b2e30d921e80569db382bf
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_container.h"
10 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
11 #include "chrome/browser/ui/views/frame/overlay_container.h"
12 #include "chrome/browser/ui/views/infobars/infobar_container_view.h"
13 #include "chrome/browser/ui/views/tabs/tab_strip.h"
14 #include "chrome/test/base/browser_with_test_window_test.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 class MockBrowserViewLayoutDelegate : public BrowserViewLayoutDelegate {
18 public:
19 MockBrowserViewLayoutDelegate()
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* GetWindowSwitcherButton() const OVERRIDE {
42 // TODO(jamescook): Add a test for Windows that exercises the layout for
43 // this button.
44 return NULL;
46 virtual bool IsTabStripVisible() const OVERRIDE {
47 return tab_strip_visible_;
49 virtual gfx::Rect GetBoundsForTabStrip(views::View* tab_strip) const
50 OVERRIDE {
51 return gfx::Rect();
53 virtual bool IsToolbarVisible() const OVERRIDE {
54 return toolbar_visible_;
56 virtual bool IsBookmarkBarVisible() const OVERRIDE {
57 return bookmark_bar_visible_;
59 virtual bool DownloadShelfNeedsLayout() const OVERRIDE {
60 return download_shelf_needs_layout_;
63 private:
64 bool tab_strip_visible_;
65 bool toolbar_visible_;
66 bool bookmark_bar_visible_;
67 bool download_shelf_needs_layout_;
69 DISALLOW_COPY_AND_ASSIGN(MockBrowserViewLayoutDelegate);
72 ///////////////////////////////////////////////////////////////////////////////
74 // A simple view that prefers an initial size.
75 class MockView : public views::View {
76 public:
77 explicit MockView(gfx::Size initial_size)
78 : size_(initial_size) {
79 SetBoundsRect(gfx::Rect(gfx::Point(), size_));
81 virtual ~MockView() {}
83 // views::View overrides:
84 virtual gfx::Size GetPreferredSize() OVERRIDE {
85 return size_;
88 private:
89 gfx::Size size_;
91 DISALLOW_COPY_AND_ASSIGN(MockView);
94 ///////////////////////////////////////////////////////////////////////////////
96 class MockImmersiveModeController : public ImmersiveModeController {
97 public:
98 MockImmersiveModeController() {}
99 virtual ~MockImmersiveModeController() {}
101 // ImmersiveModeController overrides:
102 virtual void Init(Delegate* delegate,
103 views::Widget* widget,
104 TopContainerView* top_container) OVERRIDE {}
105 virtual void SetEnabled(bool enabled) OVERRIDE {}
106 virtual bool IsEnabled() const OVERRIDE { return false; }
107 virtual bool ShouldHideTabIndicators() const OVERRIDE { return false; }
108 virtual bool ShouldHideTopViews() const OVERRIDE { return false; }
109 virtual bool IsRevealed() const OVERRIDE { return false; }
110 virtual void MaybeStackViewAtTop() OVERRIDE {}
111 virtual ImmersiveRevealedLock* GetRevealedLock(
112 AnimateReveal animate_reveal) OVERRIDE WARN_UNUSED_RESULT { return NULL; }
113 virtual void AnchorWidgetToTopContainer(views::Widget* widget,
114 int y_offset) OVERRIDE {}
115 virtual void UnanchorWidgetFromTopContainer(views::Widget* widget) OVERRIDE {}
116 virtual void OnTopContainerBoundsChanged() OVERRIDE {}
118 private:
119 DISALLOW_COPY_AND_ASSIGN(MockImmersiveModeController);
122 ///////////////////////////////////////////////////////////////////////////////
123 // Tests of BrowserViewLayout. Runs tests without constructing a BrowserView.
124 class BrowserViewLayoutTest : public BrowserWithTestWindowTest {
125 public:
126 BrowserViewLayoutTest()
127 : delegate_(NULL),
128 top_container_(NULL),
129 tab_strip_(NULL),
130 toolbar_(NULL),
131 infobar_container_(NULL),
132 contents_split_(NULL),
133 contents_container_(NULL),
134 overlay_container_(NULL),
135 active_web_view_(NULL) {}
136 virtual ~BrowserViewLayoutTest() {}
138 BrowserViewLayout* layout() { return layout_.get(); }
139 MockBrowserViewLayoutDelegate* delegate() { return delegate_; }
140 MockView* root_view() { return root_view_.get(); }
141 MockView* top_container() { return top_container_; }
142 TabStrip* tab_strip() { return tab_strip_; }
143 MockView* toolbar() { return toolbar_; }
144 InfoBarContainerView* infobar_container() { return infobar_container_; }
145 MockView* contents_split() { return contents_split_; }
146 ContentsContainer* contents_container() { return contents_container_; }
148 // BrowserWithTestWindowTest overrides:
149 virtual void SetUp() OVERRIDE {
150 BrowserWithTestWindowTest::SetUp();
152 root_view_.reset(new MockView(gfx::Size(800, 600)));
154 immersive_mode_controller_.reset(new MockImmersiveModeController);
156 top_container_ = new MockView(gfx::Size(800, 60));
157 tab_strip_ = new TabStrip(NULL);
158 top_container_->AddChildView(tab_strip_);
159 toolbar_ = new MockView(gfx::Size(800, 30));
160 top_container_->AddChildView(toolbar_);
161 root_view_->AddChildView(top_container_);
163 overlay_container_ =
164 new OverlayContainer(NULL, immersive_mode_controller_.get());
165 root_view_->AddChildView(overlay_container_);
167 infobar_container_ = new InfoBarContainerView(NULL, NULL);
168 root_view_->AddChildView(infobar_container_);
170 contents_split_ = new MockView(gfx::Size(800, 600));
171 active_web_view_ = new MockView(gfx::Size(800, 600));
172 contents_container_ = new ContentsContainer(active_web_view_);
173 contents_split_->AddChildView(contents_container_);
174 root_view_->AddChildView(contents_split_);
176 // TODO(jamescook): Attach |layout_| to |root_view_|?
177 layout_.reset(new BrowserViewLayout);
178 delegate_ = new MockBrowserViewLayoutDelegate;
179 layout_->Init(delegate_,
180 browser(),
181 NULL, // BrowserView.
182 top_container_,
183 tab_strip_,
184 toolbar_,
185 infobar_container_,
186 contents_split_,
187 contents_container_,
188 overlay_container_,
189 immersive_mode_controller_.get());
192 private:
193 scoped_ptr<BrowserViewLayout> layout_;
194 MockBrowserViewLayoutDelegate* delegate_; // Owned by |layout_|.
195 scoped_ptr<MockView> root_view_;
197 // Views owned by |root_view_|.
198 MockView* top_container_;
199 TabStrip* tab_strip_;
200 MockView* toolbar_;
201 InfoBarContainerView* infobar_container_;
202 MockView* contents_split_;
203 ContentsContainer* contents_container_;
204 OverlayContainer* overlay_container_;
205 MockView* active_web_view_;
207 scoped_ptr<MockImmersiveModeController> immersive_mode_controller_;
209 DISALLOW_COPY_AND_ASSIGN(BrowserViewLayoutTest);
212 // Test basic construction and initialization.
213 TEST_F(BrowserViewLayoutTest, BrowserViewLayout) {
214 EXPECT_TRUE(layout()->browser());
215 EXPECT_TRUE(layout()->GetWebContentsModalDialogHost());
216 EXPECT_EQ(BrowserViewLayout::kInstantUINone, layout()->GetInstantUIState());
217 EXPECT_FALSE(layout()->InfobarVisible());
220 // Test the core layout functions.
221 TEST_F(BrowserViewLayoutTest, Layout) {
222 // Simulate a window with no interesting UI.
223 delegate()->set_tab_strip_visible(false);
224 delegate()->set_toolbar_visible(false);
225 delegate()->set_bookmark_bar_visible(false);
226 layout()->Layout(root_view());
228 // Top views are zero-height.
229 EXPECT_EQ("0,0 0x0", tab_strip()->bounds().ToString());
230 EXPECT_EQ("0,0 800x0", toolbar()->bounds().ToString());
231 EXPECT_EQ("0,0 800x0", infobar_container()->bounds().ToString());
232 // Contents split fills the window.
233 EXPECT_EQ("0,0 800x600", contents_split()->bounds().ToString());
235 // Turn on the toolbar, like in a pop-up window.
236 delegate()->set_toolbar_visible(true);
237 layout()->Layout(root_view());
239 // Now the toolbar has bounds and other views shift down.
240 EXPECT_EQ("0,0 0x0", tab_strip()->bounds().ToString());
241 EXPECT_EQ("0,0 800x30", toolbar()->bounds().ToString());
242 EXPECT_EQ("0,30 800x0", infobar_container()->bounds().ToString());
243 EXPECT_EQ("0,30 800x570", contents_split()->bounds().ToString());
245 // TODO(jamescook): Tab strip and bookmark bar.
248 TEST_F(BrowserViewLayoutTest, LayoutDownloadShelf) {
249 scoped_ptr<MockView> download_shelf(new MockView(gfx::Size(800, 50)));
250 layout()->set_download_shelf(download_shelf.get());
252 // If download shelf doesn't need layout, it doesn't move the bottom edge.
253 delegate()->set_download_shelf_needs_layout(false);
254 const int kBottom = 500;
255 EXPECT_EQ(kBottom, layout()->LayoutDownloadShelf(kBottom));
257 // Download shelf layout moves up the bottom edge and sets visibility.
258 delegate()->set_download_shelf_needs_layout(true);
259 download_shelf->SetVisible(false);
260 EXPECT_EQ(450, layout()->LayoutDownloadShelf(kBottom));
261 EXPECT_TRUE(download_shelf->visible());
262 EXPECT_EQ("0,450 0x50", download_shelf->bounds().ToString());