Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / display / chromeos / touchscreen_delegate_impl_unittest.cc
blob9e80712177d5e3813304868658b5c9e842fe3020
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 <vector>
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"
15 namespace ui {
17 namespace {
19 class MockTouchscreenDeviceManager : public TouchscreenDeviceManager {
20 public:
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 {
30 return devices_;
33 private:
34 std::vector<TouchscreenDevice> devices_;
36 DISALLOW_COPY_AND_ASSIGN(MockTouchscreenDeviceManager);
39 } // namespace
41 class TouchscreenDelegateImplTest : public testing::Test {
42 public:
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 will always match to internal touchscreen. If internal
52 // touchscreen can't be detected, it is then associated to a touch screen
53 // with matching size.
54 TestDisplaySnapshot* snapshot = new TestDisplaySnapshot();
55 DisplayMode* mode = new DisplayMode(gfx::Size(1920, 1080), false, 60.0);
56 snapshot->set_type(DISPLAY_CONNECTION_TYPE_INTERNAL);
57 snapshot->set_modes(std::vector<const DisplayMode*>(1, mode));
58 snapshot->set_native_mode(mode);
59 displays_.push_back(snapshot);
61 snapshot = new TestDisplaySnapshot();
62 mode = new DisplayMode(gfx::Size(800, 600), false, 60.0);
63 snapshot->set_modes(std::vector<const DisplayMode*>(1, mode));
64 snapshot->set_native_mode(mode);
65 displays_.push_back(snapshot);
67 // Display without native mode. Must not be matched to any touch screen.
68 displays_.push_back(new TestDisplaySnapshot());
70 snapshot = new TestDisplaySnapshot();
71 mode = new DisplayMode(gfx::Size(1024, 768), false, 60.0);
72 snapshot->set_modes(std::vector<const DisplayMode*>(1, mode));
73 snapshot->set_native_mode(mode);
74 displays_.push_back(snapshot);
77 virtual void TearDown() OVERRIDE {
78 // The snapshots do not own the modes, so we need to delete them.
79 for (size_t i = 0; i < displays_.size(); ++i) {
80 if (displays_[i]->native_mode())
81 delete displays_[i]->native_mode();
84 displays_.clear();
87 std::vector<DisplayConfigurator::DisplayState> GetDisplayStates() {
88 std::vector<DisplayConfigurator::DisplayState> states(displays_.size());
89 for (size_t i = 0; i < displays_.size(); ++i)
90 states[i].display = displays_[i];
92 return states;
95 protected:
96 MockTouchscreenDeviceManager* device_manager_; // Not owned.
97 scoped_ptr<TouchscreenDelegateImpl> delegate_;
98 ScopedVector<DisplaySnapshot> displays_;
100 private:
101 DISALLOW_COPY_AND_ASSIGN(TouchscreenDelegateImplTest);
104 TEST_F(TouchscreenDelegateImplTest, NoTouchscreens) {
105 std::vector<DisplayConfigurator::DisplayState> display_states =
106 GetDisplayStates();
107 delegate_->AssociateTouchscreens(&display_states);
109 for (size_t i = 0; i < display_states.size(); ++i)
110 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[i].touch_device_id);
113 TEST_F(TouchscreenDelegateImplTest, OneToOneMapping) {
114 device_manager_->AddDevice(TouchscreenDevice(1, gfx::Size(800, 600), false));
115 device_manager_->AddDevice(TouchscreenDevice(2, gfx::Size(1024, 768), false));
117 std::vector<DisplayConfigurator::DisplayState> display_states =
118 GetDisplayStates();
119 delegate_->AssociateTouchscreens(&display_states);
121 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[0].touch_device_id);
122 EXPECT_EQ(1, display_states[1].touch_device_id);
123 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[2].touch_device_id);
124 EXPECT_EQ(2, display_states[3].touch_device_id);
127 TEST_F(TouchscreenDelegateImplTest, MapToCorrectDisplaySize) {
128 device_manager_->AddDevice(TouchscreenDevice(2, gfx::Size(1024, 768), false));
130 std::vector<DisplayConfigurator::DisplayState> display_states =
131 GetDisplayStates();
132 delegate_->AssociateTouchscreens(&display_states);
134 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[0].touch_device_id);
135 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[1].touch_device_id);
136 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[2].touch_device_id);
137 EXPECT_EQ(2, display_states[3].touch_device_id);
140 TEST_F(TouchscreenDelegateImplTest, MapWhenSizeDiffersByOne) {
141 device_manager_->AddDevice(TouchscreenDevice(1, gfx::Size(801, 600), false));
142 device_manager_->AddDevice(TouchscreenDevice(2, gfx::Size(1023, 768), false));
144 std::vector<DisplayConfigurator::DisplayState> display_states =
145 GetDisplayStates();
146 delegate_->AssociateTouchscreens(&display_states);
148 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[0].touch_device_id);
149 EXPECT_EQ(1, display_states[1].touch_device_id);
150 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[2].touch_device_id);
151 EXPECT_EQ(2, display_states[3].touch_device_id);
154 TEST_F(TouchscreenDelegateImplTest, MapWhenSizesDoNotMatch) {
155 device_manager_->AddDevice(TouchscreenDevice(1, gfx::Size(1022, 768), false));
156 device_manager_->AddDevice(TouchscreenDevice(2, gfx::Size(802, 600), false));
158 std::vector<DisplayConfigurator::DisplayState> display_states =
159 GetDisplayStates();
160 delegate_->AssociateTouchscreens(&display_states);
162 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[0].touch_device_id);
163 EXPECT_EQ(1, display_states[1].touch_device_id);
164 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[2].touch_device_id);
165 EXPECT_EQ(2, display_states[3].touch_device_id);
168 TEST_F(TouchscreenDelegateImplTest, MapInternalTouchscreen) {
169 device_manager_->AddDevice(
170 TouchscreenDevice(1, gfx::Size(1920, 1080), false));
171 device_manager_->AddDevice(TouchscreenDevice(2, gfx::Size(9999, 888), true));
173 std::vector<DisplayConfigurator::DisplayState> display_states =
174 GetDisplayStates();
175 delegate_->AssociateTouchscreens(&display_states);
177 // Internal touchscreen is always mapped to internal display.
178 EXPECT_EQ(2, display_states[0].touch_device_id);
179 EXPECT_EQ(1, display_states[1].touch_device_id);
180 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[2].touch_device_id);
181 EXPECT_EQ(TouchscreenDevice::kInvalidId, display_states[3].touch_device_id);
184 } // namespace ui