test_screen.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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/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"
16
17namespace aura {
18
19namespace {
20
21bool IsRotationPortrait(gfx::Display::Rotation rotation) {
22  return rotation == gfx::Display::ROTATE_90 ||
23         rotation == gfx::Display::ROTATE_270;
24}
25
26}  // namespace
27
28// static
29TestScreen* 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));
34}
35
36// static
37TestScreen* TestScreen::CreateFullscreen() {
38  return new TestScreen(gfx::Rect(WindowTreeHost::GetNativeScreenSize()));
39}
40
41TestScreen::~TestScreen() {
42}
43
44WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
45  DCHECK(!host_);
46  host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel()));
47  host_->window()->AddObserver(this);
48  host_->InitHost();
49  return host_;
50}
51
52void 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());
56}
57
58void 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());
64  }
65  display_.set_rotation(rotation);
66  display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
67  host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
68}
69
70void TestScreen::SetUIScale(float ui_scale) {
71  ui_scale_ = 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());
77}
78
79gfx::Transform TestScreen::GetRotationTransform() const {
80  gfx::Transform rotate;
81  switch (display_.rotation()) {
82    case gfx::Display::ROTATE_0:
83      break;
84    case gfx::Display::ROTATE_90:
85      rotate.Translate(display_.bounds().height(), 0);
86      rotate.Rotate(90);
87      break;
88    case gfx::Display::ROTATE_270:
89      rotate.Translate(0, display_.bounds().width());
90      rotate.Rotate(270);
91      break;
92    case gfx::Display::ROTATE_180:
93      rotate.Translate(display_.bounds().width(),
94                       display_.bounds().height());
95      rotate.Rotate(180);
96      break;
97  }
98
99  return rotate;
100}
101
102gfx::Transform TestScreen::GetUIScaleTransform() const {
103  gfx::Transform ui_scale;
104  ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
105  return ui_scale;
106}
107
108bool TestScreen::IsDIPEnabled() {
109  return true;
110}
111
112void 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())));
117}
118
119void TestScreen::OnWindowDestroying(Window* window) {
120  if (host_->window() == window)
121    host_ = NULL;
122}
123
124gfx::Point TestScreen::GetCursorScreenPoint() {
125  return Env::GetInstance()->last_mouse_location();
126}
127
128gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
129  return GetWindowAtScreenPoint(GetCursorScreenPoint());
130}
131
132gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
133  return host_->window()->GetTopWindowContainingPoint(point);
134}
135
136int TestScreen::GetNumDisplays() const {
137  return 1;
138}
139
140std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
141  return std::vector<gfx::Display>(1, display_);
142}
143
144gfx::Display TestScreen::GetDisplayNearestWindow(
145    gfx::NativeWindow window) const {
146  return display_;
147}
148
149gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
150  return display_;
151}
152
153gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
154  return display_;
155}
156
157gfx::Display TestScreen::GetPrimaryDisplay() const {
158  return display_;
159}
160
161void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
162}
163
164void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
165}
166
167TestScreen::TestScreen(const gfx::Rect& screen_bounds)
168    : host_(NULL),
169      ui_scale_(1.0f) {
170  static int64 synthesized_display_id = 2000;
171  display_.set_id(synthesized_display_id++);
172  display_.SetScaleAndBounds(1.0f, screen_bounds);
173}
174
175}  // namespace aura
176