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/root_window.h"
10 #include "ui/aura/window.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 RootWindow* TestScreen::CreateRootWindowForPrimaryDisplay() {
35 DCHECK(!root_window_);
36 root_window_ = new RootWindow(
37 RootWindow::CreateParams(gfx::Rect(display_.GetSizeInPixel())));
38 root_window_->window()->AddObserver(this);
43 void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
44 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
45 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
46 root_window_->host()->NotifyHostResized(bounds_in_pixel.size());
49 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
50 display_.set_rotation(rotation);
51 // TODO(oshima|mukai): Update the display_ as well.
52 root_window_->host()->SetTransform(
53 GetRotationTransform() * GetUIScaleTransform());
56 void TestScreen::SetUIScale(float ui_scale) {
58 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
59 gfx::Rect new_bounds = gfx::ToNearestRect(
60 gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
61 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
62 root_window_->host()->SetTransform(
63 GetRotationTransform() * GetUIScaleTransform());
66 gfx::Transform TestScreen::GetRotationTransform() const {
67 gfx::Transform rotate;
68 float one_pixel = 1.0f / display_.device_scale_factor();
69 switch (display_.rotation()) {
70 case gfx::Display::ROTATE_0:
72 case gfx::Display::ROTATE_90:
73 rotate.Translate(display_.bounds().height() - one_pixel, 0);
76 case gfx::Display::ROTATE_270:
77 rotate.Translate(0, display_.bounds().width() - one_pixel);
80 case gfx::Display::ROTATE_180:
81 rotate.Translate(display_.bounds().width() - one_pixel,
82 display_.bounds().height() - one_pixel);
90 gfx::Transform TestScreen::GetUIScaleTransform() const {
91 gfx::Transform ui_scale;
92 ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
96 bool TestScreen::IsDIPEnabled() {
100 void TestScreen::OnWindowBoundsChanged(
101 Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
102 DCHECK_EQ(root_window_->window(), window);
103 display_.SetSize(gfx::ToFlooredSize(
104 gfx::ScaleSize(new_bounds.size(), display_.device_scale_factor())));
107 void TestScreen::OnWindowDestroying(Window* window) {
108 if (root_window_->window() == window)
112 gfx::Point TestScreen::GetCursorScreenPoint() {
113 return Env::GetInstance()->last_mouse_location();
116 gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
117 return GetWindowAtScreenPoint(GetCursorScreenPoint());
120 gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
121 return root_window_->window()->GetTopWindowContainingPoint(point);
124 int TestScreen::GetNumDisplays() const {
128 std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
129 return std::vector<gfx::Display>(1, display_);
132 gfx::Display TestScreen::GetDisplayNearestWindow(
133 gfx::NativeWindow window) const {
137 gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
141 gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
145 gfx::Display TestScreen::GetPrimaryDisplay() const {
149 void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
152 void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
155 TestScreen::TestScreen(const gfx::Rect& screen_bounds)
156 : root_window_(NULL),
158 static int64 synthesized_display_id = 2000;
159 display_.set_id(synthesized_display_id++);
160 display_.SetScaleAndBounds(1.0f, screen_bounds);