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.h" | 5 #include "chrome/browser/ui/views/frame/browser_view.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 | 8 |
9 #include "chrome/app/chrome_command_ids.h" | |
10 #include "chrome/browser/search_engines/template_url_service.h" | |
11 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
12 #include "chrome/browser/ui/browser_commands.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
14 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" | |
15 #include "chrome/browser/ui/views/frame/browser_view_layout.h" | |
16 #include "chrome/browser/ui/views/frame/top_container_view.h" | |
17 #include "chrome/browser/ui/views/infobars/infobar_container_view.h" | |
18 #include "chrome/browser/ui/views/tabs/tab_strip.h" | |
19 #include "chrome/browser/ui/views/toolbar_view.h" | |
20 #include "chrome/common/url_constants.h" | |
21 #include "chrome/test/base/browser_with_test_window_test.h" | 9 #include "chrome/test/base/browser_with_test_window_test.h" |
22 #include "chrome/test/base/scoped_testing_local_state.h" | 10 #include "chrome/test/base/scoped_testing_local_state.h" |
23 #include "chrome/test/base/testing_browser_process.h" | 11 #include "chrome/test/base/testing_browser_process.h" |
24 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "ui/views/controls/single_split_view.h" | |
27 #include "ui/views/controls/webview/webview.h" | |
28 | 14 |
29 #if defined(OS_CHROMEOS) | 15 #if defined(OS_CHROMEOS) |
30 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 16 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
31 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" | 17 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" |
32 #endif | 18 #endif |
33 | 19 |
34 namespace { | |
35 | |
36 // Tab strip bounds depend on the window frame sizes. | |
37 gfx::Point ExpectedTabStripOrigin(BrowserView* browser_view) { | |
38 gfx::Rect tabstrip_bounds( | |
39 browser_view->frame()->GetBoundsForTabStrip(browser_view->tabstrip())); | |
40 gfx::Point tabstrip_origin(tabstrip_bounds.origin()); | |
41 views::View::ConvertPointToTarget(browser_view->parent(), | |
42 browser_view, | |
43 &tabstrip_origin); | |
44 return tabstrip_origin; | |
45 } | |
46 | |
47 // Caller owns the returned service. | |
48 ProfileKeyedService* CreateTemplateURLService( | |
49 content::BrowserContext* profile) { | |
50 return new TemplateURLService(static_cast<Profile*>(profile)); | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 class BrowserViewTest : public BrowserWithTestWindowTest { | 20 class BrowserViewTest : public BrowserWithTestWindowTest { |
56 public: | 21 public: |
57 BrowserViewTest(); | 22 BrowserViewTest(); |
58 virtual ~BrowserViewTest() {} | 23 virtual ~BrowserViewTest() {} |
59 | 24 |
60 // BrowserWithTestWindowTest overrides: | 25 // BrowserWithTestWindowTest overrides: |
61 virtual void SetUp() OVERRIDE; | 26 virtual void SetUp() OVERRIDE; |
62 virtual void TearDown() OVERRIDE; | 27 virtual void TearDown() OVERRIDE; |
63 virtual TestingProfile* CreateProfile() OVERRIDE; | |
64 virtual BrowserWindow* CreateBrowserWindow() OVERRIDE; | 28 virtual BrowserWindow* CreateBrowserWindow() OVERRIDE; |
65 | 29 |
66 BrowserView* browser_view() { return browser_view_; } | 30 BrowserView* browser_view() { return browser_view_; } |
67 | 31 |
68 private: | 32 private: |
69 BrowserView* browser_view_; // Not owned. | 33 BrowserView* browser_view_; // Not owned. |
70 scoped_ptr<ScopedTestingLocalState> local_state_; | 34 scoped_ptr<ScopedTestingLocalState> local_state_; |
71 DISALLOW_COPY_AND_ASSIGN(BrowserViewTest); | 35 DISALLOW_COPY_AND_ASSIGN(BrowserViewTest); |
72 }; | 36 }; |
73 | 37 |
74 BrowserViewTest::BrowserViewTest() | 38 BrowserViewTest::BrowserViewTest() |
75 : browser_view_(NULL) { | 39 : browser_view_(NULL) { |
76 } | 40 } |
77 | 41 |
78 void BrowserViewTest::SetUp() { | 42 void BrowserViewTest::SetUp() { |
79 local_state_.reset( | 43 local_state_.reset( |
80 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal())); | 44 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal())); |
81 #if defined(OS_CHROMEOS) | 45 #if defined(OS_CHROMEOS) |
82 chromeos::input_method::InitializeForTesting( | 46 chromeos::input_method::InitializeForTesting( |
83 new chromeos::input_method::MockInputMethodManager); | 47 new chromeos::input_method::MockInputMethodManager); |
84 #endif | 48 #endif |
85 BrowserWithTestWindowTest::SetUp(); | 49 BrowserWithTestWindowTest::SetUp(); |
86 browser_view_ = static_cast<BrowserView*>(browser()->window()); | 50 browser_view_ = static_cast<BrowserView*>(browser()->window()); |
87 // Memory ownership is tricky here. BrowserView has taken ownership of | 51 // Memory ownership is tricky here. BrowserView has taken ownership of |
88 // |browser|, so BrowserWithTestWindowTest cannot continue to own it. | 52 // |browser|, so BrowserWithTestWindowTest cannot continue to own it. |
89 ASSERT_TRUE(release_browser()); | 53 ASSERT_TRUE(release_browser()); |
90 } | 54 } |
91 | 55 |
92 void BrowserViewTest::TearDown() { | 56 void BrowserViewTest::TearDown() { |
93 // Clean up any tabs we opened, otherwise Browser crashes in destruction. | |
94 browser_view_->browser()->tab_strip_model()->CloseAllTabs(); | |
95 // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up | 57 // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up |
96 // the Profile. | 58 // the Profile. |
97 browser_view_->GetWidget()->CloseNow(); | 59 browser_view_->GetWidget()->CloseNow(); |
98 browser_view_ = NULL; | 60 browser_view_ = NULL; |
99 BrowserWithTestWindowTest::TearDown(); | 61 BrowserWithTestWindowTest::TearDown(); |
100 #if defined(OS_CHROMEOS) | 62 #if defined(OS_CHROMEOS) |
101 chromeos::input_method::Shutdown(); | 63 chromeos::input_method::Shutdown(); |
102 #endif | 64 #endif |
103 local_state_.reset(NULL); | 65 local_state_.reset(NULL); |
104 } | 66 } |
105 | 67 |
106 TestingProfile* BrowserViewTest::CreateProfile() { | |
107 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile(); | |
108 // TemplateURLService is normally NULL during testing. Instant extended | |
109 // needs this service so set a custom factory function. | |
110 TemplateURLServiceFactory::GetInstance()->SetTestingFactory( | |
111 profile, &CreateTemplateURLService); | |
112 return profile; | |
113 } | |
114 | |
115 BrowserWindow* BrowserViewTest::CreateBrowserWindow() { | 68 BrowserWindow* BrowserViewTest::CreateBrowserWindow() { |
116 // Allow BrowserWithTestWindowTest to use Browser to create the default | 69 // Allow BrowserWithTestWindowTest to use Browser to create the default |
117 // BrowserView and BrowserFrame. | 70 // BrowserView and BrowserFrame. |
118 return NULL; | 71 return NULL; |
119 } | 72 } |
120 | 73 |
121 // Test basic construction and initialization. | 74 // Test basic construction and initialization. |
122 TEST_F(BrowserViewTest, BrowserView) { | 75 TEST_F(BrowserViewTest, BrowserView) { |
123 // The window is owned by the native widget, not the test class. | 76 // The window is owned by the native widget, not the test class. |
124 EXPECT_FALSE(window()); | 77 EXPECT_FALSE(window()); |
125 // |browser_view_| owns the Browser, not the test class. | 78 // |browser_view_| owns the Browser, not the test class. |
126 EXPECT_FALSE(browser()); | 79 EXPECT_FALSE(browser()); |
127 EXPECT_TRUE(browser_view()->browser()); | 80 EXPECT_TRUE(browser_view()->browser()); |
128 | 81 |
129 // Test initial state. | 82 // Test initial state. |
130 EXPECT_TRUE(browser_view()->IsTabStripVisible()); | 83 EXPECT_TRUE(browser_view()->IsTabStripVisible()); |
131 EXPECT_FALSE(browser_view()->IsOffTheRecord()); | 84 EXPECT_FALSE(browser_view()->IsOffTheRecord()); |
132 EXPECT_EQ(IDR_OTR_ICON, browser_view()->GetOTRIconResourceID()); | 85 EXPECT_EQ(IDR_OTR_ICON, browser_view()->GetOTRIconResourceID()); |
133 EXPECT_FALSE(browser_view()->IsGuestSession()); | 86 EXPECT_FALSE(browser_view()->IsGuestSession()); |
134 EXPECT_FALSE(browser_view()->ShouldShowAvatar()); | 87 EXPECT_FALSE(browser_view()->ShouldShowAvatar()); |
135 EXPECT_TRUE(browser_view()->IsBrowserTypeNormal()); | 88 EXPECT_TRUE(browser_view()->IsBrowserTypeNormal()); |
136 EXPECT_FALSE(browser_view()->IsFullscreen()); | 89 EXPECT_FALSE(browser_view()->IsFullscreen()); |
137 EXPECT_FALSE(browser_view()->IsBookmarkBarVisible()); | 90 EXPECT_FALSE(browser_view()->IsBookmarkBarVisible()); |
138 EXPECT_FALSE(browser_view()->IsBookmarkBarAnimating()); | 91 EXPECT_FALSE(browser_view()->IsBookmarkBarAnimating()); |
| 92 |
| 93 // Ensure we've initialized enough to run Layout(). |
| 94 browser_view()->Layout(); |
| 95 // TDOO(jamescook): Layout assertions. |
139 } | 96 } |
140 | |
141 // Test layout of the top-of-window UI. | |
142 TEST_F(BrowserViewTest, BrowserViewLayout) { | |
143 BookmarkBarView::DisableAnimationsForTesting(true); | |
144 | |
145 // |browser_view_| owns the Browser, not the test class. | |
146 Browser* browser = browser_view()->browser(); | |
147 TopContainerView* top_container = browser_view()->top_container(); | |
148 TabStrip* tabstrip = browser_view()->tabstrip(); | |
149 ToolbarView* toolbar = browser_view()->toolbar(); | |
150 views::SingleSplitView* contents_split = | |
151 browser_view()->GetContentsSplitForTest(); | |
152 views::WebView* contents_web_view = | |
153 browser_view()->GetContentsWebViewForTest(); | |
154 | |
155 // Start with a single tab open to a normal page. | |
156 AddTab(browser, GURL("about:blank")); | |
157 | |
158 // Verify the view hierarchy. | |
159 EXPECT_EQ(top_container, browser_view()->tabstrip()->parent()); | |
160 EXPECT_EQ(top_container, browser_view()->toolbar()->parent()); | |
161 EXPECT_EQ(top_container, browser_view()->GetBookmarkBarView()->parent()); | |
162 EXPECT_EQ(browser_view(), browser_view()->infobar_container()->parent()); | |
163 | |
164 // Top container is at the front of the view hierarchy. | |
165 EXPECT_EQ(browser_view()->child_count() - 1, | |
166 browser_view()->GetIndexOf(top_container)); | |
167 | |
168 // Verify basic layout. | |
169 EXPECT_EQ(0, top_container->x()); | |
170 EXPECT_EQ(0, top_container->y()); | |
171 EXPECT_EQ(browser_view()->width(), top_container->width()); | |
172 // Tabstrip layout varies based on window frame sizes. | |
173 gfx::Point expected_tabstrip_origin = ExpectedTabStripOrigin(browser_view()); | |
174 EXPECT_EQ(expected_tabstrip_origin.x(), tabstrip->x()); | |
175 EXPECT_EQ(expected_tabstrip_origin.y(), tabstrip->y()); | |
176 EXPECT_EQ(0, toolbar->x()); | |
177 EXPECT_EQ( | |
178 tabstrip->bounds().bottom() - | |
179 BrowserViewLayout::kToolbarTabStripVerticalOverlap, | |
180 toolbar->y()); | |
181 EXPECT_EQ(0, contents_split->x()); | |
182 EXPECT_EQ(toolbar->bounds().bottom(), contents_split->y()); | |
183 EXPECT_EQ(0, contents_web_view->x()); | |
184 EXPECT_EQ(0, contents_web_view->y()); | |
185 | |
186 // Verify bookmark bar visibility. | |
187 BookmarkBarView* bookmark_bar = browser_view()->GetBookmarkBarView(); | |
188 EXPECT_FALSE(bookmark_bar->visible()); | |
189 EXPECT_FALSE(bookmark_bar->IsDetached()); | |
190 chrome::ExecuteCommand(browser, IDC_SHOW_BOOKMARK_BAR); | |
191 EXPECT_TRUE(bookmark_bar->visible()); | |
192 EXPECT_FALSE(bookmark_bar->IsDetached()); | |
193 chrome::ExecuteCommand(browser, IDC_SHOW_BOOKMARK_BAR); | |
194 EXPECT_FALSE(bookmark_bar->visible()); | |
195 EXPECT_FALSE(bookmark_bar->IsDetached()); | |
196 | |
197 // Bookmark bar is reparented to BrowserView on NTP. | |
198 NavigateAndCommitActiveTabWithTitle(browser, | |
199 GURL(chrome::kChromeUINewTabURL), | |
200 string16()); | |
201 EXPECT_TRUE(bookmark_bar->visible()); | |
202 EXPECT_TRUE(bookmark_bar->IsDetached()); | |
203 EXPECT_EQ(browser_view(), bookmark_bar->parent()); | |
204 // Top container is still in front. | |
205 EXPECT_EQ(browser_view()->child_count() - 1, | |
206 browser_view()->GetIndexOf(top_container)); | |
207 | |
208 // Bookmark bar layout on NTP. | |
209 EXPECT_EQ(0, bookmark_bar->x()); | |
210 EXPECT_EQ( | |
211 tabstrip->bounds().bottom() + | |
212 toolbar->height() - | |
213 BrowserViewLayout::kToolbarTabStripVerticalOverlap - | |
214 views::NonClientFrameView::kClientEdgeThickness, | |
215 bookmark_bar->y()); | |
216 EXPECT_EQ(toolbar->bounds().bottom(), contents_split->y()); | |
217 // Contents view has a "top margin" pushing it below the bookmark bar. | |
218 EXPECT_EQ(bookmark_bar->height() - | |
219 views::NonClientFrameView::kClientEdgeThickness, | |
220 contents_web_view->y()); | |
221 | |
222 // Bookmark bar is parented back to top container on normal page. | |
223 NavigateAndCommitActiveTabWithTitle(browser, | |
224 GURL("about:blank"), | |
225 string16()); | |
226 EXPECT_FALSE(bookmark_bar->visible()); | |
227 EXPECT_FALSE(bookmark_bar->IsDetached()); | |
228 EXPECT_EQ(top_container, bookmark_bar->parent()); | |
229 // Top container is still in front. | |
230 EXPECT_EQ(browser_view()->child_count() - 1, | |
231 browser_view()->GetIndexOf(top_container)); | |
232 | |
233 BookmarkBarView::DisableAnimationsForTesting(false); | |
234 } | |
OLD | NEW |