Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: remoting/client/desktop_viewport_unittest.cc

Issue 2843373005: [Remoting Client] DesktopViewport Implementation (Closed)
Patch Set: Rename SimpleMatrix to ViewMatrix Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/client/desktop_viewport.cc ('k') | remoting/client/view_matrix.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 DesktopViewport viewport_;
32
33 private:
34 void OnTransformationChanged(const ViewMatrix& matrix);
35
36 ViewMatrix received_transformation_;
37 };
38
39 void DesktopViewportTest::SetUp() {
40 viewport_.RegisterOnTransformationChangedCallback(
41 base::Bind(&DesktopViewportTest::OnTransformationChanged,
42 base::Unretained(this)),
43 true);
44 }
45
46 void DesktopViewportTest::TearDown() {
47 ASSERT_TRUE(received_transformation_.IsEmpty());
48 }
49
50 void DesktopViewportTest::AssertTransformationReceived(
51 const tracked_objects::Location& from_here,
52 float scale,
53 float offset_x,
54 float offset_y) {
55 ASSERT_FALSE(received_transformation_.IsEmpty())
56 << "Matrix has not been received yet."
57 << "Location: " << from_here.ToString();
58 ViewMatrix expected(scale, {offset_x, offset_y});
59 std::array<float, 9> expected_array = expected.ToMatrixArray();
60 std::array<float, 9> actual_array = received_transformation_.ToMatrixArray();
61
62 for (int i = 0; i < 9; i++) {
63 float diff = expected_array[i] - actual_array[i];
64 ASSERT_TRUE(diff > -EPSILON && diff < EPSILON)
65 << "Matrix doesn't match. \n"
66 << base::StringPrintf("Expected scale: %f, offset: (%f, %f)\n",
67 expected_array[0], expected_array[2],
68 expected_array[5])
69 << base::StringPrintf("Actual scale: %f, offset: (%f, %f)\n",
70 actual_array[0], actual_array[2], actual_array[5])
71 << "Location: " << from_here.ToString();
72 }
73
74 received_transformation_ = ViewMatrix();
75 }
76
77 void DesktopViewportTest::OnTransformationChanged(const ViewMatrix& matrix) {
78 ASSERT_TRUE(received_transformation_.IsEmpty())
79 << "Previous matrix has not been asserted.";
80 received_transformation_ = matrix;
81 }
82
83 TEST_F(DesktopViewportTest, TestViewportInitialization1) {
84 // VP < DP. Desktop shrinks to fit.
85 // +====+------+
86 // | VP | DP |
87 // | | |
88 // +====+------+
89 viewport_.SetDesktopSize(8, 6);
90 viewport_.SetSurfaceSize(2, 3);
91 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f);
92 }
93
94 TEST_F(DesktopViewportTest, TestViewportInitialization2) {
95 // VP < DP. Desktop shrinks to fit.
96 // +-----------------+
97 // | DP |
98 // | |
99 // +=================+
100 // | VP |
101 // +=================+
102 viewport_.SetDesktopSize(8, 6);
103 viewport_.SetSurfaceSize(3, 2);
104 AssertTransformationReceived(FROM_HERE, 0.375, 0.f, 0.f);
105 }
106
107 TEST_F(DesktopViewportTest, TestViewportInitialization3) {
108 // VP < DP. Desktop shrinks to fit.
109 // +========+----+
110 // | VP | DP |
111 // +========+----+
112 viewport_.SetDesktopSize(9, 3);
113 viewport_.SetSurfaceSize(2, 1);
114 AssertTransformationReceived(FROM_HERE, 0.333f, 0.f, 0.f);
115 }
116
117 TEST_F(DesktopViewportTest, TestViewportInitialization4) {
118 // VP > DP. Desktop grows to fit.
119 // +====+------+
120 // | VP | DP |
121 // | | |
122 // +====+------+
123 viewport_.SetDesktopSize(2, 1);
124 viewport_.SetSurfaceSize(3, 4);
125 AssertTransformationReceived(FROM_HERE, 4.f, 0.f, 0.f);
126 }
127
128 TEST_F(DesktopViewportTest, TestMoveDesktop) {
129 // +====+------+
130 // | VP | DP |
131 // | | |
132 // +====+------+
133 viewport_.SetDesktopSize(8, 6);
134 viewport_.SetSurfaceSize(2, 3);
135 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f);
136
137 // <--- DP
138 // +------+====+
139 // | DP | VP |
140 // | | |
141 // +------+====+
142 viewport_.MoveDesktop(-2.f, 0.f);
143 AssertTransformationReceived(FROM_HERE, 0.5f, -2.f, 0.f);
144
145 // +====+
146 // +----| VP |
147 // | DP | |
148 // | +====+
149 // +--------+
150 // Bounces back.
151 viewport_.MoveDesktop(-1.f, 1.f);
152 AssertTransformationReceived(FROM_HERE, 0.5f, -2.f, 0.f);
153 }
154
155 TEST_F(DesktopViewportTest, TestMoveAndScaleDesktop) {
156 // Number in surface coordinate.
157 //
158 // +====+------+
159 // | VP | DP |
160 // | | | 3
161 // +====+------+
162 // 4
163 viewport_.SetDesktopSize(8, 6);
164 viewport_.SetSurfaceSize(2, 3);
165 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f);
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 viewport_.ScaleDesktop(2.f, 3.f, 1.5f);
176 AssertTransformationReceived(FROM_HERE, 0.75f, -1.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 viewport_.MoveDesktop(-10000.f, 10000.f);
187 AssertTransformationReceived(FROM_HERE, 0.75f, -4.f, 0.f);
188
189 // Scale at (2, 0) by 0.5x.
190 // VP
191 // +====+
192 // +--+----+
193 // DP | | |
194 // +--+----+
195 // +====+
196 viewport_.ScaleDesktop(2.f, 0.f, 0.5f);
197 AssertTransformationReceived(FROM_HERE, 0.375, -1.f, 0.375f);
198
199 // Scale all the way down.
200 // +========+
201 // | VP |
202 // +--------+
203 // | DP |
204 // +--------+
205 // | |
206 // +========+
207 viewport_.ScaleDesktop(20.f, 0.f, 0.0001f);
208 AssertTransformationReceived(FROM_HERE, 0.25f, 0.f, 0.75f);
209 }
210
211 TEST_F(DesktopViewportTest, TestSetViewportCenter) {
212 // Numbers in desktop coordinates.
213 //
214 // +====+------+
215 // | VP | DP |
216 // | | | 6
217 // +====+------+
218 // 8
219 viewport_.SetDesktopSize(8, 6);
220 viewport_.SetSurfaceSize(2, 3);
221 AssertTransformationReceived(FROM_HERE, 0.5f, 0.f, 0.f);
222
223 // 1.6
224 // +==+--------+
225 // |VP|2.4 DP |
226 // +==+ | 6
227 // +-----------+
228 // 8
229 viewport_.ScaleDesktop(0.f, 0.f, 2.5f);
230 AssertTransformationReceived(FROM_HERE, 1.25f, 0.f, 0.f);
231
232 // Move VP to center of the desktop.
233 // +------------------+
234 // | +1.6=+ |
235 // | | VP |2.4 | 6
236 // | +====+ |
237 // +------------------+
238 // 8
239 viewport_.SetViewportCenter(4.f, 3.f);
240 AssertTransformationReceived(FROM_HERE, 1.25f, -4.f, -2.25f);
241
242 // Move it out of bound and bounce it back.
243 // +------------------+
244 // | |
245 // | DP |
246 // | +====+
247 // | | VP |
248 // +---------------| |
249 // +====+
250 viewport_.SetViewportCenter(1000.f, 1000.f);
251 AssertTransformationReceived(FROM_HERE, 1.25f, -8.f, -4.5f);
252 }
253
254 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/client/desktop_viewport.cc ('k') | remoting/client/view_matrix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698