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

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: fixed more build issues 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 TestMonitorInfoProvider::TestMonitorInfoProvider() {};
33
34 TestMonitorInfoProvider::~TestMonitorInfoProvider() {};
35
36 void TestMonitorInfoProvider::AddMonitor(const gfx::Rect& bounds,
37 const gfx::Rect& work_area) {
38 DCHECK(bounds.Contains(work_area));
39 monitor_bounds_.push_back(bounds);
40 work_areas_.push_back(work_area);
41 }
42
43 // Overridden from WindowSizer::MonitorInfoProvider:
44 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayWorkArea() const {
45 return work_areas_[0];
46 }
47
48 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayBounds() const {
49 return monitor_bounds_[0];
50 }
51
52 gfx::Rect TestMonitorInfoProvider::GetMonitorWorkAreaMatching(
53 const gfx::Rect& match_rect) const {
54 return work_areas_[GetMonitorIndexMatchingBounds(match_rect)];
55 }
56
57 size_t TestMonitorInfoProvider::GetMonitorIndexMatchingBounds(
58 const gfx::Rect& match_rect) const {
59 int max_area = 0;
60 size_t max_area_index = 0;
61 // Loop through all the monitors, finding the one that intersects the
62 // largest area of the supplied match rect.
63 for (size_t i = 0; i < work_areas_.size(); ++i) {
64 gfx::Rect overlap(match_rect.Intersect(work_areas_[i]));
65 int area = overlap.width() * overlap.height();
66 if (area > max_area) {
67 max_area = area;
68 max_area_index = i;
69 }
70 }
71 return max_area_index;
72 }
73
74 TestStateProvider::TestStateProvider():
75 has_persistent_data_(false),
76 persistent_show_state_(ui::SHOW_STATE_DEFAULT),
77 has_last_active_data_(false),
78 last_active_show_state_(ui::SHOW_STATE_DEFAULT) {
79 }
80
81 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds,
82 const gfx::Rect& work_area,
83 ui::WindowShowState show_state,
84 bool has_persistent_data) {
85 persistent_bounds_ = bounds;
86 persistent_work_area_ = work_area;
87 persistent_show_state_ = show_state;
88 has_persistent_data_ = has_persistent_data;
89 }
90
91 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds,
92 ui::WindowShowState show_state,
93 bool has_last_active_data) {
94 last_active_bounds_ = bounds;
95 last_active_show_state_ = show_state;
96 has_last_active_data_ = has_last_active_data;
97 }
98
99 bool TestStateProvider::GetPersistentState(
100 gfx::Rect* bounds,
101 gfx::Rect* saved_work_area,
102 ui::WindowShowState* show_state) const {
103 DCHECK(show_state);
104 *bounds = persistent_bounds_;
105 *saved_work_area = persistent_work_area_;
106 if (*show_state == ui::SHOW_STATE_DEFAULT)
107 *show_state = persistent_show_state_;
108 return has_persistent_data_;
109 }
110
111 bool TestStateProvider::GetLastActiveWindowState(
112 gfx::Rect* bounds,
113 ui::WindowShowState* show_state) const {
114 DCHECK(show_state);
115 *bounds = last_active_bounds_;
116 if (*show_state == ui::SHOW_STATE_DEFAULT)
117 *show_state = last_active_show_state_;
118 return has_last_active_data_;
119 }
120
121 TestBrowserWindowAura::TestBrowserWindowAura(aura::Window *native_window)
122 : native_window_(native_window) {
123 }
124
125 TestBrowserWindowAura::~TestBrowserWindowAura() {}
126
127 gfx::NativeWindow TestBrowserWindowAura::GetNativeWindow() {
128 return native_window_;
129 }
130
131 gfx::Rect TestBrowserWindowAura::GetBounds() const {
132 return native_window_->bounds();
133 }
134
135 int kWindowTilePixels = WindowSizer::kWindowTilePixels;
136
137 // The window sizer commonly used test functions.
138 void GetWindowBoundsAndShowState(
139 const gfx::Rect& monitor1_bounds,
140 const gfx::Rect& monitor1_work_area,
141 const gfx::Rect& monitor2_bounds,
142 const gfx::Rect& bounds,
143 const gfx::Rect& work_area,
144 ui::WindowShowState show_state_persisted,
145 ui::WindowShowState show_state_last,
146 Source source,
147 gfx::Rect* out_bounds,
148 ui::WindowShowState* out_show_state,
149 const Browser* browser,
150 const gfx::Rect& passed_in) {
151 DCHECK(out_show_state);
152 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider;
153 mip->AddMonitor(monitor1_bounds, monitor1_work_area);
154 if (!monitor2_bounds.IsEmpty())
155 mip->AddMonitor(monitor2_bounds, monitor2_bounds);
156 TestStateProvider* sp = new TestStateProvider;
157 if (source == PERSISTED || source == BOTH)
158 sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
159 if (source == LAST_ACTIVE || source == BOTH)
160 sp->SetLastActiveState(bounds, show_state_last, true);
161
162 WindowSizer sizer(sp, mip, browser);
163 sizer.DetermineWindowBoundsAndShowState(passed_in,
164 out_bounds,
165 out_show_state);
166 }
167
168 void GetWindowBounds(const gfx::Rect& monitor1_bounds,
169 const gfx::Rect& monitor1_work_area,
170 const gfx::Rect& monitor2_bounds,
171 const gfx::Rect& bounds,
172 const gfx::Rect& work_area,
173 Source source,
174 gfx::Rect* out_bounds,
175 const Browser* browser,
176 const gfx::Rect& passed_in) {
177 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
178 GetWindowBoundsAndShowState(
179 monitor1_bounds, monitor1_work_area, monitor2_bounds, bounds, work_area,
180 ui::SHOW_STATE_DEFAULT, ui::SHOW_STATE_DEFAULT, source, out_bounds,
181 &out_show_state, browser, passed_in);
182 }
183
184 ui::WindowShowState GetWindowShowState(
185 ui::WindowShowState show_state_persisted,
186 ui::WindowShowState show_state_last,
187 Source source,
188 const Browser* browser) {
189 gfx::Rect bounds = tentwentyfour;
190 gfx::Rect work_area = tentwentyfour;
191 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider;
192 mip->AddMonitor(tentwentyfour, tentwentyfour);
193 TestStateProvider* sp = new TestStateProvider;
194 if (source == PERSISTED || source == BOTH)
195 sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
196 if (source == LAST_ACTIVE || source == BOTH)
197 sp->SetLastActiveState(bounds, show_state_last, true);
198
199 WindowSizer sizer(sp, mip, browser);
200
201 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
202 gfx::Rect out_bounds;
203 sizer.DetermineWindowBoundsAndShowState(
204 gfx::Rect(),
205 &out_bounds,
206 &out_show_state);
207 return out_show_state;
208 }
209
210 // Test that the default show state override behavior is properly handled.
211 TEST_F(WindowSizerTestWithBrowser, TestShowStateDefaults) {
212 // Creating a browser & window to play with.
213 scoped_ptr<aura::Window> window(
214 aura::test::CreateTestWindowWithId(0, NULL));
215 window->SetBounds(gfx::Rect(16, 32, 640, 320));
216
217 scoped_ptr<TestingProfile> profile(new TestingProfile());
218
219 scoped_ptr<BrowserWindow> browser_window(
220 new TestBrowserWindowAura(window.get()));
221 Browser::CreateParams window_params(Browser::TYPE_TABBED, profile.get());
222 window_params.window = browser_window.get();
223 scoped_ptr<Browser> browser(new Browser(window_params));
224
225 // Create also a popup browser since that behaves slightly different for
226 // defaults.
227 scoped_ptr<aura::Window> popup(
228 aura::test::CreateTestWindowWithId(1, NULL));
229 popup->SetBounds(gfx::Rect(16, 32, 128, 256));
230
231 scoped_ptr<BrowserWindow> browser_popup(
232 new TestBrowserWindowAura(popup.get()));
233 Browser::CreateParams popup_params(Browser::TYPE_POPUP, profile.get());
234 popup_params.window = browser_window.get();
235 scoped_ptr<Browser> popup_browser(new Browser(popup_params));
236
237 // Check that a browser creation state always get used if not given as
238 // SHOW_STATE_DEFAULT.
239 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
240 ui::SHOW_STATE_MAXIMIZED,
241 DEFAULT,
242 browser.get()), ui::SHOW_STATE_DEFAULT);
243 browser->set_initial_show_state(ui::SHOW_STATE_MINIMIZED);
244 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
245 ui::SHOW_STATE_MAXIMIZED,
246 BOTH,
247 browser.get()), ui::SHOW_STATE_MINIMIZED);
248 browser->set_initial_show_state(ui::SHOW_STATE_NORMAL);
249 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
250 ui::SHOW_STATE_MAXIMIZED,
251 BOTH,
252 browser.get()), ui::SHOW_STATE_NORMAL);
253 browser->set_initial_show_state(ui::SHOW_STATE_MAXIMIZED);
254 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
255 ui::SHOW_STATE_NORMAL,
256 BOTH,
257 browser.get()), ui::SHOW_STATE_MAXIMIZED);
258
259 // Check that setting the maximized command line option is forcing the
260 // maximized state.
261 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kStartMaximized);
262
263 browser->set_initial_show_state(ui::SHOW_STATE_NORMAL);
264 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
265 ui::SHOW_STATE_NORMAL,
266 BOTH,
267 browser.get()), ui::SHOW_STATE_MAXIMIZED);
268
269 // The popup should favor the initial show state over the command line.
270 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
271 ui::SHOW_STATE_NORMAL,
272 BOTH,
273 popup_browser.get()), ui::SHOW_STATE_NORMAL);
274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698