Update {virtual,override,final} to follow C++11 style in chrome/browser/chromeos.
[chromium-blink-merge.git] / ash / touch / touch_transformer_controller_unittest.cc
blob3dbc8cade551d31543984109829ccd1201b849c4
1 // Copyright 2014 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 "ash/touch/touch_transformer_controller.h"
7 #include "ash/shell.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ui/aura/window_tree_host.h"
10 #include "ui/events/devices/device_data_manager.h"
11 #include "ui/events/test/events_test_utils_x11.h"
12 #include "ui/gfx/display.h"
14 namespace ash {
16 namespace {
18 DisplayInfo CreateDisplayInfo(int64 id,
19 unsigned int touch_device_id,
20 const gfx::Rect& bounds) {
21 DisplayInfo info(id, std::string(), false);
22 info.SetBounds(bounds);
23 info.set_touch_device_id(touch_device_id);
25 // Create a default mode.
26 std::vector<DisplayMode> default_modes(
27 1, DisplayMode(bounds.size(), 60, false, true));
28 info.SetDisplayModes(default_modes);
30 return info;
33 ui::TouchscreenDevice CreateTouchscreenDevice(unsigned int id,
34 const gfx::Size& size) {
35 return ui::TouchscreenDevice(id, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL,
36 std::string(), size, 0);
39 } // namespace
41 typedef test::AshTestBase TouchTransformerControllerTest;
43 TEST_F(TouchTransformerControllerTest, TouchTransformerMirrorModeLetterboxing) {
44 // The internal display has native resolution of 2560x1700, and in
45 // mirror mode it is configured as 1920x1200. This is in letterboxing
46 // mode.
47 DisplayInfo internal_display_info =
48 CreateDisplayInfo(1, 10u, gfx::Rect(0, 0, 1920, 1200));
49 internal_display_info.set_is_aspect_preserving_scaling(true);
50 std::vector<DisplayMode> internal_modes;
51 internal_modes.push_back(
52 DisplayMode(gfx::Size(2560, 1700), 60, false, true));
53 internal_modes.push_back(
54 DisplayMode(gfx::Size(1920, 1200), 60, false, false));
55 internal_display_info.SetDisplayModes(internal_modes);
57 DisplayInfo external_display_info =
58 CreateDisplayInfo(2, 11u, gfx::Rect(0, 0, 1920, 1200));
60 gfx::Size fb_size(1920, 1200);
62 // Create the touchscreens with the same size as the framebuffer so we can
63 // share the tests between Ozone & X11.
64 ui::TouchscreenDevice internal_touchscreen =
65 CreateTouchscreenDevice(10, fb_size);
66 ui::TouchscreenDevice external_touchscreen =
67 CreateTouchscreenDevice(11, fb_size);
69 TouchTransformerController* tt_controller =
70 Shell::GetInstance()->touch_transformer_controller();
71 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
73 device_manager->UpdateTouchInfoForDisplay(
74 internal_display_info.id(),
75 internal_display_info.touch_device_id(),
76 tt_controller->GetTouchTransform(internal_display_info,
77 internal_touchscreen,
78 fb_size));
80 device_manager->UpdateTouchInfoForDisplay(
81 external_display_info.id(),
82 external_display_info.touch_device_id(),
83 tt_controller->GetTouchTransform(external_display_info,
84 external_touchscreen,
85 fb_size));
87 EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(10));
88 EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(11));
90 // External touch display has the default TouchTransformer.
91 float x = 100.0;
92 float y = 100.0;
93 device_manager->ApplyTouchTransformer(11, &x, &y);
94 EXPECT_EQ(100, x);
95 EXPECT_EQ(100, y);
97 // In letterboxing, there is (1-2560*(1200/1920)/1700)/2 = 2.95% of the
98 // height on both the top & bottom region of the screen is blank.
99 // When touch events coming at Y range [0, 1200), the mapping should be
100 // [0, ~35] ---> < 0
101 // [~35, ~1165] ---> [0, 1200)
102 // [~1165, 1200] ---> >= 1200
103 x = 100.0;
104 y = 35.0;
105 device_manager->ApplyTouchTransformer(10, &x, &y);
106 EXPECT_EQ(100, static_cast<int>(x));
107 EXPECT_EQ(0, static_cast<int>(y));
109 x = 100.0;
110 y = 1165.0;
111 device_manager->ApplyTouchTransformer(10, &x, &y);
112 EXPECT_EQ(100, static_cast<int>(x));
113 EXPECT_EQ(1200, static_cast<int>(y));
116 TEST_F(TouchTransformerControllerTest, TouchTransformerMirrorModePillarboxing) {
117 // The internal display has native resolution of 1366x768, and in
118 // mirror mode it is configured as 1024x768. This is in pillarboxing
119 // mode.
120 DisplayInfo internal_display_info =
121 CreateDisplayInfo(1, 10, gfx::Rect(0, 0, 1024, 768));
122 internal_display_info.set_is_aspect_preserving_scaling(true);
123 std::vector<DisplayMode> internal_modes;
124 internal_modes.push_back(
125 DisplayMode(gfx::Size(1366, 768), 60, false, true));
126 internal_modes.push_back(
127 DisplayMode(gfx::Size(1024, 768), 60, false, false));
128 internal_display_info.SetDisplayModes(internal_modes);
130 DisplayInfo external_display_info =
131 CreateDisplayInfo(2, 11, gfx::Rect(0, 0, 1024, 768));
133 gfx::Size fb_size(1024, 768);
135 // Create the touchscreens with the same size as the framebuffer so we can
136 // share the tests between Ozone & X11.
137 ui::TouchscreenDevice internal_touchscreen =
138 CreateTouchscreenDevice(10, fb_size);
139 ui::TouchscreenDevice external_touchscreen =
140 CreateTouchscreenDevice(11, fb_size);
142 TouchTransformerController* tt_controller =
143 Shell::GetInstance()->touch_transformer_controller();
144 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
146 device_manager->UpdateTouchInfoForDisplay(
147 internal_display_info.id(),
148 internal_display_info.touch_device_id(),
149 tt_controller->GetTouchTransform(internal_display_info,
150 internal_touchscreen,
151 fb_size));
153 device_manager->UpdateTouchInfoForDisplay(
154 external_display_info.id(),
155 external_display_info.touch_device_id(),
156 tt_controller->GetTouchTransform(external_display_info,
157 external_touchscreen,
158 fb_size));
160 EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(10));
161 EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(11));
163 // External touch display has the default TouchTransformer.
164 float x = 100.0;
165 float y = 100.0;
166 device_manager->ApplyTouchTransformer(11, &x, &y);
167 EXPECT_EQ(100, x);
168 EXPECT_EQ(100, y);
170 // In pillarboxing, there is (1-768*(1024/768)/1366)/2 = 12.5% of the
171 // width on both the left & rigth region of the screen is blank.
172 // When touch events coming at X range [0, 1024), the mapping should be
173 // [0, ~128] ---> < 0
174 // [~128, ~896] ---> [0, 1024)
175 // [~896, 1024] ---> >= 1024
176 x = 128.0;
177 y = 100.0;
178 device_manager->ApplyTouchTransformer(10, &x, &y);
179 EXPECT_EQ(0, static_cast<int>(x));
180 EXPECT_EQ(100, static_cast<int>(y));
182 x = 896.0;
183 y = 100.0;
184 device_manager->ApplyTouchTransformer(10, &x, &y);
185 EXPECT_EQ(1024, static_cast<int>(x));
186 EXPECT_EQ(100, static_cast<int>(y));
189 TEST_F(TouchTransformerControllerTest, TouchTransformerExtendedMode) {
190 // The internal display has size 1366 x 768. The external display has
191 // size 2560x1600. The total frame buffer is 2560x2428,
192 // where 2428 = 768 + 60 (hidden gap) + 1600
193 // and the sceond monitor is translated to Point (0, 828) in the
194 // framebuffer.
195 DisplayInfo display1 = CreateDisplayInfo(1, 5u, gfx::Rect(0, 0, 1366, 768));
196 DisplayInfo display2 =
197 CreateDisplayInfo(2, 6u, gfx::Rect(0, 828, 2560, 1600));
198 gfx::Size fb_size(2560, 2428);
200 // Create the touchscreens with the same size as the framebuffer so we can
201 // share the tests between Ozone & X11.
202 ui::TouchscreenDevice touchscreen1 = CreateTouchscreenDevice(5, fb_size);
203 ui::TouchscreenDevice touchscreen2 = CreateTouchscreenDevice(6, fb_size);
205 TouchTransformerController* tt_controller =
206 Shell::GetInstance()->touch_transformer_controller();
207 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
209 device_manager->UpdateTouchInfoForDisplay(
210 display1.id(),
211 display1.touch_device_id(),
212 tt_controller->GetTouchTransform(display1,
213 touchscreen1,
214 fb_size));
216 device_manager->UpdateTouchInfoForDisplay(
217 display2.id(),
218 display2.touch_device_id(),
219 tt_controller->GetTouchTransform(display2,
220 touchscreen2,
221 fb_size));
223 EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(5));
224 EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(6));
226 // Mapping for touch events from internal touch display:
227 // [0, 2560) x [0, 2428) -> [0, 1366) x [0, 768)
228 float x = 0.0;
229 float y = 0.0;
230 device_manager->ApplyTouchTransformer(5, &x, &y);
231 EXPECT_EQ(0, static_cast<int>(x));
232 EXPECT_EQ(0, static_cast<int>(y));
234 x = 2559.0;
235 y = 2427.0;
236 device_manager->ApplyTouchTransformer(5, &x, &y);
237 EXPECT_EQ(1365, static_cast<int>(x));
238 EXPECT_EQ(767, static_cast<int>(y));
240 // Mapping for touch events from external touch display:
241 // [0, 2560) x [0, 2428) -> [0, 2560) x [0, 1600)
242 x = 0.0;
243 y = 0.0;
244 device_manager->ApplyTouchTransformer(6, &x, &y);
245 #if defined(USE_OZONE)
246 // On ozone we expect screen coordinates so add display origin.
247 EXPECT_EQ(0 + 0, static_cast<int>(x));
248 EXPECT_EQ(0 + 828, static_cast<int>(y));
249 #else
250 EXPECT_EQ(0, static_cast<int>(x));
251 EXPECT_EQ(0, static_cast<int>(y));
252 #endif
254 x = 2559.0;
255 y = 2427.0;
256 device_manager->ApplyTouchTransformer(6, &x, &y);
257 #if defined(USE_OZONE)
258 // On ozone we expect screen coordinates so add display origin.
259 EXPECT_EQ(2559 + 0, static_cast<int>(x));
260 EXPECT_EQ(1599 + 828, static_cast<int>(y));
261 #else
262 EXPECT_EQ(2559, static_cast<int>(x));
263 EXPECT_EQ(1599, static_cast<int>(y));
264 #endif
267 TEST_F(TouchTransformerControllerTest, TouchRadiusScale) {
268 DisplayInfo display = CreateDisplayInfo(1, 5u, gfx::Rect(0, 0, 2560, 1600));
269 ui::TouchscreenDevice touch_device =
270 CreateTouchscreenDevice(5, gfx::Size(1001, 1001));
272 TouchTransformerController* tt_controller =
273 Shell::GetInstance()->touch_transformer_controller();
274 // Default touchscreen position range is 1001x1001;
275 EXPECT_EQ(sqrt((2560.0 * 1600.0) / (1001.0 * 1001.0)),
276 tt_controller->GetTouchResolutionScale(display, touch_device));
279 } // namespace ash