Implement listing tests to a JSON file for iOS gtest test launcher
[chromium-blink-merge.git] / ash / touch / touch_transformer_controller_unittest.cc
blobf733a059eb14a70830fe1a60bbea98f2b0f54579
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.set_display_modes(default_modes);
30 return info;
33 ui::TouchscreenDevice CreateTouchscreenDevice(unsigned int id,
34 const gfx::Size& size) {
35 return ui::TouchscreenDevice(id,
36 ui::InputDeviceType::INPUT_DEVICE_EXTERNAL,
37 std::string(),
38 size);
41 } // namespace
43 typedef test::AshTestBase TouchTransformerControllerTest;
45 TEST_F(TouchTransformerControllerTest, TouchTransformerMirrorModeLetterboxing) {
46 // The internal display has native resolution of 2560x1700, and in
47 // mirror mode it is configured as 1920x1200. This is in letterboxing
48 // mode.
49 DisplayInfo internal_display_info =
50 CreateDisplayInfo(1, 10u, gfx::Rect(0, 0, 1920, 1200));
51 internal_display_info.set_is_aspect_preserving_scaling(true);
52 std::vector<DisplayMode> internal_modes;
53 internal_modes.push_back(
54 DisplayMode(gfx::Size(2560, 1700), 60, false, true));
55 internal_modes.push_back(
56 DisplayMode(gfx::Size(1920, 1200), 60, false, false));
57 internal_display_info.set_display_modes(internal_modes);
59 DisplayInfo external_display_info =
60 CreateDisplayInfo(2, 11u, gfx::Rect(0, 0, 1920, 1200));
62 gfx::Size fb_size(1920, 1200);
64 // Create the touchscreens with the same size as the framebuffer so we can
65 // share the tests between Ozone & X11.
66 ui::TouchscreenDevice internal_touchscreen =
67 CreateTouchscreenDevice(10, fb_size);
68 ui::TouchscreenDevice external_touchscreen =
69 CreateTouchscreenDevice(11, fb_size);
71 TouchTransformerController* tt_controller =
72 Shell::GetInstance()->touch_transformer_controller();
73 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
75 device_manager->UpdateTouchInfoForDisplay(
76 internal_display_info.id(),
77 internal_display_info.touch_device_id(),
78 tt_controller->GetTouchTransform(internal_display_info,
79 internal_touchscreen,
80 fb_size));
82 device_manager->UpdateTouchInfoForDisplay(
83 external_display_info.id(),
84 external_display_info.touch_device_id(),
85 tt_controller->GetTouchTransform(external_display_info,
86 external_touchscreen,
87 fb_size));
89 EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(10));
90 EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(11));
92 // External touch display has the default TouchTransformer.
93 float x = 100.0;
94 float y = 100.0;
95 device_manager->ApplyTouchTransformer(11, &x, &y);
96 EXPECT_EQ(100, x);
97 EXPECT_EQ(100, y);
99 // In letterboxing, there is (1-2560*(1200/1920)/1700)/2 = 2.95% of the
100 // height on both the top & bottom region of the screen is blank.
101 // When touch events coming at Y range [0, 1200), the mapping should be
102 // [0, ~35] ---> < 0
103 // [~35, ~1165] ---> [0, 1200)
104 // [~1165, 1200] ---> >= 1200
105 x = 100.0;
106 y = 35.0;
107 device_manager->ApplyTouchTransformer(10, &x, &y);
108 EXPECT_EQ(100, static_cast<int>(x));
109 EXPECT_EQ(0, static_cast<int>(y));
111 x = 100.0;
112 y = 1165.0;
113 device_manager->ApplyTouchTransformer(10, &x, &y);
114 EXPECT_EQ(100, static_cast<int>(x));
115 EXPECT_EQ(1200, static_cast<int>(y));
118 TEST_F(TouchTransformerControllerTest, TouchTransformerMirrorModePillarboxing) {
119 // The internal display has native resolution of 1366x768, and in
120 // mirror mode it is configured as 1024x768. This is in pillarboxing
121 // mode.
122 DisplayInfo internal_display_info =
123 CreateDisplayInfo(1, 10, gfx::Rect(0, 0, 1024, 768));
124 internal_display_info.set_is_aspect_preserving_scaling(true);
125 std::vector<DisplayMode> internal_modes;
126 internal_modes.push_back(
127 DisplayMode(gfx::Size(1366, 768), 60, false, true));
128 internal_modes.push_back(
129 DisplayMode(gfx::Size(1024, 768), 60, false, false));
130 internal_display_info.set_display_modes(internal_modes);
132 DisplayInfo external_display_info =
133 CreateDisplayInfo(2, 11, gfx::Rect(0, 0, 1024, 768));
135 gfx::Size fb_size(1024, 768);
137 // Create the touchscreens with the same size as the framebuffer so we can
138 // share the tests between Ozone & X11.
139 ui::TouchscreenDevice internal_touchscreen =
140 CreateTouchscreenDevice(10, fb_size);
141 ui::TouchscreenDevice external_touchscreen =
142 CreateTouchscreenDevice(11, fb_size);
144 TouchTransformerController* tt_controller =
145 Shell::GetInstance()->touch_transformer_controller();
146 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
148 device_manager->UpdateTouchInfoForDisplay(
149 internal_display_info.id(),
150 internal_display_info.touch_device_id(),
151 tt_controller->GetTouchTransform(internal_display_info,
152 internal_touchscreen,
153 fb_size));
155 device_manager->UpdateTouchInfoForDisplay(
156 external_display_info.id(),
157 external_display_info.touch_device_id(),
158 tt_controller->GetTouchTransform(external_display_info,
159 external_touchscreen,
160 fb_size));
162 EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(10));
163 EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(11));
165 // External touch display has the default TouchTransformer.
166 float x = 100.0;
167 float y = 100.0;
168 device_manager->ApplyTouchTransformer(11, &x, &y);
169 EXPECT_EQ(100, x);
170 EXPECT_EQ(100, y);
172 // In pillarboxing, there is (1-768*(1024/768)/1366)/2 = 12.5% of the
173 // width on both the left & rigth region of the screen is blank.
174 // When touch events coming at X range [0, 1024), the mapping should be
175 // [0, ~128] ---> < 0
176 // [~128, ~896] ---> [0, 1024)
177 // [~896, 1024] ---> >= 1024
178 x = 128.0;
179 y = 100.0;
180 device_manager->ApplyTouchTransformer(10, &x, &y);
181 EXPECT_EQ(0, static_cast<int>(x));
182 EXPECT_EQ(100, static_cast<int>(y));
184 x = 896.0;
185 y = 100.0;
186 device_manager->ApplyTouchTransformer(10, &x, &y);
187 EXPECT_EQ(1024, static_cast<int>(x));
188 EXPECT_EQ(100, static_cast<int>(y));
191 TEST_F(TouchTransformerControllerTest, TouchTransformerExtendedMode) {
192 // The internal display has size 1366 x 768. The external display has
193 // size 2560x1600. The total frame buffer is 2560x2428,
194 // where 2428 = 768 + 60 (hidden gap) + 1600
195 // and the sceond monitor is translated to Point (0, 828) in the
196 // framebuffer.
197 DisplayInfo display1 = CreateDisplayInfo(1, 5u, gfx::Rect(0, 0, 1366, 768));
198 DisplayInfo display2 =
199 CreateDisplayInfo(2, 6u, gfx::Rect(0, 828, 2560, 1600));
200 gfx::Size fb_size(2560, 2428);
202 // Create the touchscreens with the same size as the framebuffer so we can
203 // share the tests between Ozone & X11.
204 ui::TouchscreenDevice touchscreen1 = CreateTouchscreenDevice(5, fb_size);
205 ui::TouchscreenDevice touchscreen2 = CreateTouchscreenDevice(6, fb_size);
207 TouchTransformerController* tt_controller =
208 Shell::GetInstance()->touch_transformer_controller();
209 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
211 device_manager->UpdateTouchInfoForDisplay(
212 display1.id(),
213 display1.touch_device_id(),
214 tt_controller->GetTouchTransform(display1,
215 touchscreen1,
216 fb_size));
218 device_manager->UpdateTouchInfoForDisplay(
219 display2.id(),
220 display2.touch_device_id(),
221 tt_controller->GetTouchTransform(display2,
222 touchscreen2,
223 fb_size));
225 EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(5));
226 EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(6));
228 // Mapping for touch events from internal touch display:
229 // [0, 2560) x [0, 2428) -> [0, 1366) x [0, 768)
230 float x = 0.0;
231 float y = 0.0;
232 device_manager->ApplyTouchTransformer(5, &x, &y);
233 EXPECT_EQ(0, static_cast<int>(x));
234 EXPECT_EQ(0, static_cast<int>(y));
236 x = 2559.0;
237 y = 2427.0;
238 device_manager->ApplyTouchTransformer(5, &x, &y);
239 EXPECT_EQ(1365, static_cast<int>(x));
240 EXPECT_EQ(767, static_cast<int>(y));
242 // Mapping for touch events from external touch display:
243 // [0, 2560) x [0, 2428) -> [0, 2560) x [0, 1600)
244 x = 0.0;
245 y = 0.0;
246 device_manager->ApplyTouchTransformer(6, &x, &y);
247 EXPECT_EQ(0, static_cast<int>(x));
248 EXPECT_EQ(0, static_cast<int>(y));
250 x = 2559.0;
251 y = 2427.0;
252 device_manager->ApplyTouchTransformer(6, &x, &y);
253 EXPECT_EQ(2559, static_cast<int>(x));
254 EXPECT_EQ(1599, static_cast<int>(y));
257 TEST_F(TouchTransformerControllerTest, TouchRadiusScale) {
258 DisplayInfo display = CreateDisplayInfo(1, 5u, gfx::Rect(0, 0, 2560, 1600));
259 ui::TouchscreenDevice touch_device =
260 CreateTouchscreenDevice(5, gfx::Size(1001, 1001));
262 TouchTransformerController* tt_controller =
263 Shell::GetInstance()->touch_transformer_controller();
264 // Default touchscreen position range is 1001x1001;
265 EXPECT_EQ(sqrt((2560.0 * 1600.0) / (1001.0 * 1001.0)),
266 tt_controller->GetTouchResolutionScale(display, touch_device));
269 } // namespace ash