Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "ui/gfx/rect.h" | |
|
sky
2012/10/08 21:01:25
after <vector>
Mr4D (OOO till 08-26)
2012/10/08 23:04:27
Done.
| |
| 9 | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "chrome/browser/ui/window_sizer/window_sizer.h" | |
|
sky
2012/10/08 21:01:25
sort
Mr4D (OOO till 08-26)
2012/10/08 23:04:27
Done.
| |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "chrome/test/base/test_browser_window.h" | |
| 16 #include "content/public/test/test_browser_thread.h" | |
| 17 | |
| 8 // Some standard monitor sizes (no task bar). | 18 // Some standard monitor sizes (no task bar). |
| 9 static const gfx::Rect tentwentyfour(0, 0, 1024, 768); | 19 static const gfx::Rect tentwentyfour(0, 0, 1024, 768); |
| 10 static const gfx::Rect twelveeighty(0, 0, 1280, 1024); | 20 static const gfx::Rect twelveeighty(0, 0, 1280, 1024); |
| 11 static const gfx::Rect sixteenhundred(0, 0, 1600, 1200); | 21 static const gfx::Rect sixteenhundred(0, 0, 1600, 1200); |
| 12 static const gfx::Rect sixteeneighty(0, 0, 1680, 1050); | 22 static const gfx::Rect sixteeneighty(0, 0, 1680, 1050); |
| 13 static const gfx::Rect nineteentwenty(0, 0, 1920, 1200); | 23 static const gfx::Rect nineteentwenty(0, 0, 1920, 1200); |
| 14 | 24 |
| 15 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 25 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 16 // the immediate left of the primary 1024x768 monitor. | 26 // the immediate left of the primary 1024x768 monitor. |
| 17 static const gfx::Rect left_nonprimary(-1024, 0, 1024, 768); | 27 static const gfx::Rect left_nonprimary(-1024, 0, 1024, 768); |
| 18 | 28 |
| 19 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 29 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 20 // the immediate right of the primary 1024x768 monitor. | 30 // the immediate right of the primary 1024x768 monitor. |
| 21 static const gfx::Rect right_nonprimary(1024, 0, 1024, 768); | 31 static const gfx::Rect right_nonprimary(1024, 0, 1024, 768); |
| 22 | 32 |
| 23 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 33 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 24 // the immediate top of the primary 1024x768 monitor. | 34 // the immediate top of the primary 1024x768 monitor. |
| 25 static const gfx::Rect top_nonprimary(0, -768, 1024, 768); | 35 static const gfx::Rect top_nonprimary(0, -768, 1024, 768); |
| 26 | 36 |
| 27 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 37 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 28 // the immediate bottom of the primary 1024x768 monitor. | 38 // the immediate bottom of the primary 1024x768 monitor. |
| 29 static const gfx::Rect bottom_nonprimary(0, 768, 1024, 768); | 39 static const gfx::Rect bottom_nonprimary(0, 768, 1024, 768); |
| 30 | 40 |
| 31 // The work area for 1024x768 monitors with different taskbar orientations. | 41 // The work area for 1024x768 monitors with different taskbar orientations. |
| 32 static const gfx::Rect taskbar_bottom_work_area(0, 0, 1024, 734); | 42 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); | 43 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); | 44 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); | 45 static const gfx::Rect taskbar_right_work_area(0, 0, 917, 768); |
| 36 | 46 |
| 37 static int kWindowTilePixels = WindowSizer::kWindowTilePixels; | 47 extern int kWindowTilePixels; |
| 38 | 48 |
| 39 // Testing implementation of WindowSizer::MonitorInfoProvider that we can use | 49 // Testing implementation of WindowSizer::MonitorInfoProvider that we can use |
| 40 // to fake various monitor layouts and sizes. | 50 // to fake various monitor layouts and sizes. |
| 41 class TestMonitorInfoProvider : public MonitorInfoProvider { | 51 class TestMonitorInfoProvider : public MonitorInfoProvider { |
| 42 public: | 52 public: |
| 43 TestMonitorInfoProvider(); | 53 TestMonitorInfoProvider(); |
| 44 virtual ~TestMonitorInfoProvider(); | 54 virtual ~TestMonitorInfoProvider(); |
| 45 | 55 |
| 46 void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area); | 56 void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area); |
| 47 | 57 |
| 48 // Overridden from WindowSizer::MonitorInfoProvider: | 58 // Overridden from WindowSizer::MonitorInfoProvider: |
| 49 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE; | 59 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE; |
| 50 | 60 |
| 51 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE; | 61 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE; |
| 52 | 62 |
| 53 virtual gfx::Rect GetMonitorWorkAreaMatching( | 63 virtual gfx::Rect GetMonitorWorkAreaMatching( |
| 54 const gfx::Rect& match_rect) const OVERRIDE; | 64 const gfx::Rect& match_rect) const OVERRIDE; |
| 55 | 65 |
| 56 private: | 66 private: |
| 57 size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const; | 67 size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const; |
| 58 | 68 |
| 59 std::vector<gfx::Rect> monitor_bounds_; | 69 std::vector<gfx::Rect> monitor_bounds_; |
| 60 std::vector<gfx::Rect> work_areas_; | 70 std::vector<gfx::Rect> work_areas_; |
| 61 | 71 |
| 62 DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider); | 72 DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider); |
| 63 }; | 73 }; |
| 64 | 74 |
| 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 | 75 // Testing implementation of WindowSizer::StateProvider that we use to fake |
| 108 // persistent storage and existing windows. | 76 // persistent storage and existing windows. |
| 109 class TestStateProvider : public WindowSizer::StateProvider { | 77 class TestStateProvider : public WindowSizer::StateProvider { |
| 110 public: | 78 public: |
| 111 TestStateProvider(); | 79 TestStateProvider(); |
| 112 virtual ~TestStateProvider(); | 80 virtual ~TestStateProvider() {}; |
| 113 | 81 |
| 114 void SetPersistentState(const gfx::Rect& bounds, | 82 void SetPersistentState(const gfx::Rect& bounds, |
| 115 const gfx::Rect& work_area, | 83 const gfx::Rect& work_area, |
| 84 ui::WindowShowState show_state, | |
| 116 bool has_persistent_data); | 85 bool has_persistent_data); |
| 117 void SetLastActiveState(const gfx::Rect& bounds, bool has_last_active_data); | 86 void SetLastActiveState(const gfx::Rect& bounds, |
| 87 ui::WindowShowState show_state, | |
| 88 bool has_last_active_data); | |
| 118 | 89 |
| 119 // Overridden from WindowSizer::StateProvider: | 90 // Overridden from WindowSizer::StateProvider: |
| 120 virtual bool GetPersistentState(gfx::Rect* bounds, | 91 virtual bool GetPersistentState( |
| 121 gfx::Rect* saved_work_area) const OVERRIDE; | 92 gfx::Rect* bounds, |
| 122 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const OVERRIDE; | 93 gfx::Rect* saved_work_area, |
| 94 ui::WindowShowState* show_state) const OVERRIDE; | |
| 95 virtual bool GetLastActiveWindowState( | |
| 96 gfx::Rect* bounds, | |
| 97 ui::WindowShowState* show_state) const OVERRIDE; | |
| 123 | 98 |
| 124 private: | 99 private: |
| 125 gfx::Rect persistent_bounds_; | 100 gfx::Rect persistent_bounds_; |
| 126 gfx::Rect persistent_work_area_; | 101 gfx::Rect persistent_work_area_; |
| 127 bool has_persistent_data_; | 102 bool has_persistent_data_; |
| 103 ui::WindowShowState persistent_show_state_; | |
| 128 | 104 |
| 129 gfx::Rect last_active_bounds_; | 105 gfx::Rect last_active_bounds_; |
| 130 bool has_last_active_data_; | 106 bool has_last_active_data_; |
| 107 ui::WindowShowState last_active_show_state_; | |
| 131 | 108 |
| 132 DISALLOW_COPY_AND_ASSIGN(TestStateProvider); | 109 DISALLOW_COPY_AND_ASSIGN(TestStateProvider); |
| 133 }; | 110 }; |
| 134 | 111 |
| 135 TestStateProvider::TestStateProvider(): | 112 // Several convenience functions which allow to set up a state for |
| 136 has_persistent_data_(false), | 113 // window sizer test operations with a single call. |
| 137 has_last_active_data_(false) { | |
| 138 } | |
| 139 | 114 |
| 140 TestStateProvider::~TestStateProvider() {} | 115 enum Source { DEFAULT, LAST_ACTIVE, PERSISTED, BOTH }; |
| 141 | 116 |
| 142 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds, | 117 // Set up the window bounds, monitor bounds, show states and more to get the |
| 143 const gfx::Rect& work_area, | 118 // resulting |out_bounds| and |out_show_state| from the WindowSizer. |
| 144 bool has_persistent_data) { | 119 void GetWindowBoundsAndShowState(const gfx::Rect& monitor1_bounds, |
| 145 persistent_bounds_ = bounds; | 120 const gfx::Rect& monitor1_work_area, |
| 146 persistent_work_area_ = work_area; | 121 const gfx::Rect& monitor2_bounds, |
| 147 has_persistent_data_ = has_persistent_data; | 122 const gfx::Rect& bounds, |
| 148 } | 123 const gfx::Rect& work_area, |
| 124 ui::WindowShowState show_state_persisted, | |
| 125 ui::WindowShowState show_state_last, | |
| 126 Source source, | |
|
sky
2012/10/08 21:01:25
Isn't not clear what source means here. Please doc
Mr4D (OOO till 08-26)
2012/10/08 23:04:27
Done.
| |
| 127 gfx::Rect* out_bounds, | |
| 128 ui::WindowShowState* out_show_state, | |
|
sky
2012/10/08 21:01:25
make out params last.
Mr4D (OOO till 08-26)
2012/10/08 23:04:27
Done.
| |
| 129 const Browser* browser, | |
| 130 const gfx::Rect& passed_in); | |
| 149 | 131 |
| 150 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds, | 132 // Set up the window bounds, monitor bounds, and work area to get the |
| 151 bool has_last_active_data) { | 133 // resulting |out_bounds| from the WindowSizer. |
| 152 last_active_bounds_ = bounds; | 134 void GetWindowBounds(const gfx::Rect& monitor1_bounds, |
| 153 has_last_active_data_ = has_last_active_data; | 135 const gfx::Rect& monitor1_work_area, |
| 154 } | 136 const gfx::Rect& monitor2_bounds, |
| 137 const gfx::Rect& bounds, | |
| 138 const gfx::Rect& work_area, | |
| 139 Source source, | |
| 140 gfx::Rect* out_bounds, | |
|
sky
2012/10/08 21:01:25
same comment about source and out params.
Mr4D (OOO till 08-26)
2012/10/08 23:04:27
There are already over 160 calls to this function
| |
| 141 const Browser* browser, | |
| 142 const gfx::Rect& passed_in); | |
| 155 | 143 |
| 156 bool TestStateProvider::GetPersistentState(gfx::Rect* bounds, | 144 // Set up the various show states and get the resulting show state from |
| 157 gfx::Rect* saved_work_area) const { | 145 // the WindowSizer. |
| 158 *bounds = persistent_bounds_; | 146 ui::WindowShowState GetWindowShowState( |
| 159 *saved_work_area = persistent_work_area_; | 147 ui::WindowShowState show_state_persisted, |
| 160 return has_persistent_data_; | 148 ui::WindowShowState show_state_last, |
| 161 } | 149 Source source, |
| 162 | 150 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 | 151 |
| 194 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_ | 152 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_ |
| OLD | NEW |