File: touch_transform_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (903 lines) | stat: -rw-r--r-- 35,491 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/display/manager/touch_transform_controller.h"

#include <memory>
#include <string>
#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/manager/default_touch_transform_setter.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/manager/test/touch_device_manager_test_api.h"
#include "ui/display/manager/touch_device_manager.h"
#include "ui/display/screen_base.h"
#include "ui/events/devices/device_data_manager.h"

namespace display::test {

namespace {

constexpr int kDisplayId1 = 1;
constexpr int kDisplayId2 = 2;
constexpr int kTouchId1 = 5;
constexpr int kTouchId2 = 6;

ui::TouchDeviceTransform CreateTouchDeviceTransform(
    int64_t display_id,
    int32_t device_id,
    const gfx::Transform& transform,
    double radius_scale = 1.0) {
  ui::TouchDeviceTransform touch_device_transform;
  touch_device_transform.display_id = display_id;
  touch_device_transform.device_id = device_id;
  touch_device_transform.transform = transform;
  touch_device_transform.radius_scale = radius_scale;
  return touch_device_transform;
}

ui::TouchscreenDevice CreateTouchscreenDevice(unsigned int id,
                                              const gfx::Size& size) {
  return ui::TouchscreenDevice(id, ui::InputDeviceType::INPUT_DEVICE_USB,
                               std::string(), size, 0);
}

std::string GetTouchPointString(
    const TouchCalibrationData::CalibrationPointPairQuad& pts) {
  std::string str = "Failed for point pairs: ";
  for (std::size_t row = 0; row < pts.size(); row++) {
    str += "{(" + base::NumberToString(pts[row].first.x()) + "," +
           base::NumberToString(pts[row].first.y()) + "), (" +
           base::NumberToString(pts[row].second.x()) + "," +
           base::NumberToString(pts[row].second.y()) + ")} ";
  }
  return str;
}

// Checks if the touch input has been calibrated properly. The input is said to
// be calibrated if any touch input is transformed to the correct corresponding
// display point within an error delta of |max_error_delta.width()| along the X
// axis and |max_error_delta.height()| along the Y axis;
void CheckPointsOfInterests(const int touch_id,
                            const gfx::Size& touch_size,
                            const gfx::Size& display_size,
                            const gfx::Size& max_error_delta,
                            const std::string& error_msg) {
  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  float x, y;

  // Origin of the touch device should correspond to origin of the display.
  x = y = 0.0;
  device_manager->ApplyTouchTransformer(touch_id, &x, &y);
  EXPECT_NEAR(0, x, max_error_delta.width()) << error_msg;
  EXPECT_NEAR(0, y, max_error_delta.height()) << error_msg;

  // Center of the touch device should correspond to the center of the display
  // device.
  x = touch_size.width() / 2;
  y = touch_size.height() / 2;
  device_manager->ApplyTouchTransformer(touch_id, &x, &y);
  EXPECT_NEAR(display_size.width() / 2, x, max_error_delta.width())
      << error_msg;
  EXPECT_NEAR(display_size.height() / 2, y, max_error_delta.height())
      << error_msg;

  // Bottom right corner of the touch device should correspond to rightmost
  // corner of display device.
  x = touch_size.width();
  y = touch_size.height();
  device_manager->ApplyTouchTransformer(touch_id, &x, &y);
  EXPECT_NEAR(display_size.width(), x, max_error_delta.width()) << error_msg;
  EXPECT_NEAR(display_size.height(), y, max_error_delta.height()) << error_msg;
}

}  //  namespace

class TouchTransformControllerTest : public testing::Test {
 public:
  TouchTransformControllerTest() {}

  TouchTransformControllerTest(const TouchTransformControllerTest&) = delete;
  TouchTransformControllerTest& operator=(const TouchTransformControllerTest&) =
      delete;

  ~TouchTransformControllerTest() override {}

  gfx::Transform GetTouchTransform(
      const ManagedDisplayInfo& display,
      const ManagedDisplayInfo& touch_display,
      const ui::TouchscreenDevice& touchscreen) const {
    return touch_transform_controller_->GetTouchTransform(
        display, touch_display, touchscreen);
  }

  double GetTouchResolutionScale(
      const ManagedDisplayInfo& touch_display,
      const ui::TouchscreenDevice& touch_device) const {
    return touch_transform_controller_->GetTouchResolutionScale(touch_display,
                                                                touch_device);
  }

  TouchDeviceManager* touch_device_manager() { return touch_device_manager_; }

