Upstream version 7.36.149.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/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
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 WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
35   DCHECK(!host_);
36   host_ = WindowTreeHost::Create(gfx::Rect(display_.GetSizeInPixel()));
37   host_->window()->AddObserver(this);
38   host_->InitHost();
39   return host_;
40 }
41
42 void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
43   gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
44   display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
45   host_->OnHostResized(bounds_in_pixel.size());
46 }
47
48 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
49   display_.set_rotation(rotation);
50   // TODO(oshima|mukai): Update the display_ as well.
51   host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
52 }
53
54 void TestScreen::SetUIScale(float ui_scale) {
55   ui_scale_ = ui_scale;
56   gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
57   gfx::Rect new_bounds = gfx::ToNearestRect(
58       gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
59   display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
60   host_->SetRootTransform(GetRotationTransform() * GetUIScaleTransform());
61 }
62
63 gfx::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
87 gfx::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
93 bool TestScreen::IsDIPEnabled() {
94   return true;
95 }
96
97 void 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
104 void TestScreen::OnWindowDestroying(Window* window) {
105   if (host_->window() == window)
106     host_ = NULL;
107 }
108
109 gfx::Point TestScreen::GetCursorScreenPoint() {
110   return Env::GetInstance()->last_mouse_location();
111 }
112
113 gfx::NativeWindow TestScreen::GetWindowUnderCursor() {
114   return GetWindowAtScreenPoint(GetCursorScreenPoint());
115 }
116
117 gfx::NativeWindow TestScreen::GetWindowAtScreenPoint(const gfx::Point& point) {
118   return host_->window()->GetTopWindowContainingPoint(point);
119 }
120
121 int TestScreen::GetNumDisplays() const {
122   return 1;
123 }
124
125 std::vector<gfx::Display> TestScreen::GetAllDisplays() const {
126   return std::vector<gfx::Display>(1, display_);
127 }
128
129 gfx::Display TestScreen::GetDisplayNearestWindow(
130     gfx::NativeWindow window) const {
131   return display_;
132 }
133
134 gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
135   return display_;
136 }
137
138 gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
139   return display_;
140 }
141
142 gfx::Display TestScreen::GetPrimaryDisplay() const {
143   return display_;
144 }
145
146 void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
147 }
148
149 void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
150 }
151
152 TestScreen::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