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/root_window_host.h"
11 #include "ui/aura/window.h"
12 #include "ui/gfx/native_widget_types.h"
13 #include "ui/gfx/rect_conversions.h"
14 #include "ui/gfx/screen.h"
19 TestScreen* TestScreen::Create() {
20 // Use (0,0) because the desktop aura tests are executed in
21 // native environment where the display's origin is (0,0).
22 return new TestScreen(gfx::Rect(0, 0, 800, 600));
26 TestScreen* TestScreen::CreateFullscreen() {
27 return new TestScreen(gfx::Rect(RootWindowHost::GetNativeScreenSize()));
30 TestScreen::~TestScreen() {
33 RootWindow* TestScreen::CreateRootWindowForPrimaryDisplay() {
34 DCHECK(!root_window_);
35 root_window_ = new RootWindow(RootWindow::CreateParams(display_.bounds()));
36 root_window_->AddObserver(this);
41 void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
42 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
43 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
44 root_window_->OnHostResized(bounds_in_pixel.size());
47 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
48 display_.set_rotation(rotation);
49 // TODO(oshima|mukai): Update the display_ as well.
50 root_window_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
53 void TestScreen::SetUIScale(float ui_scale) {
55 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
56 gfx::Rect new_bounds = gfx::ToNearestRect(
57 gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
58 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
59 root_window_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
62 gfx::Transform TestScreen::GetRotationTransform() const {
63 gfx::Transform rotate;
64 float one_pixel = 1.0f / display_.device_scale_factor();
65 switch (display_.rotation()) {
66 case gfx::Display::ROTATE_0:
68 case gfx::Display::ROTATE_90:
69 rotate.Translate(display_.bounds().height() - one_pixel, 0);
72 case gfx::Display::ROTATE_270:
73 rotate.Translate(0, display_.bounds().width() - one_pixel);
76 case gfx::Display::ROTATE_180:
77 rotate.Translate(display_.bounds().width() - one_pixel,
78 display_.bounds().height() - one_pixel);
86 gfx::Transform TestScreen::GetUIScaleTransform() const {
87 gfx::Transform ui_scale;
88 ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
92 bool TestScreen::IsDIPEnabled() {
96 void TestScreen::OnWindowBoundsChanged(
97 Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
98 DCHECK_EQ(root_window_, window);
99 display_.SetSize(new_bounds.size());
102 void TestScreen::OnWindowDestroying(Window* window) {
103 if (root_window_ == window)
107 gfx::Point TestScreen::GetCursorScreenPoint() {
108 return Env::GetInstance()->last_mouse_location();
111 gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
112 return GetWindowAtScreenPoint(GetCursorScreenPoint());
115 gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
116 return root_window_->GetTopWindowContainingPoint(point);
119 int TestScreen::GetNumDisplays() const {
123 std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
124 return std::vector<gfx::Display>(1, display_);
127 gfx::Display TestScreen::GetDisplayNearestWindow(
128 gfx::NativeWindow window) const {
132 gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
136 gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
140 gfx::Display TestScreen::GetPrimaryDisplay() const {
144 void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
147 void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
150 TestScreen::TestScreen(const gfx::Rect& screen_bounds)
151 : root_window_(NULL),
153 static int64 synthesized_display_id = 2000;
154 display_.set_id(synthesized_display_id++);
155 display_.SetScaleAndBounds(1.0f, screen_bounds);