  // testing::Test:
  void SetUp() override {
    ui::DeviceDataManager::CreateInstance();
    std::unique_ptr<ScreenBase> screen = std::make_unique<ScreenBase>();
    Screen::SetScreenInstance(screen.get());
    display_manager_ = std::make_unique<DisplayManager>(std::move(screen));
    touch_device_manager_ = display_manager_->touch_device_manager();
    touch_transform_controller_ = std::make_unique<TouchTransformController>(
        display_manager_.get(),
        std::make_unique<DefaultTouchTransformSetter>());
  }

  void TearDown() override {
    Screen::SetScreenInstance(nullptr);
    ui::DeviceDataManager::DeleteInstance();
  }

  ManagedDisplayInfo CreateDisplayInfo(int64_t id,
                                       const ui::TouchscreenDevice& device,
                                       const gfx::Rect& bounds) {
    ManagedDisplayInfo info = display::CreateDisplayInfo(id, bounds);

    // Create a default mode.
    ManagedDisplayInfo::ManagedDisplayModeList default_modes(
        1, ManagedDisplayMode(bounds.size(), 60, false, true));
    info.SetManagedDisplayModes(default_modes);

    // Associate the display and touch device.
    test::TouchDeviceManagerTestApi tdm_test_api(touch_device_manager_);
    tdm_test_api.Associate(&info, device);

    return info;
  }

