Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / ui / aura / test / test_screen.cc
blobcdeb093eda082e57cb35f87b2e1679741bbd3709
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.
5 #include "ui/aura/test/test_screen.h"
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/base/ime/input_method.h"
13 #include "ui/gfx/geometry/rect_conversions.h"
14 #include "ui/gfx/geometry/size_conversions.h"
15 #include "ui/gfx/native_widget_types.h"
16 #include "ui/gfx/screen.h"
18 namespace aura {
20 namespace {
22 bool IsRotationPortrait(gfx::Display::Rotation rotation) {
23 return rotation == gfx::Display::ROTATE_90 ||
24 rotation == gfx::Display::ROTATE_270;
27 } // namespace
29 // static
30 TestScreen* TestScreen::Create(const gfx::Size& size) {
31 const gfx::Size kDefaultSize(800, 600);
32 // Use (0,0) because the desktop aura tests are executed in
33 // native environment where the display's origin is (0,0).
34 return new TestScreen(gfx::Rect(size.IsEmpty() ? kDefaultSize : size));
37 // static
38 TestScreen* TestScreen::CreateFullscreen() {
39 return new TestScreen(gfx::Rect(WindowTreeHost::GetNativeScreenSize()));
42 TestScreen::~TestScreen() {
45 WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
46 DCHECK(!host_);
47 host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel()));
48 // Some tests don't correctly manage window focus/activation states.
49 // Makes sure InputMethod is default focused so that IME basics can work.
50 host_->GetInputMethod()->OnFocus();
51 host_->window()->AddObserver(this);
52 host_->InitHost();
53 return host_;
56 void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
57 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
58 display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
59 host_->OnHostResized(bounds_in_pixel.size());
62 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
63 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
64 gfx::Rect new_bounds(bounds_in_pixel);
65 if (IsRotationPortrait(rotation) != IsRotationPortrait(display_.rotation())) {
66 new_bounds.set_width(bounds_in_pixel.height());
67 new_bounds.set_height(bounds_in_pixel.width());
69 display_.set_rotation(rotation);
70 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
71 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
74 void TestScreen::SetUIScale(float ui_scale) {
75 ui_scale_ = ui_scale;
76 gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
77 gfx::Rect new_bounds = gfx::ToNearestRect(
78 gfx::ScaleRect(gfx::RectF(bounds_in_pixel), 1.0f / ui_scale));
79 display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
80 host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
83 void TestScreen::SetWorkAreaInsets(const gfx::Insets& insets) {
84 display_.UpdateWorkAreaFromInsets(insets);
87 gfx::Transform TestScreen::GetRotationTransform() const {
88 gfx::Transform rotate;
89 switch (display_.rotation()) {
90 case gfx::Display::ROTATE_0:
91 break;
92 case gfx::Display::ROTATE_90:
93 rotate.Translate(display_.bounds().height(), 0);
94 rotate.Rotate(90);
95 break;
96 case gfx::Display::ROTATE_270:
97 rotate.Translate(0, display_.bounds().width());
98 rotate.Rotate(270);
99 break;
100 case gfx::Display::ROTATE_180:
101 rotate.Translate(display_.bounds().width(),
102 display_.bounds().height());
103 rotate.Rotate(180);
104 break;
107 return rotate;
110 gfx::Transform TestScreen::GetUIScaleTransform() const {
111 gfx::Transform ui_scale;
112 ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
113 return ui_scale;
116 void TestScreen::OnWindowBoundsChanged(
117 Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
118 DCHECK_EQ(host_->window(), window);
119 display_.SetSize(gfx::ToFlooredSize(
120 gfx::ScaleSize(new_bounds.size(), display_.device_scale_factor())));
123 void TestScreen::OnWindowDestroying(Window* window) {
124 if (host_->window() == window)
125 host_ = NULL;
128 gfx::Point TestScreen::GetCursorScreenPoint() {
129 return Env::GetInstance()->last_mouse_location();
132 gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
133 return GetWindowAtScreenPoint(GetCursorScreenPoint());
136 gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
137 return host_->window()->GetTopWindowContainingPoint(point);
140 int TestScreen::GetNumDisplays() const {
141 return 1;
144 std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
145 return std::vector<gfx::Display>(1, display_);
148 gfx::Display TestScreen::GetDisplayNearestWindow(
149 gfx::NativeWindow window) const {
150 return display_;
153 gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
154 return display_;
157 gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
158 return display_;
161 gfx::Display TestScreen::GetPrimaryDisplay() const {
162 return display_;
165 void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
168 void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
171 TestScreen::TestScreen(const gfx::Rect& screen_bounds)
172 : host_(NULL),
173 ui_scale_(1.0f) {
174 static int64 synthesized_display_id = 2000;
175 display_.set_id(synthesized_display_id++);
176 display_.SetScaleAndBounds(1.0f, screen_bounds);
179 } // namespace aura