| OLD | NEW |
| (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 #ifndef CHROME_BROWSER_UI_WINDOW_SIZER_COMMON_UNITTEST_H_ | |
| 6 #define CHROME_BROWSER_UI_WINDOW_SIZER_COMMON_UNITTEST_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 // Some standard monitor sizes (no task bar). | |
| 10 static const gfx::Rect tentwentyfour(0, 0, 1024, 768); | |
| 11 static const gfx::Rect twelveeighty(0, 0, 1280, 1024); | |
| 12 static const gfx::Rect sixteenhundred(0, 0, 1600, 1200); | |
| 13 static const gfx::Rect sixteeneighty(0, 0, 1680, 1050); | |
| 14 static const gfx::Rect nineteentwenty(0, 0, 1920, 1200); | |
| 15 | |
| 16 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | |
| 17 // the immediate left of the primary 1024x768 monitor. | |
| 18 static const gfx::Rect left_nonprimary(-1024, 0, 1024, 768); | |
| 19 | |
| 20 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | |
| 21 // the immediate right of the primary 1024x768 monitor. | |
| 22 static const gfx::Rect right_nonprimary(1024, 0, 1024, 768); | |
| 23 | |
| 24 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | |
| 25 // the immediate top of the primary 1024x768 monitor. | |
| 26 static const gfx::Rect top_nonprimary(0, -768, 1024, 768); | |
| 27 | |
| 28 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | |
| 29 // the immediate bottom of the primary 1024x768 monitor. | |
| 30 static const gfx::Rect bottom_nonprimary(0, 768, 1024, 768); | |
| 31 | |
| 32 // The work area for 1024x768 monitors with different taskbar orientations. | |
| 33 static const gfx::Rect taskbar_bottom_work_area(0, 0, 1024, 734); | |
| 34 static const gfx::Rect taskbar_top_work_area(0, 34, 1024, 734); | |
| 35 static const gfx::Rect taskbar_left_work_area(107, 0, 917, 768); | |
| 36 static const gfx::Rect taskbar_right_work_area(0, 0, 917, 768); | |
| 37 | |
| 38 static int kWindowTilePixels = WindowSizer::kWindowTilePixels; | |
| 39 | |
| 40 // Testing implementation of WindowSizer::MonitorInfoProvider that we can use | |
| 41 // to fake various monitor layouts and sizes. | |
| 42 class TestMonitorInfoProvider : public MonitorInfoProvider { | |
| 43 public: | |
| 44 TestMonitorInfoProvider(); | |
| 45 virtual ~TestMonitorInfoProvider(); | |
| 46 | |
| 47 void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area); | |
| 48 | |
| 49 // Overridden from WindowSizer::MonitorInfoProvider: | |
| 50 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE; | |
| 51 | |
| 52 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE; | |
| 53 | |
| 54 virtual gfx::Rect GetMonitorWorkAreaMatching( | |
| 55 const gfx::Rect& match_rect) const OVERRIDE; | |
| 56 | |
| 57 private: | |
| 58 size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const; | |
| 59 | |
| 60 std::vector<gfx::Rect> monitor_bounds_; | |
| 61 std::vector<gfx::Rect> work_areas_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider); | |
| 64 }; | |
| 65 | |
| 66 TestMonitorInfoProvider::TestMonitorInfoProvider() {} | |
| 67 TestMonitorInfoProvider::~TestMonitorInfoProvider() {} | |
| 68 | |
| 69 void TestMonitorInfoProvider::AddMonitor(const gfx::Rect& bounds, | |
| 70 const gfx::Rect& work_area) { | |
| 71 DCHECK(bounds.Contains(work_area)); | |
| 72 monitor_bounds_.push_back(bounds); | |
| 73 work_areas_.push_back(work_area); | |
| 74 } | |
| 75 | |
| 76 // Overridden from WindowSizer::MonitorInfoProvider: | |
| 77 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayWorkArea() const { | |
| 78 return work_areas_[0]; | |
| 79 } | |
| 80 | |
| 81 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayBounds() const { | |
| 82 return monitor_bounds_[0]; | |
| 83 } | |
| 84 | |
| 85 gfx::Rect TestMonitorInfoProvider::GetMonitorWorkAreaMatching( | |
| 86 const gfx::Rect& match_rect) const { | |
| 87 return work_areas_[GetMonitorIndexMatchingBounds(match_rect)]; | |
| 88 } | |
| 89 | |
| 90 size_t TestMonitorInfoProvider::GetMonitorIndexMatchingBounds( | |
| 91 const gfx::Rect& match_rect) const { | |
| 92 int max_area = 0; | |
| 93 size_t max_area_index = 0; | |
| 94 // Loop through all the monitors, finding the one that intersects the | |
| 95 // largest area of the supplied match rect. | |
| 96 for (size_t i = 0; i < work_areas_.size(); ++i) { | |
| 97 gfx::Rect overlap(match_rect.Intersect(work_areas_[i])); | |
| 98 int area = overlap.width() * overlap.height(); | |
| 99 if (area > max_area) { | |
| 100 max_area = area; | |
| 101 max_area_index = i; | |
| 102 } | |
| 103 } | |
| 104 return max_area_index; | |
| 105 } | |
| 106 | |
| 107 | |
| 108 // Testing implementation of WindowSizer::StateProvider that we use to fake | |
| 109 // persistent storage and existing windows. | |
| 110 class TestStateProvider : public WindowSizer::StateProvider { | |
| 111 public: | |
| 112 TestStateProvider(); | |
| 113 virtual ~TestStateProvider(); | |
| 114 | |
| 115 void SetPersistentState(const gfx::Rect& bounds, | |
| 116 const gfx::Rect& work_area, | |
| 117 bool has_persistent_data); | |
| 118 void SetLastActiveState(const gfx::Rect& bounds, bool has_last_active_data); | |
| 119 | |
| 120 // Overridden from WindowSizer::StateProvider: | |
| 121 virtual bool GetPersistentState(gfx::Rect* bounds, | |
| 122 gfx::Rect* saved_work_area) const OVERRIDE; | |
| 123 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const OVERRIDE; | |
| 124 | |
| 125 private: | |
| 126 gfx::Rect persistent_bounds_; | |
| 127 gfx::Rect persistent_work_area_; | |
| 128 bool has_persistent_data_; | |
| 129 | |
| 130 gfx::Rect last_active_bounds_; | |
| 131 bool has_last_active_data_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(TestStateProvider); | |
| 134 }; | |
| 135 | |
| 136 TestStateProvider::TestStateProvider(): | |
| 137 has_persistent_data_(false), | |
| 138 has_last_active_data_(false) { | |
| 139 } | |
| 140 | |
| 141 TestStateProvider::~TestStateProvider() {} | |
| 142 | |
| 143 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds, | |
| 144 const gfx::Rect& work_area, | |
| 145 bool has_persistent_data) { | |
| 146 persistent_bounds_ = bounds; | |
| 147 persistent_work_area_ = work_area; | |
| 148 has_persistent_data_ = has_persistent_data; | |
| 149 } | |
| 150 | |
| 151 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds, | |
| 152 bool has_last_active_data) { | |
| 153 last_active_bounds_ = bounds; | |
| 154 has_last_active_data_ = has_last_active_data; | |
| 155 } | |
| 156 | |
| 157 bool TestStateProvider::GetPersistentState(gfx::Rect* bounds, | |
| 158 gfx::Rect* saved_work_area) const { | |
| 159 *bounds = persistent_bounds_; | |
| 160 *saved_work_area = persistent_work_area_; | |
| 161 return has_persistent_data_; | |
| 162 } | |
| 163 | |
| 164 bool TestStateProvider::GetLastActiveWindowState(gfx::Rect* bounds) const { | |
| 165 *bounds = last_active_bounds_; | |
| 166 return has_last_active_data_; | |
| 167 } | |
| 168 | |
| 169 // A convenience function to read the window bounds from the window sizer | |
| 170 // according to the specified configuration. | |
| 171 enum Source { DEFAULT, LAST_ACTIVE, PERSISTED }; | |
| 172 static void GetWindowBounds(const gfx::Rect& monitor1_bounds, | |
| 173 const gfx::Rect& monitor1_work_area, | |
| 174 const gfx::Rect& monitor2_bounds, | |
| 175 const gfx::Rect& state, | |
| 176 const gfx::Rect& work_area, | |
| 177 Source source, | |
| 178 gfx::Rect* out_bounds, | |
| 179 const Browser* browser, | |
| 180 const gfx::Rect& passed_in) { | |
| 181 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider; | |
| 182 mip->AddMonitor(monitor1_bounds, monitor1_work_area); | |
| 183 if (!monitor2_bounds.IsEmpty()) | |
| 184 mip->AddMonitor(monitor2_bounds, monitor2_bounds); | |
| 185 TestStateProvider* sp = new TestStateProvider; | |
| 186 if (source == PERSISTED) | |
| 187 sp->SetPersistentState(state, work_area, true); | |
| 188 else if (source == LAST_ACTIVE) | |
| 189 sp->SetLastActiveState(state, true); | |
| 190 | |
| 191 WindowSizer sizer(sp, mip, browser); | |
| 192 sizer.DetermineWindowBounds(passed_in, out_bounds); | |
| 193 } | |
| 194 | |
| 195 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_COMMON_UNITTEST_H_ | |
| OLD | NEW |