| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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 "remoting/client/desktop_viewport.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace remoting { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const float EPSILON = 0.001f; | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 class DesktopViewportTest : public testing::Test { | |
| 21 public: | |
| 22 void SetUp() override; | |
| 23 void TearDown() override; | |
| 24 | |
| 25 protected: | |
| 26 void AssertTransformationReceived(const tracked_objects::Location& from_here, | |
| 27 float scale, | |
| 28 float offset_x, | |
| 29 float offset_y); | |
| 30 | |
| 31 ViewMatrix ReleaseReceivedTransformation(); | |
| 32 | |
| 33 DesktopViewport viewport_; | |
| 34 | |
| 35 private: | |
| 36 void OnTransformationChanged(const ViewMatrix& matrix); | |
| 37 | |
| 38 ViewMatrix received_transformation_; | |
| 39 }; | |
| 40 | |
| 41 void DesktopViewportTest::SetUp() { | |
| 42 viewport_.RegisterOnTransformationChangedCallback( | |
| 43 base::Bind(&DesktopViewportTest::OnTransformationChanged, | |
| 44 base::Unretained(this)), | |
| 45 true); | |
| 46 } | |
| 47 | |
| 48 void DesktopViewportTest::TearDown() { | |
| 49 ASSERT_TRUE(received_transformation_.IsEmpty()); | |
| 50 } | |
| 51 | |
| 52 void DesktopViewportTest::AssertTransformationReceived( | |
| 53 const tracked_objects::Location& from_here, | |
| 54 float scale, | |
| 55 float offset_x, | |
| 56 float offset_y) { | |
| 57 ASSERT_FALSE(received_transformation_.IsEmpty()) | |
| 58 << "Matrix has not been received yet." | |
| 59 << "Location: " << from_here.ToString(); | |
| 60 ViewMatrix expected(scale, {offset_x, offset_y}); | |
| 61 std::array<float, 9> expected_array = expected.ToMatrixArray(); | |
| 62 std::array<float, 9> actual_array = received_transformation_.ToMatrixArray(); | |
| 63 | |
| 64 for (int i = 0; i < 9; i++) { | |
| 65 float diff = expected_array[i] - actual_array[i]; | |
| 66 ASSERT_TRUE(diff > -EPSILON && diff < EPSILON) | |
| 67 << "Matrix doesn't match. \n" | |
| 68 << base::StringPrintf("Expected scale: %f, offset: (%f, %f)\n", | |
| 69 expected_array[0], expected_array[2], | |
| 70 expected_array[5]) | |
| 71 << base::StringPrintf("Actual scale: %f, offset: (%f, %f)\n", | |
| 72 actual_array[0], actual_array[2], actual_array[5]) | |
| 73 << "Location: " << from_here.ToString(); | |
| 74 } | |
| 75 | |
| 76 received_transformation_ = ViewMatrix(); | |
| 77 } | |
| 78 | |
| 79 ViewMatrix DesktopViewportTest::ReleaseReceivedTransformation() { | |
| 80 EXPECT_FALSE(received_transformation_.IsEmpty()); | |
| 81 ViewMatrix out = received_transformation_; | |
| 82 received_transformation_ = ViewMatrix(); | |
| 83 return out; | |
| 84 } | |
| 85 | |
| 86 void DesktopViewportTest::OnTransformationChanged(const ViewMatrix& matrix) { | |
| 87 ASSERT_TRUE(received_transformation_.IsEmpty()) | |
| 88 << "Previous matrix has not been asserted."; | |
| 89 received_transformation_ = matrix; | |
| 90 } | |
| 91 | |
| 92 TEST_F(DesktopViewportTest, TestViewportInitialization1) { | |
| 93 // VP < DP. Desktop shrinks to fit. | |
| 94 // +====+------+ | |
| 95 // | VP | DP | | |
| 96 // | | | | |
| 97 // +====+------+ | |
| 98 viewport_.SetDesktopSize(8, 6); | |
| 99 viewport_.SetSurfaceSize(2, 3); | |
| 100 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 101 } | |
| 102 | |
| 103 TEST_F(DesktopViewportTest, TestViewportInitialization2) { | |
| 104 // VP < DP. Desktop shrinks to fit. | |
| 105 // +-----------------+ | |
| 106 // | DP | | |
| 107 // | | | |
| 108 // +=================+ | |
| 109 // | VP | | |
| 110 // +=================+ | |
| 111 viewport_.SetDesktopSize(8, 6); | |
| 112 viewport_.SetSurfaceSize(3, 2); | |
| 113 AssertTransformationReceived(FROM_HERE, 0.375, 0.f, 0.f); | |
| 114 } | |
| 115 | |
| 116 TEST_F(DesktopViewportTest, TestViewportInitialization3) { | |
| 117 // VP < DP. Desktop shrinks to fit. | |
| 118 // +========+----+ | |
| 119 // | VP | DP | | |
| 120 // +========+----+ | |
| 121 viewport_.SetDesktopSize(9, 3); | |
| 122 viewport_.SetSurfaceSize(2, 1); | |
| 123 AssertTransformationReceived(FROM_HERE, 0.333f, 0.f, 0.f); | |
| 124 } | |
| 125 | |
| 126 TEST_F(DesktopViewportTest, TestViewportInitialization4) { | |
| 127 // VP > DP. Desktop grows to fit. | |
| 128 // +====+------+ | |
| 129 // | VP | DP | | |
| 130 // | | | | |
| 131 // +====+------+ | |
| 132 viewport_.SetDesktopSize(2, 1); | |
| 133 viewport_.SetSurfaceSize(3, 4); | |
| 134 AssertTransformationReceived(FROM_HERE, 4.f, 0.f, 0.f); | |
| 135 } | |
| 136 | |
| 137 TEST_F(DesktopViewportTest, TestMoveDesktop) { | |
| 138 // +====+------+ | |
| 139 // | VP | DP | | |
| 140 // | | | | |
| 141 // +====+------+ | |
| 142 viewport_.SetDesktopSize(8, 6); | |
| 143 viewport_.SetSurfaceSize(2, 3); | |
| 144 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 145 | |
| 146 // <--- DP | |
| 147 // +------+====+ | |
| 148 // | DP | VP | | |
| 149 // | | | | |
| 150 // +------+====+ | |
| 151 viewport_.MoveDesktop(-2.f, 0.f); | |
| 152 AssertTransformationReceived(FROM_HERE, 0.5f, -2.f, 0.f); | |
| 153 | |
| 154 // +====+ | |
| 155 // +----| VP | | |
| 156 // | DP | | | |
| 157 // | +====+ | |
| 158 // +--------+ | |
| 159 // Bounces back. | |
| 160 viewport_.MoveDesktop(-1.f, 1.f); | |
| 161 AssertTransformationReceived(FROM_HERE, 0.5f, -2.f, 0.f); | |
| 162 } | |
| 163 | |
| 164 TEST_F(DesktopViewportTest, TestMoveAndScaleDesktop) { | |
| 165 // Number in surface coordinate. | |
| 166 // | |
| 167 // +====+------+ | |
| 168 // | VP | DP | | |
| 169 // | | | 3 | |
| 170 // +====+------+ | |
| 171 // 4 | |
| 172 viewport_.SetDesktopSize(8, 6); | |
| 173 viewport_.SetSurfaceSize(2, 3); | |
| 174 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 175 | |
| 176 // Scale at pivot point (2, 3) by 1.5x. | |
| 177 // +------------------+ | |
| 178 // | | | |
| 179 // | +====+ DP | 4.5 | |
| 180 // | | VP | | | |
| 181 // | | | | | |
| 182 // +---+====+---------+ | |
| 183 // 2 6 | |
| 184 viewport_.ScaleDesktop(2.f, 3.f, 1.5f); | |
| 185 AssertTransformationReceived(FROM_HERE, 0.75f, -1.f, -1.5f); | |
| 186 | |
| 187 // Move VP to the top-right. | |
| 188 // +-------------+====+ | |
| 189 // | | VP | | |
| 190 // | DP | | | |
| 191 // | +====+ 4.5 | |
| 192 // | 2 | | |
| 193 // +------------------+ | |
| 194 // 6 | |
| 195 viewport_.MoveDesktop(-10000.f, 10000.f); | |
| 196 AssertTransformationReceived(FROM_HERE, 0.75f, -4.f, 0.f); | |
| 197 | |
| 198 // Scale at (2, 0) by 0.5x. | |
| 199 // VP | |
| 200 // +====+ | |
| 201 // +--+----+ | |
| 202 // DP | | | | |
| 203 // +--+----+ | |
| 204 // +====+ | |
| 205 viewport_.ScaleDesktop(2.f, 0.f, 0.5f); | |
| 206 AssertTransformationReceived(FROM_HERE, 0.375, -1.f, 0.375f); | |
| 207 | |
| 208 // Scale all the way down. | |
| 209 // +========+ | |
| 210 // | VP | | |
| 211 // +--------+ | |
| 212 // | DP | | |
| 213 // +--------+ | |
| 214 // | | | |
| 215 // +========+ | |
| 216 viewport_.ScaleDesktop(20.f, 0.f, 0.0001f); | |
| 217 AssertTransformationReceived(FROM_HERE, 0.25f, 0.f, 0.75f); | |
| 218 } | |
| 219 | |
| 220 TEST_F(DesktopViewportTest, TestSetViewportCenter) { | |
| 221 // Numbers in desktop coordinates. | |
| 222 // | |
| 223 // +====+------+ | |
| 224 // | VP | DP | | |
| 225 // | | | 6 | |
| 226 // +====+------+ | |
| 227 // 8 | |
| 228 viewport_.SetDesktopSize(8, 6); | |
| 229 viewport_.SetSurfaceSize(2, 3); | |
| 230 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 231 | |
| 232 // 1.6 | |
| 233 // +==+--------+ | |
| 234 // |VP|2.4 DP | | |
| 235 // +==+ | 6 | |
| 236 // +-----------+ | |
| 237 // 8 | |
| 238 viewport_.ScaleDesktop(0.f, 0.f, 2.5f); | |
| 239 AssertTransformationReceived(FROM_HERE, 1.25f, 0.f, 0.f); | |
| 240 | |
| 241 // Move VP to center of the desktop. | |
| 242 // +------------------+ | |
| 243 // | +1.6=+ | | |
| 244 // | | VP |2.4 | 6 | |
| 245 // | +====+ | | |
| 246 // +------------------+ | |
| 247 // 8 | |
| 248 viewport_.SetViewportCenter(4.f, 3.f); | |
| 249 AssertTransformationReceived(FROM_HERE, 1.25f, -4.f, -2.25f); | |
| 250 | |
| 251 // Move it out of bound and bounce it back. | |
| 252 // +------------------+ | |
| 253 // | | | |
| 254 // | DP | | |
| 255 // | +====+ | |
| 256 // | | VP | | |
| 257 // +---------------| | | |
| 258 // +====+ | |
| 259 viewport_.SetViewportCenter(1000.f, 1000.f); | |
| 260 AssertTransformationReceived(FROM_HERE, 1.25f, -8.f, -4.5f); | |
| 261 } | |
| 262 | |
| 263 TEST_F(DesktopViewportTest, TestScaleDesktop) { | |
| 264 // Number in surface coordinate. | |
| 265 // | |
| 266 // +====+------+ | |
| 267 // | VP | DP | | |
| 268 // | | | 3 | |
| 269 // +====+------+ | |
| 270 // 4 | |
| 271 viewport_.SetDesktopSize(8, 6); | |
| 272 viewport_.SetSurfaceSize(2, 3); | |
| 273 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 274 | |
| 275 ViewMatrix old_transformation(0.5f, {0.f, 0.f}); | |
| 276 | |
| 277 ViewMatrix::Point surface_point = old_transformation.MapPoint({1.2f, 1.3f}); | |
| 278 | |
| 279 // Scale a little bit at a pivot point. | |
| 280 viewport_.ScaleDesktop(surface_point.x, surface_point.y, 1.1f); | |
| 281 | |
| 282 ViewMatrix new_transformation = ReleaseReceivedTransformation(); | |
| 283 | |
| 284 // Verify the pivot point is fixed. | |
| 285 ViewMatrix::Point new_surface_point = | |
| 286 new_transformation.MapPoint({1.2f, 1.3f}); | |
| 287 ASSERT_FLOAT_EQ(surface_point.x, new_surface_point.x); | |
| 288 ASSERT_FLOAT_EQ(surface_point.y, new_surface_point.y); | |
| 289 | |
| 290 // Verify the scale is correct. | |
| 291 ASSERT_FLOAT_EQ(old_transformation.GetScale() * 1.1f, | |
| 292 new_transformation.GetScale()); | |
| 293 } | |
| 294 | |
| 295 } // namespace remoting | |
| OLD | NEW |