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