test_screen.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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
19// static
20TestScreen* TestScreen::Create(const gfx::Size& size) {
21  const gfx::Size kDefaultSize(800, 600);
22  // Use (0,0) because the desktop aura tests are executed in
23  // native environment where the display's origin is (0,0).
24  return new TestScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size));
25}
26
27// static
28TestScreen* TestScreen::CreateFullscreen() {
29  return new TestScreen(gfx::Rect(WindowTreeHost::GetNativeScreenSize()));
30}
31
32TestScreen::~TestScreen() {
33}
34
35WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
36  DCHECK(!host_);
37  host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel()));
38  host_->window()->AddObserver(this);
39  host_->InitHost();
40  return host_;
41}
42
43void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
44  gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
45  display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
46  host_->OnHostResized(bounds_in_pixel.size());
47}
48
49void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
50  display_.set_rotation(rotation);
51  // TODO(oshima|mukai): Update the display_ as well.
52  host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
53}
54
55void TestScreen::SetUIScale(float ui_scale) {
56  ui_scale_ = ui_scale;
57  gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
58  gfx::Rect new_bounds = gfx::ToNearestRect(
59      gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
60  display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
61  host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
62}
63
64gfx::Transform TestScreen::GetRotationTransform() const {
65  gfx::Transform rotate;
66  float one_pixel = 1.0f / display_.device_scale_factor();
67  switch (display_.rotation()) {
68    case gfx::Display::ROTATE_0:
69      break;
70    case gfx::Display::ROTATE_90:
71      rotate.Translate(display_.bounds().height() - one_pixel, 0);
72      rotate.Rotate(90);
73      break;
74    case gfx::Display::ROTATE_270:
75      rotate.Translate(0, display_.bounds().width() - one_pixel);
76      rotate.Rotate(270);
77      break;
78    case gfx::Display::ROTATE_180:
79      rotate.Translate(display_.bounds().width() - one_pixel,
80                       display_.bounds().height() - one_pixel);
81      rotate.Rotate(180);
82      break;
83  }
84
85  return rotate;
86}
87
88gfx::Transform TestScreen::GetUIScaleTransform() const {
89  gfx::Transform ui_scale;
90  ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
91  return ui_scale;
92}
93
94bool TestScreen::IsDIPEnabled() {
95  return true;
96}
97
98void TestScreen::OnWindowBoundsChanged(
99    Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
100  DCHECK_EQ(host_->window(), window);
101  display_.SetSize(gfx::ToFlooredSize(
102      gfx::ScaleSize(new_bounds.size(), display_.device_scale_factor())));
103}
104
105void TestScreen::OnWindowDestroying(Window* window) {
106  if (host_->window() == window)
107    host_ = NULL;
108}
109
110gfx::Point TestScreen::GetCursorScreenPoint() {
111  return Env::GetInstance()->last_mouse_location();
112}
113
114gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
115  return GetWindowAtScreenPoint(GetCursorScreenPoint());
116}
117
118gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
119  return host_->window()->GetTopWindowContainingPoint(point);
120}
121
122int TestScreen::GetNumDisplays() const {
123  return 1;
124}
125
126std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
127  return std::vector<gfx::Display>(1, display_);
128}
129
130gfx::Display TestScreen::GetDisplayNearestWindow(
131    gfx::NativeWindow window) const {
132  return display_;
133}
134
135gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
136  return display_;
137}
138
139gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
140  return display_;
141}
142
143gfx::Display TestScreen::GetPrimaryDisplay() const {
144  return display_;
145}
146
147void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
148}
149
150void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
151}
152
153TestScreen::TestScreen(const gfx::Rect& screen_bounds)
154    : host_(NULL),
155      ui_scale_(1.0f) {
156  static int64 synthesized_display_id = 2000;
157  display_.set_id(synthesized_display_id++);
158  display_.SetScaleAndBounds(1.0f, screen_bounds);
159}
160
161}  // namespace aura
162