 private:
  std::unique_ptr<DisplayManager> display_manager_;
  std::unique_ptr<TouchTransformController> touch_transform_controller_;
  raw_ptr<TouchDeviceManager> touch_device_manager_;
};

TEST_F(TouchTransformControllerTest, MirrorModeLetterboxing) {
  gfx::Size fb_size(1920, 1200);
  // TODO(kylechar): Check the TouchscreenDevice size makes sense for Ozone.
  ui::TouchscreenDevice internal_touchscreen =
      CreateTouchscreenDevice(10, fb_size);
  ui::TouchscreenDevice external_touchscreen =
      CreateTouchscreenDevice(11, fb_size);

  // The internal display has native resolution of 2560x1700, and in
  // mirror mode it is configured as 1920x1200. This is in letterboxing
  // mode.
  ManagedDisplayInfo internal_display_info =
      CreateDisplayInfo(1, internal_touchscreen, gfx::Rect(0, 0, 1920, 1200));
  internal_display_info.set_is_aspect_preserving_scaling(true);

  ManagedDisplayInfo::ManagedDisplayModeList internal_modes;

  internal_modes.push_back(
      ManagedDisplayMode(gfx::Size(2560, 1700), 60, false, true));
  internal_modes.push_back(
      ManagedDisplayMode(gfx::Size(1920, 1200), 60, false, false));
  internal_display_info.SetManagedDisplayModes(internal_modes);

  ManagedDisplayInfo external_display_info =
      CreateDisplayInfo(2, external_touchscreen, gfx::Rect(0, 0, 1920, 1200));

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();

  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      internal_display_info.id(), internal_touchscreen.id,
      GetTouchTransform(internal_display_info, internal_display_info,
                        internal_touchscreen)));
  transforms.push_back(CreateTouchDeviceTransform(
      internal_display_info.id(), external_touchscreen.id,
      GetTouchTransform(external_display_info, external_display_info,
                        external_touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(1, device_manager->GetTargetDisplayForTouchDevice(10));
  EXPECT_EQ(1, device_manager->GetTargetDisplayForTouchDevice(11));

  // External touch display has the default TouchTransformer.
  float x = 100.0;
  float y = 100.0;
  device_manager->ApplyTouchTransformer(11, &x, &y);
  EXPECT_EQ(100, x);
  EXPECT_EQ(100, y);

  // In letterboxing, there is (1-2560*(1200/1920)/1700)/2 = 2.95% of the
  // height on both the top & bottom region of the screen is blank.
  // When touch events coming at Y range [0, 1200), the mapping should be
  // [0, ~35] ---> < 0
  // [~35, ~1165] ---> [0, 1200)
  // [~1165, 1200] ---> >= 1200
  x = 100.0;
  y = 35.0;
  device_manager->ApplyTouchTransformer(10, &x, &y);
  EXPECT_NEAR(100, x, 0.5);
  EXPECT_NEAR(0, y, 0.5);

  x = 100.0;
  y = 1165.0;
  device_manager->ApplyTouchTransformer(10, &x, &y);
  EXPECT_NEAR(100, x, 0.5);
  EXPECT_NEAR(1200, y, 0.5);
}

TEST_F(TouchTransformControllerTest, MirrorModePillarboxing) {
  gfx::Size fb_size(1024, 768);
  // TODO(kylechar): Check the TouchscreenDevice size makes sense for Ozone.
  ui::TouchscreenDevice internal_touchscreen =
      CreateTouchscreenDevice(10, fb_size);
  ui::TouchscreenDevice external_touchscreen =
      CreateTouchscreenDevice(11, fb_size);

  // The internal display has native resolution of 1366x768, and in
  // mirror mode it is configured as 1024x768. This is in pillarboxing
  // mode.
  ManagedDisplayInfo internal_display_info =
      CreateDisplayInfo(1, internal_touchscreen, gfx::Rect(0, 0, 1024, 768));
  internal_display_info.set_is_aspect_preserving_scaling(true);
  ManagedDisplayInfo::ManagedDisplayModeList internal_modes;
  internal_modes.push_back(
      ManagedDisplayMode(gfx::Size(1366, 768), 60, false, true));
  internal_modes.push_back(
      ManagedDisplayMode(gfx::Size(1024, 768), 60, false, false));
  internal_display_info.SetManagedDisplayModes(internal_modes);

  ManagedDisplayInfo external_display_info =
      CreateDisplayInfo(2, external_touchscreen, gfx::Rect(0, 0, 1024, 768));

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  std::vector<ui::TouchDeviceTransform> transforms;

  transforms.push_back(CreateTouchDeviceTransform(
      internal_display_info.id(), internal_touchscreen.id,
      GetTouchTransform(internal_display_info, internal_display_info,
                        internal_touchscreen)));

  transforms.push_back(CreateTouchDeviceTransform(
      internal_display_info.id(), external_touchscreen.id,
      GetTouchTransform(external_display_info, external_display_info,
                        external_touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(1, device_manager->GetTargetDisplayForTouchDevice(10));
  EXPECT_EQ(1, device_manager->GetTargetDisplayForTouchDevice(11));

  // External touch display has the default TouchTransformer.
  float x = 100.0;
  float y = 100.0;
  device_manager->ApplyTouchTransformer(11, &x, &y);
  EXPECT_EQ(100, x);
  EXPECT_EQ(100, y);

  // In pillarboxing, there is (1-768*(1024/768)/1366)/2 = 12.5% of the
  // width on both the left & right region of the screen is blank.
  // When touch events coming at X range [0, 1024), the mapping should be
  // [0, ~128] ---> < 0
  // [~128, ~896] ---> [0, 1024)
  // [~896, 1024] ---> >= 1024
  x = 128.0;
  y = 100.0;
  device_manager->ApplyTouchTransformer(10, &x, &y);
  EXPECT_NEAR(0, x, 0.5);
  EXPECT_NEAR(100, y, 0.5);

  x = 896.0;
  y = 100.0;
  device_manager->ApplyTouchTransformer(10, &x, &y);
  EXPECT_NEAR(1024, x, 0.5);
  EXPECT_NEAR(100, y, 0.5);
}

TEST_F(TouchTransformControllerTest, SoftwareMirrorMode) {
  gfx::Size fb_size(1920, 1990);
  // TODO(kylechar): Check the TouchscreenDevice size makes sense for Ozone.
  ui::TouchscreenDevice display1_touchscreen =
      CreateTouchscreenDevice(10, fb_size);
  ui::TouchscreenDevice display2_touchscreen =
      CreateTouchscreenDevice(11, fb_size);

  // External display 1 has size 1280x850. External display 2 has size
  // 1920x1080. When using software mirroring to mirror display 1 onto
  // display 2, the displays are in extended mode and we map touches from both
  // displays to display 1.
  // The total frame buffer is 1920x1990,
  // where 1990 = 850 + 60 (hidden gap) + 1080 and the second monitor is
  // translated to point (0, 950) in the framebuffer.
  ManagedDisplayInfo display1_info =
      CreateDisplayInfo(1, display1_touchscreen, gfx::Rect(0, 0, 1280, 850));
  ManagedDisplayInfo::ManagedDisplayModeList display1_modes;
  display1_modes.push_back(
      ManagedDisplayMode(gfx::Size(1280, 850), 60, false, true));
  display1_info.SetManagedDisplayModes(display1_modes);

  ManagedDisplayInfo display2_info =
      CreateDisplayInfo(2, display2_touchscreen, gfx::Rect(0, 950, 1920, 1080));
  ManagedDisplayInfo::ManagedDisplayModeList display2_modes;
  display2_modes.push_back(
      ManagedDisplayMode(gfx::Size(1920, 1080), 60, false, true));
  display2_info.SetManagedDisplayModes(display2_modes);

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  std::vector<ui::TouchDeviceTransform> transforms;

  transforms.push_back(CreateTouchDeviceTransform(
      display1_info.id(), display1_touchscreen.id,
      GetTouchTransform(display1_info, display1_info, display1_touchscreen)));

  transforms.push_back(CreateTouchDeviceTransform(
      display1_info.id(), display2_touchscreen.id,
      GetTouchTransform(display1_info, display2_info, display2_touchscreen)));

  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(1, device_manager->GetTargetDisplayForTouchDevice(10));
  EXPECT_EQ(1, device_manager->GetTargetDisplayForTouchDevice(11));

  // Mapping for touch events from display 1's touchscreen:
  // [0, 1920) x [0, 1990) -> [0, 1280) x [0, 850)
  float x = 0.0;
  float y = 0.0;
  device_manager->ApplyTouchTransformer(10, &x, &y);
  EXPECT_NEAR(0, x, 0.5);
  EXPECT_NEAR(0, y, 0.5);

  x = 1920.0;
  y = 1990.0;
  device_manager->ApplyTouchTransformer(10, &x, &y);
  EXPECT_NEAR(1280, x, 0.5);
  EXPECT_NEAR(850, y, 0.5);

  // In pillarboxing, there is (1-1280*(1080/850)/1920)/2 = 7.65% of the
  // width on both the left & right region of the screen is blank.
  // Events come in the range [0, 1920) x [0, 1990).
  //
  // X mapping:
  // [0, ~147] ---> < 0
  // [~147, ~1773] ---> [0, 1280)
  // [~1773, 1920] ---> >= 1280
  // Y mapping:
  // [0, 1990) -> [0, 1080)
  x = 147.0;
  y = 0.0;
  device_manager->ApplyTouchTransformer(11, &x, &y);
  EXPECT_NEAR(0, x, 0.5);
  EXPECT_NEAR(0, y, 0.5);

  x = 1773.0;
  y = 1990.0;
  device_manager->ApplyTouchTransformer(11, &x, &y);
  EXPECT_NEAR(1280, x, 0.5);
  EXPECT_NEAR(850, y, 0.5);
}

TEST_F(TouchTransformControllerTest, ExtendedMode) {
  gfx::Size fb_size(2560, 2428);

  // TODO(kylechar): Check the TouchscreenDevice size makes sense for Ozone.
  ui::TouchscreenDevice touchscreen1 = CreateTouchscreenDevice(5, fb_size);
  ui::TouchscreenDevice touchscreen2 = CreateTouchscreenDevice(6, fb_size);

  // The internal display has size 1366 x 768. The external display has
  // size 2560x1600. The total frame buffer is 2560x2428,
  // where 2428 = 768 + 60 (hidden gap) + 1600
  // and the second monitor is translated to Point (0, 828) in the
  // framebuffer.
  ManagedDisplayInfo display1 =
      CreateDisplayInfo(1, touchscreen1, gfx::Rect(0, 0, 1366, 768));
  ManagedDisplayInfo display2 =
      CreateDisplayInfo(2, touchscreen2, gfx::Rect(0, 828, 2560, 1600));

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      display1.id(), touchscreen1.id,
      GetTouchTransform(display1, display1, touchscreen1)));

  transforms.push_back(CreateTouchDeviceTransform(
      display2.id(), touchscreen2.id,
      GetTouchTransform(display2, display2, touchscreen2)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(1, device_manager->GetTargetDisplayForTouchDevice(5));
  EXPECT_EQ(2, device_manager->GetTargetDisplayForTouchDevice(6));

  // Mapping for touch events from internal touch display:
  // [0, 2560) x [0, 2428) -> [0, 1366) x [0, 768)
  float x = 0.0;
  float y = 0.0;
  device_manager->ApplyTouchTransformer(5, &x, &y);
  EXPECT_NEAR(0, x, 0.5);
  EXPECT_NEAR(0, y, 0.5);

  x = 2559.0;
  y = 2427.0;
  device_manager->ApplyTouchTransformer(5, &x, &y);
  EXPECT_NEAR(1365, x, 0.5);
  EXPECT_NEAR(768, y, 0.5);

  // Mapping for touch events from external touch display:
  // [0, 2560) x [0, 2428) -> [0, 2560) x [0, 1600)
  x = 0.0;
  y = 0.0;
  device_manager->ApplyTouchTransformer(6, &x, &y);
  // On ozone we expect screen coordinates so add display origin.
  EXPECT_NEAR(0 + 0, x, 0.5);
  EXPECT_NEAR(0 + 828, y, 0.5);

  x = 2559.0;
  y = 2427.0;
  device_manager->ApplyTouchTransformer(6, &x, &y);
  // On ozone we expect screen coordinates so add display origin.
  EXPECT_NEAR(2559 + 0, x, 0.5);
  EXPECT_NEAR(1599 + 828, y, 0.5);
}

TEST_F(TouchTransformControllerTest, TouchRadiusScale) {
  ui::TouchscreenDevice touch_device =
      CreateTouchscreenDevice(5, gfx::Size(100001, 100001));
  ManagedDisplayInfo display =
      CreateDisplayInfo(1, touch_device, gfx::Rect(0, 0, 2560, 1600));

  // Default touchscreen position range is 100001x100001;
  EXPECT_EQ(sqrt((2560.0 * 1600.0) / (100001.0 * 100001.0)),
            GetTouchResolutionScale(display, touch_device));
}

TEST_F(TouchTransformControllerTest, OzoneTranslation) {
  // The internal display has size 1920 x 1200. The external display has
  // size 1920x1200. The total frame buffer is 1920x2450,
  // where 2458 = 1200 + 50 (hidden gap) + 1200
  // and the second monitor is translated to Point (0, 1250) in the
  // framebuffer.
  const gfx::Size kDisplaySize(1920, 1200);
  const int kHiddenGap = 50;

  ui::TouchscreenDevice touchscreen1 =
      CreateTouchscreenDevice(kTouchId1, kDisplaySize);
  ui::TouchscreenDevice touchscreen2 =
      CreateTouchscreenDevice(kTouchId2, kDisplaySize);

  ManagedDisplayInfo display1 = CreateDisplayInfo(
      kDisplayId1, touchscreen1,
      gfx::Rect(0, 0, kDisplaySize.width(), kDisplaySize.height()));
  ManagedDisplayInfo display2 =
      CreateDisplayInfo(kDisplayId2, touchscreen2,
                        gfx::Rect(0, kDisplaySize.height() + kHiddenGap,
                                  kDisplaySize.width(), kDisplaySize.height()));

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  std::vector<ui::TouchDeviceTransform> transforms;

  // Mirror displays. Touch screen 2 is associated to display 1.
  transforms.push_back(CreateTouchDeviceTransform(
      display1.id(), touchscreen1.id,
      GetTouchTransform(display1, display1, touchscreen1)));

  transforms.push_back(CreateTouchDeviceTransform(
      display1.id(), touchscreen2.id,
      GetTouchTransform(display1, display2, touchscreen2)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId1));
  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId2));

  float x, y;

  x = y = 0.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_NEAR(0, x, 0.5);
  EXPECT_NEAR(0, y, 0.5);

  x = y = 0.0;
  device_manager->ApplyTouchTransformer(kTouchId2, &x, &y);
  EXPECT_NEAR(0, x, 0.5);
  EXPECT_NEAR(0, y, 0.5);

  x = 1920.0;
  y = 1200.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_NEAR(1920, x, 0.5);
  EXPECT_NEAR(1200, y, 0.5);

  x = 1920.0;
  y = 1200.0;
  device_manager->ApplyTouchTransformer(kTouchId2, &x, &y);
  EXPECT_NEAR(1920, x, 0.5);
  EXPECT_NEAR(1200, y, 0.5);

  // Remove mirroring of displays.
  transforms.push_back(CreateTouchDeviceTransform(
      display2.id(), touchscreen2.id,
      GetTouchTransform(display2, display2, touchscreen2)));
  device_manager->ConfigureTouchDevices(transforms);

  x = 1920.0;
  y = 1200.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_NEAR(1920, x, 0.5);
  EXPECT_NEAR(1200, y, 0.5);

  x = 1920.0;
  y = 1200.0;
  device_manager->ApplyTouchTransformer(kTouchId2, &x, &y);
  EXPECT_NEAR(1920, x, 0.5);
  EXPECT_NEAR(1200 + kDisplaySize.height() + kHiddenGap, y, 0.5);
}

TEST_F(TouchTransformControllerTest, AccurateUserTouchCalibration) {
  const gfx::Size kDisplaySize(1920, 1200);
  const gfx::Size kTouchSize(1920, 1200);

  ui::TouchscreenDevice touchscreen =
      CreateTouchscreenDevice(kTouchId1, kTouchSize);

  ManagedDisplayInfo display = CreateDisplayInfo(
      kDisplayId1, touchscreen,
      gfx::Rect(0, 0, kDisplaySize.width(), kDisplaySize.height()));

  // Assuming the user provided accurate inputs during calibration. ie the user
  // actually tapped (100,100) when asked to tap (100,100) with no human error.
  TouchCalibrationData::CalibrationPointPairQuad user_input = {{
      std::make_pair(gfx::Point(100, 100), gfx::Point(100, 100)),
      std::make_pair(gfx::Point(1820, 100), gfx::Point(1820, 100)),
      std::make_pair(gfx::Point(100, 1100), gfx::Point(100, 1100)),
      std::make_pair(gfx::Point(1820, 1100), gfx::Point(1820, 1100)),
  }};
  TouchCalibrationData touch_data(user_input, kDisplaySize);

  const std::string msg = GetTouchPointString(user_input);

  touch_device_manager()->AddTouchCalibrationData(touchscreen, display.id(),
                                                  touch_data);

  EXPECT_FALSE(touch_device_manager()
                   ->GetCalibrationData(touchscreen, display.id())
                   .IsEmpty());

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      display.id(), touchscreen.id,
      GetTouchTransform(display, display, touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId1));

  CheckPointsOfInterests(kTouchId1, kTouchSize, kDisplaySize, gfx::Size(1, 1),
                         msg);
}

TEST_F(TouchTransformControllerTest, ErrorProneUserTouchCalibration) {
  const gfx::Size kDisplaySize(1920, 1200);
  const gfx::Size kTouchSize(1920, 1200);
  // User touch inputs have a max error of 5%.
  const float kError = 0.05;
  // The maximum user error rate is |kError|%. Since the calibration is
  // performed with a best fit algorithm, the error rate observed should be less
  // than |kError|.
  const gfx::Size kMaxErrorDelta = gfx::ScaleToCeiledSize(kTouchSize, kError);

  ui::TouchscreenDevice touchscreen =
      CreateTouchscreenDevice(kTouchId1, kTouchSize);

  ManagedDisplayInfo display = CreateDisplayInfo(
      kDisplayId1, touchscreen,
      gfx::Rect(0, 0, kDisplaySize.width(), kDisplaySize.height()));

  // Assuming the user provided inaccurate inputs during calibration. ie the
  // user did not tap (100,100) when asked to tap (100,100) due to human error.
  TouchCalibrationData::CalibrationPointPairQuad user_input = {
      {std::make_pair(gfx::Point(100, 100), gfx::Point(130, 60)),
       std::make_pair(gfx::Point(1820, 100), gfx::Point(1878, 130)),
       std::make_pair(gfx::Point(100, 1100), gfx::Point(158, 1060)),
       std::make_pair(gfx::Point(1820, 1100), gfx::Point(1790, 1140))}};
  TouchCalibrationData touch_data(user_input, kDisplaySize);

  const std::string msg = GetTouchPointString(user_input);

  touch_device_manager()->AddTouchCalibrationData(touchscreen, display.id(),
                                                  touch_data);

  EXPECT_FALSE(touch_device_manager()
                   ->GetCalibrationData(touchscreen, display.id())
                   .IsEmpty());

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      display.id(), touchscreen.id,
      GetTouchTransform(display, display, touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId1));

  CheckPointsOfInterests(kTouchId1, kTouchSize, kDisplaySize, kMaxErrorDelta,
                         msg);
}

TEST_F(TouchTransformControllerTest, ResolutionChangeUserTouchCalibration) {
  const gfx::Size kDisplaySize(2560, 1600);
  const gfx::Size kTouchSize(1920, 1200);
  // User touch inputs have a max error of 5%.
  const float kError = 0.05;
  // The maximum user error rate is |kError|%. Since the calibration is
  // performed with a best fit algorithm, the error rate observed should be less
  // tha |kError|.
  gfx::Size kMaxErrorDelta = gfx::ScaleToCeiledSize(kDisplaySize, kError);

  ui::TouchscreenDevice touchscreen =
      CreateTouchscreenDevice(kTouchId1, kTouchSize);

  ManagedDisplayInfo display = CreateDisplayInfo(
      kDisplayId1, touchscreen,
      gfx::Rect(0, 0, kDisplaySize.width(), kDisplaySize.height()));

  // The calibration was performed at a resolution different from the curent
  // resolution of the display.
  const gfx::Size CALIBRATION_SIZE(1920, 1200);
  TouchCalibrationData::CalibrationPointPairQuad user_input = {
      {std::make_pair(gfx::Point(100, 100), gfx::Point(50, 70)),
       std::make_pair(gfx::Point(1820, 100), gfx::Point(1780, 70)),
       std::make_pair(gfx::Point(100, 1100), gfx::Point(70, 1060)),
       std::make_pair(gfx::Point(1820, 1100), gfx::Point(1770, 1140))}};

  TouchCalibrationData touch_data(user_input, CALIBRATION_SIZE);

  const std::string msg = GetTouchPointString(user_input);

  touch_device_manager()->AddTouchCalibrationData(touchscreen, display.id(),
                                                  touch_data);

  EXPECT_FALSE(touch_device_manager()
                   ->GetCalibrationData(touchscreen, display.id())
                   .IsEmpty());

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      display.id(), touchscreen.id,
      GetTouchTransform(display, display, touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId1));

  CheckPointsOfInterests(kTouchId1, kTouchSize, kDisplaySize, kMaxErrorDelta,
                         msg);
}

