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