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 #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 "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 14 |
| 15 TestMonitorInfoProvider::TestMonitorInfoProvider() {}; |
| 16 |
| 17 TestMonitorInfoProvider::~TestMonitorInfoProvider() {}; |
| 18 |
| 19 void TestMonitorInfoProvider::AddMonitor(const gfx::Rect& bounds, |
| 20 const gfx::Rect& work_area) { |
| 21 DCHECK(bounds.Contains(work_area)); |
| 22 monitor_bounds_.push_back(bounds); |
| 23 work_areas_.push_back(work_area); |
| 24 } |
| 25 |
| 26 // Overridden from WindowSizer::MonitorInfoProvider: |
| 27 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayWorkArea() const { |
| 28 return work_areas_[0]; |
| 29 } |
| 30 |
| 31 gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayBounds() const { |
| 32 return monitor_bounds_[0]; |
| 33 } |
| 34 |
| 35 gfx::Rect TestMonitorInfoProvider::GetMonitorWorkAreaMatching( |
| 36 const gfx::Rect& match_rect) const { |
| 37 return work_areas_[GetMonitorIndexMatchingBounds(match_rect)]; |
| 38 } |
| 39 |
| 40 size_t TestMonitorInfoProvider::GetMonitorIndexMatchingBounds( |
| 41 const gfx::Rect& match_rect) const { |
| 42 int max_area = 0; |
| 43 size_t max_area_index = 0; |
| 44 // Loop through all the monitors, finding the one that intersects the |
| 45 // largest area of the supplied match rect. |
| 46 for (size_t i = 0; i < work_areas_.size(); ++i) { |
| 47 gfx::Rect overlap(match_rect.Intersect(work_areas_[i])); |
| 48 int area = overlap.width() * overlap.height(); |
| 49 if (area > max_area) { |
| 50 max_area = area; |
| 51 max_area_index = i; |
| 52 } |
| 53 } |
| 54 return max_area_index; |
| 55 } |
| 56 |
| 57 TestStateProvider::TestStateProvider(): |
| 58 has_persistent_data_(false), |
| 59 persistent_show_state_(ui::SHOW_STATE_DEFAULT), |
| 60 has_last_active_data_(false), |
| 61 last_active_show_state_(ui::SHOW_STATE_DEFAULT) { |
| 62 } |
| 63 |
| 64 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds, |
| 65 const gfx::Rect& work_area, |
| 66 ui::WindowShowState show_state, |
| 67 bool has_persistent_data) { |
| 68 persistent_bounds_ = bounds; |
| 69 persistent_work_area_ = work_area; |
| 70 persistent_show_state_ = show_state; |
| 71 has_persistent_data_ = has_persistent_data; |
| 72 } |
| 73 |
| 74 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds, |
| 75 ui::WindowShowState show_state, |
| 76 bool has_last_active_data) { |
| 77 last_active_bounds_ = bounds; |
| 78 last_active_show_state_ = show_state; |
| 79 has_last_active_data_ = has_last_active_data; |
| 80 } |
| 81 |
| 82 bool TestStateProvider::GetPersistentState( |
| 83 gfx::Rect* bounds, |
| 84 gfx::Rect* saved_work_area, |
| 85 ui::WindowShowState* show_state) const { |
| 86 DCHECK(show_state); |
| 87 *bounds = persistent_bounds_; |
| 88 *saved_work_area = persistent_work_area_; |
| 89 if (*show_state == ui::SHOW_STATE_DEFAULT) |
| 90 *show_state = persistent_show_state_; |
| 91 return has_persistent_data_; |
| 92 } |
| 93 |
| 94 bool TestStateProvider::GetLastActiveWindowState( |
| 95 gfx::Rect* bounds, |
| 96 ui::WindowShowState* show_state) const { |
| 97 DCHECK(show_state); |
| 98 *bounds = last_active_bounds_; |
| 99 if (*show_state == ui::SHOW_STATE_DEFAULT) |
| 100 *show_state = last_active_show_state_; |
| 101 return has_last_active_data_; |
| 102 } |
| 103 |
| 104 int kWindowTilePixels = WindowSizer::kWindowTilePixels; |
| 105 |
| 106 // The window sizer commonly used test functions. |
| 107 void GetWindowBoundsAndShowState( |
| 108 const gfx::Rect& monitor1_bounds, |
| 109 const gfx::Rect& monitor1_work_area, |
| 110 const gfx::Rect& monitor2_bounds, |
| 111 const gfx::Rect& bounds, |
| 112 const gfx::Rect& work_area, |
| 113 ui::WindowShowState show_state_persisted, |
| 114 ui::WindowShowState show_state_last, |
| 115 Source source, |
| 116 const Browser* browser, |
| 117 const gfx::Rect& passed_in, |
| 118 gfx::Rect* out_bounds, |
| 119 ui::WindowShowState* out_show_state) { |
| 120 DCHECK(out_show_state); |
| 121 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider; |
| 122 mip->AddMonitor(monitor1_bounds, monitor1_work_area); |
| 123 if (!monitor2_bounds.IsEmpty()) |
| 124 mip->AddMonitor(monitor2_bounds, monitor2_bounds); |
| 125 TestStateProvider* sp = new TestStateProvider; |
| 126 if (source == PERSISTED || source == BOTH) |
| 127 sp->SetPersistentState(bounds, work_area, show_state_persisted, true); |
| 128 if (source == LAST_ACTIVE || source == BOTH) |
| 129 sp->SetLastActiveState(bounds, show_state_last, true); |
| 130 |
| 131 WindowSizer sizer(sp, mip, browser); |
| 132 sizer.DetermineWindowBoundsAndShowState(passed_in, |
| 133 out_bounds, |
| 134 out_show_state); |
| 135 } |
| 136 |
| 137 void GetWindowBounds(const gfx::Rect& monitor1_bounds, |
| 138 const gfx::Rect& monitor1_work_area, |
| 139 const gfx::Rect& monitor2_bounds, |
| 140 const gfx::Rect& bounds, |
| 141 const gfx::Rect& work_area, |
| 142 Source source, |
| 143 gfx::Rect* out_bounds, |
| 144 const Browser* browser, |
| 145 const gfx::Rect& passed_in) { |
| 146 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT; |
| 147 GetWindowBoundsAndShowState( |
| 148 monitor1_bounds, monitor1_work_area, monitor2_bounds, bounds, work_area, |
| 149 ui::SHOW_STATE_DEFAULT, ui::SHOW_STATE_DEFAULT, source, browser, |
| 150 passed_in, out_bounds, &out_show_state); |
| 151 } |
| 152 |
| 153 ui::WindowShowState GetWindowShowState( |
| 154 ui::WindowShowState show_state_persisted, |
| 155 ui::WindowShowState show_state_last, |
| 156 Source source, |
| 157 const Browser* browser) { |
| 158 gfx::Rect bounds = tentwentyfour; |
| 159 gfx::Rect work_area = tentwentyfour; |
| 160 TestMonitorInfoProvider* mip = new TestMonitorInfoProvider; |
| 161 mip->AddMonitor(tentwentyfour, tentwentyfour); |
| 162 TestStateProvider* sp = new TestStateProvider; |
| 163 if (source == PERSISTED || source == BOTH) |
| 164 sp->SetPersistentState(bounds, work_area, show_state_persisted, true); |
| 165 if (source == LAST_ACTIVE || source == BOTH) |
| 166 sp->SetLastActiveState(bounds, show_state_last, true); |
| 167 |
| 168 WindowSizer sizer(sp, mip, browser); |
| 169 |
| 170 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT; |
| 171 gfx::Rect out_bounds; |
| 172 sizer.DetermineWindowBoundsAndShowState( |
| 173 gfx::Rect(), |
| 174 &out_bounds, |
| 175 &out_show_state); |
| 176 return out_show_state; |
| 177 } |
OLD | NEW |