Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / window_sizer / window_sizer_common_unittest.cc
blob776028828f7d1abdfc819afbde45e551620d18e0
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.
5 #include "chrome/browser/ui/window_sizer/window_sizer_common_unittest.h"
7 #include "ash/wm/window_resizer.h"
8 #include "base/compiler_specific.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/display.h"
14 #include "ui/gfx/screen.h"
16 #if defined(USE_AURA)
17 #include "ui/aura/window.h"
18 #endif
20 namespace {
22 class TestScreen : public gfx::Screen {
23 public:
24 TestScreen() {}
25 virtual ~TestScreen() {}
27 // Overridden from gfx::Screen:
28 virtual bool IsDIPEnabled() override {
29 NOTREACHED();
30 return false;
33 virtual gfx::Point GetCursorScreenPoint() override {
34 NOTREACHED();
35 return gfx::Point();
38 virtual gfx::NativeWindow GetWindowUnderCursor() override {
39 NOTREACHED();
40 return NULL;
43 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
44 override {
45 NOTREACHED();
46 return NULL;
49 virtual int GetNumDisplays() const override {
50 return displays_.size();
53 virtual std::vector<gfx::Display> GetAllDisplays() const override {
54 return displays_;
57 virtual gfx::Display GetDisplayNearestWindow(
58 gfx::NativeView view) const override {
59 #if defined(USE_AURA)
60 return GetDisplayMatching(view->GetBoundsInScreen());
61 #else
62 NOTREACHED();
63 return gfx::Display();
64 #endif
67 virtual gfx::Display GetDisplayNearestPoint(
68 const gfx::Point& point) const override {
69 NOTREACHED();
70 return gfx::Display();
73 virtual gfx::Display GetDisplayMatching(
74 const gfx::Rect& match_rect) const override {
75 int max_area = 0;
76 size_t max_area_index = 0;
78 for (size_t i = 0; i < displays_.size(); ++i) {
79 gfx::Rect overlap = displays_[i].bounds();
80 overlap.Intersect(match_rect);
81 int area = overlap.width() * overlap.height();
82 if (area > max_area) {
83 max_area = area;
84 max_area_index = i;
87 return displays_[max_area_index];
90 virtual gfx::Display GetPrimaryDisplay() const override {
91 return displays_[0];
94 virtual void AddObserver(gfx::DisplayObserver* observer) override {
95 NOTREACHED();
98 virtual void RemoveObserver(gfx::DisplayObserver* observer) override {
99 NOTREACHED();
102 void AddDisplay(const gfx::Rect& bounds,
103 const gfx::Rect& work_area) {
104 gfx::Display display(displays_.size(), bounds);
105 display.set_work_area(work_area);
106 displays_.push_back(display);
109 private:
110 std::vector<gfx::Display> displays_;
112 DISALLOW_COPY_AND_ASSIGN(TestScreen);
115 class TestTargetDisplayProvider : public WindowSizer::TargetDisplayProvider {
116 public:
117 TestTargetDisplayProvider() {}
118 virtual ~TestTargetDisplayProvider() {}
120 virtual gfx::Display GetTargetDisplay(
121 const gfx::Screen* screen,
122 const gfx::Rect& bounds) const override {
123 // On ash, the bounds is used as a indicator to specify
124 // the target display.
125 return screen->GetDisplayMatching(bounds);
128 private:
129 DISALLOW_COPY_AND_ASSIGN(TestTargetDisplayProvider);
132 } // namespace
134 TestStateProvider::TestStateProvider():
135 has_persistent_data_(false),
136 persistent_show_state_(ui::SHOW_STATE_DEFAULT),
137 has_last_active_data_(false),
138 last_active_show_state_(ui::SHOW_STATE_DEFAULT) {
141 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds,
142 const gfx::Rect& work_area,
143 ui::WindowShowState show_state,
144 bool has_persistent_data) {
145 persistent_bounds_ = bounds;
146 persistent_work_area_ = work_area;
147 persistent_show_state_ = show_state;
148 has_persistent_data_ = has_persistent_data;
151 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds,
152 ui::WindowShowState show_state,
153 bool has_last_active_data) {
154 last_active_bounds_ = bounds;
155 last_active_show_state_ = show_state;
156 has_last_active_data_ = has_last_active_data;
159 bool TestStateProvider::GetPersistentState(
160 gfx::Rect* bounds,
161 gfx::Rect* saved_work_area,
162 ui::WindowShowState* show_state) const {
163 DCHECK(show_state);
164 *bounds = persistent_bounds_;
165 *saved_work_area = persistent_work_area_;
166 if (*show_state == ui::SHOW_STATE_DEFAULT)
167 *show_state = persistent_show_state_;
168 return has_persistent_data_;
171 bool TestStateProvider::GetLastActiveWindowState(
172 gfx::Rect* bounds,
173 ui::WindowShowState* show_state) const {
174 DCHECK(show_state);
175 *bounds = last_active_bounds_;
176 if (*show_state == ui::SHOW_STATE_DEFAULT)
177 *show_state = last_active_show_state_;
178 return has_last_active_data_;
181 int kWindowTilePixels = WindowSizer::kWindowTilePixels;
183 // The window sizer commonly used test functions.
184 void GetWindowBoundsAndShowState(
185 const gfx::Rect& monitor1_bounds,
186 const gfx::Rect& monitor1_work_area,
187 const gfx::Rect& monitor2_bounds,
188 const gfx::Rect& bounds,
189 const gfx::Rect& work_area,
190 ui::WindowShowState show_state_persisted,
191 ui::WindowShowState show_state_last,
192 Source source,
193 const Browser* browser,
194 const gfx::Rect& passed_in,
195 gfx::Rect* out_bounds,
196 ui::WindowShowState* out_show_state) {
197 DCHECK(out_show_state);
198 TestScreen test_screen;
199 test_screen.AddDisplay(monitor1_bounds, monitor1_work_area);
200 if (!monitor2_bounds.IsEmpty())
201 test_screen.AddDisplay(monitor2_bounds, monitor2_bounds);
202 scoped_ptr<TestStateProvider> sp(new TestStateProvider);
203 if (source == PERSISTED || source == BOTH)
204 sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
205 if (source == LAST_ACTIVE || source == BOTH)
206 sp->SetLastActiveState(bounds, show_state_last, true);
207 scoped_ptr<WindowSizer::TargetDisplayProvider> tdp(
208 new TestTargetDisplayProvider);
210 WindowSizer sizer(sp.Pass(), tdp.Pass(), &test_screen, browser);
211 sizer.DetermineWindowBoundsAndShowState(passed_in,
212 out_bounds,
213 out_show_state);
216 void GetWindowBounds(const gfx::Rect& monitor1_bounds,
217 const gfx::Rect& monitor1_work_area,
218 const gfx::Rect& monitor2_bounds,
219 const gfx::Rect& bounds,
220 const gfx::Rect& work_area,
221 Source source,
222 const Browser* browser,
223 const gfx::Rect& passed_in,
224 gfx::Rect* out_bounds) {
225 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
226 GetWindowBoundsAndShowState(
227 monitor1_bounds, monitor1_work_area, monitor2_bounds, bounds, work_area,
228 ui::SHOW_STATE_DEFAULT, ui::SHOW_STATE_DEFAULT, source, browser,
229 passed_in, out_bounds, &out_show_state);
232 ui::WindowShowState GetWindowShowState(
233 ui::WindowShowState show_state_persisted,
234 ui::WindowShowState show_state_last,
235 Source source,
236 const Browser* browser,
237 const gfx::Rect& bounds,
238 const gfx::Rect& display_config) {
239 TestScreen test_screen;
240 test_screen.AddDisplay(display_config, display_config);
241 scoped_ptr<TestStateProvider> sp(new TestStateProvider);
242 if (source == PERSISTED || source == BOTH)
243 sp->SetPersistentState(bounds, display_config, show_state_persisted, true);
244 if (source == LAST_ACTIVE || source == BOTH)
245 sp->SetLastActiveState(bounds, show_state_last, true);
246 scoped_ptr<WindowSizer::TargetDisplayProvider> tdp(
247 new TestTargetDisplayProvider);
249 WindowSizer sizer(sp.Pass(), tdp.Pass(), &test_screen, browser);
251 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
252 gfx::Rect out_bounds;
253 sizer.DetermineWindowBoundsAndShowState(
254 gfx::Rect(),
255 &out_bounds,
256 &out_show_state);
257 return out_show_state;
260 #if !defined(OS_MACOSX)
261 TEST(WindowSizerTestCommon,
262 PersistedWindowOffscreenWithNonAggressiveRepositioning) {
263 { // off the left but the minimum visibility condition is barely satisfied
264 // without relocaiton.
265 gfx::Rect initial_bounds(-470, 50, 500, 400);
267 gfx::Rect window_bounds;
268 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
269 initial_bounds, gfx::Rect(), PERSISTED,
270 NULL, gfx::Rect(), &window_bounds);
271 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
274 { // off the left and the minimum visibility condition is satisfied by
275 // relocation.
276 gfx::Rect window_bounds;
277 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
278 gfx::Rect(-471, 50, 500, 400), gfx::Rect(), PERSISTED,
279 NULL, gfx::Rect(), &window_bounds);
280 EXPECT_EQ(gfx::Rect(-470 /* not -471 */, 50, 500, 400).ToString(),
281 window_bounds.ToString());
284 { // off the top
285 gfx::Rect initial_bounds(50, -370, 500, 400);
287 gfx::Rect window_bounds;
288 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
289 gfx::Rect(50, -370, 500, 400), gfx::Rect(), PERSISTED,
290 NULL, gfx::Rect(), &window_bounds);
291 EXPECT_EQ("50,0 500x400", window_bounds.ToString());
294 { // off the right but the minimum visibility condition is barely satisified
295 // without relocation.
296 gfx::Rect initial_bounds(994, 50, 500, 400);
298 gfx::Rect window_bounds;
299 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
300 initial_bounds, gfx::Rect(), PERSISTED,
301 NULL, gfx::Rect(), &window_bounds);
302 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
305 { // off the right and the minimum visibility condition is satisified by
306 // relocation.
307 gfx::Rect window_bounds;
308 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
309 gfx::Rect(995, 50, 500, 400), gfx::Rect(), PERSISTED,
310 NULL, gfx::Rect(), &window_bounds);
311 EXPECT_EQ(gfx::Rect(994 /* not 995 */, 50, 500, 400).ToString(),
312 window_bounds.ToString());
315 { // off the bottom but the minimum visibility condition is barely satisified
316 // without relocation.
317 gfx::Rect initial_bounds(50, 738, 500, 400);
319 gfx::Rect window_bounds;
320 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
321 initial_bounds, gfx::Rect(), PERSISTED,
322 NULL, gfx::Rect(), &window_bounds);
323 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
326 { // off the bottom and the minimum visibility condition is satisified by
327 // relocation.
328 gfx::Rect window_bounds;
329 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
330 gfx::Rect(50, 739, 500, 400), gfx::Rect(), PERSISTED,
331 NULL, gfx::Rect(), &window_bounds);
332 EXPECT_EQ(gfx::Rect(50, 738 /* not 739 */, 500, 400).ToString(),
333 window_bounds.ToString());
336 { // off the topleft
337 gfx::Rect window_bounds;
338 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
339 gfx::Rect(-471, -371, 500, 400), gfx::Rect(), PERSISTED,
340 NULL, gfx::Rect(), &window_bounds);
341 EXPECT_EQ(gfx::Rect(-470 /* not -471 */, 0, 500, 400).ToString(),
342 window_bounds.ToString());
345 { // off the topright and the minimum visibility condition is satisified by
346 // relocation.
347 gfx::Rect window_bounds;
348 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
349 gfx::Rect(995, -371, 500, 400), gfx::Rect(), PERSISTED,
350 NULL, gfx::Rect(), &window_bounds);
351 EXPECT_EQ(gfx::Rect(994 /* not 995 */, 0, 500, 400).ToString(),
352 window_bounds.ToString());
355 { // off the bottomleft and the minimum visibility condition is satisified by
356 // relocation.
357 gfx::Rect window_bounds;
358 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
359 gfx::Rect(-471, 739, 500, 400), gfx::Rect(), PERSISTED,
360 NULL, gfx::Rect(), &window_bounds);
361 EXPECT_EQ(gfx::Rect(-470 /* not -471 */,
362 738 /* not 739 */,
363 500,
364 400).ToString(),
365 window_bounds.ToString());
368 { // off the bottomright and the minimum visibility condition is satisified by
369 // relocation.
370 gfx::Rect window_bounds;
371 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
372 gfx::Rect(995, 739, 500, 400), gfx::Rect(), PERSISTED,
373 NULL, gfx::Rect(), &window_bounds);
374 EXPECT_EQ(gfx::Rect(994 /* not 995 */,
375 738 /* not 739 */,
376 500,
377 400).ToString(),
378 window_bounds.ToString());
381 { // entirely off left
382 gfx::Rect window_bounds;
383 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
384 gfx::Rect(-700, 50, 500, 400), gfx::Rect(), PERSISTED,
385 NULL, gfx::Rect(), &window_bounds);
386 EXPECT_EQ(gfx::Rect(-470 /* not -700 */, 50, 500, 400).ToString(),
387 window_bounds.ToString());
390 { // entirely off left (monitor was detached since last run)
391 gfx::Rect window_bounds;
392 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
393 gfx::Rect(-700, 50, 500, 400), left_s1024x768, PERSISTED,
394 NULL, gfx::Rect(), &window_bounds);
395 EXPECT_EQ("0,50 500x400", window_bounds.ToString());
398 { // entirely off top
399 gfx::Rect window_bounds;
400 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
401 gfx::Rect(50, -500, 500, 400), gfx::Rect(), PERSISTED,
402 NULL, gfx::Rect(), &window_bounds);
403 EXPECT_EQ("50,0 500x400", window_bounds.ToString());
406 { // entirely off top (monitor was detached since last run)
407 gfx::Rect window_bounds;
408 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
409 gfx::Rect(50, -500, 500, 400), top_s1024x768,
410 PERSISTED, NULL, gfx::Rect(), &window_bounds);
411 EXPECT_EQ("50,0 500x400", window_bounds.ToString());
414 { // entirely off right
415 gfx::Rect window_bounds;
416 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
417 gfx::Rect(1200, 50, 500, 400), gfx::Rect(), PERSISTED,
418 NULL, gfx::Rect(), &window_bounds);
419 EXPECT_EQ(gfx::Rect(994 /* not 1200 */, 50, 500, 400).ToString(),
420 window_bounds.ToString());
423 { // entirely off right (monitor was detached since last run)
424 gfx::Rect window_bounds;
425 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
426 gfx::Rect(1200, 50, 500, 400), right_s1024x768,
427 PERSISTED, NULL, gfx::Rect(), &window_bounds);
428 EXPECT_EQ("524,50 500x400", window_bounds.ToString());
431 { // entirely off bottom
432 gfx::Rect window_bounds;
433 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
434 gfx::Rect(50, 800, 500, 400), gfx::Rect(), PERSISTED,
435 NULL, gfx::Rect(), &window_bounds);
436 EXPECT_EQ(gfx::Rect(50, 738 /* not 800 */, 500, 400).ToString(),
437 window_bounds.ToString());
440 { // entirely off bottom (monitor was detached since last run)
441 gfx::Rect window_bounds;
442 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
443 gfx::Rect(50, 800, 500, 400), bottom_s1024x768,
444 PERSISTED, NULL, gfx::Rect(), &window_bounds);
445 EXPECT_EQ("50,368 500x400", window_bounds.ToString());
449 // Test that the window is sized appropriately for the first run experience
450 // where the default window bounds calculation is invoked.
451 TEST(WindowSizerTestCommon, AdjustFitSize) {
452 { // Check that the window gets resized to the screen.
453 gfx::Rect window_bounds;
454 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), gfx::Rect(),
455 gfx::Rect(), DEFAULT, NULL,
456 gfx::Rect(-10, -10, 1024 + 20, 768 + 20), &window_bounds);
457 EXPECT_EQ("0,0 1024x768", window_bounds.ToString());
460 { // Check that a window which hangs out of the screen get moved back in.
461 gfx::Rect window_bounds;
462 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), gfx::Rect(),
463 gfx::Rect(), DEFAULT, NULL,
464 gfx::Rect(1020, 700, 100, 100), &window_bounds);
465 EXPECT_EQ("924,668 100x100", window_bounds.ToString());
469 #endif // defined(OS_MACOSX)