Chromium Code Reviews| 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 { | |
|
joedow
2017/04/28 21:29:53
This system is a bit different than other unit tes
Yuwei
2017/04/28 23:53:40
I just reversed the order to use the more common c
| |
| 21 public: | |
| 22 void SetUp() override; | |
| 23 void TearDown() override; | |
| 24 | |
| 25 protected: | |
| 26 void ExpectTransformation(const tracked_objects::Location& from_here, | |
|
joedow
2017/04/28 21:29:53
I'd rename this to SetExpectedTransform() or SetEx
Yuwei
2017/04/28 23:53:40
Renamed to AssertTransformationReceived.
| |
| 27 float scale, | |
| 28 float offset_x, | |
| 29 float offset_y); | |
| 30 | |
| 31 DesktopViewport viewport_; | |
| 32 | |
| 33 private: | |
| 34 void OnTransformationChanged(const std::array<float, 9>& matrix); | |
| 35 | |
| 36 bool expect_matrix_ = false; | |
| 37 std::array<float, 9> expected_matrix_; | |
|
joedow
2017/04/28 21:29:53
expected_transfrom_? The method above doesn't men
Yuwei
2017/04/28 23:53:40
Use |received_transformation_| now
| |
| 38 tracked_objects::Location expectation_location_; | |
| 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_FALSE(expect_matrix_); | |
| 50 } | |
| 51 | |
| 52 void DesktopViewportTest::ExpectTransformation( | |
| 53 const tracked_objects::Location& from_here, | |
| 54 float scale, | |
| 55 float offset_x, | |
| 56 float offset_y) { | |
| 57 ASSERT_FALSE(expect_matrix_) << "Previous matrix has not received yet."; | |
| 58 expect_matrix_ = true; | |
| 59 SimpleMatrix expected(scale, {offset_x, offset_y}); | |
| 60 expected_matrix_ = expected.ToMatrixArray(); | |
| 61 expectation_location_ = from_here; | |
| 62 } | |
| 63 | |
| 64 void DesktopViewportTest::OnTransformationChanged( | |
| 65 const std::array<float, 9>& matrix) { | |
| 66 ASSERT_TRUE(expect_matrix_); | |
| 67 | |
| 68 for (int i = 0; i < 9; i++) { | |
| 69 float diff = matrix[i] - expected_matrix_[i]; | |
| 70 ASSERT_TRUE(diff > -EPSILON && diff < EPSILON) | |
| 71 << "Matrix doesn't match. \n" | |
| 72 << base::StringPrintf("Expected scale: %f, offset: (%f, %f)\n", | |
| 73 expected_matrix_[0], expected_matrix_[2], | |
| 74 expected_matrix_[5]) | |
| 75 << base::StringPrintf("Actual scale: %f, offset: (%f, %f)\n", matrix[0], | |
| 76 matrix[2], matrix[5]) | |
| 77 << "Location: " << expectation_location_.ToString(); | |
| 78 } | |
| 79 | |
| 80 expect_matrix_ = false; | |
|
joedow
2017/04/28 21:29:53
If SimpleMatrix had an 'isEmpty()' method, you cou
Yuwei
2017/04/28 23:53:40
Done.
| |
| 81 } | |
| 82 | |
| 83 TEST_F(DesktopViewportTest, TestViewportInitialization1) { | |
| 84 // VP < DP. Desktop shrinks to fit. | |
| 85 // +====+------+ | |
| 86 // | VP | DP | | |
| 87 // | | | | |
| 88 // +====+------+ | |
| 89 ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 90 viewport_.SetDesktopSize(8, 6); | |
| 91 viewport_.SetSurfaceSize(2, 3); | |
| 92 } | |
| 93 | |
| 94 TEST_F(DesktopViewportTest, TestViewportInitialization2) { | |
| 95 // VP < DP. Desktop shrinks to fit. | |
| 96 // +-----------------+ | |
| 97 // | DP | | |
| 98 // | | | |
| 99 // +=================+ | |
| 100 // | VP | | |
| 101 // +=================+ | |
| 102 ExpectTransformation(FROM_HERE, 0.375, 0.f, 0.f); | |
| 103 viewport_.SetDesktopSize(8, 6); | |
| 104 viewport_.SetSurfaceSize(3, 2); | |
| 105 } | |
| 106 | |
| 107 TEST_F(DesktopViewportTest, TestViewportInitialization3) { | |
| 108 // VP < DP. Desktop shrinks to fit. | |
| 109 // +========+----+ | |
| 110 // | VP | DP | | |
| 111 // +========+----+ | |
| 112 ExpectTransformation(FROM_HERE, 0.333f, 0.f, 0.f); | |
| 113 viewport_.SetDesktopSize(9, 3); | |
| 114 viewport_.SetSurfaceSize(2, 1); | |
| 115 } | |
| 116 | |
| 117 TEST_F(DesktopViewportTest, TestViewportInitialization4) { | |
| 118 // VP > DP. Desktop grows to fit. | |
| 119 // +====+------+ | |
| 120 // | VP | DP | | |
| 121 // | | | | |
| 122 // +====+------+ | |
| 123 ExpectTransformation(FROM_HERE, 4.f, 0.f, 0.f); | |
| 124 viewport_.SetDesktopSize(2, 1); | |
| 125 viewport_.SetSurfaceSize(3, 4); | |
| 126 } | |
| 127 | |
| 128 TEST_F(DesktopViewportTest, TestMoveDesktop) { | |
| 129 // +====+------+ | |
| 130 // | VP | DP | | |
| 131 // | | | | |
| 132 // +====+------+ | |
| 133 ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 134 viewport_.SetDesktopSize(8, 6); | |
| 135 viewport_.SetSurfaceSize(2, 3); | |
| 136 | |
| 137 // <--- DP | |
| 138 // +------+====+ | |
| 139 // | DP | VP | | |
| 140 // | | | | |
| 141 // +------+====+ | |
| 142 ExpectTransformation(FROM_HERE, 0.5f, -2.f, 0.f); | |
| 143 viewport_.MoveDesktop(-2.f, 0.f); | |
| 144 | |
| 145 // +====+ | |
| 146 // +----| VP | | |
| 147 // | DP | | | |
| 148 // | +====+ | |
| 149 // +--------+ | |
| 150 // Bounces back. | |
| 151 ExpectTransformation(FROM_HERE, 0.5f, -2.f, 0.f); | |
| 152 viewport_.MoveDesktop(-1.f, 1.f); | |
| 153 } | |
| 154 | |
| 155 TEST_F(DesktopViewportTest, TestMoveAndScaleDesktop) { | |
| 156 // Number in surface coordinate. | |
| 157 // | |
| 158 // +====+------+ | |
| 159 // | VP | DP | | |
| 160 // | | | 3 | |
| 161 // +====+------+ | |
| 162 // 4 | |
| 163 ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 164 viewport_.SetDesktopSize(8, 6); | |
| 165 viewport_.SetSurfaceSize(2, 3); | |
| 166 | |
| 167 // Scale at pivot point (2, 3) by 1.5x. | |
| 168 // +------------------+ | |
| 169 // | | | |
| 170 // | +====+ DP | 4.5 | |
| 171 // | | VP | | | |
| 172 // | | | | | |
| 173 // +---+====+---------+ | |
| 174 // 2 6 | |
| 175 ExpectTransformation(FROM_HERE, 0.75f, -1.f, -1.5f); | |
| 176 viewport_.ScaleDesktop(2.f, 3.f, 1.5f); | |
| 177 | |
| 178 // Move VP to the top-right. | |
| 179 // +-------------+====+ | |
| 180 // | | VP | | |
| 181 // | DP | | | |
| 182 // | +====+ 4.5 | |
| 183 // | 2 | | |
| 184 // +------------------+ | |
| 185 // 6 | |
| 186 ExpectTransformation(FROM_HERE, 0.75f, -4.f, 0.f); | |
| 187 viewport_.MoveDesktop(-10000.f, 10000.f); | |
| 188 | |
| 189 // Scale at (2, 0) by 0.5x. | |
| 190 // VP | |
| 191 // +====+ | |
| 192 // +--+----+ | |
| 193 // DP | | | | |
| 194 // +--+----+ | |
| 195 // +====+ | |
| 196 ExpectTransformation(FROM_HERE, 0.375, -1.f, 0.375f); | |
| 197 viewport_.ScaleDesktop(2.f, 0.f, 0.5f); | |
| 198 | |
| 199 // Scale all the way down. | |
| 200 // +========+ | |
| 201 // | VP | | |
| 202 // +--------+ | |
| 203 // | DP | | |
| 204 // +--------+ | |
| 205 // | | | |
| 206 // +========+ | |
| 207 ExpectTransformation(FROM_HERE, 0.25f, 0.f, 0.75f); | |
| 208 viewport_.ScaleDesktop(20.f, 0.f, 0.0001f); | |
| 209 } | |
| 210 | |
| 211 TEST_F(DesktopViewportTest, TestSetViewportCenter) { | |
| 212 // Numbers in desktop coordinates. | |
| 213 // | |
| 214 // +====+------+ | |
| 215 // | VP | DP | | |
| 216 // | | | 6 | |
| 217 // +====+------+ | |
| 218 // 8 | |
| 219 ExpectTransformation(FROM_HERE, 0.5f, 0.f, 0.f); | |
| 220 viewport_.SetDesktopSize(8, 6); | |
| 221 viewport_.SetSurfaceSize(2, 3); | |
| 222 | |
| 223 // 1.6 | |
| 224 // +==+--------+ | |
| 225 // |VP|2.4 DP | | |
| 226 // +==+ | 6 | |
| 227 // +-----------+ | |
| 228 // 8 | |
| 229 ExpectTransformation(FROM_HERE, 1.25f, 0.f, 0.f); | |
| 230 viewport_.ScaleDesktop(0.f, 0.f, 2.5f); | |
| 231 | |
| 232 // Move VP to center of the desktop. | |
| 233 // +------------------+ | |
| 234 // | +1.6=+ | | |
| 235 // | | VP |2.4 | 6 | |
| 236 // | +====+ | | |
| 237 // +------------------+ | |
| 238 // 8 | |
| 239 ExpectTransformation(FROM_HERE, 1.25f, -4.f, -2.25f); | |
| 240 viewport_.SetViewportCenter(4.f, 3.f); | |
| 241 | |
| 242 // Move it out of bound and bounce it back. | |
| 243 // +------------------+ | |
| 244 // | | | |
| 245 // | DP | | |
| 246 // | +====+ | |
| 247 // | | VP | | |
| 248 // +---------------| | | |
| 249 // +====+ | |
| 250 ExpectTransformation(FROM_HERE, 1.25f, -8.f, -4.5f); | |
| 251 viewport_.SetViewportCenter(1000.f, 1000.f); | |
| 252 } | |
| 253 | |
| 254 } // namespace remoting | |
| OLD | NEW |