| 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 #include "chrome/browser/ui/ash/window_positioner.h" | 5 #include "chrome/browser/ui/ash/window_positioner.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
| 9 #include "ash/test/test_shell_delegate.h" | 9 #include "ash/test/test_shell_delegate.h" |
| 10 #include "ash/wm/window_resizer.h" | 10 #include "ash/wm/window_resizer.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 aura::Window* popup() { return popup_.get(); } | 69 aura::Window* popup() { return popup_.get(); } |
| 70 aura::Window* panel() { return panel_.get(); } | 70 aura::Window* panel() { return panel_.get(); } |
| 71 | 71 |
| 72 Browser* window_browser() { return window_owning_browser_.get(); } | 72 Browser* window_browser() { return window_owning_browser_.get(); } |
| 73 Browser* popup_browser() { return popup_owning_browser_.get(); } | 73 Browser* popup_browser() { return popup_owning_browser_.get(); } |
| 74 Browser* panel_browser() { return panel_owning_browser_.get(); } | 74 Browser* panel_browser() { return panel_owning_browser_.get(); } |
| 75 | 75 |
| 76 WindowPositioner* window_positioner() { return window_positioner_; } | 76 WindowPositioner* window_positioner() { return window_positioner_; } |
| 77 | 77 |
| 78 // The positioner & desktop's used grid alignment size. | 78 // The positioner & desktop's used grid alignment size. |
| 79 int grid_size_; | 79 const int grid_size_; |
| 80 | 80 |
| 81 private: | 81 private: |
| 82 WindowPositioner* window_positioner_; | 82 WindowPositioner* window_positioner_; |
| 83 | 83 |
| 84 // These two need to be deleted after everything else is gone. | 84 // These two need to be deleted after everything else is gone. |
| 85 scoped_ptr<content::TestBrowserThread> ui_thread_; | 85 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 86 scoped_ptr<TestingProfile> profile_; | 86 scoped_ptr<TestingProfile> profile_; |
| 87 | 87 |
| 88 // These get created for each session. | 88 // These get created for each session. |
| 89 scoped_ptr<aura::Window> window_; | 89 scoped_ptr<aura::Window> window_; |
| 90 scoped_ptr<aura::Window> popup_; | 90 scoped_ptr<aura::Window> popup_; |
| 91 scoped_ptr<aura::Window> panel_; | 91 scoped_ptr<aura::Window> panel_; |
| 92 | 92 |
| 93 scoped_ptr<BrowserWindow> browser_window_; | 93 scoped_ptr<BrowserWindow> browser_window_; |
| 94 scoped_ptr<BrowserWindow> browser_popup_; | 94 scoped_ptr<BrowserWindow> browser_popup_; |
| 95 scoped_ptr<BrowserWindow> browser_panel_; | 95 scoped_ptr<BrowserWindow> browser_panel_; |
| 96 | 96 |
| 97 scoped_ptr<Browser> window_owning_browser_; | 97 scoped_ptr<Browser> window_owning_browser_; |
| 98 scoped_ptr<Browser> popup_owning_browser_; | 98 scoped_ptr<Browser> popup_owning_browser_; |
| 99 scoped_ptr<Browser> panel_owning_browser_; | 99 scoped_ptr<Browser> panel_owning_browser_; |
| 100 | 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(WindowPositionerTest); | 101 DISALLOW_COPY_AND_ASSIGN(WindowPositionerTest); |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 WindowPositionerTest::WindowPositionerTest() | 104 WindowPositionerTest::WindowPositionerTest() |
| 105 : window_positioner_(NULL) { | 105 : grid_size_(WindowPositioner::kMinimumWindowOffset), |
| 106 window_positioner_(NULL) { |
| 106 // Create a message loop. | 107 // Create a message loop. |
| 107 MessageLoopForUI* ui_loop = message_loop(); | 108 MessageLoopForUI* ui_loop = message_loop(); |
| 108 ui_thread_.reset( | 109 ui_thread_.reset( |
| 109 new content::TestBrowserThread(content::BrowserThread::UI, ui_loop)); | 110 new content::TestBrowserThread(content::BrowserThread::UI, ui_loop)); |
| 110 | 111 |
| 111 // Create a browser profile. | 112 // Create a browser profile. |
| 112 profile_.reset(new TestingProfile()); | 113 profile_.reset(new TestingProfile()); |
| 113 } | 114 } |
| 114 | 115 |
| 115 WindowPositionerTest::~WindowPositionerTest() { | 116 WindowPositionerTest::~WindowPositionerTest() { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 143 browser_panel_.reset(new TestBrowserWindowAura(panel_.get())); | 144 browser_panel_.reset(new TestBrowserWindowAura(panel_.get())); |
| 144 Browser::CreateParams panel_params(Browser::TYPE_PANEL, profile_.get()); | 145 Browser::CreateParams panel_params(Browser::TYPE_PANEL, profile_.get()); |
| 145 panel_params.window = browser_panel_.get(); | 146 panel_params.window = browser_panel_.get(); |
| 146 panel_owning_browser_.reset(new Browser(panel_params)); | 147 panel_owning_browser_.reset(new Browser(panel_params)); |
| 147 // We hide all windows upon start - each user is required to set it up | 148 // We hide all windows upon start - each user is required to set it up |
| 148 // as he needs it. | 149 // as he needs it. |
| 149 window()->Hide(); | 150 window()->Hide(); |
| 150 popup()->Hide(); | 151 popup()->Hide(); |
| 151 panel()->Hide(); | 152 panel()->Hide(); |
| 152 window_positioner_ = new WindowPositioner(); | 153 window_positioner_ = new WindowPositioner(); |
| 153 | |
| 154 // Get the alignment size. | |
| 155 grid_size_ = ash::Shell::GetInstance()->GetGridSize(); | |
| 156 if (!grid_size_) { | |
| 157 grid_size_ = WindowPositioner::kMinimumWindowOffset; | |
| 158 } else { | |
| 159 while (grid_size_ < WindowPositioner::kMinimumWindowOffset) | |
| 160 grid_size_ *= 2; | |
| 161 } | |
| 162 } | 154 } |
| 163 | 155 |
| 164 void WindowPositionerTest::TearDown() { | 156 void WindowPositionerTest::TearDown() { |
| 165 // Since the AuraTestBase is needed to create our assets, we have to | 157 // Since the AuraTestBase is needed to create our assets, we have to |
| 166 // also delete them before we tear it down. | 158 // also delete them before we tear it down. |
| 167 window_owning_browser_.reset(NULL); | 159 window_owning_browser_.reset(NULL); |
| 168 popup_owning_browser_.reset(NULL); | 160 popup_owning_browser_.reset(NULL); |
| 169 panel_owning_browser_.reset(NULL); | 161 panel_owning_browser_.reset(NULL); |
| 170 | 162 |
| 171 browser_window_.reset(NULL); | 163 browser_window_.reset(NULL); |
| 172 browser_popup_.reset(NULL); | 164 browser_popup_.reset(NULL); |
| 173 browser_panel_.reset(NULL); | 165 browser_panel_.reset(NULL); |
| 174 | 166 |
| 175 window_.reset(NULL); | 167 window_.reset(NULL); |
| 176 popup_.reset(NULL); | 168 popup_.reset(NULL); |
| 177 panel_.reset(NULL); | 169 panel_.reset(NULL); |
| 178 | 170 |
| 179 AshTestBase::TearDown(); | 171 AshTestBase::TearDown(); |
| 180 delete window_positioner_; | 172 delete window_positioner_; |
| 181 window_positioner_ = NULL; | 173 window_positioner_ = NULL; |
| 182 } | 174 } |
| 183 | 175 |
| 176 int AlignToGridRoundDown(int location, int grid_size) { |
| 177 if (grid_size <= 1 || location % grid_size == 0) |
| 178 return location; |
| 179 return location / grid_size * grid_size; |
| 180 } |
| 181 |
| 184 TEST_F(WindowPositionerTest, cascading) { | 182 TEST_F(WindowPositionerTest, cascading) { |
| 185 const gfx::Rect work_area = gfx::Screen::GetPrimaryDisplay().work_area(); | 183 const gfx::Rect work_area = gfx::Screen::GetPrimaryDisplay().work_area(); |
| 186 | 184 |
| 187 // First see that the window will cascade down when there is no space. | 185 // First see that the window will cascade down when there is no space. |
| 188 window()->SetBounds(work_area); | 186 window()->SetBounds(work_area); |
| 189 window()->Show(); | 187 window()->Show(); |
| 190 | 188 |
| 191 gfx::Rect popup_position(0, 0, 200, 200); | 189 gfx::Rect popup_position(0, 0, 200, 200); |
| 192 // Check that it gets cascaded. | 190 // Check that it gets cascaded. |
| 193 gfx::Rect cascade_1 = window_positioner()->GetPopupPosition(popup_position); | 191 gfx::Rect cascade_1 = window_positioner()->GetPopupPosition(popup_position); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 gfx::Rect cascade_5 = | 234 gfx::Rect cascade_5 = |
| 237 window_positioner()->GetPopupPosition(popup_position_5); | 235 window_positioner()->GetPopupPosition(popup_position_5); |
| 238 EXPECT_EQ(gfx::Rect(work_area.x() + grid_size_, | 236 EXPECT_EQ(gfx::Rect(work_area.x() + grid_size_, |
| 239 work_area.y() + grid_size_, | 237 work_area.y() + grid_size_, |
| 240 popup_position_5.width(), popup_position_5.height()), | 238 popup_position_5.width(), popup_position_5.height()), |
| 241 cascade_5); | 239 cascade_5); |
| 242 } | 240 } |
| 243 | 241 |
| 244 TEST_F(WindowPositionerTest, filling) { | 242 TEST_F(WindowPositionerTest, filling) { |
| 245 const gfx::Rect work_area = gfx::Screen::GetPrimaryDisplay().work_area(); | 243 const gfx::Rect work_area = gfx::Screen::GetPrimaryDisplay().work_area(); |
| 246 int grid = ash::Shell::GetInstance()->GetGridSize(); | |
| 247 gfx::Rect popup_position(0, 0, 256, 128); | 244 gfx::Rect popup_position(0, 0, 256, 128); |
| 248 // Leave space on the left and the right and see if we fill top to bottom. | 245 // Leave space on the left and the right and see if we fill top to bottom. |
| 249 window()->SetBounds(gfx::Rect(work_area.x() + popup_position.width(), | 246 window()->SetBounds(gfx::Rect(work_area.x() + popup_position.width(), |
| 250 work_area.y(), | 247 work_area.y(), |
| 251 work_area.width() - 2 * popup_position.width(), | 248 work_area.width() - 2 * popup_position.width(), |
| 252 work_area.height())); | 249 work_area.height())); |
| 253 window()->Show(); | 250 window()->Show(); |
| 254 // Check that we are positioned in the top left corner. | 251 // Check that we are positioned in the top left corner. |
| 255 gfx::Rect top_left = window_positioner()->GetPopupPosition(popup_position); | 252 gfx::Rect top_left = window_positioner()->GetPopupPosition(popup_position); |
| 256 EXPECT_EQ(gfx::Rect(work_area.x(), work_area.y(), | 253 EXPECT_EQ(gfx::Rect(work_area.x(), work_area.y(), |
| 257 popup_position.width(), popup_position.height()), | 254 popup_position.width(), popup_position.height()), |
| 258 top_left); | 255 top_left); |
| 259 | 256 |
| 260 // Now block the found location. | 257 // Now block the found location. |
| 261 popup()->SetBounds(top_left); | 258 popup()->SetBounds(top_left); |
| 262 popup()->Show(); | 259 popup()->Show(); |
| 263 gfx::Rect mid_left = window_positioner()->GetPopupPosition(popup_position); | 260 gfx::Rect mid_left = window_positioner()->GetPopupPosition(popup_position); |
| 264 EXPECT_EQ(gfx::Rect(work_area.x(), | 261 EXPECT_EQ(gfx::Rect(work_area.x(), |
| 265 ash::WindowResizer::AlignToGridRoundDown( | 262 AlignToGridRoundDown( |
| 266 work_area.y() + top_left.height(), grid), | 263 work_area.y() + top_left.height(), grid_size_), |
| 267 popup_position.width(), popup_position.height()), | 264 popup_position.width(), popup_position.height()), |
| 268 mid_left); | 265 mid_left); |
| 269 | 266 |
| 270 // Block now everything so that we can only put the popup on the bottom | 267 // Block now everything so that we can only put the popup on the bottom |
| 271 // of the left side. | 268 // of the left side. |
| 272 // Note: We need to keep one "grid spacing free" if the window does not | 269 // Note: We need to keep one "grid spacing free" if the window does not |
| 273 // fit into the grid (which is true for 200 height).` | 270 // fit into the grid (which is true for 200 height).` |
| 274 popup()->SetBounds(gfx::Rect(work_area.x(), work_area.y(), | 271 popup()->SetBounds(gfx::Rect(work_area.x(), work_area.y(), |
| 275 popup_position.width(), | 272 popup_position.width(), |
| 276 work_area.height() - popup_position.height() - | 273 work_area.height() - popup_position.height() - |
| 277 grid + 1)); | 274 grid_size_ + 1)); |
| 278 gfx::Rect bottom_left = window_positioner()->GetPopupPosition( | 275 gfx::Rect bottom_left = window_positioner()->GetPopupPosition( |
| 279 popup_position); | 276 popup_position); |
| 280 EXPECT_EQ(gfx::Rect(work_area.x(), | 277 EXPECT_EQ(gfx::Rect(work_area.x(), |
| 281 work_area.bottom() - popup_position.height(), | 278 work_area.bottom() - popup_position.height(), |
| 282 popup_position.width(), popup_position.height()), | 279 popup_position.width(), popup_position.height()), |
| 283 bottom_left); | 280 bottom_left); |
| 284 | 281 |
| 285 // Block now enough to force the right side. | 282 // Block now enough to force the right side. |
| 286 popup()->SetBounds(gfx::Rect(work_area.x(), work_area.y(), | 283 popup()->SetBounds(gfx::Rect(work_area.x(), work_area.y(), |
| 287 popup_position.width(), | 284 popup_position.width(), |
| 288 work_area.height() - popup_position.height() + | 285 work_area.height() - popup_position.height() + |
| 289 1)); | 286 1)); |
| 290 gfx::Rect top_right = window_positioner()->GetPopupPosition( | 287 gfx::Rect top_right = window_positioner()->GetPopupPosition( |
| 291 popup_position); | 288 popup_position); |
| 292 EXPECT_EQ(gfx::Rect(ash::WindowResizer::AlignToGridRoundDown( | 289 EXPECT_EQ(gfx::Rect(AlignToGridRoundDown(work_area.right() - |
| 293 work_area.right() - popup_position.width(), grid), | 290 popup_position.width(), grid_size_), |
| 294 work_area.y(), | 291 work_area.y(), |
| 295 popup_position.width(), popup_position.height()), | 292 popup_position.width(), popup_position.height()), |
| 296 top_right); | 293 top_right); |
| 297 } | 294 } |
| 298 | 295 |
| 299 TEST_F(WindowPositionerTest, blockedByPanel) { | 296 TEST_F(WindowPositionerTest, blockedByPanel) { |
| 300 const gfx::Rect work_area = gfx::Screen::GetPrimaryDisplay().work_area(); | 297 const gfx::Rect work_area = gfx::Screen::GetPrimaryDisplay().work_area(); |
| 301 | 298 |
| 302 gfx::Rect pop_position(0, 0, 200, 200); | 299 gfx::Rect pop_position(0, 0, 200, 200); |
| 303 // Let the panel cover everything. | 300 // Let the panel cover everything. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 318 | 315 |
| 319 // Check that the popup is placed full screen. | 316 // Check that the popup is placed full screen. |
| 320 gfx::Rect full = window_positioner()->GetPopupPosition(pop_position); | 317 gfx::Rect full = window_positioner()->GetPopupPosition(pop_position); |
| 321 EXPECT_EQ(gfx::Rect(work_area.x(), work_area.y(), | 318 EXPECT_EQ(gfx::Rect(work_area.x(), work_area.y(), |
| 322 pop_position.width(), pop_position.height()), | 319 pop_position.width(), pop_position.height()), |
| 323 full); | 320 full); |
| 324 } | 321 } |
| 325 | 322 |
| 326 } // namespace test | 323 } // namespace test |
| 327 } // namespace ash | 324 } // namespace ash |
| OLD | NEW |