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"
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"
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
);
33 ui::TouchscreenDevice
CreateTouchscreenDevice(unsigned int id
,
34 const gfx::Size
& size
) {
35 return ui::TouchscreenDevice(id
, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
,
41 typedef test::AshTestBase TouchTransformerControllerTest
;
43 TEST_F(TouchTransformerControllerTest
, MirrorModeLetterboxing
) {
44 // The internal display has native resolution of 2560x1700, and in
45 // mirror mode it is configured as 1920x1200. This is in letterboxing
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(), internal_display_info
.touch_device_id(),
75 tt_controller
->GetTouchTransform(internal_display_info
,
76 internal_display_info
,
77 internal_touchscreen
, fb_size
));
79 device_manager
->UpdateTouchInfoForDisplay(
80 external_display_info
.id(), external_display_info
.touch_device_id(),
81 tt_controller
->GetTouchTransform(external_display_info
,
82 external_display_info
,
83 external_touchscreen
, fb_size
));
85 EXPECT_EQ(1, device_manager
->GetDisplayForTouchDevice(10));
86 EXPECT_EQ(2, device_manager
->GetDisplayForTouchDevice(11));
88 // External touch display has the default TouchTransformer.
91 device_manager
->ApplyTouchTransformer(11, &x
, &y
);
95 // In letterboxing, there is (1-2560*(1200/1920)/1700)/2 = 2.95% of the
96 // height on both the top & bottom region of the screen is blank.
97 // When touch events coming at Y range [0, 1200), the mapping should be
99 // [~35, ~1165] ---> [0, 1200)
100 // [~1165, 1200] ---> >= 1200
103 device_manager
->ApplyTouchTransformer(10, &x
, &y
);
104 EXPECT_NEAR(100, x
, 0.5);
105 EXPECT_NEAR(0, y
, 0.5);
109 device_manager
->ApplyTouchTransformer(10, &x
, &y
);
110 EXPECT_NEAR(100, x
, 0.5);
111 EXPECT_NEAR(1200, y
, 0.5);
114 TEST_F(TouchTransformerControllerTest
, MirrorModePillarboxing
) {
115 // The internal display has native resolution of 1366x768, and in
116 // mirror mode it is configured as 1024x768. This is in pillarboxing
118 DisplayInfo internal_display_info
=
119 CreateDisplayInfo(1, 10, gfx::Rect(0, 0, 1024, 768));
120 internal_display_info
.set_is_aspect_preserving_scaling(true);
121 std::vector
<DisplayMode
> internal_modes
;
122 internal_modes
.push_back(
123 DisplayMode(gfx::Size(1366, 768), 60, false, true));
124 internal_modes
.push_back(
125 DisplayMode(gfx::Size(1024, 768), 60, false, false));
126 internal_display_info
.SetDisplayModes(internal_modes
);
128 DisplayInfo external_display_info
=
129 CreateDisplayInfo(2, 11, gfx::Rect(0, 0, 1024, 768));
131 gfx::Size
fb_size(1024, 768);
133 // Create the touchscreens with the same size as the framebuffer so we can
134 // share the tests between Ozone & X11.
135 ui::TouchscreenDevice internal_touchscreen
=
136 CreateTouchscreenDevice(10, fb_size
);
137 ui::TouchscreenDevice external_touchscreen
=
138 CreateTouchscreenDevice(11, fb_size
);
140 TouchTransformerController
* tt_controller
=
141 Shell::GetInstance()->touch_transformer_controller();
142 ui::DeviceDataManager
* device_manager
= ui::DeviceDataManager::GetInstance();
144 device_manager
->UpdateTouchInfoForDisplay(
145 internal_display_info
.id(), internal_display_info
.touch_device_id(),
146 tt_controller
->GetTouchTransform(internal_display_info
,
147 internal_display_info
,
148 internal_touchscreen
, fb_size
));
150 device_manager
->UpdateTouchInfoForDisplay(
151 external_display_info
.id(), external_display_info
.touch_device_id(),
152 tt_controller
->GetTouchTransform(external_display_info
,
153 external_display_info
,
154 external_touchscreen
, fb_size
));
156 EXPECT_EQ(1, device_manager
->GetDisplayForTouchDevice(10));
157 EXPECT_EQ(2, device_manager
->GetDisplayForTouchDevice(11));
159 // External touch display has the default TouchTransformer.
162 device_manager
->ApplyTouchTransformer(11, &x
, &y
);
166 // In pillarboxing, there is (1-768*(1024/768)/1366)/2 = 12.5% of the
167 // width on both the left & rigth region of the screen is blank.
168 // When touch events coming at X range [0, 1024), the mapping should be
169 // [0, ~128] ---> < 0
170 // [~128, ~896] ---> [0, 1024)
171 // [~896, 1024] ---> >= 1024
174 device_manager
->ApplyTouchTransformer(10, &x
, &y
);
175 EXPECT_NEAR(0, x
, 0.5);
176 EXPECT_NEAR(100, y
, 0.5);
180 device_manager
->ApplyTouchTransformer(10, &x
, &y
);
181 EXPECT_NEAR(1024, x
, 0.5);
182 EXPECT_NEAR(100, y
, 0.5);
185 TEST_F(TouchTransformerControllerTest
, SoftwareMirrorMode
) {
186 // External display 1 has size 1280x850. External display 2 has size
187 // 1920x1080. When using software mirroring to mirror display 1 onto
188 // display 2, the displays are in extended mode and we map touches from both
189 // displays to display 1.
190 // The total frame buffer is 1920x1990,
191 // where 1990 = 850 + 60 (hidden gap) + 1080 and the second monitor is
192 // translated to point (0, 950) in the framebuffer.
193 DisplayInfo display1_info
=
194 CreateDisplayInfo(1, 10u, gfx::Rect(0, 0, 1280, 850));
195 std::vector
<DisplayMode
> display1_modes
;
196 display1_modes
.push_back(DisplayMode(gfx::Size(1280, 850), 60, false, true));
197 display1_info
.SetDisplayModes(display1_modes
);
199 DisplayInfo display2_info
=
200 CreateDisplayInfo(2, 11u, gfx::Rect(0, 950, 1920, 1080));
201 std::vector
<DisplayMode
> display2_modes
;
202 display2_modes
.push_back(DisplayMode(gfx::Size(1920, 1080), 60, false, true));
203 display2_info
.SetDisplayModes(display2_modes
);
205 gfx::Size
fb_size(1920, 1990);
207 // Create the touchscreens with the same size as the framebuffer so we can
208 // share the tests between Ozone & X11.
209 ui::TouchscreenDevice display1_touchscreen
=
210 CreateTouchscreenDevice(10, fb_size
);
211 ui::TouchscreenDevice display2_touchscreen
=
212 CreateTouchscreenDevice(11, fb_size
);
214 TouchTransformerController
* tt_controller
=
215 Shell::GetInstance()->touch_transformer_controller();
216 ui::DeviceDataManager
* device_manager
= ui::DeviceDataManager::GetInstance();
218 device_manager
->UpdateTouchInfoForDisplay(
219 display1_info
.id(), display1_info
.touch_device_id(),
220 tt_controller
->GetTouchTransform(display1_info
, display1_info
,
221 display1_touchscreen
, fb_size
));
223 device_manager
->UpdateTouchInfoForDisplay(
224 display2_info
.id(), display2_info
.touch_device_id(),
225 tt_controller
->GetTouchTransform(display1_info
, display2_info
,
226 display2_touchscreen
, fb_size
));
228 EXPECT_EQ(1, device_manager
->GetDisplayForTouchDevice(10));
229 EXPECT_EQ(2, device_manager
->GetDisplayForTouchDevice(11));
231 // Mapping for touch events from display 1's touchscreen:
232 // [0, 1920) x [0, 1990) -> [0, 1280) x [0, 850)
235 device_manager
->ApplyTouchTransformer(10, &x
, &y
);
236 EXPECT_NEAR(0, x
, 0.5);
237 EXPECT_NEAR(0, y
, 0.5);
241 device_manager
->ApplyTouchTransformer(10, &x
, &y
);
242 EXPECT_NEAR(1280, x
, 0.5);
243 EXPECT_NEAR(850, y
, 0.5);
245 // In pillarboxing, there is (1-1280*(1080/850)/1920)/2 = 7.65% of the
246 // width on both the left & right region of the screen is blank.
247 // Events come in the range [0, 1920) x [0, 1990).
250 // [0, ~147] ---> < 0
251 // [~147, ~1773] ---> [0, 1280)
252 // [~1773, 1920] ---> >= 1280
254 // [0, 1990) -> [0, 1080)
257 device_manager
->ApplyTouchTransformer(11, &x
, &y
);
258 EXPECT_NEAR(0, x
, 0.5);
259 EXPECT_NEAR(0, y
, 0.5);
263 device_manager
->ApplyTouchTransformer(11, &x
, &y
);
264 EXPECT_NEAR(1280, x
, 0.5);
265 EXPECT_NEAR(850, y
, 0.5);
268 TEST_F(TouchTransformerControllerTest
, ExtendedMode
) {
269 // The internal display has size 1366 x 768. The external display has
270 // size 2560x1600. The total frame buffer is 2560x2428,
271 // where 2428 = 768 + 60 (hidden gap) + 1600
272 // and the second monitor is translated to Point (0, 828) in the
274 DisplayInfo display1
= CreateDisplayInfo(1, 5u, gfx::Rect(0, 0, 1366, 768));
275 DisplayInfo display2
=
276 CreateDisplayInfo(2, 6u, gfx::Rect(0, 828, 2560, 1600));
277 gfx::Size
fb_size(2560, 2428);
279 // Create the touchscreens with the same size as the framebuffer so we can
280 // share the tests between Ozone & X11.
281 ui::TouchscreenDevice touchscreen1
= CreateTouchscreenDevice(5, fb_size
);
282 ui::TouchscreenDevice touchscreen2
= CreateTouchscreenDevice(6, fb_size
);
284 TouchTransformerController
* tt_controller
=
285 Shell::GetInstance()->touch_transformer_controller();
286 ui::DeviceDataManager
* device_manager
= ui::DeviceDataManager::GetInstance();
288 device_manager
->UpdateTouchInfoForDisplay(
289 display1
.id(), display1
.touch_device_id(),
290 tt_controller
->GetTouchTransform(display1
, display1
, touchscreen1
,
293 device_manager
->UpdateTouchInfoForDisplay(
294 display2
.id(), display2
.touch_device_id(),
295 tt_controller
->GetTouchTransform(display2
, display2
, touchscreen2
,
298 EXPECT_EQ(1, device_manager
->GetDisplayForTouchDevice(5));
299 EXPECT_EQ(2, device_manager
->GetDisplayForTouchDevice(6));
301 // Mapping for touch events from internal touch display:
302 // [0, 2560) x [0, 2428) -> [0, 1366) x [0, 768)
305 device_manager
->ApplyTouchTransformer(5, &x
, &y
);
306 EXPECT_NEAR(0, x
, 0.5);
307 EXPECT_NEAR(0, y
, 0.5);
311 device_manager
->ApplyTouchTransformer(5, &x
, &y
);
312 EXPECT_NEAR(1365, x
, 0.5);
313 EXPECT_NEAR(768, y
, 0.5);
315 // Mapping for touch events from external touch display:
316 // [0, 2560) x [0, 2428) -> [0, 2560) x [0, 1600)
319 device_manager
->ApplyTouchTransformer(6, &x
, &y
);
320 #if defined(USE_OZONE)
321 // On ozone we expect screen coordinates so add display origin.
322 EXPECT_NEAR(0 + 0, x
, 0.5);
323 EXPECT_NEAR(0 + 828, y
, 0.5);
325 EXPECT_NEAR(0, x
, 0.5);
326 EXPECT_NEAR(0, y
, 0.5);
331 device_manager
->ApplyTouchTransformer(6, &x
, &y
);
332 #if defined(USE_OZONE)
333 // On ozone we expect screen coordinates so add display origin.
334 EXPECT_NEAR(2559 + 0, x
, 0.5);
335 EXPECT_NEAR(1599 + 828, y
, 0.5);
337 EXPECT_NEAR(2559, x
, 0.5);
338 EXPECT_NEAR(1599, y
, 0.5);
342 TEST_F(TouchTransformerControllerTest
, TouchRadiusScale
) {
343 DisplayInfo display
= CreateDisplayInfo(1, 5u, gfx::Rect(0, 0, 2560, 1600));
344 ui::TouchscreenDevice touch_device
=
345 CreateTouchscreenDevice(5, gfx::Size(1001, 1001));
347 TouchTransformerController
* tt_controller
=
348 Shell::GetInstance()->touch_transformer_controller();
349 // Default touchscreen position range is 1001x1001;
350 EXPECT_EQ(sqrt((2560.0 * 1600.0) / (1001.0 * 1001.0)),
351 tt_controller
->GetTouchResolutionScale(display
, touch_device
));