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/size_conversions.h"
13 #include "ui/gfx/native_widget_types.h"
14 #include "ui/gfx/rect_conversions.h"
15 #include "ui/gfx/screen.h"
20 TestScreen* TestScreen::Create() {
21 // Use (0,0) because the desktop aura tests are executed in
22 // native environment where the display's origin is (0,0).
23 return new TestScreen(gfx::Rect(0, 0, 800, 600));
27 TestScreen* TestScreen::CreateFullscreen() {
28 return new TestScreen(gfx::Rect(WindowTreeHost::GetNativeScreenSize()));
31 TestScreen::~TestScreen() {
34 WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
36 host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel()));
37 host_->window()->AddObserver(this);
42 void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
43 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
44 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
45 host_->OnHostResized(bounds_in_pixel.size());
48 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
49 display_.set_rotation(rotation);
50 // TODO(oshima|mukai): Update the display_ as well.
51 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
54 void TestScreen::SetUIScale(float ui_scale) {
56 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
57 gfx::Rect new_bounds = gfx::ToNearestRect(
58 gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
59 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
60 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
63 gfx::Transform TestScreen::GetRotationTransform() const {
64 gfx::Transform rotate;
65 float one_pixel = 1.0f / display_.device_scale_factor();
66 switch (display_.rotation()) {
67 case gfx::Display::ROTATE_0:
69 case gfx::Display::ROTATE_90:
70 rotate.Translate(display_.bounds().height() - one_pixel, 0);
73 case gfx::Display::ROTATE_270:
74 rotate.Translate(0, display_.bounds().width() - one_pixel);
77 case gfx::Display::ROTATE_180:
78 rotate.Translate(display_.bounds().width() - one_pixel,
79 display_.bounds().height() - one_pixel);
87 gfx::Transform TestScreen::GetUIScaleTransform() const {
88 gfx::Transform ui_scale;
89 ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
93 bool TestScreen::IsDIPEnabled() {
97 void TestScreen::OnWindowBoundsChanged(
98 Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
99 DCHECK_EQ(host_->window(), window);
100 display_.SetSize(gfx::ToFlooredSize(
101 gfx::ScaleSize(new_bounds.size(), display_.device_scale_factor())));
104 void TestScreen::OnWindowDestroying(Window* window) {
105 if (host_->window() == window)
109 gfx::Point TestScreen::GetCursorScreenPoint() {
110 return Env::GetInstance()->last_mouse_location();
113 gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
114 return GetWindowAtScreenPoint(GetCursorScreenPoint());
117 gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
118 return host_->window()->GetTopWindowContainingPoint(point);
121 int TestScreen::GetNumDisplays() const {
125 std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
126 return std::vector<gfx::Display>(1, display_);
129 gfx::Display TestScreen::GetDisplayNearestWindow(
130 gfx::NativeWindow window) const {
134 gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
138 gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
142 gfx::Display TestScreen::GetPrimaryDisplay() const {
146 void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
149 void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
152 TestScreen::TestScreen(const gfx::Rect& screen_bounds)
155 static int64 synthesized_display_id = 2000;
156 display_.set_id(synthesized_display_id++);
157 display_.SetScaleAndBounds(1.0f, screen_bounds);