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/layout_constants.h"
15 #include "chrome/browser/ui/views/tabs/tab_strip.h"
16 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
17 #include "chrome/common/url_constants.h"
18 #include "ui/views/controls/single_split_view.h"
19 #include "ui/views/controls/webview/webview.h"
23 // Tab strip bounds depend on the window frame sizes.
24 gfx::Point
ExpectedTabStripOrigin(BrowserView
* browser_view
) {
25 gfx::Rect
tabstrip_bounds(
26 browser_view
->frame()->GetBoundsForTabStrip(browser_view
->tabstrip()));
27 gfx::Point
tabstrip_origin(tabstrip_bounds
.origin());
28 views::View::ConvertPointToTarget(browser_view
->parent(),
31 return tabstrip_origin
;
36 typedef TestWithBrowserView BrowserViewTest
;
38 // Test basic construction and initialization.
39 TEST_F(BrowserViewTest
, BrowserView
) {
40 // The window is owned by the native widget, not the test class.
41 EXPECT_FALSE(window());
43 EXPECT_TRUE(browser_view()->browser());
45 // Test initial state.
46 EXPECT_TRUE(browser_view()->IsTabStripVisible());
47 EXPECT_FALSE(browser_view()->IsOffTheRecord());
48 EXPECT_FALSE(browser_view()->IsGuestSession());
49 EXPECT_FALSE(browser_view()->ShouldShowAvatar());
50 EXPECT_TRUE(browser_view()->IsBrowserTypeNormal());
51 EXPECT_FALSE(browser_view()->IsFullscreen());
52 EXPECT_FALSE(browser_view()->IsBookmarkBarVisible());
53 EXPECT_FALSE(browser_view()->IsBookmarkBarAnimating());
56 // Test layout of the top-of-window UI.
57 TEST_F(BrowserViewTest
, BrowserViewLayout
) {
58 BookmarkBarView::DisableAnimationsForTesting(true);
60 // |browser_view_| owns the Browser, not the test class.
61 Browser
* browser
= browser_view()->browser();
62 TopContainerView
* top_container
= browser_view()->top_container();
63 TabStrip
* tabstrip
= browser_view()->tabstrip();
64 ToolbarView
* toolbar
= browser_view()->toolbar();
65 views::View
* contents_container
=
66 browser_view()->GetContentsContainerForTest();
67 views::WebView
* contents_web_view
=
68 browser_view()->GetContentsWebViewForTest();
69 views::WebView
* devtools_web_view
=
70 browser_view()->GetDevToolsWebViewForTest();
72 // Start with a single tab open to a normal page.
73 AddTab(browser
, GURL("about:blank"));
75 // Verify the view hierarchy.
76 EXPECT_EQ(top_container
, browser_view()->tabstrip()->parent());
77 EXPECT_EQ(top_container
, browser_view()->toolbar()->parent());
78 EXPECT_EQ(top_container
, browser_view()->GetBookmarkBarView()->parent());
79 EXPECT_EQ(browser_view(), browser_view()->infobar_container()->parent());
81 // Find bar host is at the front of the view hierarchy, followed by the top
83 EXPECT_EQ(browser_view()->child_count() - 1,
84 browser_view()->GetIndexOf(browser_view()->find_bar_host_view()));
85 EXPECT_EQ(browser_view()->child_count() - 2,
86 browser_view()->GetIndexOf(top_container
));
88 // Verify basic layout.
89 EXPECT_EQ(0, top_container
->x());
90 EXPECT_EQ(0, top_container
->y());
91 EXPECT_EQ(browser_view()->width(), top_container
->width());
92 // Tabstrip layout varies based on window frame sizes.
93 gfx::Point expected_tabstrip_origin
= ExpectedTabStripOrigin(browser_view());
94 EXPECT_EQ(expected_tabstrip_origin
.x(), tabstrip
->x());
95 EXPECT_EQ(expected_tabstrip_origin
.y(), tabstrip
->y());
96 EXPECT_EQ(0, toolbar
->x());
97 const int overlap
= GetLayoutConstant(TABSTRIP_TOOLBAR_OVERLAP
);
98 EXPECT_EQ(tabstrip
->bounds().bottom() - overlap
, toolbar
->y());
99 EXPECT_EQ(0, contents_container
->x());
100 EXPECT_EQ(toolbar
->bounds().bottom(), contents_container
->y());
101 EXPECT_EQ(top_container
->bounds().bottom(), contents_container
->y());
102 EXPECT_EQ(0, devtools_web_view
->x());
103 EXPECT_EQ(0, devtools_web_view
->y());
104 EXPECT_EQ(0, contents_web_view
->x());
105 EXPECT_EQ(0, contents_web_view
->y());
107 // Verify bookmark bar visibility.
108 BookmarkBarView
* bookmark_bar
= browser_view()->GetBookmarkBarView();
109 EXPECT_FALSE(bookmark_bar
->visible());
110 EXPECT_FALSE(bookmark_bar
->IsDetached());
111 chrome::ExecuteCommand(browser
, IDC_SHOW_BOOKMARK_BAR
);
112 EXPECT_TRUE(bookmark_bar
->visible());
113 EXPECT_FALSE(bookmark_bar
->IsDetached());
114 chrome::ExecuteCommand(browser
, IDC_SHOW_BOOKMARK_BAR
);
115 EXPECT_FALSE(bookmark_bar
->visible());
116 EXPECT_FALSE(bookmark_bar
->IsDetached());
118 // Bookmark bar is reparented to BrowserView on NTP.
119 NavigateAndCommitActiveTabWithTitle(browser
,
120 GURL(chrome::kChromeUINewTabURL
),
122 EXPECT_TRUE(bookmark_bar
->visible());
123 EXPECT_TRUE(bookmark_bar
->IsDetached());
124 EXPECT_EQ(browser_view(), bookmark_bar
->parent());
125 // Find bar host is still at the front of the view hierarchy, followed by
126 // the top container.
127 EXPECT_EQ(browser_view()->child_count() - 1,
128 browser_view()->GetIndexOf(browser_view()->find_bar_host_view()));
129 EXPECT_EQ(browser_view()->child_count() - 2,
130 browser_view()->GetIndexOf(top_container
));
132 // Bookmark bar layout on NTP.
133 EXPECT_EQ(0, bookmark_bar
->x());
135 tabstrip
->bounds().bottom() + toolbar
->height() - overlap
-
136 views::NonClientFrameView::kClientEdgeThickness
,
138 EXPECT_EQ(toolbar
->bounds().bottom(), contents_container
->y());
139 // Contents view has a "top margin" pushing it below the bookmark bar.
140 EXPECT_EQ(bookmark_bar
->height() -
141 views::NonClientFrameView::kClientEdgeThickness
,
142 devtools_web_view
->y());
143 EXPECT_EQ(bookmark_bar
->height() -
144 views::NonClientFrameView::kClientEdgeThickness
,
145 contents_web_view
->y());
147 // Bookmark bar is parented back to top container on normal page.
148 NavigateAndCommitActiveTabWithTitle(browser
,
151 EXPECT_FALSE(bookmark_bar
->visible());
152 EXPECT_FALSE(bookmark_bar
->IsDetached());
153 EXPECT_EQ(top_container
, bookmark_bar
->parent());
154 // Top container is still second from front.
155 EXPECT_EQ(browser_view()->child_count() - 2,
156 browser_view()->GetIndexOf(top_container
));
158 BookmarkBarView::DisableAnimationsForTesting(false);
161 class BrowserViewHostedAppTest
: public TestWithBrowserView
{
163 BrowserViewHostedAppTest()
164 : TestWithBrowserView(Browser::TYPE_POPUP
,
165 chrome::HOST_DESKTOP_TYPE_NATIVE
,
168 ~BrowserViewHostedAppTest() override
{}
171 DISALLOW_COPY_AND_ASSIGN(BrowserViewHostedAppTest
);
174 // Test basic layout for hosted apps.
175 TEST_F(BrowserViewHostedAppTest
, Layout
) {
176 // Add a tab because the browser starts out without any tabs at all.
177 AddTab(browser(), GURL("about:blank"));
179 views::View
* contents_container
=
180 browser_view()->GetContentsContainerForTest();
182 // The tabstrip, toolbar and bookmark bar should not be visible for hosted
184 EXPECT_FALSE(browser_view()->tabstrip()->visible());
185 EXPECT_FALSE(browser_view()->toolbar()->visible());
186 EXPECT_FALSE(browser_view()->IsBookmarkBarVisible());
188 gfx::Point header_offset
;
189 views::View::ConvertPointToTarget(
191 browser_view()->frame()->non_client_view()->frame_view(),
194 // The position of the bottom of the header (the bar with the window
195 // controls) in the coordinates of BrowserView.
196 int bottom_of_header
= browser_view()->frame()->GetTopInset() -
199 // The web contents should be flush with the bottom of the header.
200 EXPECT_EQ(bottom_of_header
, contents_container
->y());
202 // The find bar should overlap the 1px header/web-contents separator at the
203 // bottom of the header.
204 EXPECT_EQ(browser_view()->frame()->GetTopInset() - 1,
205 browser_view()->GetFindBarBoundingBox().y());