OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/frame/browser_view_layout.h" | 5 #include "chrome/browser/ui/views/frame/browser_view_layout.h" |
6 | 6 |
7 #include "chrome/browser/ui/views/frame/browser_view.h" | 7 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 8 #include "chrome/browser/ui/views/frame/contents_container.h" |
| 9 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h" |
| 10 #include "chrome/browser/ui/views/frame/overlay_container.h" |
8 #include "chrome/browser/ui/views/infobars/infobar_container_view.h" | 11 #include "chrome/browser/ui/views/infobars/infobar_container_view.h" |
| 12 #include "chrome/browser/ui/views/tabs/tab_strip.h" |
9 #include "chrome/test/base/browser_with_test_window_test.h" | 13 #include "chrome/test/base/browser_with_test_window_test.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
11 | 15 |
| 16 class MockBrowserViewLayoutDelegate : public BrowserViewLayoutDelegate { |
| 17 public: |
| 18 MockBrowserViewLayoutDelegate() |
| 19 : tab_strip_visible_(true), |
| 20 toolbar_visible_(true), |
| 21 bookmark_bar_visible_(true), |
| 22 download_shelf_needs_layout_(false) { |
| 23 } |
| 24 virtual ~MockBrowserViewLayoutDelegate() {} |
| 25 |
| 26 void set_download_shelf_needs_layout(bool layout) { |
| 27 download_shelf_needs_layout_ = layout; |
| 28 } |
| 29 void set_tab_strip_visible(bool visible) { |
| 30 tab_strip_visible_ = visible; |
| 31 } |
| 32 void set_toolbar_visible(bool visible) { |
| 33 toolbar_visible_ = visible; |
| 34 } |
| 35 void set_bookmark_bar_visible(bool visible) { |
| 36 bookmark_bar_visible_ = visible; |
| 37 } |
| 38 |
| 39 // BrowserViewLayout::Delegate overrides: |
| 40 virtual bool IsTabStripVisible() const OVERRIDE { |
| 41 return tab_strip_visible_; |
| 42 } |
| 43 virtual gfx::Rect GetBoundsForTabStrip(views::View* tab_strip) const |
| 44 OVERRIDE { |
| 45 return gfx::Rect(); |
| 46 } |
| 47 virtual bool IsToolbarVisible() const OVERRIDE { |
| 48 return toolbar_visible_; |
| 49 } |
| 50 virtual bool IsBookmarkBarVisible() const OVERRIDE { |
| 51 return bookmark_bar_visible_; |
| 52 } |
| 53 virtual bool DownloadShelfNeedsLayout() const OVERRIDE { |
| 54 return download_shelf_needs_layout_; |
| 55 } |
| 56 |
| 57 private: |
| 58 bool tab_strip_visible_; |
| 59 bool toolbar_visible_; |
| 60 bool bookmark_bar_visible_; |
| 61 bool download_shelf_needs_layout_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(MockBrowserViewLayoutDelegate); |
| 64 }; |
| 65 |
| 66 /////////////////////////////////////////////////////////////////////////////// |
| 67 |
| 68 // A simple view that prefers an initial size. |
| 69 class MockView : public views::View { |
| 70 public: |
| 71 explicit MockView(gfx::Size initial_size) |
| 72 : size_(initial_size) { |
| 73 SetBoundsRect(gfx::Rect(gfx::Point(), size_)); |
| 74 } |
| 75 virtual ~MockView() {} |
| 76 |
| 77 // views::View overrides: |
| 78 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 79 return size_; |
| 80 } |
| 81 |
| 82 private: |
| 83 gfx::Size size_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(MockView); |
| 86 }; |
| 87 |
| 88 /////////////////////////////////////////////////////////////////////////////// |
| 89 |
| 90 class MockImmersiveModeController : public ImmersiveModeController { |
| 91 public: |
| 92 MockImmersiveModeController() {} |
| 93 virtual ~MockImmersiveModeController() {} |
| 94 |
| 95 // ImmersiveModeController overrides: |
| 96 virtual void Init(BrowserView* browser_view) OVERRIDE {} |
| 97 virtual void SetEnabled(bool enabled) OVERRIDE {} |
| 98 virtual bool IsEnabled() const OVERRIDE { return false; } |
| 99 virtual bool ShouldHideTabIndicators() const OVERRIDE { return false; } |
| 100 virtual bool ShouldHideTopViews() const OVERRIDE { return false; } |
| 101 virtual bool IsRevealed() const OVERRIDE { return false; } |
| 102 virtual void MaybeStackViewAtTop() OVERRIDE {} |
| 103 virtual ImmersiveRevealedLock* GetRevealedLock( |
| 104 AnimateReveal animate_reveal) OVERRIDE WARN_UNUSED_RESULT { return NULL; } |
| 105 virtual void AnchorWidgetToTopContainer(views::Widget* widget, |
| 106 int y_offset) OVERRIDE {} |
| 107 virtual void UnanchorWidgetFromTopContainer(views::Widget* widget) OVERRIDE {} |
| 108 virtual void OnTopContainerBoundsChanged() OVERRIDE {} |
| 109 |
| 110 private: |
| 111 DISALLOW_COPY_AND_ASSIGN(MockImmersiveModeController); |
| 112 }; |
| 113 |
| 114 /////////////////////////////////////////////////////////////////////////////// |
12 // Tests of BrowserViewLayout. Runs tests without constructing a BrowserView. | 115 // Tests of BrowserViewLayout. Runs tests without constructing a BrowserView. |
13 class BrowserViewLayoutTest : public BrowserWithTestWindowTest { | 116 class BrowserViewLayoutTest : public BrowserWithTestWindowTest { |
14 public: | 117 public: |
15 BrowserViewLayoutTest() {} | 118 BrowserViewLayoutTest() |
| 119 : top_container_(NULL), |
| 120 tab_strip_(NULL), |
| 121 toolbar_(NULL), |
| 122 infobar_container_(NULL), |
| 123 contents_split_(NULL), |
| 124 contents_container_(NULL), |
| 125 overlay_container_(NULL), |
| 126 active_web_view_(NULL) {} |
16 virtual ~BrowserViewLayoutTest() {} | 127 virtual ~BrowserViewLayoutTest() {} |
17 | 128 |
| 129 BrowserViewLayout* layout() { return layout_.get(); } |
| 130 MockBrowserViewLayoutDelegate* delegate() { return delegate_.get(); } |
| 131 MockView* root_view() { return root_view_.get(); } |
| 132 MockView* top_container() { return top_container_; } |
| 133 TabStrip* tab_strip() { return tab_strip_; } |
| 134 MockView* toolbar() { return toolbar_; } |
| 135 InfoBarContainerView* infobar_container() { return infobar_container_; } |
| 136 MockView* contents_split() { return contents_split_; } |
| 137 ContentsContainer* contents_container() { return contents_container_; } |
| 138 |
| 139 // BrowserWithTestWindowTest overrides: |
18 virtual void SetUp() OVERRIDE { | 140 virtual void SetUp() OVERRIDE { |
19 BrowserWithTestWindowTest::SetUp(); | 141 BrowserWithTestWindowTest::SetUp(); |
20 | 142 |
21 // Because we have a TestBrowserWindow, not a BrowserView, |layout_| is | 143 delegate_.reset(new MockBrowserViewLayoutDelegate); |
22 // not attached to a host view. | 144 root_view_.reset(new MockView(gfx::Size(800, 600))); |
| 145 |
| 146 top_container_ = new MockView(gfx::Size(800, 60)); |
| 147 tab_strip_ = new TabStrip(NULL); |
| 148 top_container_->AddChildView(tab_strip_); |
| 149 toolbar_ = new MockView(gfx::Size(800, 30)); |
| 150 top_container_->AddChildView(toolbar_); |
| 151 root_view_->AddChildView(top_container_); |
| 152 |
| 153 overlay_container_ = new OverlayContainer(NULL); |
| 154 root_view_->AddChildView(overlay_container_); |
| 155 |
| 156 infobar_container_ = new InfoBarContainerView(NULL, NULL); |
| 157 root_view_->AddChildView(infobar_container_); |
| 158 |
| 159 contents_split_ = new MockView(gfx::Size(800, 600)); |
| 160 active_web_view_ = new MockView(gfx::Size(800, 600)); |
| 161 contents_container_ = new ContentsContainer(active_web_view_); |
| 162 contents_split_->AddChildView(contents_container_); |
| 163 root_view_->AddChildView(contents_split_); |
| 164 |
| 165 immersive_mode_controller_.reset(new MockImmersiveModeController); |
| 166 |
| 167 // TODO(jamescook): Attach |layout_| to |root_view_|? |
23 layout_.reset(new BrowserViewLayout); | 168 layout_.reset(new BrowserViewLayout); |
24 infobar_container_.reset(new InfoBarContainerView(NULL, NULL)); | |
25 layout_->Init(browser(), | 169 layout_->Init(browser(), |
26 NULL, | 170 delegate_.get(), |
27 infobar_container_.get(), | 171 NULL, // BrowserView. |
28 NULL, | 172 top_container_, |
29 NULL, | 173 tab_strip_, |
30 NULL); | 174 toolbar_, |
| 175 infobar_container_, |
| 176 contents_split_, |
| 177 contents_container_, |
| 178 overlay_container_, |
| 179 immersive_mode_controller_.get()); |
31 } | 180 } |
32 | 181 |
33 BrowserViewLayout* layout() { return layout_.get(); } | |
34 | |
35 private: | 182 private: |
36 scoped_ptr<BrowserViewLayout> layout_; | 183 scoped_ptr<BrowserViewLayout> layout_; |
37 scoped_ptr<InfoBarContainerView> infobar_container_; | 184 scoped_ptr<MockBrowserViewLayoutDelegate> delegate_; |
| 185 scoped_ptr<MockView> root_view_; |
| 186 |
| 187 // Views owned by |root_view_|. |
| 188 MockView* top_container_; |
| 189 TabStrip* tab_strip_; |
| 190 MockView* toolbar_; |
| 191 InfoBarContainerView* infobar_container_; |
| 192 MockView* contents_split_; |
| 193 ContentsContainer* contents_container_; |
| 194 OverlayContainer* overlay_container_; |
| 195 MockView* active_web_view_; |
| 196 |
| 197 scoped_ptr<MockImmersiveModeController> immersive_mode_controller_; |
38 | 198 |
39 DISALLOW_COPY_AND_ASSIGN(BrowserViewLayoutTest); | 199 DISALLOW_COPY_AND_ASSIGN(BrowserViewLayoutTest); |
40 }; | 200 }; |
41 | 201 |
42 // Test basic construction and initialization. | 202 // Test basic construction and initialization. |
43 TEST_F(BrowserViewLayoutTest, BrowserViewLayout) { | 203 TEST_F(BrowserViewLayoutTest, BrowserViewLayout) { |
44 EXPECT_TRUE(layout()->browser()); | 204 EXPECT_TRUE(layout()->browser()); |
45 EXPECT_TRUE(layout()->GetWebContentsModalDialogHost()); | 205 EXPECT_TRUE(layout()->GetWebContentsModalDialogHost()); |
46 EXPECT_EQ(BrowserViewLayout::kInstantUINone, layout()->GetInstantUIState()); | 206 EXPECT_EQ(BrowserViewLayout::kInstantUINone, layout()->GetInstantUIState()); |
47 EXPECT_FALSE(layout()->InfobarVisible()); | 207 EXPECT_FALSE(layout()->InfobarVisible()); |
48 // TODO(jamescook): Add more as we break dependencies. | |
49 } | 208 } |
50 | 209 |
51 // Test the core layout functions. | 210 // Test the core layout functions. |
52 TEST_F(BrowserViewLayoutTest, Layout) { | 211 TEST_F(BrowserViewLayoutTest, Layout) { |
53 // We don't have a bookmark bar yet, so no contents offset is required. | 212 // Simulate a window with no interesting UI. |
54 EXPECT_EQ(0, layout()->GetContentsOffsetForBookmarkBar()); | 213 delegate()->set_tab_strip_visible(false); |
55 EXPECT_EQ(0, layout()->GetTopMarginForActiveContent()); | 214 delegate()->set_toolbar_visible(false); |
56 // TODO(jamescook): Add more as we break dependencies. | 215 delegate()->set_bookmark_bar_visible(false); |
| 216 layout()->Layout(root_view()); |
| 217 |
| 218 // Top views are zero-height. |
| 219 EXPECT_EQ("0,0 0x0", tab_strip()->bounds().ToString()); |
| 220 EXPECT_EQ("0,0 800x0", toolbar()->bounds().ToString()); |
| 221 EXPECT_EQ("0,0 800x0", infobar_container()->bounds().ToString()); |
| 222 // Contents split fills the window. |
| 223 EXPECT_EQ("0,0 800x600", contents_split()->bounds().ToString()); |
| 224 |
| 225 // Turn on the toolbar, like in a pop-up window. |
| 226 delegate()->set_toolbar_visible(true); |
| 227 layout()->Layout(root_view()); |
| 228 |
| 229 // Now the toolbar has bounds and other views shift down. |
| 230 EXPECT_EQ("0,0 0x0", tab_strip()->bounds().ToString()); |
| 231 EXPECT_EQ("0,0 800x30", toolbar()->bounds().ToString()); |
| 232 EXPECT_EQ("0,30 800x0", infobar_container()->bounds().ToString()); |
| 233 EXPECT_EQ("0,30 800x570", contents_split()->bounds().ToString()); |
| 234 |
| 235 // TODO(jamescook): Tab strip and bookmark bar. |
57 } | 236 } |
| 237 |
| 238 TEST_F(BrowserViewLayoutTest, LayoutDownloadShelf) { |
| 239 scoped_ptr<MockView> download_shelf(new MockView(gfx::Size(800, 50))); |
| 240 layout()->set_download_shelf(download_shelf.get()); |
| 241 |
| 242 // If download shelf doesn't need layout, it doesn't move the bottom edge. |
| 243 delegate()->set_download_shelf_needs_layout(false); |
| 244 const int kBottom = 500; |
| 245 EXPECT_EQ(kBottom, layout()->LayoutDownloadShelf(kBottom)); |
| 246 |
| 247 // Download shelf layout moves up the bottom edge and sets visibility. |
| 248 delegate()->set_download_shelf_needs_layout(true); |
| 249 download_shelf->SetVisible(false); |
| 250 EXPECT_EQ(450, layout()->LayoutDownloadShelf(kBottom)); |
| 251 EXPECT_TRUE(download_shelf->visible()); |
| 252 EXPECT_EQ("0,450 0x50", download_shelf->bounds().ToString()); |
| 253 } |
OLD | NEW |