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.h"
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
10 #include "chrome/browser/ui/views/frame/browser_view_layout.h"
11 #include "chrome/browser/ui/views/frame/test_with_browser_view.h"
12 #include "chrome/browser/ui/views/frame/top_container_view.h"
13 #include "chrome/browser/ui/views/infobars/infobar_container_view.h"
14 #include "chrome/browser/ui/views/tabs/tab_strip.h"
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
16 #include "chrome/common/url_constants.h"
17 #include "grit/generated_resources.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/views/controls/single_split_view.h"
21 #include "ui/views/controls/webview/webview.h"
25 // Tab strip bounds depend on the window frame sizes.
26 gfx::Point ExpectedTabStripOrigin(BrowserView* browser_view) {
27 gfx::Rect tabstrip_bounds(
28 browser_view->frame()->GetBoundsForTabStrip(browser_view->tabstrip()));
29 gfx::Point tabstrip_origin(tabstrip_bounds.origin());
30 views::View::ConvertPointToTarget(browser_view->parent(),
33 return tabstrip_origin;
38 typedef TestWithBrowserView BrowserViewTest;
40 // Test basic construction and initialization.
41 TEST_F(BrowserViewTest, BrowserView) {
42 // The window is owned by the native widget, not the test class.
43 EXPECT_FALSE(window());
45 EXPECT_TRUE(browser_view()->browser());
47 // Test initial state.
48 EXPECT_TRUE(browser_view()->IsTabStripVisible());
49 EXPECT_FALSE(browser_view()->IsOffTheRecord());
50 EXPECT_EQ(IDR_OTR_ICON, browser_view()->GetOTRIconResourceID());
51 EXPECT_FALSE(browser_view()->IsGuestSession());
52 EXPECT_FALSE(browser_view()->ShouldShowAvatar());
53 EXPECT_TRUE(browser_view()->IsBrowserTypeNormal());
54 EXPECT_FALSE(browser_view()->IsFullscreen());
55 EXPECT_FALSE(browser_view()->IsBookmarkBarVisible());
56 EXPECT_FALSE(browser_view()->IsBookmarkBarAnimating());
59 // Test layout of the top-of-window UI.
60 TEST_F(BrowserViewTest, BrowserViewLayout) {
61 BookmarkBarView::DisableAnimationsForTesting(true);
63 // |browser_view_| owns the Browser, not the test class.
64 Browser* browser = browser_view()->browser();
65 TopContainerView* top_container = browser_view()->top_container();
66 TabStrip* tabstrip = browser_view()->tabstrip();
67 ToolbarView* toolbar = browser_view()->toolbar();
68 views::View* contents_container =
69 browser_view()->GetContentsContainerForTest();
70 views::WebView* contents_web_view =
71 browser_view()->GetContentsWebViewForTest();
72 views::WebView* devtools_web_view =
73 browser_view()->GetDevToolsWebViewForTest();
75 // Start with a single tab open to a normal page.
76 AddTab(browser, GURL("about:blank"));
78 // Verify the view hierarchy.
79 EXPECT_EQ(top_container, browser_view()->tabstrip()->parent());
80 EXPECT_EQ(top_container, browser_view()->toolbar()->parent());
81 EXPECT_EQ(top_container, browser_view()->GetBookmarkBarView()->parent());
82 EXPECT_EQ(browser_view(), browser_view()->infobar_container()->parent());
84 // Find bar host is at the front of the view hierarchy, followed by the top
86 EXPECT_EQ(browser_view()->child_count() - 1,
87 browser_view()->GetIndexOf(browser_view()->find_bar_host_view()));
88 EXPECT_EQ(browser_view()->child_count() - 2,
89 browser_view()->GetIndexOf(top_container));
91 // Verify basic layout.
92 EXPECT_EQ(0, top_container->x());
93 EXPECT_EQ(0, top_container->y());
94 EXPECT_EQ(browser_view()->width(), top_container->width());
95 // Tabstrip layout varies based on window frame sizes.
96 gfx::Point expected_tabstrip_origin = ExpectedTabStripOrigin(browser_view());
97 EXPECT_EQ(expected_tabstrip_origin.x(), tabstrip->x());
98 EXPECT_EQ(expected_tabstrip_origin.y(), tabstrip->y());
99 EXPECT_EQ(0, toolbar->x());
101 tabstrip->bounds().bottom() -
102 BrowserViewLayout::kToolbarTabStripVerticalOverlap,
104 EXPECT_EQ(0, contents_container->x());
105 EXPECT_EQ(toolbar->bounds().bottom(), contents_container->y());
106 EXPECT_EQ(top_container->bounds().bottom(), contents_container->y());
107 EXPECT_EQ(0, devtools_web_view->x());
108 EXPECT_EQ(0, devtools_web_view->y());
109 EXPECT_EQ(0, contents_web_view->x());
110 EXPECT_EQ(0, contents_web_view->y());
112 // Verify bookmark bar visibility.
113 BookmarkBarView* bookmark_bar = browser_view()->GetBookmarkBarView();
114 EXPECT_FALSE(bookmark_bar->visible());
115 EXPECT_FALSE(bookmark_bar->IsDetached());
116 chrome::ExecuteCommand(browser, IDC_SHOW_BOOKMARK_BAR);
117 EXPECT_TRUE(bookmark_bar->visible());
118 EXPECT_FALSE(bookmark_bar->IsDetached());
119 chrome::ExecuteCommand(browser, IDC_SHOW_BOOKMARK_BAR);
120 EXPECT_FALSE(bookmark_bar->visible());
121 EXPECT_FALSE(bookmark_bar->IsDetached());
123 // Bookmark bar is reparented to BrowserView on NTP.
124 NavigateAndCommitActiveTabWithTitle(browser,
125 GURL(chrome::kChromeUINewTabURL),
127 EXPECT_TRUE(bookmark_bar->visible());
128 EXPECT_TRUE(bookmark_bar->IsDetached());
129 EXPECT_EQ(browser_view(), bookmark_bar->parent());
130 // Find bar host is still at the front of the view hierarchy, followed by
131 // the top container.
132 EXPECT_EQ(browser_view()->child_count() - 1,
133 browser_view()->GetIndexOf(browser_view()->find_bar_host_view()));
134 EXPECT_EQ(browser_view()->child_count() - 2,
135 browser_view()->GetIndexOf(top_container));
137 // Bookmark bar layout on NTP.
138 EXPECT_EQ(0, bookmark_bar->x());
140 tabstrip->bounds().bottom() +
142 BrowserViewLayout::kToolbarTabStripVerticalOverlap -
143 views::NonClientFrameView::kClientEdgeThickness,
145 EXPECT_EQ(toolbar->bounds().bottom(), contents_container->y());
146 // Contents view has a "top margin" pushing it below the bookmark bar.
147 EXPECT_EQ(bookmark_bar->height() -
148 views::NonClientFrameView::kClientEdgeThickness,
149 devtools_web_view->y());
150 EXPECT_EQ(bookmark_bar->height() -
151 views::NonClientFrameView::kClientEdgeThickness,
152 contents_web_view->y());
154 // Bookmark bar is parented back to top container on normal page.
155 NavigateAndCommitActiveTabWithTitle(browser,
158 EXPECT_FALSE(bookmark_bar->visible());
159 EXPECT_FALSE(bookmark_bar->IsDetached());
160 EXPECT_EQ(top_container, bookmark_bar->parent());
161 // Top container is still second from front.
162 EXPECT_EQ(browser_view()->child_count() - 2,
163 browser_view()->GetIndexOf(top_container));
165 BookmarkBarView::DisableAnimationsForTesting(false);
168 class BrowserViewHostedAppTest : public TestWithBrowserView {
170 BrowserViewHostedAppTest()
171 : TestWithBrowserView(Browser::TYPE_POPUP,
172 chrome::HOST_DESKTOP_TYPE_NATIVE,
175 virtual ~BrowserViewHostedAppTest() {
179 DISALLOW_COPY_AND_ASSIGN(BrowserViewHostedAppTest);
182 // Test basic layout for hosted apps.
183 TEST_F(BrowserViewHostedAppTest, Layout) {
184 // Add a tab because the browser starts out without any tabs at all.
185 AddTab(browser(), GURL("about:blank"));
187 views::View* contents_container =
188 browser_view()->GetContentsContainerForTest();
190 // The tabstrip, toolbar and bookmark bar should not be visible for hosted
192 EXPECT_FALSE(browser_view()->tabstrip()->visible());
193 EXPECT_FALSE(browser_view()->toolbar()->visible());
194 EXPECT_FALSE(browser_view()->IsBookmarkBarVisible());
196 gfx::Point header_offset;
197 views::View::ConvertPointToTarget(
199 browser_view()->frame()->non_client_view()->frame_view(),
202 // The position of the bottom of the header (the bar with the window
203 // controls) in the coordinates of BrowserView.
204 int bottom_of_header = browser_view()->frame()->GetTopInset() -
207 // The web contents should be flush with the bottom of the header.
208 EXPECT_EQ(bottom_of_header, contents_container->y());
210 // The find bar should overlap the 1px header/web-contents separator at the
211 // bottom of the header.
212 EXPECT_EQ(browser_view()->frame()->GetTopInset() - 1,
213 browser_view()->GetFindBarBoundingBox().y());