15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/test/test_screen.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/env.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/window_tree_host.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/geometry/size_conversions.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/native_widget_types.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/rect_conversions.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/screen.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace aura {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace {
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)bool IsRotationPortrait(gfx::Display::Rotation rotation) {
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return rotation == gfx::Display::ROTATE_90 ||
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)         rotation == gfx::Display::ROTATE_270;
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TestScreen* TestScreen::Create(const gfx::Size& size) {
306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const gfx::Size kDefaultSize(800, 600);
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Use (0,0) because the desktop aura tests are executed in
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // native environment where the display's origin is (0,0).
336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  return new TestScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size));
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TestScreen* TestScreen::CreateFullscreen() {
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return new TestScreen(gfx::Rect(WindowTreeHost::GetNativeScreenSize()));
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestScreen::~TestScreen() {
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(!host_);
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel()));
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  host_->window()->AddObserver(this);
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  host_->InitHost();
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return host_;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
53a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  host_->OnHostResized(bounds_in_pixel.size());
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  gfx::Rect new_bounds(bounds_in_pixel);
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (IsRotationPortrait(rotation) != IsRotationPortrait(display_.rotation())) {
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    new_bounds.set_width(bounds_in_pixel.height());
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    new_bounds.set_height(bounds_in_pixel.width());
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  display_.set_rotation(rotation);
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
67a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void TestScreen::SetUIScale(float ui_scale) {
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ui_scale_ = ui_scale;
72a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
73a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  gfx::Rect new_bounds = gfx::ToNearestRect(
74a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
75a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
76a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void TestScreen::SetWorkAreaInsets(const gfx::Insets& insets) {
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  display_.UpdateWorkAreaFromInsets(insets);
8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)gfx::Transform TestScreen::GetRotationTransform() const {
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Transform rotate;
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (display_.rotation()) {
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case gfx::Display::ROTATE_0:
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case gfx::Display::ROTATE_90:
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      rotate.Translate(display_.bounds().height(), 0);
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      rotate.Rotate(90);
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case gfx::Display::ROTATE_270:
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      rotate.Translate(0, display_.bounds().width());
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      rotate.Rotate(270);
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case gfx::Display::ROTATE_180:
976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      rotate.Translate(display_.bounds().width(),
986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                       display_.bounds().height());
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      rotate.Rotate(180);
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      break;
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return rotate;
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)gfx::Transform TestScreen::GetUIScaleTransform() const {
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  gfx::Transform ui_scale;
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return ui_scale;
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TestScreen::IsDIPEnabled() {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestScreen::OnWindowBoundsChanged(
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK_EQ(host_->window(), window);
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  display_.SetSize(gfx::ToFlooredSize(
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      gfx::ScaleSize(new_bounds.size(), display_.device_scale_factor())));
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestScreen::OnWindowDestroying(Window* window) {
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (host_->window() == window)
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    host_ = NULL;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Point TestScreen::GetCursorScreenPoint() {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Env::GetInstance()->last_mouse_location();
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
132424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return GetWindowAtScreenPoint(GetCursorScreenPoint());
134424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return host_->window()->GetTopWindowContainingPoint(point);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)int TestScreen::GetNumDisplays() const {
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return 1;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return std::vector<gfx::Display>(1, display_);
146424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display TestScreen::GetDisplayNearestWindow(
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    gfx::NativeWindow window) const {
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return display_;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return display_;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return display_;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::Display TestScreen::GetPrimaryDisplay() const {
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return display_;
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TestScreen::TestScreen(const gfx::Rect& screen_bounds)
172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : host_(NULL),
17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ui_scale_(1.0f) {
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static int64 synthesized_display_id = 2000;
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  display_.set_id(synthesized_display_id++);
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  display_.SetScaleAndBounds(1.0f, screen_bounds);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace aura
180