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/browser/devtools/devtools_window_testing.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_tabstrip.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "content/public/browser/invalidate_type.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_observer.h"
16 class BrowserViewTest : public InProcessBrowserTest {
18 BrowserViewTest() : InProcessBrowserTest(), devtools_(NULL) {}
21 BrowserView* browser_view() {
22 return BrowserView::GetBrowserViewForBrowser(browser());
25 views::WebView* devtools_web_view() {
26 return browser_view()->GetDevToolsWebViewForTest();
29 views::WebView* contents_web_view() {
30 return browser_view()->GetContentsWebViewForTest();
33 void OpenDevToolsWindow(bool docked) {
35 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), docked);
38 void CloseDevToolsWindow() {
39 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_);
42 void SetDevToolsBounds(const gfx::Rect& bounds) {
43 DevToolsWindowTesting::Get(devtools_)->SetInspectedPageBounds(bounds);
46 DevToolsWindow* devtools_;
49 DISALLOW_COPY_AND_ASSIGN(BrowserViewTest);
54 // Used to simulate scenario in a crash. When WebContentsDestroyed() is invoked
55 // updates the navigation state of another tab.
56 class TestWebContentsObserver : public content::WebContentsObserver {
58 TestWebContentsObserver(content::WebContents* source,
59 content::WebContents* other)
60 : content::WebContentsObserver(source),
62 virtual ~TestWebContentsObserver() {}
64 virtual void WebContentsDestroyed() OVERRIDE {
65 other_->NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
66 content::INVALIDATE_TYPE_URL | content::INVALIDATE_TYPE_LOAD));
70 content::WebContents* other_;
72 DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver);
77 // Verifies don't crash when CloseNow() is invoked with two tabs in a browser.
78 // Additionally when one of the tabs is destroyed NotifyNavigationStateChanged()
79 // is invoked on the other.
80 IN_PROC_BROWSER_TEST_F(BrowserViewTest, CloseWithTabs) {
82 new Browser(Browser::CreateParams(browser()->profile(),
83 browser()->host_desktop_type()));
84 chrome::AddTabAt(browser2, GURL(), -1, true);
85 chrome::AddTabAt(browser2, GURL(), -1, true);
86 TestWebContentsObserver observer(
87 browser2->tab_strip_model()->GetWebContentsAt(0),
88 browser2->tab_strip_model()->GetWebContentsAt(1));
89 BrowserView::GetBrowserViewForBrowser(browser2)->GetWidget()->CloseNow();
92 // Same as CloseWithTabs, but activates the first tab, which is the first tab
93 // BrowserView will destroy.
94 IN_PROC_BROWSER_TEST_F(BrowserViewTest, CloseWithTabsStartWithActive) {
96 new Browser(Browser::CreateParams(browser()->profile(),
97 browser()->host_desktop_type()));
98 chrome::AddTabAt(browser2, GURL(), -1, true);
99 chrome::AddTabAt(browser2, GURL(), -1, true);
100 browser2->tab_strip_model()->ActivateTabAt(0, true);
101 TestWebContentsObserver observer(
102 browser2->tab_strip_model()->GetWebContentsAt(0),
103 browser2->tab_strip_model()->GetWebContentsAt(1));
104 BrowserView::GetBrowserViewForBrowser(browser2)->GetWidget()->CloseNow();
107 // Verifies that page and devtools WebViews are being correctly layed out
108 // when DevTools is opened/closed/updated/undocked.
109 IN_PROC_BROWSER_TEST_F(BrowserViewTest, DevToolsUpdatesBrowserWindow) {
110 gfx::Rect full_bounds =
111 browser_view()->GetContentsContainerForTest()->GetLocalBounds();
112 gfx::Rect small_bounds(10, 20, 30, 40);
114 browser_view()->UpdateDevTools();
115 EXPECT_FALSE(devtools_web_view()->web_contents());
116 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
117 EXPECT_EQ(full_bounds, contents_web_view()->bounds());
120 OpenDevToolsWindow(true);
121 EXPECT_TRUE(devtools_web_view()->web_contents());
122 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
124 SetDevToolsBounds(small_bounds);
125 EXPECT_TRUE(devtools_web_view()->web_contents());
126 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
127 EXPECT_EQ(small_bounds, contents_web_view()->bounds());
129 browser_view()->UpdateDevTools();
130 EXPECT_TRUE(devtools_web_view()->web_contents());
131 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
132 EXPECT_EQ(small_bounds, contents_web_view()->bounds());
134 CloseDevToolsWindow();
135 EXPECT_FALSE(devtools_web_view()->web_contents());
136 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
137 EXPECT_EQ(full_bounds, contents_web_view()->bounds());
139 browser_view()->UpdateDevTools();
140 EXPECT_FALSE(devtools_web_view()->web_contents());
141 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
142 EXPECT_EQ(full_bounds, contents_web_view()->bounds());
145 OpenDevToolsWindow(false);
146 EXPECT_TRUE(devtools_web_view()->web_contents());
147 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
149 SetDevToolsBounds(small_bounds);
150 EXPECT_TRUE(devtools_web_view()->web_contents());
151 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
152 EXPECT_EQ(small_bounds, contents_web_view()->bounds());
154 browser_view()->UpdateDevTools();
155 EXPECT_TRUE(devtools_web_view()->web_contents());
156 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
157 EXPECT_EQ(small_bounds, contents_web_view()->bounds());
159 CloseDevToolsWindow();
160 EXPECT_FALSE(devtools_web_view()->web_contents());
161 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
162 EXPECT_EQ(full_bounds, contents_web_view()->bounds());
164 browser_view()->UpdateDevTools();
165 EXPECT_FALSE(devtools_web_view()->web_contents());
166 EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
167 EXPECT_EQ(full_bounds, contents_web_view()->bounds());