test_screen.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/test/test_screen.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/env.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/window.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/window_tree_host.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/geometry/size_conversions.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/native_widget_types.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/rect_conversions.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/screen.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace aura {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TestScreen* TestScreen::Create() {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Use (0,0) because the desktop aura tests are executed in
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // native environment where the display's origin is (0,0).
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return new TestScreen(gfx::Rect(0, 0, 800, 600));
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TestScreen* TestScreen::CreateFullscreen() {
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return new TestScreen(gfx::Rect(WindowTreeHost::GetNativeScreenSize()));
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TestScreen::~TestScreen() {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(!host_);
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel()));
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host_->window()->AddObserver(this);
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host_->InitHost();
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return host_;
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host_->OnHostResized(bounds_in_pixel.size());
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  display_.set_rotation(rotation);
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(oshima|mukai): Update the display_ as well.
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TestScreen::SetUIScale(float ui_scale) {
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ui_scale_ = ui_scale;
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Rect new_bounds = gfx::ToNearestRect(
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
61}
62
63gfx::Transform TestScreen::GetRotationTransform() const {
64  gfx::Transform rotate;
65  float one_pixel = 1.0f / display_.device_scale_factor();
66  switch (display_.rotation()) {
67    case gfx::Display::ROTATE_0:
68      break;
69    case gfx::Display::ROTATE_90:
70      rotate.Translate(display_.bounds().height() - one_pixel, 0);
71      rotate.Rotate(90);
72      break;
73    case gfx::Display::ROTATE_270:
74      rotate.Translate(0, display_.bounds().width() - one_pixel);
75      rotate.Rotate(270);
76      break;
77    case gfx::Display::ROTATE_180:
78      rotate.Translate(display_.bounds().width() - one_pixel,
79                       display_.bounds().height() - one_pixel);
80      rotate.Rotate(180);
81      break;
82  }
83
84  return rotate;
85}
86
87gfx::Transform TestScreen::GetUIScaleTransform() const {
88  gfx::Transform ui_scale;
89  ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
90  return ui_scale;
91}
92
93bool TestScreen::IsDIPEnabled() {
94  return true;
95}
96
97void TestScreen::OnWindowBoundsChanged(
98    Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
99  DCHECK_EQ(host_->window(), window);
100  display_.SetSize(gfx::ToFlooredSize(
101      gfx::ScaleSize(new_bounds.size(), display_.device_scale_factor())));
102}
103
104void TestScreen::OnWindowDestroying(Window* window) {
105  if (host_->window() == window)
106    host_ = NULL;
107}
108
109gfx::Point TestScreen::GetCursorScreenPoint() {
110  return Env::GetInstance()->last_mouse_location();
111}
112
113gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
114  return GetWindowAtScreenPoint(GetCursorScreenPoint());
115}
116
117gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
118  return host_->window()->GetTopWindowContainingPoint(point);
119}
120
121int TestScreen::GetNumDisplays() const {
122  return 1;
123}
124
125std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
126  return std::vector<gfx::Display>(1, display_);
127}
128
129gfx::Display TestScreen::GetDisplayNearestWindow(
130    gfx::NativeWindow window) const {
131  return display_;
132}
133
134gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
135  return display_;
136}
137
138gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
139  return display_;
140}
141
142gfx::Display TestScreen::GetPrimaryDisplay() const {
143  return display_;
144}
145
146void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
147}
148
149void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
150}
151
152TestScreen::TestScreen(const gfx::Rect& screen_bounds)
153    : host_(NULL),
154      ui_scale_(1.0f) {
155  static int64 synthesized_display_id = 2000;
156  display_.set_id(synthesized_display_id++);
157  display_.SetScaleAndBounds(1.0f, screen_bounds);
158}
159
160}  // namespace aura
161