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