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.
5 #include "ui/aura/test/test_screen.h"
7 #include "base/logging.h"
8 #include "ui/aura/env.h"
9 #include "ui/aura/window.h"
10 #include "ui/aura/window_event_dispatcher.h"
11 #include "ui/aura/window_tree_host.h"
12 #include "ui/gfx/geometry/rect_conversions.h"
13 #include "ui/gfx/geometry/size_conversions.h"
14 #include "ui/gfx/native_widget_types.h"
15 #include "ui/gfx/screen.h"
21 bool IsRotationPortrait(gfx::Display::Rotation rotation
) {
22 return rotation
== gfx::Display::ROTATE_90
||
23 rotation
== gfx::Display::ROTATE_270
;
29 TestScreen
* TestScreen::Create(const gfx::Size
& size
) {
30 const gfx::Size
kDefaultSize(800, 600);
31 // Use (0,0) because the desktop aura tests are executed in
32 // native environment where the display's origin is (0,0).
33 return new TestScreen(gfx::Rect(size
.IsEmpty() ? kDefaultSize
: size
));
37 TestScreen
* TestScreen::CreateFullscreen() {
38 return new TestScreen(gfx::Rect(WindowTreeHost::GetNativeScreenSize()));
41 TestScreen::~TestScreen() {
44 WindowTreeHost
* TestScreen::CreateHostForPrimaryDisplay() {
46 host_
= WindowTreeHost::Create(gfx::Rect(display_
.GetSizeInPixel()));
47 host_
->window()->AddObserver(this);
52 void TestScreen::SetDeviceScaleFactor(float device_scale_factor
) {
53 gfx::Rect
bounds_in_pixel(display_
.GetSizeInPixel());
54 display_
.SetScaleAndBounds(device_scale_factor
, bounds_in_pixel
);
55 host_
->OnHostResized(bounds_in_pixel
.size());
58 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation
) {
59 gfx::Rect
bounds_in_pixel(display_
.GetSizeInPixel());
60 gfx::Rect
new_bounds(bounds_in_pixel
);
61 if (IsRotationPortrait(rotation
) != IsRotationPortrait(display_
.rotation())) {
62 new_bounds
.set_width(bounds_in_pixel
.height());
63 new_bounds
.set_height(bounds_in_pixel
.width());
65 display_
.set_rotation(rotation
);
66 display_
.SetScaleAndBounds(display_
.device_scale_factor(), new_bounds
);
67 host_
->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
70 void TestScreen::SetUIScale(float ui_scale
) {
72 gfx::Rect
bounds_in_pixel(display_
.GetSizeInPixel());
73 gfx::Rect new_bounds
= gfx::ToNearestRect(
74 gfx::ScaleRect(bounds_in_pixel
, 1.0f
/ ui_scale
));
75 display_
.SetScaleAndBounds(display_
.device_scale_factor(), new_bounds
);
76 host_
->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
79 void TestScreen::SetWorkAreaInsets(const gfx::Insets
& insets
) {
80 display_
.UpdateWorkAreaFromInsets(insets
);
83 gfx::Transform
TestScreen::GetRotationTransform() const {
84 gfx::Transform rotate
;
85 switch (display_
.rotation()) {
86 case gfx::Display::ROTATE_0
:
88 case gfx::Display::ROTATE_90
:
89 rotate
.Translate(display_
.bounds().height(), 0);
92 case gfx::Display::ROTATE_270
:
93 rotate
.Translate(0, display_
.bounds().width());
96 case gfx::Display::ROTATE_180
:
97 rotate
.Translate(display_
.bounds().width(),
98 display_
.bounds().height());
106 gfx::Transform
TestScreen::GetUIScaleTransform() const {
107 gfx::Transform ui_scale
;
108 ui_scale
.Scale(1.0f
/ ui_scale_
, 1.0f
/ ui_scale_
);
112 void TestScreen::OnWindowBoundsChanged(
113 Window
* window
, const gfx::Rect
& old_bounds
, const gfx::Rect
& new_bounds
) {
114 DCHECK_EQ(host_
->window(), window
);
115 display_
.SetSize(gfx::ToFlooredSize(
116 gfx::ScaleSize(new_bounds
.size(), display_
.device_scale_factor())));
119 void TestScreen::OnWindowDestroying(Window
* window
) {
120 if (host_
->window() == window
)
124 gfx::Point
TestScreen::GetCursorScreenPoint() {
125 return Env::GetInstance()->last_mouse_location();
128 gfx::NativeWindow
TestScreen::GetWindowUnderCursor() {
129 return GetWindowAtScreenPoint(GetCursorScreenPoint());
132 gfx::NativeWindow
TestScreen::GetWindowAtScreenPoint(const gfx::Point
& point
) {
133 return host_
->window()->GetTopWindowContainingPoint(point
);
136 int TestScreen::GetNumDisplays() const {
140 std::vector
<gfx::Display
> TestScreen::GetAllDisplays() const {
141 return std::vector
<gfx::Display
>(1, display_
);
144 gfx::Display
TestScreen::GetDisplayNearestWindow(
145 gfx::NativeWindow window
) const {
149 gfx::Display
TestScreen::GetDisplayNearestPoint(const gfx::Point
& point
) const {
153 gfx::Display
TestScreen::GetDisplayMatching(const gfx::Rect
& match_rect
) const {
157 gfx::Display
TestScreen::GetPrimaryDisplay() const {
161 void TestScreen::AddObserver(gfx::DisplayObserver
* observer
) {
164 void TestScreen::RemoveObserver(gfx::DisplayObserver
* observer
) {
167 TestScreen::TestScreen(const gfx::Rect
& screen_bounds
)
170 static int64 synthesized_display_id
= 2000;
171 display_
.set_id(synthesized_display_id
++);
172 display_
.SetScaleAndBounds(1.0f
, screen_bounds
);