test_screen.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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.
4
5#include "ui/aura/test/test_screen.h"
6
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"
15
16namespace aura {
17
18// static
19TestScreen* 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));
23}
24
25// static
26TestScreen* TestScreen::CreateFullscreen() {
27  return new TestScreen(gfx::Rect(RootWindowHost::GetNativeScreenSize()));
28}
29
30TestScreen::~TestScreen() {
31}
32
33RootWindow* TestScreen::CreateRootWindowForPrimaryDisplay() {
34  DCHECK(!root_window_);
35  root_window_ = new RootWindow(RootWindow::CreateParams(display_.bounds()));
36  root_window_->AddObserver(this);
37  root_window_->Init();
38  return root_window_;
39}
40
41void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
42  gfx::Rect bounds = display_.bounds();
43  gfx::Rect bounds_in_pixel = gfx::ToNearestRect(
44      gfx::ScaleRect(bounds, display_.device_scale_factor()));
45  display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
46  root_window_->OnHostResized(bounds_in_pixel.size());
47}
48
49void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
50  display_.set_rotation(rotation);
51  root_window_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
52}
53
54void TestScreen::SetUIScale(float ui_scale) {
55  ui_scale_ = ui_scale;
56  root_window_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
57}
58
59gfx::Transform TestScreen::GetRotationTransform() const {
60  gfx::Transform rotate;
61  float one_pixel = 1.0f / display_.device_scale_factor();
62  switch (display_.rotation()) {
63    case gfx::Display::ROTATE_0:
64      break;
65    case gfx::Display::ROTATE_90:
66      rotate.Translate(display_.bounds().height() - one_pixel, 0);
67      rotate.Rotate(90);
68      break;
69    case gfx::Display::ROTATE_270:
70      rotate.Translate(0, display_.bounds().width() - one_pixel);
71      rotate.Rotate(270);
72      break;
73    case gfx::Display::ROTATE_180:
74      rotate.Translate(display_.bounds().width() - one_pixel,
75                       display_.bounds().height() - one_pixel);
76      rotate.Rotate(180);
77      break;
78  }
79
80  return rotate;
81}
82
83gfx::Transform TestScreen::GetUIScaleTransform() const {
84  gfx::Transform ui_scale;
85  ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
86  return ui_scale;
87}
88
89bool TestScreen::IsDIPEnabled() {
90  return true;
91}
92
93void TestScreen::OnWindowBoundsChanged(
94    Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
95  DCHECK_EQ(root_window_, window);
96  display_.SetSize(new_bounds.size());
97}
98
99void TestScreen::OnWindowDestroying(Window* window) {
100  if (root_window_ == window)
101    root_window_ = NULL;
102}
103
104gfx::Point TestScreen::GetCursorScreenPoint() {
105  return Env::GetInstance()->last_mouse_location();
106}
107
108gfx::NativeWindow TestScreen::GetWindowAtCursorScreenPoint() {
109  const gfx::Point point = GetCursorScreenPoint();
110  return root_window_->GetTopWindowContainingPoint(point);
111}
112
113int TestScreen::GetNumDisplays() {
114  return 1;
115}
116
117gfx::Display TestScreen::GetDisplayNearestWindow(
118    gfx::NativeWindow window) const {
119  return display_;
120}
121
122gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
123  return display_;
124}
125
126gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
127  return display_;
128}
129
130gfx::Display TestScreen::GetPrimaryDisplay() const {
131  return display_;
132}
133
134void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
135}
136
137void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
138}
139
140TestScreen::TestScreen(const gfx::Rect& screen_bounds)
141    : root_window_(NULL),
142      ui_scale_(1.0f) {
143  static int64 synthesized_display_id = 2000;
144  display_.set_id(synthesized_display_id++);
145  display_.SetScaleAndBounds(1.0f, screen_bounds);
146}
147
148}  // namespace aura
149