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.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_vector.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/display/chromeos/display_configurator.h"
11 #include "ui/display/chromeos/test/test_display_snapshot.h"
12 #include "ui/display/chromeos/touchscreen_delegate_impl.h"
13 #include "ui/display/types/chromeos/touchscreen_device_manager.h"
19 class MockTouchscreenDeviceManager
: public TouchscreenDeviceManager
{
21 MockTouchscreenDeviceManager() {}
22 virtual ~MockTouchscreenDeviceManager() {}
24 void AddDevice(const TouchscreenDevice
& device
) {
25 devices_
.push_back(device
);
28 // TouchscreenDeviceManager overrides:
29 virtual std::vector
<TouchscreenDevice
> GetDevices() OVERRIDE
{
34 std::vector
<TouchscreenDevice
> devices_
;
36 DISALLOW_COPY_AND_ASSIGN(MockTouchscreenDeviceManager
);
41 class TouchscreenDelegateImplTest
: public testing::Test
{
43 TouchscreenDelegateImplTest() {}
44 virtual ~TouchscreenDelegateImplTest() {}
46 virtual void SetUp() OVERRIDE
{
47 device_manager_
= new MockTouchscreenDeviceManager();
48 delegate_
.reset(new TouchscreenDelegateImpl(
49 scoped_ptr
<TouchscreenDeviceManager
>(device_manager_
)));
51 // Internal display. Must not be matched to a touch screen unless the size
53 TestDisplaySnapshot
* snapshot
= new TestDisplaySnapshot();
54 DisplayMode
* mode
= new DisplayMode(gfx::Size(1920, 1080), false, 60.0);
55 snapshot
->set_type(DISPLAY_CONNECTION_TYPE_INTERNAL
);
56 snapshot
->set_modes(std::vector
<const DisplayMode
*>(1, mode
));
57 snapshot
->set_native_mode(mode
);
58 displays_
.push_back(snapshot
);
60 snapshot
= new TestDisplaySnapshot();
61 mode
= new DisplayMode(gfx::Size(800, 600), false, 60.0);
62 snapshot
->set_modes(std::vector
<const DisplayMode
*>(1, mode
));
63 snapshot
->set_native_mode(mode
);
64 displays_
.push_back(snapshot
);
66 // Display without native mode. Must not be matched to any touch screen.
67 displays_
.push_back(new TestDisplaySnapshot());
69 snapshot
= new TestDisplaySnapshot();
70 mode
= new DisplayMode(gfx::Size(1024, 768), false, 60.0);
71 snapshot
->set_modes(std::vector
<const DisplayMode
*>(1, mode
));
72 snapshot
->set_native_mode(mode
);
73 displays_
.push_back(snapshot
);
76 virtual void TearDown() OVERRIDE
{
77 // The snapshots do not own the modes, so we need to delete them.
78 for (size_t i
= 0; i
< displays_
.size(); ++i
) {
79 if (displays_
[i
]->native_mode())
80 delete displays_
[i
]->native_mode();
86 std::vector
<DisplayConfigurator::DisplayState
> GetDisplayStates() {
87 std::vector
<DisplayConfigurator::DisplayState
> states(displays_
.size());
88 for (size_t i
= 0; i
< displays_
.size(); ++i
)
89 states
[i
].display
= displays_
[i
];
95 MockTouchscreenDeviceManager
* device_manager_
; // Not owned.
96 scoped_ptr
<TouchscreenDelegateImpl
> delegate_
;
97 ScopedVector
<DisplaySnapshot
> displays_
;
100 DISALLOW_COPY_AND_ASSIGN(TouchscreenDelegateImplTest
);
103 TEST_F(TouchscreenDelegateImplTest
, NoTouchscreens
) {
104 std::vector
<DisplayConfigurator::DisplayState
> display_states
=
106 delegate_
->AssociateTouchscreens(&display_states
);
108 for (size_t i
= 0; i
< display_states
.size(); ++i
)
109 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[i
].touch_device_id
);
112 TEST_F(TouchscreenDelegateImplTest
, OneToOneMapping
) {
113 device_manager_
->AddDevice(TouchscreenDevice(1, gfx::Size(800, 600)));
114 device_manager_
->AddDevice(TouchscreenDevice(2, gfx::Size(1024, 768)));
116 std::vector
<DisplayConfigurator::DisplayState
> display_states
=
118 delegate_
->AssociateTouchscreens(&display_states
);
120 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[0].touch_device_id
);
121 EXPECT_EQ(1, display_states
[1].touch_device_id
);
122 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[2].touch_device_id
);
123 EXPECT_EQ(2, display_states
[3].touch_device_id
);
126 TEST_F(TouchscreenDelegateImplTest
, MapToCorrectDisplaySize
) {
127 device_manager_
->AddDevice(TouchscreenDevice(2, gfx::Size(1024, 768)));
129 std::vector
<DisplayConfigurator::DisplayState
> display_states
=
131 delegate_
->AssociateTouchscreens(&display_states
);
133 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[0].touch_device_id
);
134 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[1].touch_device_id
);
135 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[2].touch_device_id
);
136 EXPECT_EQ(2, display_states
[3].touch_device_id
);
139 TEST_F(TouchscreenDelegateImplTest
, MapWhenSizeDiffersByOne
) {
140 device_manager_
->AddDevice(TouchscreenDevice(1, gfx::Size(801, 600)));
141 device_manager_
->AddDevice(TouchscreenDevice(2, gfx::Size(1023, 768)));
143 std::vector
<DisplayConfigurator::DisplayState
> display_states
=
145 delegate_
->AssociateTouchscreens(&display_states
);
147 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[0].touch_device_id
);
148 EXPECT_EQ(1, display_states
[1].touch_device_id
);
149 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[2].touch_device_id
);
150 EXPECT_EQ(2, display_states
[3].touch_device_id
);
153 TEST_F(TouchscreenDelegateImplTest
, MapWhenSizesDoNotMatch
) {
154 device_manager_
->AddDevice(TouchscreenDevice(1, gfx::Size(1022, 768)));
155 device_manager_
->AddDevice(TouchscreenDevice(2, gfx::Size(802, 600)));
157 std::vector
<DisplayConfigurator::DisplayState
> display_states
=
159 delegate_
->AssociateTouchscreens(&display_states
);
161 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[0].touch_device_id
);
162 EXPECT_EQ(1, display_states
[1].touch_device_id
);
163 EXPECT_EQ(TouchscreenDevice::kInvalidId
, display_states
[2].touch_device_id
);
164 EXPECT_EQ(2, display_states
[3].touch_device_id
);