Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_view_interactive_uitest.cc
blob1458f1d6af1db12715092fdfef4cdaf0d5bde950
1 // Copyright (c) 2015 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/ui/browser_commands.h"
8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
9 #include "chrome/test/base/in_process_browser_test.h"
10 #include "ui/views/focus/focus_manager.h"
12 #if defined(USE_AURA)
13 #include "chrome/browser/ui/browser_window_state.h"
14 #include "ui/aura/client/aura_constants.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_delegate.h"
17 #include "ui/gfx/screen.h"
18 #endif
20 using views::FocusManager;
22 typedef InProcessBrowserTest BrowserViewTest;
24 // Active window and focus testing is not reliable on Windows crbug.com/79493
25 #if defined(OS_WIN)
26 #define MAYBE_FullscreenClearsFocus DISABLED_FullscreenClearsFocus
27 #else
28 #define MAYBE_FullscreenClearsFocus FullscreenClearsFocus
29 #endif
30 IN_PROC_BROWSER_TEST_F(BrowserViewTest, MAYBE_FullscreenClearsFocus) {
31 BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());
32 LocationBarView* location_bar_view = browser_view->GetLocationBarView();
33 FocusManager* focus_manager = browser_view->GetFocusManager();
35 // Focus starts in the location bar or one of its children.
36 EXPECT_TRUE(location_bar_view->Contains(focus_manager->GetFocusedView()));
38 chrome::ToggleFullscreenMode(browser());
39 EXPECT_TRUE(browser_view->IsFullscreen());
41 // Focus is released from the location bar.
42 EXPECT_FALSE(location_bar_view->Contains(focus_manager->GetFocusedView()));
45 #if defined(USE_AURA)
46 namespace {
48 class BrowserViewTestParam : public BrowserViewTest,
49 public testing::WithParamInterface<bool> {
50 public:
51 bool TestApp() { return GetParam(); }
54 } // namespace
56 // Test that docked state is remembered for app browser windows and not
57 // remembered for tabbed browser windows.
58 IN_PROC_BROWSER_TEST_P(BrowserViewTestParam, BrowserRemembersDockedState) {
59 // Open a new browser window (app or tabbed depending on a parameter).
60 bool test_app = TestApp();
61 Browser::CreateParams params =
62 test_app ? Browser::CreateParams::CreateForApp(
63 "test_browser_app", true /* trusted_source */, gfx::Rect(),
64 browser()->profile(), browser()->host_desktop_type())
65 : Browser::CreateParams(browser()->profile(),
66 browser()->host_desktop_type());
67 params.initial_show_state = ui::SHOW_STATE_DEFAULT;
68 bool is_ash = browser()->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH;
69 // Default |browser()| is not used by this test.
70 browser()->window()->Close();
72 // Create a new app browser
73 Browser* browser = new Browser(params);
74 ASSERT_TRUE(browser);
75 gfx::NativeWindow window = browser->window()->GetNativeWindow();
76 gfx::Rect original_bounds(gfx::Rect(150, 250, 350, 100));
77 window->SetBounds(original_bounds);
78 window->Show();
79 // Dock the browser window using |kShowStateKey| property.
80 gfx::Rect work_area = gfx::Screen::GetScreenFor(window)
81 ->GetDisplayNearestPoint(window->bounds().origin())
82 .work_area();
83 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_DOCKED);
85 // Saved placement should reflect docked state (for app windows only in Ash).
86 gfx::Rect bounds;
87 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT;
88 const views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
89 widget->widget_delegate()->GetSavedWindowPlacement(widget, &bounds,
90 &show_state);
91 EXPECT_EQ(is_ash && test_app ? ui::SHOW_STATE_DOCKED : ui::SHOW_STATE_DEFAULT,
92 show_state);
93 // Docking is only relevant on Ash desktop.
94 if (!is_ash)
95 return;
97 // Saved placement should reflect restore bounds.
98 ASSERT_NE(nullptr, window->GetProperty(aura::client::kRestoreBoundsKey));
99 original_bounds = *window->GetProperty(aura::client::kRestoreBoundsKey);
100 gfx::Rect expected_bounds = work_area;
101 expected_bounds.ClampToCenteredSize(original_bounds.size());
102 expected_bounds.set_y(original_bounds.y());
103 EXPECT_EQ(expected_bounds.ToString(), bounds.ToString());
104 EXPECT_EQ(expected_bounds.ToString(), original_bounds.ToString());
106 // Browser window should be docked.
107 int width = 250; // same as DockedWindowLayoutManager::kIdealWidth.
108 if (window->delegate() && window->delegate()->GetMinimumSize().width() != 0)
109 width = std::max(width, window->delegate()->GetMinimumSize().width());
110 expected_bounds = work_area;
111 expected_bounds.set_width(width);
112 expected_bounds.set_x(work_area.right() - expected_bounds.width());
113 EXPECT_EQ(expected_bounds.ToString(), window->GetTargetBounds().ToString());
114 EXPECT_EQ(ui::SHOW_STATE_DOCKED,
115 window->GetProperty(aura::client::kShowStateKey));
116 browser->window()->Close();
118 // Newly created browser with the same app name should retain docked state
119 // for app browser window but leave it as normal for a tabbed browser.
120 browser = new Browser(params);
121 ASSERT_TRUE(browser);
122 browser->window()->Show();
123 window = browser->window()->GetNativeWindow();
124 EXPECT_EQ(test_app ? expected_bounds.ToString() : original_bounds.ToString(),
125 window->GetTargetBounds().ToString());
126 EXPECT_EQ(test_app ? ui::SHOW_STATE_DOCKED : ui::SHOW_STATE_NORMAL,
127 window->GetProperty(aura::client::kShowStateKey));
129 // Undocking the browser window should restore original size and vertical
130 // offset while centering the window horizontally.
131 // Tabbed window is already not docked.
132 expected_bounds = work_area;
133 expected_bounds.ClampToCenteredSize(original_bounds.size());
134 expected_bounds.set_y(original_bounds.y());
135 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
136 EXPECT_EQ(expected_bounds.ToString(), window->GetTargetBounds().ToString());
137 EXPECT_EQ(ui::SHOW_STATE_NORMAL,
138 window->GetProperty(aura::client::kShowStateKey));
139 browser->window()->Close();
141 // Re-create the browser window with the same app name.
142 browser = new Browser(params);
143 ASSERT_TRUE(browser);
144 browser->window()->Show();
146 // Newly created browser should retain undocked state and bounds.
147 window = browser->window()->GetNativeWindow();
148 EXPECT_EQ(expected_bounds.ToString(), window->GetTargetBounds().ToString());
149 EXPECT_EQ(ui::SHOW_STATE_NORMAL,
150 window->GetProperty(aura::client::kShowStateKey));
153 INSTANTIATE_TEST_CASE_P(BrowserViewTestTabbedOrApp,
154 BrowserViewTestParam,
155 testing::Bool());
156 #endif