Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / frame / browser_view_browsertest.cc
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.
4
5 #include "chrome/browser/ui/views/frame/browser_view.h"
6
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"
15
16 class BrowserViewTest : public InProcessBrowserTest {
17  public:
18   BrowserViewTest() : InProcessBrowserTest(), devtools_(NULL) {}
19
20  protected:
21   BrowserView* browser_view() {
22     return BrowserView::GetBrowserViewForBrowser(browser());
23   }
24
25   views::WebView* devtools_web_view() {
26     return browser_view()->GetDevToolsWebViewForTest();
27   }
28
29   views::WebView* contents_web_view() {
30     return browser_view()->GetContentsWebViewForTest();
31   }
32
33   void OpenDevToolsWindow(bool docked) {
34     devtools_ =
35         DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), docked);
36   }
37
38   void CloseDevToolsWindow() {
39     DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_);
40   }
41
42   void SetDevToolsBounds(const gfx::Rect& bounds) {
43     DevToolsWindowTesting::Get(devtools_)->SetInspectedPageBounds(bounds);
44   }
45
46   DevToolsWindow* devtools_;
47
48  private:
49   DISALLOW_COPY_AND_ASSIGN(BrowserViewTest);
50 };
51
52 namespace {
53
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 {
57  public:
58   TestWebContentsObserver(content::WebContents* source,
59                           content::WebContents* other)
60       : content::WebContentsObserver(source),
61         other_(other) {}
62   virtual ~TestWebContentsObserver() {}
63
64   virtual void WebContentsDestroyed() OVERRIDE {
65     other_->NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
66         content::INVALIDATE_TYPE_URL | content::INVALIDATE_TYPE_LOAD));
67   }
68
69  private:
70   content::WebContents* other_;
71
72   DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver);
73 };
74
75 }  // namespace
76
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) {
81   Browser* browser2 =
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();
90 }
91
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) {
95   Browser* browser2 =
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();
105 }
106
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);
113
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());
118
119   // Docked.
120   OpenDevToolsWindow(true);
121   EXPECT_TRUE(devtools_web_view()->web_contents());
122   EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
123
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());
128
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());
133
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());
138
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());
143
144   // Undocked.
145   OpenDevToolsWindow(false);
146   EXPECT_TRUE(devtools_web_view()->web_contents());
147   EXPECT_EQ(full_bounds, devtools_web_view()->bounds());
148
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());
153
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());
158
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());
163
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());
168 }