| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ui/aura/test/test_screen.h" | 5 #include "headless/lib/browser/headless_screen.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ui/aura/env.h" | 10 #include "ui/aura/env.h" |
| 11 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura/window_event_dispatcher.h" | 12 #include "ui/aura/window_event_dispatcher.h" |
| 13 #include "ui/aura/window_tree_host.h" | 13 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/base/ime/input_method.h" | 14 #include "ui/base/ime/input_method.h" |
| 15 #include "ui/gfx/geometry/rect_conversions.h" | 15 #include "ui/gfx/geometry/rect_conversions.h" |
| 16 #include "ui/gfx/geometry/size_conversions.h" | 16 #include "ui/gfx/geometry/size_conversions.h" |
| 17 #include "ui/gfx/native_widget_types.h" | 17 #include "ui/gfx/native_widget_types.h" |
| 18 #include "ui/gfx/screen.h" | 18 #include "ui/gfx/screen.h" |
| 19 | 19 |
| 20 namespace aura { | 20 namespace headless { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 bool IsRotationPortrait(gfx::Display::Rotation rotation) { | 24 bool IsRotationPortrait(gfx::Display::Rotation rotation) { |
| 25 return rotation == gfx::Display::ROTATE_90 || | 25 return rotation == gfx::Display::ROTATE_90 || |
| 26 rotation == gfx::Display::ROTATE_270; | 26 rotation == gfx::Display::ROTATE_270; |
| 27 } | 27 } |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 // static | 31 // static |
| 32 TestScreen* TestScreen::Create(const gfx::Size& size) { | 32 HeadlessScreen* HeadlessScreen::Create(const gfx::Size& size) { |
| 33 const gfx::Size kDefaultSize(800, 600); | 33 const gfx::Size kDefaultSize(800, 600); |
| 34 // Use (0,0) because the desktop aura tests are executed in | 34 return new HeadlessScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size)); |
| 35 // native environment where the display's origin is (0,0). | |
| 36 return new TestScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size)); | |
| 37 } | 35 } |
| 38 | 36 |
| 39 TestScreen::~TestScreen() { | 37 HeadlessScreen::~HeadlessScreen() {} |
| 40 } | |
| 41 | 38 |
| 42 WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() { | 39 aura::WindowTreeHost* HeadlessScreen::CreateHostForPrimaryDisplay() { |
| 43 DCHECK(!host_); | 40 DCHECK(!host_); |
| 44 host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel())); | 41 host_ = aura::WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel())); |
| 45 // Some tests don't correctly manage window focus/activation states. | 42 // Some tests don't correctly manage window focus/activation states. |
| 46 // Makes sure InputMethod is default focused so that IME basics can work. | 43 // Makes sure InputMethod is default focused so that IME basics can work. |
| 47 host_->GetInputMethod()->OnFocus(); | 44 host_->GetInputMethod()->OnFocus(); |
| 48 host_->window()->AddObserver(this); | 45 host_->window()->AddObserver(this); |
| 49 host_->InitHost(); | 46 host_->InitHost(); |
| 50 return host_; | 47 return host_; |
| 51 } | 48 } |
| 52 | 49 |
| 53 void TestScreen::SetDeviceScaleFactor(float device_scale_factor) { | 50 void HeadlessScreen::SetDeviceScaleFactor(float device_scale_factor) { |
| 54 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); | 51 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); |
| 55 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel); | 52 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel); |
| 56 host_->OnHostResized(bounds_in_pixel.size()); | |
| 57 } | 53 } |
| 58 | 54 |
| 59 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) { | 55 void HeadlessScreen::SetDisplayRotation(gfx::Display::Rotation rotation) { |
| 60 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); | 56 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); |
| 61 gfx::Rect new_bounds(bounds_in_pixel); | 57 gfx::Rect new_bounds(bounds_in_pixel); |
| 62 if (IsRotationPortrait(rotation) != IsRotationPortrait(display_.rotation())) { | 58 if (IsRotationPortrait(rotation) != IsRotationPortrait(display_.rotation())) { |
| 63 new_bounds.set_width(bounds_in_pixel.height()); | 59 new_bounds.set_width(bounds_in_pixel.height()); |
| 64 new_bounds.set_height(bounds_in_pixel.width()); | 60 new_bounds.set_height(bounds_in_pixel.width()); |
| 65 } | 61 } |
| 66 display_.set_rotation(rotation); | 62 display_.set_rotation(rotation); |
| 67 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); | 63 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); |
| 68 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); | 64 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); |
| 69 } | 65 } |
| 70 | 66 |
| 71 void TestScreen::SetUIScale(float ui_scale) { | 67 void HeadlessScreen::SetUIScale(float ui_scale) { |
| 72 ui_scale_ = ui_scale; | 68 ui_scale_ = ui_scale; |
| 73 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); | 69 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel()); |
| 74 gfx::Rect new_bounds = gfx::ToNearestRect( | 70 gfx::Rect new_bounds = gfx::ToNearestRect( |
| 75 gfx::ScaleRect(gfx::RectF(bounds_in_pixel), 1.0f / ui_scale)); | 71 gfx::ScaleRect(gfx::RectF(bounds_in_pixel), 1.0f / ui_scale)); |
| 76 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); | 72 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds); |
| 77 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); | 73 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform()); |
| 78 } | 74 } |
| 79 | 75 |
| 80 void TestScreen::SetWorkAreaInsets(const gfx::Insets& insets) { | 76 void HeadlessScreen::SetWorkAreaInsets(const gfx::Insets& insets) { |
| 81 display_.UpdateWorkAreaFromInsets(insets); | 77 display_.UpdateWorkAreaFromInsets(insets); |
| 82 } | 78 } |
| 83 | 79 |
| 84 gfx::Transform TestScreen::GetRotationTransform() const { | 80 gfx::Transform HeadlessScreen::GetRotationTransform() const { |
| 85 gfx::Transform rotate; | 81 gfx::Transform rotate; |
| 86 switch (display_.rotation()) { | 82 switch (display_.rotation()) { |
| 87 case gfx::Display::ROTATE_0: | 83 case gfx::Display::ROTATE_0: |
| 88 break; | 84 break; |
| 89 case gfx::Display::ROTATE_90: | 85 case gfx::Display::ROTATE_90: |
| 90 rotate.Translate(display_.bounds().height(), 0); | 86 rotate.Translate(display_.bounds().height(), 0); |
| 91 rotate.Rotate(90); | 87 rotate.Rotate(90); |
| 92 break; | 88 break; |
| 93 case gfx::Display::ROTATE_270: | 89 case gfx::Display::ROTATE_270: |
| 94 rotate.Translate(0, display_.bounds().width()); | 90 rotate.Translate(0, display_.bounds().width()); |
| 95 rotate.Rotate(270); | 91 rotate.Rotate(270); |
| 96 break; | 92 break; |
| 97 case gfx::Display::ROTATE_180: | 93 case gfx::Display::ROTATE_180: |
| 98 rotate.Translate(display_.bounds().width(), | 94 rotate.Translate(display_.bounds().width(), display_.bounds().height()); |
| 99 display_.bounds().height()); | |
| 100 rotate.Rotate(180); | 95 rotate.Rotate(180); |
| 101 break; | 96 break; |
| 102 } | 97 } |
| 103 | 98 |
| 104 return rotate; | 99 return rotate; |
| 105 } | 100 } |
| 106 | 101 |
| 107 gfx::Transform TestScreen::GetUIScaleTransform() const { | 102 gfx::Transform HeadlessScreen::GetUIScaleTransform() const { |
| 108 gfx::Transform ui_scale; | 103 gfx::Transform ui_scale; |
| 109 ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_); | 104 ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_); |
| 110 return ui_scale; | 105 return ui_scale; |
| 111 } | 106 } |
| 112 | 107 |
| 113 void TestScreen::OnWindowBoundsChanged( | 108 void HeadlessScreen::OnWindowBoundsChanged(aura::Window* window, |
| 114 Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) { | 109 const gfx::Rect& old_bounds, |
| 110 const gfx::Rect& new_bounds) { |
| 115 DCHECK_EQ(host_->window(), window); | 111 DCHECK_EQ(host_->window(), window); |
| 116 display_.SetSize(gfx::ScaleToFlooredSize(new_bounds.size(), | 112 display_.SetSize(gfx::ScaleToFlooredSize(new_bounds.size(), |
| 117 display_.device_scale_factor())); | 113 display_.device_scale_factor())); |
| 118 } | 114 } |
| 119 | 115 |
| 120 void TestScreen::OnWindowDestroying(Window* window) { | 116 void HeadlessScreen::OnWindowDestroying(aura::Window* window) { |
| 121 if (host_->window() == window) | 117 if (host_->window() == window) |
| 122 host_ = NULL; | 118 host_ = NULL; |
| 123 } | 119 } |
| 124 | 120 |
| 125 gfx::Point TestScreen::GetCursorScreenPoint() { | 121 gfx::Point HeadlessScreen::GetCursorScreenPoint() { |
| 126 return Env::GetInstance()->last_mouse_location(); | 122 return aura::Env::GetInstance()->last_mouse_location(); |
| 127 } | 123 } |
| 128 | 124 |
| 129 gfx::NativeWindow TestScreen::GetWindowUnderCursor() { | 125 gfx::NativeWindow HeadlessScreen::GetWindowUnderCursor() { |
| 130 return GetWindowAtScreenPoint(GetCursorScreenPoint()); | 126 return GetWindowAtScreenPoint(GetCursorScreenPoint()); |
| 131 } | 127 } |
| 132 | 128 |
| 133 gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) { | 129 gfx::NativeWindow HeadlessScreen::GetWindowAtScreenPoint( |
| 130 const gfx::Point& point) { |
| 134 if (!host_ || !host_->window()) | 131 if (!host_ || !host_->window()) |
| 135 return nullptr; | 132 return nullptr; |
| 136 return host_->window()->GetTopWindowContainingPoint(point); | 133 return host_->window()->GetTopWindowContainingPoint(point); |
| 137 } | 134 } |
| 138 | 135 |
| 139 int TestScreen::GetNumDisplays() const { | 136 int HeadlessScreen::GetNumDisplays() const { |
| 140 return 1; | 137 return 1; |
| 141 } | 138 } |
| 142 | 139 |
| 143 std::vector<gfx::Display> TestScreen::GetAllDisplays() const { | 140 std::vector<gfx::Display> HeadlessScreen::GetAllDisplays() const { |
| 144 return std::vector<gfx::Display>(1, display_); | 141 return std::vector<gfx::Display>(1, display_); |
| 145 } | 142 } |
| 146 | 143 |
| 147 gfx::Display TestScreen::GetDisplayNearestWindow( | 144 gfx::Display HeadlessScreen::GetDisplayNearestWindow( |
| 148 gfx::NativeWindow window) const { | 145 gfx::NativeWindow window) const { |
| 149 return display_; | 146 return display_; |
| 150 } | 147 } |
| 151 | 148 |
| 152 gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const { | 149 gfx::Display HeadlessScreen::GetDisplayNearestPoint( |
| 150 const gfx::Point& point) const { |
| 153 return display_; | 151 return display_; |
| 154 } | 152 } |
| 155 | 153 |
| 156 gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const { | 154 gfx::Display HeadlessScreen::GetDisplayMatching( |
| 155 const gfx::Rect& match_rect) const { |
| 157 return display_; | 156 return display_; |
| 158 } | 157 } |
| 159 | 158 |
| 160 gfx::Display TestScreen::GetPrimaryDisplay() const { | 159 gfx::Display HeadlessScreen::GetPrimaryDisplay() const { |
| 161 return display_; | 160 return display_; |
| 162 } | 161 } |
| 163 | 162 |
| 164 void TestScreen::AddObserver(gfx::DisplayObserver* observer) { | 163 void HeadlessScreen::AddObserver(gfx::DisplayObserver* observer) {} |
| 165 } | |
| 166 | 164 |
| 167 void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) { | 165 void HeadlessScreen::RemoveObserver(gfx::DisplayObserver* observer) {} |
| 168 } | |
| 169 | 166 |
| 170 TestScreen::TestScreen(const gfx::Rect& screen_bounds) | 167 HeadlessScreen::HeadlessScreen(const gfx::Rect& screen_bounds) |
| 171 : host_(NULL), | 168 : host_(NULL), ui_scale_(1.0f) { |
| 172 ui_scale_(1.0f) { | |
| 173 static int64_t synthesized_display_id = 2000; | 169 static int64_t synthesized_display_id = 2000; |
| 174 display_.set_id(synthesized_display_id++); | 170 display_.set_id(synthesized_display_id++); |
| 175 display_.SetScaleAndBounds(1.0f, screen_bounds); | 171 display_.SetScaleAndBounds(1.0f, screen_bounds); |
| 176 } | 172 } |
| 177 | 173 |
| 178 } // namespace aura | 174 } // namespace headless |
| OLD | NEW |