Home | History | Annotate | Download | only in touch
      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.
      4 
      5 #include "ash/touch/touch_transformer_controller.h"
      6 
      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/device_data_manager.h"
     11 #include "ui/events/test/events_test_utils_x11.h"
     12 #include "ui/gfx/display.h"
     13 
     14 namespace ash {
     15 
     16 namespace {
     17 DisplayInfo CreateDisplayInfo(int64 id,
     18                               int touch_device_id,
     19                               const gfx::Rect& bounds) {
     20   DisplayInfo info(id, std::string(), false);
     21   info.SetBounds(bounds);
     22   info.set_touch_device_id(touch_device_id);
     23   return info;
     24 }
     25 }  //  namespace
     26 
     27 typedef test::AshTestBase TouchTransformerControllerTest;
     28 
     29 TEST_F(TouchTransformerControllerTest, TouchTransformerMirrorModeLetterboxing) {
     30   // The internal display has native resolution of 2560x1700, and in
     31   // mirror mode it is configured as 1920x1200. This is in letterboxing
     32   // mode.
     33   DisplayInfo internal_display_info =
     34       CreateDisplayInfo(1, 10, gfx::Rect(0, 0, 1920, 1200));
     35   std::vector<DisplayMode> internal_modes;
     36   internal_modes.push_back(
     37       DisplayMode(gfx::Size(2560, 1700), 60, false, true));
     38   internal_modes.push_back(
     39       DisplayMode(gfx::Size(1920, 1200), 60, false, false));
     40   internal_display_info.set_display_modes(internal_modes);
     41 
     42   DisplayInfo external_display_info =
     43       CreateDisplayInfo(2, 11, gfx::Rect(0, 0, 1920, 1200));
     44 
     45   TouchTransformerController* tt_controller =
     46       Shell::GetInstance()->touch_transformer_controller();
     47   ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
     48 
     49   tt_controller->force_compute_mirror_mode_touch_transformer_ = true;
     50   device_manager->UpdateTouchInfoForDisplay(
     51       internal_display_info.id(),
     52       internal_display_info.touch_device_id(),
     53       tt_controller->GetMirrorModeTouchTransformer(internal_display_info));
     54 
     55   tt_controller->force_compute_mirror_mode_touch_transformer_ = false;
     56   device_manager->UpdateTouchInfoForDisplay(
     57       external_display_info.id(),
     58       external_display_info.touch_device_id(),
     59       tt_controller->GetMirrorModeTouchTransformer(external_display_info));
     60 
     61   EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(10));
     62   EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(11));
     63 
     64   // External touch display has the default TouchTransformer.
     65   float x = 100.0;
     66   float y = 100.0;
     67   device_manager->ApplyTouchTransformer(11, &x, &y);
     68   EXPECT_EQ(100, x);
     69   EXPECT_EQ(100, y);
     70 
     71   // In letterboxing, there is (1-2560*(1200/1920)/1700)/2 = 2.95% of the
     72   // height on both the top & bottom region of the screen is blank.
     73   // When touch events coming at Y range [0, 1200), the mapping should be
     74   // [0, ~35] ---> < 0
     75   // [~35, ~1165] ---> [0, 1200)
     76   // [~1165, 1200] ---> >= 1200
     77   x = 100.0;
     78   y = 35.0;
     79   device_manager->ApplyTouchTransformer(10, &x, &y);
     80   EXPECT_EQ(100, static_cast<int>(x));
     81   EXPECT_EQ(0, static_cast<int>(y));
     82 
     83   x = 100.0;
     84   y = 1165.0;
     85   device_manager->ApplyTouchTransformer(10, &x, &y);
     86   EXPECT_EQ(100, static_cast<int>(x));
     87   EXPECT_EQ(1200, static_cast<int>(y));
     88 }
     89 
     90 TEST_F(TouchTransformerControllerTest, TouchTransformerMirrorModePillarboxing) {
     91   // The internal display has native resolution of 1366x768, and in
     92   // mirror mode it is configured as 1024x768. This is in pillarboxing
     93   // mode.
     94   DisplayInfo internal_display_info =
     95       CreateDisplayInfo(1, 10, gfx::Rect(0, 0, 1024, 768));
     96   std::vector<DisplayMode> internal_modes;
     97   internal_modes.push_back(
     98       DisplayMode(gfx::Size(1366, 768), 60, false, true));
     99   internal_modes.push_back(
    100       DisplayMode(gfx::Size(1024, 768), 60, false, false));
    101   internal_display_info.set_display_modes(internal_modes);
    102 
    103   DisplayInfo external_display_info =
    104       CreateDisplayInfo(2, 11, gfx::Rect(0, 0, 1024, 768));
    105 
    106   TouchTransformerController* tt_controller =
    107       Shell::GetInstance()->touch_transformer_controller();
    108   ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
    109 
    110   tt_controller->force_compute_mirror_mode_touch_transformer_ = true;
    111   device_manager->UpdateTouchInfoForDisplay(
    112       internal_display_info.id(),
    113       internal_display_info.touch_device_id(),
    114       tt_controller->GetMirrorModeTouchTransformer(internal_display_info));
    115 
    116   tt_controller->force_compute_mirror_mode_touch_transformer_ = false;
    117   device_manager->UpdateTouchInfoForDisplay(
    118       external_display_info.id(),
    119       external_display_info.touch_device_id(),
    120       tt_controller->GetMirrorModeTouchTransformer(external_display_info));
    121 
    122   EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(10));
    123   EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(11));
    124 
    125   // External touch display has the default TouchTransformer.
    126   float x = 100.0;
    127   float y = 100.0;
    128   device_manager->ApplyTouchTransformer(11, &x, &y);
    129   EXPECT_EQ(100, x);
    130   EXPECT_EQ(100, y);
    131 
    132   // In pillarboxing, there is (1-768*(1024/768)/1366)/2 = 12.5% of the
    133   // width on both the left & rigth region of the screen is blank.
    134   // When touch events coming at X range [0, 1024), the mapping should be
    135   // [0, ~128] ---> < 0
    136   // [~128, ~896] ---> [0, 1024)
    137   // [~896, 1024] ---> >= 1024
    138   x = 128.0;
    139   y = 100.0;
    140   device_manager->ApplyTouchTransformer(10, &x, &y);
    141   EXPECT_EQ(0, static_cast<int>(x));
    142   EXPECT_EQ(100, static_cast<int>(y));
    143 
    144   x = 896.0;
    145   y = 100.0;
    146   device_manager->ApplyTouchTransformer(10, &x, &y);
    147   EXPECT_EQ(1024, static_cast<int>(x));
    148   EXPECT_EQ(100, static_cast<int>(y));
    149 }
    150 
    151 TEST_F(TouchTransformerControllerTest, TouchTransformerExtendedMode) {
    152   // The internal display has size 1366 x 768. The external display has
    153   // size 2560x1600. The total frame buffer is 2560x2428,
    154   // where 2428 = 768 + 60 (hidden gap) + 1600
    155   // and the sceond monitor is translated to Point (0, 828) in the
    156   // framebuffer.
    157   DisplayInfo display1 = CreateDisplayInfo(1, 5, gfx::Rect(0, 0, 1366, 768));
    158   DisplayInfo display2 = CreateDisplayInfo(2, 6, gfx::Rect(0, 828, 2560, 1600));
    159   gfx::Size fb_size(2560, 2428);
    160 
    161   TouchTransformerController* tt_controller =
    162       Shell::GetInstance()->touch_transformer_controller();
    163   ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
    164 
    165   device_manager->UpdateTouchInfoForDisplay(
    166       display1.id(),
    167       display1.touch_device_id(),
    168       tt_controller->GetExtendedModeTouchTransformer(display1, fb_size));
    169 
    170   device_manager->UpdateTouchInfoForDisplay(
    171       display2.id(),
    172       display2.touch_device_id(),
    173       tt_controller->GetExtendedModeTouchTransformer(display2, fb_size));
    174 
    175   EXPECT_EQ(1, device_manager->GetDisplayForTouchDevice(5));
    176   EXPECT_EQ(2, device_manager->GetDisplayForTouchDevice(6));
    177 
    178   // Mapping for touch events from internal touch display:
    179   // [0, 2560) x [0, 2428) -> [0, 1366) x [0, 768)
    180   float x = 0.0;
    181   float y = 0.0;
    182   device_manager->ApplyTouchTransformer(5, &x, &y);
    183   EXPECT_EQ(0, static_cast<int>(x));
    184   EXPECT_EQ(0, static_cast<int>(y));
    185 
    186   x = 2559.0;
    187   y = 2427.0;
    188   device_manager->ApplyTouchTransformer(5, &x, &y);
    189   EXPECT_EQ(1365, static_cast<int>(x));
    190   EXPECT_EQ(767, static_cast<int>(y));
    191 
    192   // Mapping for touch events from external touch display:
    193   // [0, 2560) x [0, 2428) -> [0, 2560) x [0, 1600)
    194   x = 0.0;
    195   y = 0.0;
    196   device_manager->ApplyTouchTransformer(6, &x, &y);
    197   EXPECT_EQ(0, static_cast<int>(x));
    198   EXPECT_EQ(0, static_cast<int>(y));
    199 
    200   x = 2559.0;
    201   y = 2427.0;
    202   device_manager->ApplyTouchTransformer(6, &x, &y);
    203   EXPECT_EQ(2559, static_cast<int>(x));
    204   EXPECT_EQ(1599, static_cast<int>(y));
    205 }
    206 
    207 TEST_F(TouchTransformerControllerTest, TouchRadiusScale) {
    208   DisplayInfo display = CreateDisplayInfo(1, 5, gfx::Rect(0, 0, 2560, 1600));
    209   std::vector<unsigned int> devices;
    210   devices.push_back(5);
    211   ui::SetUpTouchDevicesForTest(devices);
    212 
    213   TouchTransformerController* tt_controller =
    214       Shell::GetInstance()->touch_transformer_controller();
    215   // Default touchscreen position range is 1001x1001;
    216   EXPECT_EQ(sqrt((2560.0 * 1600.0) / (1001.0 * 1001.0)),
    217             tt_controller->GetTouchResolutionScale(display));
    218 }
    219 
    220 }  // namespace ash
    221