| 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" |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 #include "ash/test/ash_test_base.h" |
| 13 #include "base/logging.h" |
| 14 #include "chrome/browser/ui/window_sizer/window_sizer.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "chrome/test/base/test_browser_window.h" |
| 17 #include "content/public/test/test_browser_thread.h" |
| 18 |
| 8 // Some standard monitor sizes (no task bar). | 19 // Some standard monitor sizes (no task bar). |
| 9 static const gfx::Rect tentwentyfour(0, 0, 1024, 768); | 20 static const gfx::Rect tentwentyfour(0, 0, 1024, 768); |
| 10 static const gfx::Rect twelveeighty(0, 0, 1280, 1024); | 21 static const gfx::Rect twelveeighty(0, 0, 1280, 1024); |
| 11 static const gfx::Rect sixteenhundred(0, 0, 1600, 1200); | 22 static const gfx::Rect sixteenhundred(0, 0, 1600, 1200); |
| 12 static const gfx::Rect sixteeneighty(0, 0, 1680, 1050); | 23 static const gfx::Rect sixteeneighty(0, 0, 1680, 1050); |
| 13 static const gfx::Rect nineteentwenty(0, 0, 1920, 1200); | 24 static const gfx::Rect nineteentwenty(0, 0, 1920, 1200); |
| 14 | 25 |
| 15 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 26 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 16 // the immediate left of the primary 1024x768 monitor. | 27 // the immediate left of the primary 1024x768 monitor. |
| 17 static const gfx::Rect left_nonprimary(-1024, 0, 1024, 768); | 28 static const gfx::Rect left_nonprimary(-1024, 0, 1024, 768); |
| 18 | 29 |
| 19 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 30 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 20 // the immediate right of the primary 1024x768 monitor. | 31 // the immediate right of the primary 1024x768 monitor. |
| 21 static const gfx::Rect right_nonprimary(1024, 0, 1024, 768); | 32 static const gfx::Rect right_nonprimary(1024, 0, 1024, 768); |
| 22 | 33 |
| 23 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 34 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 24 // the immediate top of the primary 1024x768 monitor. | 35 // the immediate top of the primary 1024x768 monitor. |
| 25 static const gfx::Rect top_nonprimary(0, -768, 1024, 768); | 36 static const gfx::Rect top_nonprimary(0, -768, 1024, 768); |
| 26 | 37 |
| 27 // Represents a 1024x768 monitor that is not the primary monitor, arranged to | 38 // Represents a 1024x768 monitor that is not the primary monitor, arranged to |
| 28 // the immediate bottom of the primary 1024x768 monitor. | 39 // the immediate bottom of the primary 1024x768 monitor. |
| 29 static const gfx::Rect bottom_nonprimary(0, 768, 1024, 768); | 40 static const gfx::Rect bottom_nonprimary(0, 768, 1024, 768); |
| 30 | 41 |
| 31 // The work area for 1024x768 monitors with different taskbar orientations. | 42 // The work area for 1024x768 monitors with different taskbar orientations. |
| 32 static const gfx::Rect taskbar_bottom_work_area(0, 0, 1024, 734); | 43 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); | 44 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); | 45 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); | 46 static const gfx::Rect taskbar_right_work_area(0, 0, 917, 768); |
| 36 | 47 |
| 37 static int kWindowTilePixels = WindowSizer::kWindowTilePixels; | 48 extern int kWindowTilePixels; |
| 49 |
| 50 typedef ash::test::AshTestBase WindowSizerTest; |
| 51 |
| 52 // A special test class for use with browser creation - it will create a |
| 53 // browser thread and deletes it after all other things have been destroyed. |
| 54 class WindowSizerTestWithBrowser : public WindowSizerTest { |
| 55 public: |
| 56 WindowSizerTestWithBrowser(); |
| 57 virtual ~WindowSizerTestWithBrowser(); |
| 58 |
| 59 private: |
| 60 // Note: It is important to delete the thread after the browser instances got |
| 61 // deleted. For this we transfer the thread here. |
| 62 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(WindowSizerTestWithBrowser); |
| 65 }; |
| 38 | 66 |
| 39 // Testing implementation of WindowSizer::MonitorInfoProvider that we can use | 67 // Testing implementation of WindowSizer::MonitorInfoProvider that we can use |
| 40 // to fake various monitor layouts and sizes. | 68 // to fake various monitor layouts and sizes. |
| 41 class TestMonitorInfoProvider : public MonitorInfoProvider { | 69 class TestMonitorInfoProvider : public MonitorInfoProvider { |
| 42 public: | 70 public: |
| 43 TestMonitorInfoProvider(); | 71 TestMonitorInfoProvider() {}; |
| 44 virtual ~TestMonitorInfoProvider(); | 72 virtual ~TestMonitorInfoProvider() {}; |
| 45 | 73 |
| 46 void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area); | 74 void AddMonitor(const gfx::Rect& bounds, const gfx::Rect& work_area); |
| 47 | 75 |
| 48 // Overridden from WindowSizer::MonitorInfoProvider: | 76 // Overridden from WindowSizer::MonitorInfoProvider: |
| 49 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE; | 77 virtual gfx::Rect GetPrimaryDisplayWorkArea() const OVERRIDE; |
| 50 | 78 |
| 51 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE; | 79 virtual gfx::Rect GetPrimaryDisplayBounds() const OVERRIDE; |
| 52 | 80 |
| 53 virtual gfx::Rect GetMonitorWorkAreaMatching( | 81 virtual gfx::Rect GetMonitorWorkAreaMatching( |
| 54 const gfx::Rect& match_rect) const OVERRIDE; | 82 const gfx::Rect& match_rect) const OVERRIDE; |
| 55 | 83 |
| 56 private: | 84 private: |
| 57 size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const; | 85 size_t GetMonitorIndexMatchingBounds(const gfx::Rect& match_rect) const; |
| 58 | 86 |
| 59 std::vector<gfx::Rect> monitor_bounds_; | 87 std::vector<gfx::Rect> monitor_bounds_; |
| 60 std::vector<gfx::Rect> work_areas_; | 88 std::vector<gfx::Rect> work_areas_; |
| 61 | 89 |
| 62 DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider); | 90 DISALLOW_COPY_AND_ASSIGN(TestMonitorInfoProvider); |
| 63 }; | 91 }; |
| 64 | 92 |
| 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 | 93 // Testing implementation of WindowSizer::StateProvider that we use to fake |
| 108 // persistent storage and existing windows. | 94 // persistent storage and existing windows. |
| 109 class TestStateProvider : public WindowSizer::StateProvider { | 95 class TestStateProvider : public WindowSizer::StateProvider { |
| 110 public: | 96 public: |
| 111 TestStateProvider(); | 97 TestStateProvider(); |
| 112 virtual ~TestStateProvider(); | 98 virtual ~TestStateProvider() {}; |
| 113 | 99 |
| 114 void SetPersistentState(const gfx::Rect& bounds, | 100 void SetPersistentState(const gfx::Rect& bounds, |
| 115 const gfx::Rect& work_area, | 101 const gfx::Rect& work_area, |
| 102 ui::WindowShowState show_state, |
| 116 bool has_persistent_data); | 103 bool has_persistent_data); |
| 117 void SetLastActiveState(const gfx::Rect& bounds, bool has_last_active_data); | 104 void SetLastActiveState(const gfx::Rect& bounds, |
| 105 ui::WindowShowState show_state, |
| 106 bool has_last_active_data); |
| 118 | 107 |
| 119 // Overridden from WindowSizer::StateProvider: | 108 // Overridden from WindowSizer::StateProvider: |
| 120 virtual bool GetPersistentState(gfx::Rect* bounds, | 109 virtual bool GetPersistentState( |
| 121 gfx::Rect* saved_work_area) const OVERRIDE; | 110 gfx::Rect* bounds, |
| 122 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const OVERRIDE; | 111 gfx::Rect* saved_work_area, |
| 112 ui::WindowShowState& show_state) const OVERRIDE; |
| 113 virtual bool GetLastActiveWindowState( |
| 114 gfx::Rect* bounds, |
| 115 ui::WindowShowState& show_state) const OVERRIDE; |
| 123 | 116 |
| 124 private: | 117 private: |
| 125 gfx::Rect persistent_bounds_; | 118 gfx::Rect persistent_bounds_; |
| 126 gfx::Rect persistent_work_area_; | 119 gfx::Rect persistent_work_area_; |
| 127 bool has_persistent_data_; | 120 bool has_persistent_data_; |
| 121 ui::WindowShowState persistent_show_state_; |
| 128 | 122 |
| 129 gfx::Rect last_active_bounds_; | 123 gfx::Rect last_active_bounds_; |
| 130 bool has_last_active_data_; | 124 bool has_last_active_data_; |
| 125 ui::WindowShowState last_active_show_state_; |
| 131 | 126 |
| 132 DISALLOW_COPY_AND_ASSIGN(TestStateProvider); | 127 DISALLOW_COPY_AND_ASSIGN(TestStateProvider); |
| 133 }; | 128 }; |
| 134 | 129 |
| 135 TestStateProvider::TestStateProvider(): | 130 // A browser window proxy which is able to associate an aura native window with |
| 136 has_persistent_data_(false), | 131 // it. |
| 137 has_last_active_data_(false) { | 132 class TestBrowserWindowAura : public TestBrowserWindow { |
| 138 } | 133 public: |
| 134 explicit TestBrowserWindowAura(aura::Window* native_window); |
| 135 virtual ~TestBrowserWindowAura(); |
| 139 | 136 |
| 140 TestStateProvider::~TestStateProvider() {} | 137 virtual gfx::NativeWindow GetNativeWindow() OVERRIDE { |
| 138 return native_window_; |
| 139 } |
| 141 | 140 |
| 142 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds, | 141 virtual gfx::Rect GetBounds() const; |
| 143 const gfx::Rect& work_area, | |
| 144 bool has_persistent_data) { | |
| 145 persistent_bounds_ = bounds; | |
| 146 persistent_work_area_ = work_area; | |
| 147 has_persistent_data_ = has_persistent_data; | |
| 148 } | |
| 149 | 142 |
| 150 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds, | 143 private: |
| 151 bool has_last_active_data) { | 144 gfx::NativeWindow native_window_; |
| 152 last_active_bounds_ = bounds; | |
| 153 has_last_active_data_ = has_last_active_data; | |
| 154 } | |
| 155 | 145 |
| 156 bool TestStateProvider::GetPersistentState(gfx::Rect* bounds, | 146 DISALLOW_COPY_AND_ASSIGN(TestBrowserWindowAura); |
| 157 gfx::Rect* saved_work_area) const { | 147 }; |
| 158 *bounds = persistent_bounds_; | |
| 159 *saved_work_area = persistent_work_area_; | |
| 160 return has_persistent_data_; | |
| 161 } | |
| 162 | 148 |
| 163 bool TestStateProvider::GetLastActiveWindowState(gfx::Rect* bounds) const { | 149 // Several convenience functions which allow to set up a state for |
| 164 *bounds = last_active_bounds_; | 150 // window sizer test operations with a single call. |
| 165 return has_last_active_data_; | |
| 166 } | |
| 167 | 151 |
| 168 // A convenience function to read the window bounds from the window sizer | 152 enum Source { DEFAULT, LAST_ACTIVE, PERSISTED, BOTH }; |
| 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 | 153 |
| 190 WindowSizer sizer(sp, mip, browser); | 154 // Set up the window bounds, monitor bounds, show states and more to get the |
| 191 sizer.DetermineWindowBounds(passed_in, out_bounds); | 155 // resulting |out_bounds| and |out_show_state| from the WindowSizer. |
| 192 } | 156 void GetWindowBoundsAndShowState(const gfx::Rect& monitor1_bounds, |
| 157 const gfx::Rect& monitor1_work_area, |
| 158 const gfx::Rect& monitor2_bounds, |
| 159 const gfx::Rect& bounds, |
| 160 const gfx::Rect& work_area, |
| 161 ui::WindowShowState show_state_persisted, |
| 162 ui::WindowShowState show_state_last, |
| 163 Source source, |
| 164 gfx::Rect* out_bounds, |
| 165 ui::WindowShowState& out_show_state, |
| 166 const Browser* browser, |
| 167 const gfx::Rect& passed_in); |
| 168 |
| 169 // Set up the window bounds, monitor bounds, and work area to get the |
| 170 // resulting |out_bounds| from the WindowSizer. |
| 171 void GetWindowBounds(const gfx::Rect& monitor1_bounds, |
| 172 const gfx::Rect& monitor1_work_area, |
| 173 const gfx::Rect& monitor2_bounds, |
| 174 const gfx::Rect& bounds, |
| 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 |
| 181 // Set up the various show states and get the resulting show state from |
| 182 // the WindowSizer. |
| 183 ui::WindowShowState GetWindowShowState( |
| 184 ui::WindowShowState show_state_persisted, |
| 185 ui::WindowShowState show_state_last, |
| 186 Source source, |
| 187 const Browser* browser); |
| 193 | 188 |
| 194 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_ | 189 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_WINDOW_SIZER_COMMON_UNITTEST_H_ |
| OLD | NEW |