Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(678)

Side by Side Diff: chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc

Issue 11072002: Combined window positioning and show state determiniation in the WindowSizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/window_sizer/window_sizer_common_unittest.h"
6
7 #include "ash/shell.h"
8 #include "ash/test/test_shell_delegate.h"
9 #include "ash/wm/window_resizer.h"
10 #include "base/compiler_specific.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/render_view_test.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/root_window.h"
19 #include "ui/aura/test/test_windows.h"
20
21 // The class function definitions from window_sizer_common_unittest.h
22 WindowSizerTestWithBrowser::WindowSizerTestWithBrowser() {
23 // Set up a UI message thread.
24 MessageLoopForUI* ui_loop = message_loop();
25 ui_thread_.reset(
26 new content::TestBrowserThread(content::BrowserThread::UI, ui_loop));
27 }
28
29 WindowSizerTestWithBrowser::~WindowSizerTestWithBrowser() {
30 }
31
32 void TestMonitorInfoProvider::AddMonitor(const gfx::Rect& bounds,
33 const gfx::Rect& work_area) {
34 DCHECK(bounds.Contains(work_area));
35 monitor_bounds_.push_back(bounds);
36 work_areas_.push_back(work_area);
37 }
38
39 // Overridden from WindowSizer::MonitorInfoProvider:
40 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayWorkArea() const {
41 return work_areas_[0];
42 }
43
44 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayBounds() const {
45 return monitor_bounds_[0];
46 }
47
48 gfx::Rect TestMonitorInfoProvider::GetMonitorWorkAreaMatching(
49 const gfx::Rect& match_rect) const {
50 return work_areas_[GetMonitorIndexMatchingBounds(match_rect)];
51 }
52
53 size_t TestMonitorInfoProvider::GetMonitorIndexMatchingBounds(
54 const gfx::Rect& match_rect) const {
55 int max_area = 0;
56 size_t max_area_index = 0;
57 // Loop through all the monitors, finding the one that intersects the
58 // largest area of the supplied match rect.
59 for (size_t i = 0; i < work_areas_.size(); ++i) {
60 gfx::Rect overlap(match_rect.Intersect(work_areas_[i]));
61 int area = overlap.width() * overlap.height();
62 if (area > max_area) {
63 max_area = area;
64 max_area_index = i;
65 }
66 }
67 return max_area_index;
68 }
69
70 TestStateProvider::TestStateProvider():
71 has_persistent_data_(false),
72 persistent_show_state_(ui::SHOW_STATE_DEFAULT),
73 has_last_active_data_(false),
74 last_active_show_state_(ui::SHOW_STATE_DEFAULT) {
75 }
76
77 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds,
78 const gfx::Rect& work_area,
79 ui::WindowShowState show_state,
80 bool has_persistent_data) {
81 persistent_bounds_ = bounds;
82 persistent_work_area_ = work_area;
83 persistent_show_state_ = show_state;
84 has_persistent_data_ = has_persistent_data;
85 }
86
87 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds,
88 ui::WindowShowState show_state,
89 bool has_last_active_data) {
90 last_active_bounds_ = bounds;
91 last_active_show_state_ = show_state;
92 has_last_active_data_ = has_last_active_data;
93 }
94
95 bool TestStateProvider::GetPersistentState(
96 gfx::Rect* bounds,
97 gfx::Rect* saved_work_area,
98 ui::WindowShowState& show_state) const {
99 *bounds = persistent_bounds_;
100 *saved_work_area = persistent_work_area_;
101 if (show_state == ui::SHOW_STATE_DEFAULT)
102 show_state = persistent_show_state_;
103 return has_persistent_data_;
104 }
105
106 bool TestStateProvider::GetLastActiveWindowState(
107 gfx::Rect* bounds,
108 ui::WindowShowState& show_state) const {
109 *bounds = last_active_bounds_;
110 if (show_state == ui::SHOW_STATE_DEFAULT)
111 show_state = last_active_show_state_;
112 return has_last_active_data_;
113 }
114
115 TestBrowserWindowAura::TestBrowserWindowAura(aura::Window *native_window)
116 : native_window_(native_window) {
117 }
118
119 TestBrowserWindowAura::~TestBrowserWindowAura() {}
120
121 gfx::Rect TestBrowserWindowAura::GetBounds() const {
122 return native_window_->bounds();
123 }
124
125 int kWindowTilePixels = WindowSizer::kWindowTilePixels;
126
127 // The window sizer commonly used test functions.
128 void GetWindowBoundsAndShowState(
129 const gfx::Rect& monitor1_bounds,
130 const gfx::Rect& monitor1_work_area,
131 const gfx::Rect& monitor2_bounds,
132 const gfx::Rect& bounds,
133 const gfx::Rect& work_area,
134 ui::WindowShowState show_state_persisted,
135 ui::WindowShowState show_state_last,
136 Source source,
137 gfx::Rect* out_bounds,
138 ui::WindowShowState& out_show_state,
139 const Browser* browser,
140 const gfx::Rect& passed_in) {
141 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider;
142 mip->AddMonitor(monitor1_bounds, monitor1_work_area);
143 if (!monitor2_bounds.IsEmpty())
144 mip->AddMonitor(monitor2_bounds, monitor2_bounds);
145 TestStateProvider* sp = new TestStateProvider;
146 if (source == PERSISTED || source == BOTH)
147 sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
148 if (source == LAST_ACTIVE || source == BOTH)
149 sp->SetLastActiveState(bounds, show_state_last, true);
150
151 WindowSizer sizer(sp, mip, browser);
152 sizer.DetermineWindowBoundsAndShowState(passed_in,
153 out_bounds,
154 out_show_state);
155 }
156
157 void GetWindowBounds(const gfx::Rect& monitor1_bounds,
158 const gfx::Rect& monitor1_work_area,
159 const gfx::Rect& monitor2_bounds,
160 const gfx::Rect& bounds,
161 const gfx::Rect& work_area,
162 Source source,
163 gfx::Rect* out_bounds,
164 const Browser* browser,
165 const gfx::Rect& passed_in) {
166 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
167 GetWindowBoundsAndShowState(
168 monitor1_bounds, monitor1_work_area, monitor2_bounds, bounds, work_area,
169 ui::SHOW_STATE_DEFAULT, ui::SHOW_STATE_DEFAULT, source, out_bounds,
170 out_show_state, browser, passed_in);
171 }
172
173 ui::WindowShowState GetWindowShowState(
174 ui::WindowShowState show_state_persisted,
175 ui::WindowShowState show_state_last,
176 Source source,
177 const Browser* browser) {
178 gfx::Rect bounds = tentwentyfour;
179 gfx::Rect work_area = tentwentyfour;
180 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider;
181 mip->AddMonitor(tentwentyfour, tentwentyfour);
182 TestStateProvider* sp = new TestStateProvider;
183 if (source == PERSISTED || source == BOTH)
184 sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
185 if (source == LAST_ACTIVE || source == BOTH)
186 sp->SetLastActiveState(bounds, show_state_last, true);
187
188 WindowSizer sizer(sp, mip, browser);
189
190 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
191 gfx::Rect out_bounds;
192 sizer.DetermineWindowBoundsAndShowState(
193 gfx::Rect(),
194 &out_bounds,
195 out_show_state);
196 return out_show_state;
197 }
198
199 // Test that the default show state override behavior is properly handled.
200 TEST_F(WindowSizerTestWithBrowser, TestShowStateDefaults) {
201 // Creating a browser & window to play with.
202 scoped_ptr<aura::Window> window(
203 aura::test::CreateTestWindowWithId(0, NULL));
204 window->SetBounds(gfx::Rect(16, 32, 640, 320));
205
206 scoped_ptr<TestingProfile> profile(new TestingProfile());
207
208 scoped_ptr<BrowserWindow> browser_window(
209 new TestBrowserWindowAura(window.get()));
210 Browser::CreateParams window_params(Browser::TYPE_TABBED, profile.get());
211 window_params.window = browser_window.get();
212 scoped_ptr<Browser> browser(new Browser(window_params));
213
214 // Create also a popup browser since that behaves slightly different for
215 // defaults.
216 scoped_ptr<aura::Window> popup(
217 aura::test::CreateTestWindowWithId(1, NULL));
218 popup->SetBounds(gfx::Rect(16, 32, 128, 256));
219
220 scoped_ptr<BrowserWindow> browser_popup(
221 new TestBrowserWindowAura(popup.get()));
222 Browser::CreateParams popup_params(Browser::TYPE_POPUP, profile.get());
223 popup_params.window = browser_window.get();
224 scoped_ptr<Browser> popup_browser(new Browser(popup_params));
225
226 // Check that a browser creation state always get used if not given as
227 // SHOW_STATE_DEFAULT.
228 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
229 ui::SHOW_STATE_MAXIMIZED,
230 DEFAULT,
231 browser.get()), ui::SHOW_STATE_DEFAULT);
232 browser->set_initial_show_state(ui::SHOW_STATE_MINIMIZED);
233 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
234 ui::SHOW_STATE_MAXIMIZED,
235 BOTH,
236 browser.get()), ui::SHOW_STATE_MINIMIZED);
237 browser->set_initial_show_state(ui::SHOW_STATE_NORMAL);
238 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
239 ui::SHOW_STATE_MAXIMIZED,
240 BOTH,
241 browser.get()), ui::SHOW_STATE_NORMAL);
242 browser->set_initial_show_state(ui::SHOW_STATE_MAXIMIZED);
243 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
244 ui::SHOW_STATE_NORMAL,
245 BOTH,
246 browser.get()), ui::SHOW_STATE_MAXIMIZED);
247
248 // Check that setting the maximized command line option is forcing the
249 // maximized state.
250 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kStartMaximized);
251
252 browser->set_initial_show_state(ui::SHOW_STATE_NORMAL);
253 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
254 ui::SHOW_STATE_NORMAL,
255 BOTH,
256 browser.get()), ui::SHOW_STATE_MAXIMIZED);
257
258 // The popup should favor the initial show state over the command line.
259 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
260 ui::SHOW_STATE_NORMAL,
261 BOTH,
262 popup_browser.get()), ui::SHOW_STATE_NORMAL);
263 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698