TEST_F(TouchTransformControllerTest, DifferentBoundsUserTouchCalibration) {
  // The display bounds is different from the touch device bounds in this test.
  const gfx::Size kDisplaySize(1024, 600);
  const gfx::Size kTouchSize(4096, 4096);
  const float kAcceptableError = 0.04;
  gfx::Size kMaxErrorDelta =
      gfx::ScaleToCeiledSize(kDisplaySize, kAcceptableError);

  ui::TouchscreenDevice touchscreen =
      CreateTouchscreenDevice(kTouchId1, kTouchSize);

  ManagedDisplayInfo display = CreateDisplayInfo(
      kDisplayId1, touchscreen,
      gfx::Rect(0, 0, kDisplaySize.width(), kDisplaySize.height()));

  // Real world data.
  TouchCalibrationData::CalibrationPointPairQuad user_input = {
      {std::make_pair(gfx::Point(136, 136), gfx::Point(538, 931)),
       std::make_pair(gfx::Point(873, 136), gfx::Point(3475, 922)),
       std::make_pair(gfx::Point(136, 411), gfx::Point(611, 2800)),
       std::make_pair(gfx::Point(873, 411), gfx::Point(3535, 2949))}};
  TouchCalibrationData touch_data(user_input, kDisplaySize);

  const std::string msg = GetTouchPointString(user_input);

  touch_device_manager()->AddTouchCalibrationData(touchscreen, display.id(),
                                                  touch_data);

  EXPECT_FALSE(touch_device_manager()
                   ->GetCalibrationData(touchscreen, display.id())
                   .IsEmpty());

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();

  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      display.id(), touchscreen.id,
      GetTouchTransform(display, display, touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId1));

  CheckPointsOfInterests(kTouchId1, kTouchSize, kDisplaySize, kMaxErrorDelta,
                         msg);
}

