- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / window_sizer / window_sizer_common_unittest.cc
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/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"
15
16 #if defined(USE_AURA)
17 #include "ui/aura/window.h"
18 #endif
19
20 namespace {
21
22 class TestScreen : public gfx::Screen {
23  public:
24   TestScreen() {}
25   virtual ~TestScreen() {}
26
27   // Overridden from gfx::Screen:
28   virtual bool IsDIPEnabled() OVERRIDE {
29     NOTREACHED();
30     return false;
31   }
32
33   virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
34     NOTREACHED();
35     return gfx::Point();
36   }
37
38   virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
39     NOTREACHED();
40     return NULL;
41   }
42
43   virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
44       OVERRIDE {
45     NOTREACHED();
46     return NULL;
47   }
48
49   virtual int GetNumDisplays() const OVERRIDE {
50     return displays_.size();
51   }
52
53   virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
54     return displays_;
55   }
56
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
65   }
66
67   virtual gfx::Display GetDisplayNearestPoint(
68       const gfx::Point& point) const OVERRIDE {
69     NOTREACHED();
70     return gfx::Display();
71   }
72
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;
77
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;
85       }
86     }
87     return displays_[max_area_index];
88   }
89
90   virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
91     return displays_[0];
92   }
93
94   virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
95     NOTREACHED();
96   }
97
98   virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
99     NOTREACHED();
100   }
101
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);
107   }
108
109  private:
110   std::vector<gfx::Display> displays_;
111
112   DISALLOW_COPY_AND_ASSIGN(TestScreen);
113 };
114
115 }  // namespace
116
117 TestStateProvider::TestStateProvider():
118     has_persistent_data_(false),
119     persistent_show_state_(ui::SHOW_STATE_DEFAULT),
120     has_last_active_data_(false),
121     last_active_show_state_(ui::SHOW_STATE_DEFAULT) {
122 }
123
124 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds,
125                                            const gfx::Rect& work_area,
126                                            ui::WindowShowState show_state,
127                                            bool has_persistent_data) {
128   persistent_bounds_ = bounds;
129   persistent_work_area_ = work_area;
130   persistent_show_state_ = show_state;
131   has_persistent_data_ = has_persistent_data;
132 }
133
134 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds,
135                                            ui::WindowShowState show_state,
136                                            bool has_last_active_data) {
137   last_active_bounds_ = bounds;
138   last_active_show_state_ = show_state;
139   has_last_active_data_ = has_last_active_data;
140 }
141
142 bool TestStateProvider::GetPersistentState(
143     gfx::Rect* bounds,
144     gfx::Rect* saved_work_area,
145     ui::WindowShowState* show_state) const {
146   DCHECK(show_state);
147   *bounds = persistent_bounds_;
148   *saved_work_area = persistent_work_area_;
149   if (*show_state == ui::SHOW_STATE_DEFAULT)
150     *show_state = persistent_show_state_;
151   return has_persistent_data_;
152 }
153
154 bool TestStateProvider::GetLastActiveWindowState(
155     gfx::Rect* bounds,
156     ui::WindowShowState* show_state) const {
157   DCHECK(show_state);
158   *bounds = last_active_bounds_;
159   if (*show_state == ui::SHOW_STATE_DEFAULT)
160     *show_state = last_active_show_state_;
161   return has_last_active_data_;
162 }
163
164 int kWindowTilePixels = WindowSizer::kWindowTilePixels;
165
166 // The window sizer commonly used test functions.
167 void GetWindowBoundsAndShowState(
168     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     ui::WindowShowState show_state_persisted,
174     ui::WindowShowState show_state_last,
175     Source source,
176     const Browser* browser,
177     const gfx::Rect& passed_in,
178     gfx::Rect* out_bounds,
179     ui::WindowShowState* out_show_state) {
180   DCHECK(out_show_state);
181   TestScreen test_screen;
182   test_screen.AddDisplay(monitor1_bounds, monitor1_work_area);
183   if (!monitor2_bounds.IsEmpty())
184     test_screen.AddDisplay(monitor2_bounds, monitor2_bounds);
185   TestStateProvider* sp = new TestStateProvider;
186   if (source == PERSISTED || source == BOTH)
187     sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
188   if (source == LAST_ACTIVE || source == BOTH)
189     sp->SetLastActiveState(bounds, show_state_last, true);
190
191   WindowSizer sizer(sp, &test_screen, browser);
192   sizer.DetermineWindowBoundsAndShowState(passed_in,
193                                           out_bounds,
194                                           out_show_state);
195 }
196
197 void GetWindowBounds(const gfx::Rect& monitor1_bounds,
198                             const gfx::Rect& monitor1_work_area,
199                             const gfx::Rect& monitor2_bounds,
200                             const gfx::Rect& bounds,
201                             const gfx::Rect& work_area,
202                             Source source,
203                             const Browser* browser,
204                             const gfx::Rect& passed_in,
205                             gfx::Rect* out_bounds) {
206   ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
207   GetWindowBoundsAndShowState(
208       monitor1_bounds, monitor1_work_area, monitor2_bounds, bounds, work_area,
209       ui::SHOW_STATE_DEFAULT, ui::SHOW_STATE_DEFAULT, source, browser,
210       passed_in, out_bounds, &out_show_state);
211 }
212
213 ui::WindowShowState GetWindowShowState(
214     ui::WindowShowState show_state_persisted,
215     ui::WindowShowState show_state_last,
216     Source source,
217     const Browser* browser,
218     const gfx::Rect& display_config) {
219   gfx::Rect bounds = display_config;
220   gfx::Rect work_area = display_config;
221   TestScreen test_screen;
222   test_screen.AddDisplay(display_config, display_config);
223   TestStateProvider* sp = new TestStateProvider;
224   if (source == PERSISTED || source == BOTH)
225     sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
226   if (source == LAST_ACTIVE || source == BOTH)
227     sp->SetLastActiveState(bounds, show_state_last, true);
228
229   WindowSizer sizer(sp, &test_screen, browser);
230
231   ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
232   gfx::Rect out_bounds;
233   sizer.DetermineWindowBoundsAndShowState(
234       gfx::Rect(),
235       &out_bounds,
236       &out_show_state);
237   return out_show_state;
238 }
239
240 #if !defined(OS_MACOSX)
241 TEST(WindowSizerTestCommon, PersistedWindowOffscreenWithNonAggressiveRepositioning) {
242   { // off the left but the minimum visibility condition is barely satisfied
243     // without relocaiton.
244     gfx::Rect initial_bounds(-470, 50, 500, 400);
245
246     gfx::Rect window_bounds;
247     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
248                     initial_bounds, gfx::Rect(), PERSISTED,
249                     NULL, gfx::Rect(), &window_bounds);
250     EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
251   }
252
253   { // off the left and the minimum visibility condition is satisfied by
254     // relocation.
255     gfx::Rect window_bounds;
256     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
257                     gfx::Rect(-471, 50, 500, 400), gfx::Rect(), PERSISTED,
258                     NULL, gfx::Rect(), &window_bounds);
259     EXPECT_EQ(gfx::Rect(-470 /* not -471 */, 50, 500, 400).ToString(),
260               window_bounds.ToString());
261   }
262
263   { // off the top
264     gfx::Rect initial_bounds(50, -370, 500, 400);
265
266     gfx::Rect window_bounds;
267     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
268                     gfx::Rect(50, -370, 500, 400), gfx::Rect(), PERSISTED,
269                     NULL, gfx::Rect(), &window_bounds);
270     EXPECT_EQ("50,0 500x400", window_bounds.ToString());
271   }
272
273   { // off the right but the minimum visibility condition is barely satisified
274     // without relocation.
275     gfx::Rect initial_bounds(994, 50, 500, 400);
276
277     gfx::Rect window_bounds;
278     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
279                     initial_bounds, gfx::Rect(), PERSISTED,
280                     NULL, gfx::Rect(), &window_bounds);
281     EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
282   }
283
284   { // off the right and the minimum visibility condition is satisified by
285     // relocation.
286     gfx::Rect window_bounds;
287     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
288                     gfx::Rect(995, 50, 500, 400), gfx::Rect(), PERSISTED,
289                     NULL, gfx::Rect(), &window_bounds);
290     EXPECT_EQ(gfx::Rect(994 /* not 995 */, 50, 500, 400).ToString(),
291               window_bounds.ToString());
292   }
293
294   { // off the bottom but the minimum visibility condition is barely satisified
295     // without relocation.
296     gfx::Rect initial_bounds(50, 738, 500, 400);
297
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());
303   }
304
305   { // off the bottom and the minimum visibility condition is satisified by
306     // relocation.
307     gfx::Rect window_bounds;
308     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
309                     gfx::Rect(50, 739, 500, 400), gfx::Rect(), PERSISTED,
310                     NULL, gfx::Rect(), &window_bounds);
311     EXPECT_EQ(gfx::Rect(50, 738 /* not 739 */, 500, 400).ToString(),
312               window_bounds.ToString());
313   }
314
315   { // off the topleft
316     gfx::Rect window_bounds;
317     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
318                     gfx::Rect(-471, -371, 500, 400), gfx::Rect(), PERSISTED,
319                     NULL, gfx::Rect(), &window_bounds);
320     EXPECT_EQ(gfx::Rect(-470 /* not -471 */, 0, 500, 400).ToString(),
321               window_bounds.ToString());
322   }
323
324   { // off the topright and the minimum visibility condition is satisified by
325     // relocation.
326     gfx::Rect window_bounds;
327     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
328                     gfx::Rect(995, -371, 500, 400), gfx::Rect(), PERSISTED,
329                     NULL, gfx::Rect(), &window_bounds);
330     EXPECT_EQ(gfx::Rect(994 /* not 995 */, 0, 500, 400).ToString(),
331               window_bounds.ToString());
332   }
333
334   { // off the bottomleft and the minimum visibility condition is satisified by
335     // relocation.
336     gfx::Rect window_bounds;
337     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
338                     gfx::Rect(-471, 739, 500, 400), gfx::Rect(), PERSISTED,
339                     NULL, gfx::Rect(), &window_bounds);
340     EXPECT_EQ(gfx::Rect(-470 /* not -471 */,
341                         738 /* not 739 */,
342                         500,
343                         400).ToString(),
344               window_bounds.ToString());
345   }
346
347   { // off the bottomright and the minimum visibility condition is satisified by
348     // relocation.
349     gfx::Rect window_bounds;
350     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
351                     gfx::Rect(995, 739, 500, 400), gfx::Rect(), PERSISTED,
352                     NULL, gfx::Rect(), &window_bounds);
353     EXPECT_EQ(gfx::Rect(994 /* not 995 */,
354                         738 /* not 739 */,
355                         500,
356                         400).ToString(),
357               window_bounds.ToString());
358   }
359
360   { // entirely off left
361     gfx::Rect window_bounds;
362     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
363                     gfx::Rect(-700, 50, 500, 400), gfx::Rect(), PERSISTED,
364                     NULL, gfx::Rect(), &window_bounds);
365     EXPECT_EQ(gfx::Rect(-470 /* not -700 */, 50, 500, 400).ToString(),
366               window_bounds.ToString());
367   }
368
369   { // entirely off left (monitor was detached since last run)
370     gfx::Rect window_bounds;
371     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
372                     gfx::Rect(-700, 50, 500, 400), left_s1024x768, PERSISTED,
373                     NULL, gfx::Rect(), &window_bounds);
374     EXPECT_EQ("0,50 500x400", window_bounds.ToString());
375   }
376
377   { // entirely off top
378     gfx::Rect window_bounds;
379     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
380                     gfx::Rect(50, -500, 500, 400), gfx::Rect(), PERSISTED,
381                     NULL, gfx::Rect(), &window_bounds);
382     EXPECT_EQ("50,0 500x400", window_bounds.ToString());
383   }
384
385   { // entirely off top (monitor was detached since last run)
386     gfx::Rect window_bounds;
387     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
388                     gfx::Rect(50, -500, 500, 400), top_s1024x768,
389                     PERSISTED, NULL, gfx::Rect(), &window_bounds);
390     EXPECT_EQ("50,0 500x400", window_bounds.ToString());
391   }
392
393   { // entirely off right
394     gfx::Rect window_bounds;
395     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
396                     gfx::Rect(1200, 50, 500, 400), gfx::Rect(), PERSISTED,
397                     NULL, gfx::Rect(), &window_bounds);
398     EXPECT_EQ(gfx::Rect(994 /* not 1200 */, 50, 500, 400).ToString(),
399               window_bounds.ToString());
400   }
401
402   { // entirely off right (monitor was detached since last run)
403     gfx::Rect window_bounds;
404     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
405                     gfx::Rect(1200, 50, 500, 400), right_s1024x768,
406                     PERSISTED, NULL, gfx::Rect(), &window_bounds);
407     EXPECT_EQ("524,50 500x400", window_bounds.ToString());
408   }
409
410   { // entirely off bottom
411     gfx::Rect window_bounds;
412     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
413                     gfx::Rect(50, 800, 500, 400), gfx::Rect(), PERSISTED,
414                     NULL, gfx::Rect(), &window_bounds);
415     EXPECT_EQ(gfx::Rect(50, 738 /* not 800 */, 500, 400).ToString(),
416               window_bounds.ToString());
417   }
418
419   { // entirely off bottom (monitor was detached since last run)
420     gfx::Rect window_bounds;
421     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
422                     gfx::Rect(50, 800, 500, 400), bottom_s1024x768,
423                     PERSISTED, NULL, gfx::Rect(), &window_bounds);
424     EXPECT_EQ("50,368 500x400", window_bounds.ToString());
425   }
426 }
427
428 // Test that the window is sized appropriately for the first run experience
429 // where the default window bounds calculation is invoked.
430 TEST(WindowSizerTestCommon, AdjustFitSize) {
431   { // Check that the window gets resized to the screen.
432     gfx::Rect window_bounds;
433     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), gfx::Rect(),
434                     gfx::Rect(), DEFAULT, NULL,
435                     gfx::Rect(-10, -10, 1024 + 20, 768 + 20), &window_bounds);
436     EXPECT_EQ("0,0 1024x768", window_bounds.ToString());
437   }
438
439   { // Check that a window which hangs out of the screen get moved back in.
440     gfx::Rect window_bounds;
441     GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), gfx::Rect(),
442                     gfx::Rect(), DEFAULT, NULL,
443                     gfx::Rect(1020, 700, 100, 100), &window_bounds);
444     EXPECT_EQ("924,668 100x100", window_bounds.ToString());
445   }
446 }
447
448 #endif // defined(OS_MACOSX)