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

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

Issue 11072002: Combined window positioning and show state determiniation in the WindowSizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_ 5 #ifndef CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_
6 #define CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_ 6 #define CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_
7 7
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "chrome/browser/ui/window_sizer/window_sizer.h"
12 #include "chrome/test/base/test_browser_window.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "ui/gfx/rect.h"
16
8 // Some standard monitor sizes (no task bar). 17 // Some standard monitor sizes (no task bar).
9 static const gfx::Rect tentwentyfour(0, 0, 1024, 768); 18 static const gfx::Rect tentwentyfour(0, 0, 1024, 768);
10 static const gfx::Rect twelveeighty(0, 0, 1280, 1024); 19 static const gfx::Rect twelveeighty(0, 0, 1280, 1024);
11 static const gfx::Rect sixteenhundred(0, 0, 1600, 1200); 20 static const gfx::Rect sixteenhundred(0, 0, 1600, 1200);
12 static const gfx::Rect sixteeneighty(0, 0, 1680, 1050); 21 static const gfx::Rect sixteeneighty(0, 0, 1680, 1050);
13 static const gfx::Rect nineteentwenty(0, 0, 1920, 1200); 22 static const gfx::Rect nineteentwenty(0, 0, 1920, 1200);
14 23
15 // Represents a 1024x768 monitor that is not the primary monitor, arranged to 24 // Represents a 1024x768 monitor that is not the primary monitor, arranged to
16 // the immediate left of the primary 1024x768 monitor. 25 // the immediate left of the primary 1024x768 monitor.
17 static const gfx::Rect left_nonprimary(-1024, 0, 1024, 768); 26 static const gfx::Rect left_nonprimary(-1024, 0, 1024, 768);
18 27
19 // Represents a 1024x768 monitor that is not the primary monitor, arranged to 28 // Represents a 1024x768 monitor that is not the primary monitor, arranged to
20 // the immediate right of the primary 1024x768 monitor. 29 // the immediate right of the primary 1024x768 monitor.
21 static const gfx::Rect right_nonprimary(1024, 0, 1024, 768); 30 static const gfx::Rect right_nonprimary(1024, 0, 1024, 768);
22 31
23 // Represents a 1024x768 monitor that is not the primary monitor, arranged to 32 // Represents a 1024x768 monitor that is not the primary monitor, arranged to
24 // the immediate top of the primary 1024x768 monitor. 33 // the immediate top of the primary 1024x768 monitor.
25 static const gfx::Rect top_nonprimary(0, -768, 1024, 768); 34 static const gfx::Rect top_nonprimary(0, -768, 1024, 768);
26 35
27 // Represents a 1024x768 monitor that is not the primary monitor, arranged to 36 // Represents a 1024x768 monitor that is not the primary monitor, arranged to
28 // the immediate bottom of the primary 1024x768 monitor. 37 // the immediate bottom of the primary 1024x768 monitor.
29 static const gfx::Rect bottom_nonprimary(0, 768, 1024, 768); 38 static const gfx::Rect bottom_nonprimary(0, 768, 1024, 768);
30 39
31 // The work area for 1024x768 monitors with different taskbar orientations. 40 // The work area for 1024x768 monitors with different taskbar orientations.
32 static const gfx::Rect taskbar_bottom_work_area(0, 0, 1024, 734); 41 static const gfx::Rect taskbar_bottom_work_area(0, 0, 1024, 734);
33 static const gfx::Rect taskbar_top_work_area(0, 34, 1024, 734); 42 static const gfx::Rect taskbar_top_work_area(0, 34, 1024, 734);
34 static const gfx::Rect taskbar_left_work_area(107, 0, 917, 768); 43 static const gfx::Rect taskbar_left_work_area(107, 0, 917, 768);
35 static const gfx::Rect taskbar_right_work_area(0, 0, 917, 768); 44 static const gfx::Rect taskbar_right_work_area(0, 0, 917, 768);
36 45
37 static int kWindowTilePixels = WindowSizer::kWindowTilePixels; 46 extern int kWindowTilePixels;
38 47
39 // Testing implementation of WindowSizer::MonitorInfoProvider that we can use 48 // Testing implementation of WindowSizer::MonitorInfoProvider that we can use
40 // to fake various monitor layouts and sizes. 49 // to fake various monitor layouts and sizes.
41 class TestMonitorInfoProvider : public MonitorInfoProvider { 50 class TestMonitorInfoProvider : public MonitorInfoProvider {
42 public: 51 public:
43 TestMonitorInfoProvider(); 52 TestMonitorInfoProvider();
44 virtual ~TestMonitorInfoProvider(); 53 virtual ~TestMonitorInfoProvider();
45 54
46 void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area); 55 void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area);
47 56
48 // Overridden from WindowSizer::MonitorInfoProvider: 57 // Overridden from WindowSizer::MonitorInfoProvider:
49 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE; 58 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE;
50 59
51 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE; 60 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE;
52 61
53 virtual gfx::Rect GetMonitorWorkAreaMatching( 62 virtual gfx::Rect GetMonitorWorkAreaMatching(
54 const gfx::Rect& match_rect) const OVERRIDE; 63 const gfx::Rect& match_rect) const OVERRIDE;
55 64
56 private: 65 private:
57 size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const; 66 size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const;
58 67
59 std::vector<gfx::Rect> monitor_bounds_; 68 std::vector<gfx::Rect> monitor_bounds_;
60 std::vector<gfx::Rect> work_areas_; 69 std::vector<gfx::Rect> work_areas_;
61 70
62 DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider); 71 DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider);
63 }; 72 };
64 73
65 TestMonitorInfoProvider::TestMonitorInfoProvider() {}
66 TestMonitorInfoProvider::~TestMonitorInfoProvider() {}
67
68 void TestMonitorInfoProvider::AddMonitor(const gfx::Rect& bounds,
69 const gfx::Rect& work_area) {
70 DCHECK(bounds.Contains(work_area));
71 monitor_bounds_.push_back(bounds);
72 work_areas_.push_back(work_area);
73 }
74
75 // Overridden from WindowSizer::MonitorInfoProvider:
76 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayWorkArea() const {
77 return work_areas_[0];
78 }
79
80 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayBounds() const {
81 return monitor_bounds_[0];
82 }
83
84 gfx::Rect TestMonitorInfoProvider::GetMonitorWorkAreaMatching(
85 const gfx::Rect& match_rect) const {
86 return work_areas_[GetMonitorIndexMatchingBounds(match_rect)];
87 }
88
89 size_t TestMonitorInfoProvider::GetMonitorIndexMatchingBounds(
90 const gfx::Rect& match_rect) const {
91 int max_area = 0;
92 size_t max_area_index = 0;
93 // Loop through all the monitors, finding the one that intersects the
94 // largest area of the supplied match rect.
95 for (size_t i = 0; i < work_areas_.size(); ++i) {
96 gfx::Rect overlap(match_rect.Intersect(work_areas_[i]));
97 int area = overlap.width() * overlap.height();
98 if (area > max_area) {
99 max_area = area;
100 max_area_index = i;
101 }
102 }
103 return max_area_index;
104 }
105
106
107 // Testing implementation of WindowSizer::StateProvider that we use to fake 74 // Testing implementation of WindowSizer::StateProvider that we use to fake
108 // persistent storage and existing windows. 75 // persistent storage and existing windows.
109 class TestStateProvider : public WindowSizer::StateProvider { 76 class TestStateProvider : public WindowSizer::StateProvider {
110 public: 77 public:
111 TestStateProvider(); 78 TestStateProvider();
112 virtual ~TestStateProvider(); 79 virtual ~TestStateProvider() {};
113 80
114 void SetPersistentState(const gfx::Rect& bounds, 81 void SetPersistentState(const gfx::Rect& bounds,
115 const gfx::Rect& work_area, 82 const gfx::Rect& work_area,
83 ui::WindowShowState show_state,
116 bool has_persistent_data); 84 bool has_persistent_data);
117 void SetLastActiveState(const gfx::Rect& bounds, bool has_last_active_data); 85 void SetLastActiveState(const gfx::Rect& bounds,
86 ui::WindowShowState show_state,
87 bool has_last_active_data);
118 88
119 // Overridden from WindowSizer::StateProvider: 89 // Overridden from WindowSizer::StateProvider:
120 virtual bool GetPersistentState(gfx::Rect* bounds, 90 virtual bool GetPersistentState(
121 gfx::Rect* saved_work_area) const OVERRIDE; 91 gfx::Rect* bounds,
122 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const OVERRIDE; 92 gfx::Rect* saved_work_area,
93 ui::WindowShowState* show_state) const OVERRIDE;
94 virtual bool GetLastActiveWindowState(
95 gfx::Rect* bounds,
96 ui::WindowShowState* show_state) const OVERRIDE;
123 97
124 private: 98 private:
125 gfx::Rect persistent_bounds_; 99 gfx::Rect persistent_bounds_;
126 gfx::Rect persistent_work_area_; 100 gfx::Rect persistent_work_area_;
127 bool has_persistent_data_; 101 bool has_persistent_data_;
102 ui::WindowShowState persistent_show_state_;
128 103
129 gfx::Rect last_active_bounds_; 104 gfx::Rect last_active_bounds_;
130 bool has_last_active_data_; 105 bool has_last_active_data_;
106 ui::WindowShowState last_active_show_state_;
131 107
132 DISALLOW_COPY_AND_ASSIGN(TestStateProvider); 108 DISALLOW_COPY_AND_ASSIGN(TestStateProvider);
133 }; 109 };
134 110
135 TestStateProvider::TestStateProvider(): 111 // Several convenience functions which allow to set up a state for
136 has_persistent_data_(false), 112 // window sizer test operations with a single call.
137 has_last_active_data_(false) {
138 }
139 113
140 TestStateProvider::~TestStateProvider() {} 114 enum Source { DEFAULT, LAST_ACTIVE, PERSISTED, BOTH };
141 115
142 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds, 116 // Set up the window bounds, monitor bounds, show states and more to get the
143 const gfx::Rect& work_area, 117 // resulting |out_bounds| and |out_show_state| from the WindowSizer.
144 bool has_persistent_data) { 118 // |source| specifies which type of data gets set for the test: Either the
145 persistent_bounds_ = bounds; 119 // last active window, the persisted value which was stored earlier, both or
146 persistent_work_area_ = work_area; 120 // none. For all these states the |bounds| and |work_area| get used, for the
147 has_persistent_data_ = has_persistent_data; 121 // show states either |show_state_persisted| or |show_state_last| will be used.
148 } 122 void GetWindowBoundsAndShowState(const gfx::Rect& monitor1_bounds,
123 const gfx::Rect& monitor1_work_area,
124 const gfx::Rect& monitor2_bounds,
125 const gfx::Rect& bounds,
126 const gfx::Rect& work_area,
127 ui::WindowShowState show_state_persisted,
128 ui::WindowShowState show_state_last,
129 Source source,
130 const Browser* browser,
131 const gfx::Rect& passed_in,
132 gfx::Rect* out_bounds,
133 ui::WindowShowState* out_show_state);
149 134
150 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds, 135 // Set up the window bounds, monitor bounds, and work area to get the
151 bool has_last_active_data) { 136 // resulting |out_bounds| from the WindowSizer.
152 last_active_bounds_ = bounds; 137 // |source| specifies which type of data gets set for the test: Either the
153 has_last_active_data_ = has_last_active_data; 138 // last active window, the persisted value which was stored earlier, both or
154 } 139 // none. For all these states the |bounds| and |work_area| get used, for the
140 // show states either |show_state_persisted| or |show_state_last| will be used.
141 void GetWindowBounds(const gfx::Rect& monitor1_bounds,
142 const gfx::Rect& monitor1_work_area,
143 const gfx::Rect& monitor2_bounds,
144 const gfx::Rect& bounds,
145 const gfx::Rect& work_area,
146 Source source,
147 gfx::Rect* out_bounds,
148 const Browser* browser,
149 const gfx::Rect& passed_in);
155 150
156 bool TestStateProvider::GetPersistentState(gfx::Rect* bounds, 151 // Set up the various show states and get the resulting show state from
157 gfx::Rect* saved_work_area) const { 152 // the WindowSizer.
158 *bounds = persistent_bounds_; 153 ui::WindowShowState GetWindowShowState(
159 *saved_work_area = persistent_work_area_; 154 ui::WindowShowState show_state_persisted,
160 return has_persistent_data_; 155 ui::WindowShowState show_state_last,
161 } 156 Source source,
162 157 const Browser* browser);
163 bool TestStateProvider::GetLastActiveWindowState(gfx::Rect* bounds) const {
164 *bounds = last_active_bounds_;
165 return has_last_active_data_;
166 }
167
168 // A convenience function to read the window bounds from the window sizer
169 // according to the specified configuration.
170 enum Source { DEFAULT, LAST_ACTIVE, PERSISTED };
171 static void GetWindowBounds(const gfx::Rect& monitor1_bounds,
172 const gfx::Rect& monitor1_work_area,
173 const gfx::Rect& monitor2_bounds,
174 const gfx::Rect& state,
175 const gfx::Rect& work_area,
176 Source source,
177 gfx::Rect* out_bounds,
178 const Browser* browser,
179 const gfx::Rect& passed_in) {
180 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider;
181 mip->AddMonitor(monitor1_bounds, monitor1_work_area);
182 if (!monitor2_bounds.IsEmpty())
183 mip->AddMonitor(monitor2_bounds, monitor2_bounds);
184 TestStateProvider* sp = new TestStateProvider;
185 if (source == PERSISTED)
186 sp->SetPersistentState(state, work_area, true);
187 else if (source == LAST_ACTIVE)
188 sp->SetLastActiveState(state, true);
189
190 WindowSizer sizer(sp, mip, browser);
191 sizer.DetermineWindowBounds(passed_in, out_bounds);
192 }
193 158
194 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_ 159 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698