TEST_F(TouchTransformControllerTest, LetterboxingUserTouchCalibration) {
  // The internal display has native resolution of 2560x1700, and in
  // mirror mode it is configured as 1920x1200. This is in letterboxing
  // mode.
  const gfx::Size kNativeDisplaySize(2560, 1700);
  const gfx::Size kDisplaySize(1920, 1200);
  const gfx::Size kTouchSize(1920, 1200);

  ui::TouchscreenDevice internal_touchscreen =
      CreateTouchscreenDevice(kTouchId1, kTouchSize);

  ManagedDisplayInfo internal_display_info = CreateDisplayInfo(
      kDisplayId1, internal_touchscreen,
      gfx::Rect(0, 0, kDisplaySize.width(), kDisplaySize.height()));
  internal_display_info.set_is_aspect_preserving_scaling(true);

  ManagedDisplayInfo::ManagedDisplayModeList internal_modes;

  internal_modes.push_back(ManagedDisplayMode(
      gfx::Size(kNativeDisplaySize.width(), kNativeDisplaySize.height()), 60,
      false, true));
  internal_modes.push_back(
      ManagedDisplayMode(gfx::Size(kDisplaySize.width(), kDisplaySize.height()),
                         60, false, false));
  internal_display_info.SetManagedDisplayModes(internal_modes);

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();

  // Assuming the user provided inaccurate inputs during calibration. ie the
  // user did not tap (100,100) when asked to tap (100,100) due to human error.
  // Since the display is of size 2560x1700 and the touch device is of size
  // 1920x1200, the corresponding points have to be scaled.
  TouchCalibrationData::CalibrationPointPairQuad user_input = {{
      std::make_pair(gfx::Point(100, 100), gfx::Point(75, 71)),
      std::make_pair(gfx::Point(2460, 100), gfx::Point(1845, 71)),
      std::make_pair(gfx::Point(100, 1600), gfx::Point(75, 1130)),
      std::make_pair(gfx::Point(2460, 1600), gfx::Point(1845, 1130)),
  }};
  // The calibration was performed at the native display resolution.
  TouchCalibrationData touch_data(user_input, kNativeDisplaySize);
  touch_device_manager()->AddTouchCalibrationData(
      internal_touchscreen, internal_display_info.id(), touch_data);

  EXPECT_FALSE(
      touch_device_manager()
          ->GetCalibrationData(internal_touchscreen, internal_display_info.id())
          .IsEmpty());

  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      internal_display_info.id(), internal_touchscreen.id,
      GetTouchTransform(internal_display_info, internal_display_info,
                        internal_touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId1));

  float x, y;
  // In letterboxing, there is (1-2560*(1200/1920)/1700)/2 = 2.95% of the
  // height on both the top & bottom region of the screen is blank.
  // When touch events coming at Y range [0, 1200), the mapping should be
  // [0, ~35] ---> < 0
  // [~35, ~1165] ---> [0, 1200)
  // [~1165, 1200] ---> >= 1200
  x = 100.0;
  y = 35.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_NEAR(100, x, 0.5);
  EXPECT_NEAR(0, y, 0.5);

  x = 100.0;
  y = 1165.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_NEAR(100, x, 0.5);
  EXPECT_NEAR(1200, y, 0.5);
}

