touch_calibrator_controller_unittest.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // Copyright 2016 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. #include "ash/display/touch_calibrator_controller.h"
  5. #include <vector>
  6. #include "ash/display/touch_calibrator_view.h"
  7. #include "ash/shell.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "ash/touch/ash_touch_transform_controller.h"
  10. #include "base/containers/contains.h"
  11. #include "ui/display/display.h"
  12. #include "ui/display/manager/display_manager.h"
  13. #include "ui/display/manager/test/touch_device_manager_test_api.h"
  14. #include "ui/display/manager/test/touch_transform_controller_test_api.h"
  15. #include "ui/display/manager/touch_device_manager.h"
  16. #include "ui/display/manager/touch_transform_setter.h"
  17. #include "ui/display/test/display_manager_test_api.h"
  18. #include "ui/events/base_event_utils.h"
  19. #include "ui/events/devices/device_data_manager_test_api.h"
  20. #include "ui/events/devices/touch_device_transform.h"
  21. #include "ui/events/event_handler.h"
  22. #include "ui/events/test/event_generator.h"
  23. #include "ui/events/test/events_test_utils.h"
  24. #include "ui/views/widget/unique_widget_ptr.h"
  25. #include "ui/views/widget/widget.h"
  26. namespace ash {
  27. namespace {
  28. ui::TouchscreenDevice GetInternalTouchDevice(int touch_device_id) {
  29. return ui::TouchscreenDevice(
  30. touch_device_id, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
  31. std::string("test internal touch device"), gfx::Size(1000, 1000), 1);
  32. }
  33. ui::TouchscreenDevice GetExternalTouchDevice(int touch_device_id) {
  34. return ui::TouchscreenDevice(
  35. touch_device_id, ui::InputDeviceType::INPUT_DEVICE_USB,
  36. std::string("test external touch device"), gfx::Size(1000, 1000), 1);
  37. }
  38. } // namespace
  39. class TouchCalibratorControllerTest : public AshTestBase {
  40. public:
  41. TouchCalibratorControllerTest() = default;
  42. TouchCalibratorControllerTest(const TouchCalibratorControllerTest&) = delete;
  43. TouchCalibratorControllerTest& operator=(
  44. const TouchCalibratorControllerTest&) = delete;
  45. void TearDown() override {
  46. // Reset all touch device and touch association.
  47. display::test::TouchDeviceManagerTestApi(touch_device_manager())
  48. .ResetTouchDeviceManager();
  49. ui::DeviceDataManagerTestApi().SetTouchscreenDevices({});
  50. display::test::TouchTransformControllerTestApi(
  51. Shell::Get()->touch_transformer_controller())
  52. .touch_transform_setter()
  53. ->ConfigureTouchDevices(std::vector<ui::TouchDeviceTransform>());
  54. AshTestBase::TearDown();
  55. }
  56. display::TouchDeviceManager* touch_device_manager() {
  57. return display_manager()->touch_device_manager();
  58. }
  59. int GetTouchDeviceId(const TouchCalibratorController& ctrl) {
  60. return ctrl.touch_device_id_;
  61. }
  62. const TouchCalibratorController::CalibrationPointPairQuad& GetTouchPointQuad(
  63. const TouchCalibratorController& ctrl) {
  64. return ctrl.touch_point_quad_;
  65. }
  66. std::map<int64_t, views::UniqueWidgetPtr>& GetCalibratorViews(
  67. TouchCalibratorController* ctrl) {
  68. return ctrl->touch_calibrator_widgets_;
  69. }
  70. const display::Display& InitDisplays() {
  71. // Initialize 2 displays each with resolution 600x500.
  72. UpdateDisplay("600x500,600x500");
  73. // Assuming index 0 points to the native display, we will calibrate the
  74. // touch display at index 1.
  75. const int kTargetDisplayIndex = 1;
  76. display::DisplayIdList display_id_list =
  77. display_manager()->GetConnectedDisplayIdList();
  78. int64_t target_display_id = display_id_list[kTargetDisplayIndex];
  79. const display::Display& touch_display =
  80. display_manager()->GetDisplayForId(target_display_id);
  81. return touch_display;
  82. }
  83. void StartCalibrationChecks(TouchCalibratorController* ctrl,
  84. const display::Display& target_display) {
  85. EXPECT_FALSE(ctrl->IsCalibrating());
  86. EXPECT_FALSE(!!ctrl->touch_calibrator_widgets_.size());
  87. TouchCalibratorController::TouchCalibrationCallback empty_callback;
  88. ctrl->StartCalibration(target_display, false /* is_custom_calibration */,
  89. std::move(empty_callback));
  90. EXPECT_TRUE(ctrl->IsCalibrating());
  91. EXPECT_EQ(ctrl->state_,
  92. TouchCalibratorController::CalibrationState::kNativeCalibration);
  93. // There should be a touch calibrator view associated with each of the
  94. // active displays.
  95. EXPECT_EQ(ctrl->touch_calibrator_widgets_.size(),
  96. display_manager()->GetConnectedDisplayIdList().size());
  97. TouchCalibratorView* target_calibrator_view =
  98. static_cast<TouchCalibratorView*>(
  99. GetCalibratorViews(ctrl)[target_display.id()]->GetContentsView());
  100. // End the background fade in animation.
  101. target_calibrator_view->SkipCurrentAnimation();
  102. // TouchCalibratorView on the display being calibrated should be at the
  103. // state where the first display point is visible.
  104. EXPECT_EQ(target_calibrator_view->state(),
  105. TouchCalibratorView::DISPLAY_POINT_1);
  106. }
  107. // Resets the timestamp threshold so that touch events are not discarded.
  108. void ResetTimestampThreshold(TouchCalibratorController* ctrl) {
  109. base::Time current = base::Time::Now();
  110. ctrl->last_touch_timestamp_ =
  111. current - TouchCalibratorController::kTouchIntervalThreshold;
  112. }
  113. // Generates a touch press and release event in the |display| with source
  114. // device id as |touch_device_id|.
  115. void GenerateTouchEvent(const display::Display& display,
  116. int touch_device_id,
  117. const gfx::Point& location = gfx::Point(20, 20)) {
  118. // Get the correct EventTarget for the given |display|.
  119. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  120. ui::EventTarget* event_target = nullptr;
  121. for (auto* window : root_windows) {
  122. if (display::Screen::GetScreen()->GetDisplayNearestWindow(window).id() ==
  123. display.id()) {
  124. event_target = window;
  125. break;
  126. }
  127. }
  128. ui::test::EventGenerator* eg = GetEventGenerator();
  129. eg->set_current_target(event_target);
  130. ui::TouchEvent press_touch_event(
  131. ui::ET_TOUCH_PRESSED, location, ui::EventTimeForNow(),
  132. ui::PointerDetails(ui::EventPointerType::kTouch, 12, 1.0f, 1.0f, 0.0f),
  133. 0);
  134. ui::TouchEvent release_touch_event(
  135. ui::ET_TOUCH_RELEASED, location, ui::EventTimeForNow(),
  136. ui::PointerDetails(ui::EventPointerType::kTouch, 12, 1.0f, 1.0f, 0.0f),
  137. 0);
  138. press_touch_event.set_source_device_id(touch_device_id);
  139. release_touch_event.set_source_device_id(touch_device_id);
  140. eg->Dispatch(&press_touch_event);
  141. eg->Dispatch(&release_touch_event);
  142. }
  143. ui::TouchscreenDevice InitTouchDevice(
  144. int64_t display_id,
  145. const ui::TouchscreenDevice& touchdevice) {
  146. ui::DeviceDataManagerTestApi().SetTouchscreenDevices({touchdevice});
  147. std::vector<ui::TouchDeviceTransform> transforms;
  148. ui::TouchDeviceTransform touch_device_transform;
  149. touch_device_transform.display_id = display_id;
  150. touch_device_transform.device_id = touchdevice.id;
  151. transforms.push_back(touch_device_transform);
  152. // This makes touchscreen target displays valid for ui::DeviceDataManager.
  153. display::test::TouchTransformControllerTestApi(
  154. Shell::Get()->touch_transformer_controller())
  155. .touch_transform_setter()
  156. ->ConfigureTouchDevices(transforms);
  157. return touchdevice;
  158. }
  159. };
  160. TEST_F(TouchCalibratorControllerTest, StartCalibration) {
  161. const display::Display& touch_display = InitDisplays();
  162. TouchCalibratorController touch_calibrator_controller;
  163. StartCalibrationChecks(&touch_calibrator_controller, touch_display);
  164. ui::EventTargetTestApi test_api(Shell::Get());
  165. ui::EventHandlerList handlers = test_api.GetPreTargetHandlers();
  166. EXPECT_TRUE(base::Contains(handlers, &touch_calibrator_controller));
  167. }
  168. TEST_F(TouchCalibratorControllerTest, KeyEventIntercept) {
  169. const display::Display& touch_display = InitDisplays();
  170. TouchCalibratorController touch_calibrator_controller;
  171. StartCalibrationChecks(&touch_calibrator_controller, touch_display);
  172. EXPECT_TRUE(touch_calibrator_controller.IsCalibrating());
  173. PressAndReleaseKey(ui::VKEY_ESCAPE);
  174. EXPECT_FALSE(touch_calibrator_controller.IsCalibrating());
  175. }
  176. TEST_F(TouchCalibratorControllerTest, TouchThreshold) {
  177. const display::Display& touch_display = InitDisplays();
  178. TouchCalibratorController touch_calibrator_controller;
  179. StartCalibrationChecks(&touch_calibrator_controller, touch_display);
  180. // Initialize touch device so that |event_transnformer_| can be computed.
  181. ui::TouchscreenDevice touchdevice =
  182. InitTouchDevice(touch_display.id(), GetExternalTouchDevice(12));
  183. base::Time current_timestamp = base::Time::Now();
  184. touch_calibrator_controller.last_touch_timestamp_ = current_timestamp;
  185. // This touch event should be rejected as the time threshold has not been
  186. // crossed.
  187. GenerateTouchEvent(touch_display, touchdevice.id);
  188. EXPECT_EQ(touch_calibrator_controller.last_touch_timestamp_,
  189. current_timestamp);
  190. ResetTimestampThreshold(&touch_calibrator_controller);
  191. // This time the events should be registered as the threshold is crossed.
  192. GenerateTouchEvent(touch_display, touchdevice.id);
  193. EXPECT_LT(current_timestamp,
  194. touch_calibrator_controller.last_touch_timestamp_);
  195. }
  196. TEST_F(TouchCalibratorControllerTest, TouchDeviceIdIsSet) {
  197. const display::Display& touch_display = InitDisplays();
  198. TouchCalibratorController touch_calibrator_controller;
  199. StartCalibrationChecks(&touch_calibrator_controller, touch_display);
  200. ui::TouchscreenDevice touchdevice =
  201. InitTouchDevice(touch_display.id(), GetExternalTouchDevice(12));
  202. ResetTimestampThreshold(&touch_calibrator_controller);
  203. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  204. ui::InputDevice::kInvalidId);
  205. GenerateTouchEvent(touch_display, touchdevice.id);
  206. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller), touchdevice.id);
  207. }
  208. TEST_F(TouchCalibratorControllerTest, CustomCalibration) {
  209. const display::Display& touch_display = InitDisplays();
  210. TouchCalibratorController touch_calibrator_controller;
  211. EXPECT_FALSE(touch_calibrator_controller.IsCalibrating());
  212. EXPECT_FALSE(!!GetCalibratorViews(&touch_calibrator_controller).size());
  213. touch_calibrator_controller.StartCalibration(
  214. touch_display, true /* is_custom_calibbration */,
  215. TouchCalibratorController::TouchCalibrationCallback());
  216. ui::TouchscreenDevice touchdevice =
  217. InitTouchDevice(touch_display.id(), GetExternalTouchDevice(12));
  218. EXPECT_TRUE(touch_calibrator_controller.IsCalibrating());
  219. EXPECT_EQ(touch_calibrator_controller.state_,
  220. TouchCalibratorController::CalibrationState::kCustomCalibration);
  221. // Native touch calibration UX should not initialize during custom calibration
  222. EXPECT_EQ(GetCalibratorViews(&touch_calibrator_controller).size(), 0UL);
  223. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  224. ui::InputDevice::kInvalidId);
  225. ResetTimestampThreshold(&touch_calibrator_controller);
  226. GenerateTouchEvent(touch_display, touchdevice.id);
  227. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller), touchdevice.id);
  228. display::TouchCalibrationData::CalibrationPointPairQuad points = {
  229. {std::make_pair(gfx::Point(10, 10), gfx::Point(11, 12)),
  230. std::make_pair(gfx::Point(190, 10), gfx::Point(195, 8)),
  231. std::make_pair(gfx::Point(10, 90), gfx::Point(12, 94)),
  232. std::make_pair(gfx::Point(190, 90), gfx::Point(189, 88))}};
  233. gfx::Size size(200, 100);
  234. display::TouchCalibrationData calibration_data(points, size);
  235. touch_calibrator_controller.CompleteCalibration(points, size);
  236. const display::ManagedDisplayInfo& info =
  237. display_manager()->GetDisplayInfo(touch_display.id());
  238. display::test::TouchDeviceManagerTestApi tdm_test_api(touch_device_manager());
  239. EXPECT_TRUE(tdm_test_api.AreAssociated(info, touchdevice));
  240. EXPECT_EQ(calibration_data,
  241. touch_device_manager()->GetCalibrationData(touchdevice, info.id()));
  242. }
  243. TEST_F(TouchCalibratorControllerTest, CustomCalibrationInvalidTouchId) {
  244. const display::Display& touch_display = InitDisplays();
  245. TouchCalibratorController touch_calibrator_controller;
  246. EXPECT_FALSE(touch_calibrator_controller.IsCalibrating());
  247. EXPECT_FALSE(!!GetCalibratorViews(&touch_calibrator_controller).size());
  248. touch_calibrator_controller.StartCalibration(
  249. touch_display, true /* is_custom_calibbration */,
  250. TouchCalibratorController::TouchCalibrationCallback());
  251. EXPECT_TRUE(touch_calibrator_controller.IsCalibrating());
  252. EXPECT_EQ(touch_calibrator_controller.state_,
  253. TouchCalibratorController::CalibrationState::kCustomCalibration);
  254. // Native touch calibration UX should not initialize during custom calibration
  255. EXPECT_EQ(GetCalibratorViews(&touch_calibrator_controller).size(), 0UL);
  256. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  257. ui::InputDevice::kInvalidId);
  258. ResetTimestampThreshold(&touch_calibrator_controller);
  259. display::TouchCalibrationData::CalibrationPointPairQuad points = {
  260. {std::make_pair(gfx::Point(10, 10), gfx::Point(11, 12)),
  261. std::make_pair(gfx::Point(190, 10), gfx::Point(195, 8)),
  262. std::make_pair(gfx::Point(10, 90), gfx::Point(12, 94)),
  263. std::make_pair(gfx::Point(190, 90), gfx::Point(189, 88))}};
  264. gfx::Size size(200, 100);
  265. display::TouchCalibrationData calibration_data(points, size);
  266. touch_calibrator_controller.CompleteCalibration(points, size);
  267. const display::ManagedDisplayInfo& info =
  268. display_manager()->GetDisplayInfo(touch_display.id());
  269. ui::TouchscreenDevice random_touchdevice(
  270. 15, ui::InputDeviceType::INPUT_DEVICE_USB,
  271. std::string("random touch device"), gfx::Size(123, 456), 1);
  272. EXPECT_EQ(calibration_data, touch_device_manager()->GetCalibrationData(
  273. random_touchdevice, info.id()));
  274. }
  275. TEST_F(TouchCalibratorControllerTest, IgnoreInternalTouchDevices) {
  276. const display::Display& touch_display = InitDisplays();
  277. // We need to initialize a touch device before starting calibration so that
  278. // the set |internal_touch_device_ids_| can be initialized.
  279. ui::TouchscreenDevice internal_touchdevice =
  280. InitTouchDevice(touch_display.id(), GetInternalTouchDevice(12));
  281. TouchCalibratorController touch_calibrator_controller;
  282. StartCalibrationChecks(&touch_calibrator_controller, touch_display);
  283. // We need to reinitialize the touch device as Starting calibration resets
  284. // everything.
  285. internal_touchdevice =
  286. InitTouchDevice(touch_display.id(), GetInternalTouchDevice(12));
  287. ResetTimestampThreshold(&touch_calibrator_controller);
  288. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  289. ui::InputDevice::kInvalidId);
  290. GenerateTouchEvent(touch_display, internal_touchdevice.id);
  291. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  292. ui::InputDevice::kInvalidId);
  293. ui::TouchscreenDevice external_touchdevice =
  294. InitTouchDevice(touch_display.id(), GetExternalTouchDevice(13));
  295. ResetTimestampThreshold(&touch_calibrator_controller);
  296. GenerateTouchEvent(touch_display, external_touchdevice.id);
  297. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  298. external_touchdevice.id);
  299. }
  300. TEST_F(TouchCalibratorControllerTest, HighDPIMonitorsCalibration) {
  301. // Initialize 3 displays each with different device scale factors.
  302. UpdateDisplay("600x500*2,400x300*3,600x500*1.5");
  303. // Index 0 points to the native internal display, we will calibrate the touch
  304. // display at index 2.
  305. const int kTargetDisplayIndex = 2;
  306. display::DisplayIdList display_id_list =
  307. display_manager()->GetConnectedDisplayIdList();
  308. int64_t internal_display_id = display_id_list[1];
  309. display::test::ScopedSetInternalDisplayId set_internal(display_manager(),
  310. internal_display_id);
  311. int64_t target_display_id = display_id_list[kTargetDisplayIndex];
  312. const display::Display& touch_display =
  313. display_manager()->GetDisplayForId(target_display_id);
  314. // We create 2 touch devices.
  315. const int kInternalTouchId = 10;
  316. const int kExternalTouchId = 11;
  317. ui::TouchscreenDevice internal_touchdevice(
  318. kInternalTouchId, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
  319. std::string("internal touch device"), gfx::Size(1000, 1000), 1);
  320. ui::TouchscreenDevice external_touchdevice(
  321. kExternalTouchId, ui::InputDeviceType::INPUT_DEVICE_USB,
  322. std::string("external touch device"), gfx::Size(1000, 1000), 1);
  323. ui::DeviceDataManagerTestApi().SetTouchscreenDevices(
  324. {internal_touchdevice, external_touchdevice});
  325. // Associate both touch devices to the internal display.
  326. std::vector<ui::TouchDeviceTransform> transforms;
  327. ui::TouchDeviceTransform touch_device_transform;
  328. touch_device_transform.display_id = internal_display_id;
  329. touch_device_transform.device_id = internal_touchdevice.id;
  330. transforms.push_back(touch_device_transform);
  331. touch_device_transform.device_id = external_touchdevice.id;
  332. transforms.push_back(touch_device_transform);
  333. display::test::TouchTransformControllerTestApi(
  334. Shell::Get()->touch_transformer_controller())
  335. .touch_transform_setter()
  336. ->ConfigureTouchDevices(transforms);
  337. // Update touch device manager with the associations.
  338. display::test::TouchDeviceManagerTestApi(touch_device_manager())
  339. .Associate(internal_display_id, internal_touchdevice);
  340. display::test::TouchDeviceManagerTestApi(touch_device_manager())
  341. .Associate(internal_display_id, external_touchdevice);
  342. TouchCalibratorController touch_calibrator_controller;
  343. touch_calibrator_controller.StartCalibration(
  344. touch_display, false /* is_custom_calibbration */,
  345. TouchCalibratorController::TouchCalibrationCallback());
  346. // Skip any UI animations associated with the start of calibration.
  347. static_cast<TouchCalibratorView*>(
  348. GetCalibratorViews(&touch_calibrator_controller)[touch_display.id()]
  349. ->GetContentsView())
  350. ->SkipCurrentAnimation();
  351. // Reinitialize the transforms, as starting calibration resets them.
  352. display::test::TouchTransformControllerTestApi(
  353. Shell::Get()->touch_transformer_controller())
  354. .touch_transform_setter()
  355. ->ConfigureTouchDevices(transforms);
  356. // The touch device id has not been set yet, as the first touch event has not
  357. // been generated.
  358. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  359. ui::InputDevice::kInvalidId);
  360. ResetTimestampThreshold(&touch_calibrator_controller);
  361. // Generate a touch event at point (100, 100) on the external touch device.
  362. gfx::Point touch_point(100, 100);
  363. GenerateTouchEvent(display_manager()->GetDisplayForId(internal_display_id),
  364. external_touchdevice.id, touch_point);
  365. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  366. external_touchdevice.id);
  367. // The touch event should be received as is.
  368. EXPECT_EQ(GetTouchPointQuad(touch_calibrator_controller).at(0).second,
  369. gfx::Point(100, 100));
  370. // The display point should have the root transform applied.
  371. EXPECT_EQ(GetTouchPointQuad(touch_calibrator_controller).at(0).first,
  372. gfx::Point(210, 210));
  373. }
  374. TEST_F(TouchCalibratorControllerTest, RotatedHighDPIMonitorsCalibration) {
  375. // Initialize 2 displays each with resolution 500x500. One of them at 2x
  376. // device scale factor.
  377. UpdateDisplay("600x500*2,600x500*1.5/r");
  378. // Index 0 points to the native internal display, we will calibrate the touch
  379. // display at index 1.
  380. const int kTargetDisplayIndex = 1;
  381. display::DisplayIdList display_id_list =
  382. display_manager()->GetConnectedDisplayIdList();
  383. int64_t internal_display_id = display_id_list[0];
  384. display::test::ScopedSetInternalDisplayId set_internal(display_manager(),
  385. internal_display_id);
  386. int64_t target_display_id = display_id_list[kTargetDisplayIndex];
  387. const display::Display& touch_display =
  388. display_manager()->GetDisplayForId(target_display_id);
  389. // We create 2 touch devices.
  390. const int kInternalTouchId = 10;
  391. const int kExternalTouchId = 11;
  392. ui::TouchscreenDevice internal_touchdevice(
  393. kInternalTouchId, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
  394. std::string("internal touch device"), gfx::Size(1000, 1000), 1);
  395. ui::TouchscreenDevice external_touchdevice(
  396. kExternalTouchId, ui::InputDeviceType::INPUT_DEVICE_USB,
  397. std::string("external touch device"), gfx::Size(1000, 1000), 1);
  398. ui::DeviceDataManagerTestApi().SetTouchscreenDevices(
  399. {internal_touchdevice, external_touchdevice});
  400. // Associate both touch devices to the internal display.
  401. std::vector<ui::TouchDeviceTransform> transforms;
  402. ui::TouchDeviceTransform touch_device_transform;
  403. touch_device_transform.display_id = internal_display_id;
  404. touch_device_transform.device_id = internal_touchdevice.id;
  405. transforms.push_back(touch_device_transform);
  406. touch_device_transform.device_id = external_touchdevice.id;
  407. transforms.push_back(touch_device_transform);
  408. display::test::TouchTransformControllerTestApi(
  409. Shell::Get()->touch_transformer_controller())
  410. .touch_transform_setter()
  411. ->ConfigureTouchDevices(transforms);
  412. // Update touch device manager with the associations.
  413. display::test::TouchDeviceManagerTestApi(touch_device_manager())
  414. .Associate(internal_display_id, internal_touchdevice);
  415. display::test::TouchDeviceManagerTestApi(touch_device_manager())
  416. .Associate(internal_display_id, external_touchdevice);
  417. TouchCalibratorController touch_calibrator_controller;
  418. touch_calibrator_controller.StartCalibration(
  419. touch_display, false /* is_custom_calibbration */,
  420. TouchCalibratorController::TouchCalibrationCallback());
  421. // Skip any UI animations associated with the start of calibration.
  422. static_cast<TouchCalibratorView*>(
  423. GetCalibratorViews(&touch_calibrator_controller)[touch_display.id()]
  424. ->GetContentsView())
  425. ->SkipCurrentAnimation();
  426. // Reinitialize the transforms, as starting calibration resets them.
  427. display::test::TouchTransformControllerTestApi(
  428. Shell::Get()->touch_transformer_controller())
  429. .touch_transform_setter()
  430. ->ConfigureTouchDevices(transforms);
  431. // The touch device id has not been set yet, as the first touch event has not
  432. // been generated.
  433. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  434. ui::InputDevice::kInvalidId);
  435. ResetTimestampThreshold(&touch_calibrator_controller);
  436. // Generate a touch event at point (100, 100) on the external touch device.
  437. gfx::Point touch_point(100, 100);
  438. // Simulate the touch event for external touch device. Since the device is
  439. // currently associated with the internal display, we generate the touch event
  440. // for the internal display.
  441. GenerateTouchEvent(display_manager()->GetDisplayForId(internal_display_id),
  442. external_touchdevice.id, touch_point);
  443. EXPECT_EQ(GetTouchDeviceId(touch_calibrator_controller),
  444. external_touchdevice.id);
  445. // The touch event should now be within the bounds the the target display.
  446. EXPECT_EQ(GetTouchPointQuad(touch_calibrator_controller).at(0).second,
  447. gfx::Point(100, 100));
  448. // The display point should have the root transform applied.
  449. EXPECT_EQ(GetTouchPointQuad(touch_calibrator_controller).at(0).first,
  450. gfx::Point(390, 210));
  451. }
  452. TEST_F(TouchCalibratorControllerTest, InternalTouchDeviceIsRejected) {
  453. const display::Display& touch_display = InitDisplays();
  454. // We need to initialize a touch device before starting calibration so that
  455. // the set |internal_touch_device_ids_| can be initialized.
  456. ui::TouchscreenDevice internal_touchdevice =
  457. InitTouchDevice(touch_display.id(), GetInternalTouchDevice(12));
  458. TouchCalibratorController touch_calibrator_controller;
  459. touch_calibrator_controller.touch_device_id_ = internal_touchdevice.id;
  460. touch_calibrator_controller.target_display_ = touch_display;
  461. display::TouchCalibrationData::CalibrationPointPairQuad points = {
  462. {std::make_pair(gfx::Point(10, 10), gfx::Point(11, 12)),
  463. std::make_pair(gfx::Point(190, 10), gfx::Point(195, 8)),
  464. std::make_pair(gfx::Point(10, 90), gfx::Point(12, 94)),
  465. std::make_pair(gfx::Point(190, 90), gfx::Point(189, 88))}};
  466. gfx::Size size(200, 100);
  467. display::TouchCalibrationData calibration_data(points, size);
  468. touch_calibrator_controller.CompleteCalibration(points, size);
  469. const display::ManagedDisplayInfo& info =
  470. display_manager()->GetDisplayInfo(touch_display.id());
  471. display::test::TouchDeviceManagerTestApi tdm_test_api(touch_device_manager());
  472. EXPECT_FALSE(tdm_test_api.AreAssociated(info, internal_touchdevice));
  473. EXPECT_TRUE(touch_device_manager()
  474. ->GetCalibrationData(internal_touchdevice, info.id())
  475. .IsEmpty());
  476. }
  477. } // namespace ash