TEST_F(TouchTransformControllerTest, PillarBoxingUserTouchCalibration) {
  // The internal display has native resolution of 2560x1700, and in
  // mirror mode it is configured as 1920x1200. This is in letterboxing
  // mode.
  const gfx::Size kNativeDisplaySize(2560, 1600);
  const gfx::Size kDisplaySize(1920, 1400);

  ui::TouchscreenDevice internal_touchscreen =
      CreateTouchscreenDevice(kTouchId1, kDisplaySize);

  ManagedDisplayInfo internal_display_info = CreateDisplayInfo(
      kDisplayId1, internal_touchscreen,
      gfx::Rect(0, 0, kDisplaySize.width(), kDisplaySize.height()));
  internal_display_info.set_is_aspect_preserving_scaling(true);

  ManagedDisplayInfo::ManagedDisplayModeList internal_modes;

  internal_modes.push_back(ManagedDisplayMode(
      gfx::Size(kNativeDisplaySize.width(), kNativeDisplaySize.height()), 60,
      false, true));
  internal_modes.push_back(
      ManagedDisplayMode(gfx::Size(kDisplaySize.width(), kDisplaySize.height()),
                         60, false, false));
  internal_display_info.SetManagedDisplayModes(internal_modes);

  ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();

  // Assuming the user provided accurate inputs during calibration. ie the user
  // actually tapped (100,100) when asked to tap (100,100) with no human error.
  // Since the display is of size 2560x1600 and the touch device is of size
  // 1920x1400, the corresponding points have to be scaled.
  TouchCalibrationData::CalibrationPointPairQuad user_input = {{
      std::make_pair(gfx::Point(100, 100), gfx::Point(75, 88)),
      std::make_pair(gfx::Point(2460, 100), gfx::Point(1845, 88)),
      std::make_pair(gfx::Point(100, 1500), gfx::Point(75, 1313)),
      std::make_pair(gfx::Point(2460, 1500), gfx::Point(1845, 1313)),
  }};
  // The calibration was performed at the native display resolution.
  TouchCalibrationData touch_data(user_input, kNativeDisplaySize);

  touch_device_manager()->AddTouchCalibrationData(
      internal_touchscreen, internal_display_info.id(), touch_data);

  EXPECT_FALSE(
      touch_device_manager()
          ->GetCalibrationData(internal_touchscreen, internal_display_info.id())
          .IsEmpty());

  std::vector<ui::TouchDeviceTransform> transforms;
  transforms.push_back(CreateTouchDeviceTransform(
      internal_display_info.id(), internal_touchscreen.id,
      GetTouchTransform(internal_display_info, internal_display_info,
                        internal_touchscreen)));
  device_manager->ConfigureTouchDevices(transforms);

  EXPECT_EQ(kDisplayId1,
            device_manager->GetTargetDisplayForTouchDevice(kTouchId1));

  float x, y;
  // In pillarboxing, there is (1-1600*(1920/1400)/2560)/2 = 7.14% of the
  // width on both the left & region region of the screen is blank.
  // When touch events coming at X range [0, 1920), the mapping should be
  // [0, ~137] ---> < 0
  // [~137, ~1782] ---> [0, 1920)
  // [~1782, 1920] ---> >= 1920
  x = 136.0;
  y = 0.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_LT(-1.0f, x);
  EXPECT_LT(x, 0.0f);
  EXPECT_NEAR(0.0f, y, 0.01f);

  x = 137.0;
  y = 0.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_LT(0.0f, x);
  EXPECT_LT(x, 1.0f);
  EXPECT_NEAR(0.0f, y, 0.01f);

  x = 1782.0;
  y = 0.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_LT(1919.0f, x);
  EXPECT_LT(x, 1920.0f);
  EXPECT_NEAR(0.0f, y, 0.01f);

  x = 1783.0;
  y = 0.0;
  device_manager->ApplyTouchTransformer(kTouchId1, &x, &y);
  EXPECT_LT(1920.0f, x);
  EXPECT_LT(x, 1921.0f);
  EXPECT_NEAR(0.0f, y, 0.01f);
}

}  // namespace display::test