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

Side by Side Diff: content/renderer/media/media_stream_constraints_util_sets_unittest.cc

Issue 2728633002: Add utility set classes to support getUserMedia constraint proccessing. (Closed)
Patch Set: Created 3 years, 9 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
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 "content/renderer/media/media_stream_constraints_util_sets.h"
6
7 #include <cmath>
8 #include <string>
9 #include <vector>
10
11 #include "content/renderer/media/media_stream_video_source.h"
12 #include "content/renderer/media/mock_constraint_factory.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace content {
16
17 using Point = ResolutionSet::Point;
18
19 namespace {
20
21 int kUnconstrainedMaxDimension = ResolutionSet::kMaxConstrainedDimension + 1;
22
23 // Checks if |point| is an element of |vertices| using
24 // Point::IsApproximatelyEqualTo() to test for equality.
25 void VerticesContain(const std::vector<Point>& vertices, const Point& point) {
26 bool result = false;
27 for (const auto& vertex : vertices) {
28 if (vertex.IsApproximatelyEqualTo(point)) {
29 result = true;
30 break;
31 }
32 }
33 EXPECT_TRUE(result);
34 }
35
36 // Determines if |vertices| is valid according to the contract for
37 // ResolutionCandidateSet::ComputeVertices().
38 bool AreValidVertices(const std::vector<Point>& vertices) {
39 // Single-segment, single point or single segment are trivial cases.
40 if (vertices.size() <= 2)
41 return true;
42 else if (vertices.size() > 6) // Polygons of more than 6 sides are not valid.
43 return false;
44
45 // For the case of 3 to 6 vertices, the vertices must be in counterclockwise
46 // order. Compute orientation using the determinant of each diagonal in the
47 // polygon, using the first vertex as reference.
48 Point prev_diagonal = vertices[1] - vertices[0];
49 for (auto vertex = vertices.begin() + 2; vertex != vertices.end(); ++vertex) {
50 Point current_diagonal = *vertex - vertices[0];
51 // The determinant of the two diagonals returns the signed area of the
52 // parallelogram they generate. The area is positive if the diagonals are in
53 // counterclockwise order, zero if the diagonals have the same direction and
54 // negative if the diagonals are in clockwise order.
55 // See https://en.wikipedia.org/wiki/Determinant#2_.C3.97_2_matrices.
56 double det = prev_diagonal.height() * current_diagonal.width() -
57 current_diagonal.height() * prev_diagonal.width();
58 if (det <= 0)
59 return false;
60 prev_diagonal = current_diagonal;
61 }
62 return true;
63 }
64
65 // This function provides an alternative method for computing the projection
66 // of |point| on the line of segment |s1||s2| to be used to compare the results
67 // provided by Point::ClosestPointInSegment(). Since it relies on library
68 // functions that use iterative numeric methods, it is less accurate in practice
hta - Chromium 2017/03/03 11:02:36 Is this ("iterative numeric methods") a reference
Guido Urdaneta 2017/03/06 11:08:23 Done.
69 // than Point::ClosestPointInSegment().
70 // This function only computes projections. The result may be outside the
71 // segment |s1||s2|.
72 Point ProjectionOnSegmentLine(const Point& point,
73 const Point& s1,
74 const Point& s2) {
75 double segment_slope =
76 (s2.width() - s1.width()) / (s2.height() - s1.height());
77 double segment_angle = std::atan(segment_slope);
78 double norm = std::sqrt(Point::Dot(point, point));
79 double angle =
80 (point.height() == 0 && point.width() == 0)
81 ? 0.0
82 : std::atan(point.width() / point.height()) - segment_angle;
83 double projection_length = norm * std::cos(angle);
84 double projection_height = projection_length * std::cos(segment_angle);
85 double projection_width = projection_length * std::sin(segment_angle);
86 return Point(projection_height, projection_width);
87 }
88
89 } // namespace
hta - Chromium 2017/03/03 11:02:36 Why not let the anonymous namespace enclose the te
Guido Urdaneta 2017/03/06 11:08:23 One of the tests is friended by MediaStreamConstra
90
91 class MediaStreamConstraintsUtilSetsTest : public testing::Test {
92 protected:
93 using P = Point;
94 MockConstraintFactory factory_;
95 };
96
97 TEST_F(MediaStreamConstraintsUtilSetsTest, VertexListValidity) {
hta - Chromium 2017/03/03 11:02:36 This actually tests a test harness function. Add a
Guido Urdaneta 2017/03/06 11:08:23 Done.
98 EXPECT_TRUE(AreValidVertices({P(1, 1)}));
99 EXPECT_TRUE(AreValidVertices({P(1, 1)}));
100 EXPECT_TRUE(AreValidVertices({P(1, 0), P(0, 1)}));
101 EXPECT_TRUE(AreValidVertices({P(1, 1), P(0, 0), P(1, 0)}));
102
103 // Not in counterclockwise order.
104 EXPECT_FALSE(AreValidVertices({P(1, 0), P(0, 0), P(1, 1)}));
105
106 // Final vertex aligned with the previous two vertices.
107 EXPECT_FALSE(AreValidVertices({P(1, 0), P(1, 1), P(1, 1.5), P(1, 0.1)}));
108
109 // Not in counterclockwise order.
110 EXPECT_FALSE(
111 AreValidVertices({P(1, 0), P(3, 0), P(2, 2), P(3.1, 1), P(0, 1)}));
112
113 EXPECT_TRUE(AreValidVertices(
114 {P(1, 0), P(3, 0), P(3.1, 1), P(3, 2), P(1, 2), P(0.9, 1)}));
115
116 // Not in counterclockwise order.
117 EXPECT_FALSE(AreValidVertices(
118 {P(1, 0), P(3, 0), P(3.1, 1), P(1, 2), P(3, 2), P(0.9, 1)}));
119
120 // Counterclockwise, but more than 6 vertices.
121 EXPECT_FALSE(AreValidVertices(
122 {P(1, 0), P(3, 0), P(3.1, 1), P(3, 2), P(2, 2.1), P(1, 2), P(0.9, 1)}));
123 }
124
125 TEST_F(MediaStreamConstraintsUtilSetsTest, PointOperations) {
126 const Point kZero(0, 0);
127
128 // Basic equality and inequality
129 EXPECT_EQ(P(0, 0), kZero);
130 EXPECT_EQ(P(50, 50), P(50, 50));
131 EXPECT_NE(kZero, P(50, 50));
132 EXPECT_NE(P(50, 50), P(100, 100));
133 EXPECT_NE(P(50, 50), P(100, 50));
134
135 // Operations with zero.
136 EXPECT_EQ(kZero, kZero + kZero);
137 EXPECT_EQ(kZero, kZero - kZero);
138 EXPECT_EQ(kZero, 0.0 * kZero);
139 EXPECT_EQ(0.0, P::Dot(kZero, kZero));
140 EXPECT_EQ(0.0, P::DistanceToPoint(kZero, kZero));
141 EXPECT_EQ(kZero, P::ClosestPointInSegment(kZero, kZero, kZero));
142
143 // Operations with zero and nonzero values.
144 EXPECT_EQ(P(50, 50), kZero + P(50, 50));
145 EXPECT_EQ(P(50, 50) + kZero, kZero + P(50, 50));
146 EXPECT_EQ(P(50, 50), P(50, 50) - kZero);
147 EXPECT_EQ(kZero, P(50, 50) - P(50, 50));
148 EXPECT_EQ(kZero, 0.0 * P(50, 50));
149 EXPECT_EQ(0.0, P::Dot(kZero, P(50, 50)));
150 EXPECT_EQ(0.0, P::Dot(P(50, 50), kZero));
151 EXPECT_EQ(5000, P::DistanceToPoint(kZero, P(50, 50)));
152 EXPECT_EQ(P::DistanceToPoint(P(50, 50), kZero),
153 P::DistanceToPoint(kZero, P(50, 50)));
154 EXPECT_EQ(kZero, P::ClosestPointInSegment(kZero, kZero, P(50, 50)));
155 EXPECT_EQ(kZero, P::ClosestPointInSegment(kZero, P(50, 50), kZero));
156 EXPECT_EQ(P(50, 50),
157 P::ClosestPointInSegment(P(50, 50), P(50, 50), P(50, 50)));
158
159 // Operations with nonzero values.
160 // Additions.
161 EXPECT_EQ(P(100, 50), P(50, 50) + P(50, 0));
162 EXPECT_EQ(P(100, 50), P(50, 0) + P(50, 50));
163
164 // Substractions.
165 EXPECT_EQ(P(50, 50), P(100, 100) - P(50, 50));
166 EXPECT_EQ(P(50, 50), P(100, 50) - P(50, 0));
167 EXPECT_EQ(P(50, 0), P(100, 50) - P(50, 50));
168
169 // Scalar-vector products.
170 EXPECT_EQ(P(50, 50), 1.0 * P(50, 50));
171 EXPECT_EQ(P(75, 75), 1.5 * P(50, 50));
172 EXPECT_EQ(P(200, 100), 2.0 * P(100, 50));
173 EXPECT_EQ(2.0 * (P(100, 100) + P(100, 50)),
174 2.0 * P(100, 100) + 2.0 * P(100, 50));
175
176 // Dot products.
177 EXPECT_EQ(2 * 50 * 100, P::Dot(P(50, 50), P(100, 100)));
178 EXPECT_EQ(P::Dot(P(100, 100), P(50, 50)), P::Dot(P(50, 50), P(100, 100)));
179 EXPECT_EQ(0, P::Dot(P(100, 0), P(0, 100)));
180
181 // Distances.
182 EXPECT_EQ(25, P::DistanceToPoint(P(4, 0), P(0, 3)));
183 EXPECT_EQ(75 * 75, P::DistanceToPoint(P(100, 0), P(25, 0)));
184 EXPECT_EQ(75 * 75, P::DistanceToPoint(P(0, 100), P(0, 25)));
185 EXPECT_EQ(5 * 5 + 9 * 9, P::DistanceToPoint(P(5, 1), P(10, 10)));
186
187 // Closest point to segment from (10,0) to (50,0).
188 EXPECT_EQ(P(25, 0), P::ClosestPointInSegment(P(25, 25), P(10, 0), P(50, 0)));
189 EXPECT_EQ(P(50, 0),
190 P::ClosestPointInSegment(P(100, 100), P(10, 0), P(50, 0)));
191 EXPECT_EQ(P(10, 0), P::ClosestPointInSegment(P(0, 100), P(10, 0), P(50, 0)));
192
193 // Closest point to segment from (0,10) to (0,50).
194 EXPECT_EQ(P(0, 25), P::ClosestPointInSegment(P(25, 25), P(0, 10), P(0, 50)));
195 EXPECT_EQ(P(0, 50),
196 P::ClosestPointInSegment(P(100, 100), P(0, 10), P(0, 50)));
197 EXPECT_EQ(P(0, 10), P::ClosestPointInSegment(P(100, 0), P(0, 10), P(0, 50)));
198
199 // Closest point to segment from (0,10) to (10,0).
200 EXPECT_EQ(P(5, 5), P::ClosestPointInSegment(P(25, 25), P(0, 10), P(10, 0)));
201 EXPECT_EQ(P(5, 5), P::ClosestPointInSegment(P(100, 100), P(0, 10), P(10, 0)));
202 EXPECT_EQ(P(10, 0), P::ClosestPointInSegment(P(100, 0), P(0, 10), P(10, 0)));
203 EXPECT_EQ(P(0, 10), P::ClosestPointInSegment(P(0, 100), P(0, 10), P(10, 0)));
204 }
205
206 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionUnconstrained) {
207 ResolutionSet set;
208 EXPECT_TRUE(set.ContainsPoint(0, 0));
209 EXPECT_TRUE(set.ContainsPoint(1, 1));
210 EXPECT_TRUE(set.ContainsPoint(2000, 2000));
211 EXPECT_FALSE(set.HasMinHeight());
212 EXPECT_FALSE(set.HasMaxHeight());
213 EXPECT_FALSE(set.HasMinWidth());
214 EXPECT_FALSE(set.HasMaxWidth());
215 EXPECT_FALSE(set.HasMinAspectRatio());
216 EXPECT_FALSE(set.HasMaxAspectRatio());
217 EXPECT_TRUE(set.IsHeightUnconstrained());
218 EXPECT_TRUE(set.IsWidthUnconstrained());
219 EXPECT_TRUE(set.IsAspectRatioUnconstrained());
220 EXPECT_FALSE(set.IsHeightEmpty());
221 EXPECT_FALSE(set.IsWidthEmpty());
222 EXPECT_FALSE(set.IsAspectRatioEmpty());
223 }
224
225 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionConstrained) {
226 ResolutionSet set = ResolutionSet::FromHeight(10, 100);
227 EXPECT_FALSE(set.ContainsPoint(0, 0));
228 EXPECT_TRUE(set.ContainsPoint(10, 10));
229 EXPECT_TRUE(set.ContainsPoint(50, 50));
230 EXPECT_TRUE(set.ContainsPoint(100, 100));
231 EXPECT_FALSE(set.ContainsPoint(500, 500));
232 EXPECT_TRUE(set.HasMinHeight());
233 EXPECT_TRUE(set.HasMaxHeight());
234 EXPECT_FALSE(set.HasMinWidth());
235 EXPECT_FALSE(set.HasMaxWidth());
236 EXPECT_FALSE(set.HasMinAspectRatio());
237 EXPECT_FALSE(set.HasMaxAspectRatio());
238 EXPECT_FALSE(set.IsHeightUnconstrained());
239 EXPECT_TRUE(set.IsWidthUnconstrained());
240 EXPECT_TRUE(set.IsAspectRatioUnconstrained());
241 EXPECT_FALSE(set.IsHeightEmpty());
242 EXPECT_FALSE(set.IsWidthEmpty());
243 EXPECT_FALSE(set.IsAspectRatioEmpty());
244
245 set = ResolutionSet::FromHeight(0, 100);
246 EXPECT_TRUE(set.ContainsPoint(0, 0));
247 EXPECT_TRUE(set.ContainsPoint(10, 10));
248 EXPECT_TRUE(set.ContainsPoint(50, 50));
249 EXPECT_TRUE(set.ContainsPoint(100, 100));
250 EXPECT_FALSE(set.ContainsPoint(500, 500));
251 EXPECT_FALSE(set.HasMinHeight());
252 EXPECT_TRUE(set.HasMaxHeight());
253 EXPECT_FALSE(set.HasMinWidth());
254 EXPECT_FALSE(set.HasMaxWidth());
255 EXPECT_FALSE(set.HasMinAspectRatio());
256 EXPECT_FALSE(set.HasMaxAspectRatio());
257 EXPECT_FALSE(set.IsHeightUnconstrained());
258 EXPECT_TRUE(set.IsWidthUnconstrained());
259 EXPECT_TRUE(set.IsAspectRatioUnconstrained());
260 EXPECT_FALSE(set.IsHeightEmpty());
261 EXPECT_FALSE(set.IsWidthEmpty());
262 EXPECT_FALSE(set.IsAspectRatioEmpty());
263
264 set = ResolutionSet::FromHeight(100, kUnconstrainedMaxDimension);
265 EXPECT_FALSE(set.ContainsPoint(0, 0));
266 EXPECT_FALSE(set.ContainsPoint(10, 10));
267 EXPECT_FALSE(set.ContainsPoint(50, 50));
268 EXPECT_TRUE(set.ContainsPoint(100, 100));
269 EXPECT_TRUE(set.ContainsPoint(500, 500));
270 EXPECT_TRUE(set.HasMinHeight());
271 EXPECT_FALSE(set.HasMaxHeight());
272 EXPECT_FALSE(set.HasMinWidth());
273 EXPECT_FALSE(set.HasMaxWidth());
274 EXPECT_FALSE(set.HasMinAspectRatio());
275 EXPECT_FALSE(set.HasMaxAspectRatio());
276 EXPECT_FALSE(set.IsHeightUnconstrained());
277 EXPECT_TRUE(set.IsWidthUnconstrained());
278 EXPECT_TRUE(set.IsAspectRatioUnconstrained());
279 EXPECT_FALSE(set.IsHeightEmpty());
280 EXPECT_FALSE(set.IsWidthEmpty());
281 EXPECT_FALSE(set.IsAspectRatioEmpty());
282
283 set = ResolutionSet::FromWidth(10, 100);
284 EXPECT_FALSE(set.ContainsPoint(0, 0));
285 EXPECT_TRUE(set.ContainsPoint(10, 10));
286 EXPECT_TRUE(set.ContainsPoint(50, 50));
287 EXPECT_TRUE(set.ContainsPoint(100, 100));
288 EXPECT_FALSE(set.ContainsPoint(500, 500));
289 EXPECT_FALSE(set.HasMinHeight());
290 EXPECT_FALSE(set.HasMaxHeight());
291 EXPECT_TRUE(set.HasMinWidth());
292 EXPECT_TRUE(set.HasMaxWidth());
293 EXPECT_FALSE(set.HasMinAspectRatio());
294 EXPECT_FALSE(set.HasMaxAspectRatio());
295 EXPECT_TRUE(set.IsHeightUnconstrained());
296 EXPECT_FALSE(set.IsWidthUnconstrained());
297 EXPECT_TRUE(set.IsAspectRatioUnconstrained());
298 EXPECT_FALSE(set.IsHeightEmpty());
299 EXPECT_FALSE(set.IsWidthEmpty());
300 EXPECT_FALSE(set.IsAspectRatioEmpty());
301
302 set = ResolutionSet::FromWidth(0, 100);
303 EXPECT_TRUE(set.ContainsPoint(0, 0));
304 EXPECT_TRUE(set.ContainsPoint(10, 10));
305 EXPECT_TRUE(set.ContainsPoint(50, 50));
306 EXPECT_TRUE(set.ContainsPoint(100, 100));
307 EXPECT_FALSE(set.ContainsPoint(500, 500));
308 EXPECT_FALSE(set.HasMinHeight());
309 EXPECT_FALSE(set.HasMaxHeight());
310 EXPECT_FALSE(set.HasMinWidth());
311 EXPECT_TRUE(set.HasMaxWidth());
312 EXPECT_FALSE(set.HasMinAspectRatio());
313 EXPECT_FALSE(set.HasMaxAspectRatio());
314 EXPECT_TRUE(set.IsHeightUnconstrained());
315 EXPECT_FALSE(set.IsWidthUnconstrained());
316 EXPECT_TRUE(set.IsAspectRatioUnconstrained());
317 EXPECT_FALSE(set.IsHeightEmpty());
318 EXPECT_FALSE(set.IsWidthEmpty());
319 EXPECT_FALSE(set.IsAspectRatioEmpty());
320
321 set = ResolutionSet::FromWidth(100, kUnconstrainedMaxDimension);
322 EXPECT_FALSE(set.ContainsPoint(0, 0));
323 EXPECT_FALSE(set.ContainsPoint(10, 10));
324 EXPECT_FALSE(set.ContainsPoint(50, 50));
325 EXPECT_TRUE(set.ContainsPoint(100, 100));
326 EXPECT_TRUE(set.ContainsPoint(500, 500));
327 EXPECT_FALSE(set.HasMinHeight());
328 EXPECT_FALSE(set.HasMaxHeight());
329 EXPECT_TRUE(set.HasMinWidth());
330 EXPECT_FALSE(set.HasMaxWidth());
331 EXPECT_FALSE(set.HasMinAspectRatio());
332 EXPECT_FALSE(set.HasMaxAspectRatio());
333 EXPECT_TRUE(set.IsHeightUnconstrained());
334 EXPECT_FALSE(set.IsWidthUnconstrained());
335 EXPECT_TRUE(set.IsAspectRatioUnconstrained());
336 EXPECT_FALSE(set.IsHeightEmpty());
337 EXPECT_FALSE(set.IsWidthEmpty());
338 EXPECT_FALSE(set.IsAspectRatioEmpty());
339
340 set = ResolutionSet::FromAspectRatio(1.0, 2.0);
341 EXPECT_TRUE(set.ContainsPoint(0, 0));
342 EXPECT_TRUE(set.ContainsPoint(10, 10));
343 EXPECT_TRUE(set.ContainsPoint(10, 20));
344 EXPECT_TRUE(set.ContainsPoint(100, 100));
345 EXPECT_TRUE(set.ContainsPoint(2000, 4000));
346 EXPECT_FALSE(set.ContainsPoint(1, 50));
347 EXPECT_FALSE(set.ContainsPoint(50, 1));
348 EXPECT_FALSE(set.HasMinHeight());
349 EXPECT_FALSE(set.HasMaxHeight());
350 EXPECT_FALSE(set.HasMinWidth());
351 EXPECT_FALSE(set.HasMaxWidth());
352 EXPECT_TRUE(set.HasMinAspectRatio());
353 EXPECT_TRUE(set.HasMaxAspectRatio());
354 EXPECT_TRUE(set.IsHeightUnconstrained());
355 EXPECT_TRUE(set.IsWidthUnconstrained());
356 EXPECT_FALSE(set.IsAspectRatioUnconstrained());
357 EXPECT_FALSE(set.IsHeightEmpty());
358 EXPECT_FALSE(set.IsWidthEmpty());
359 EXPECT_FALSE(set.IsAspectRatioEmpty());
360
361 set = ResolutionSet::FromAspectRatio(0.0, 2.0);
362 EXPECT_TRUE(set.ContainsPoint(0, 0));
363 EXPECT_TRUE(set.ContainsPoint(10, 10));
364 EXPECT_TRUE(set.ContainsPoint(10, 20));
365 EXPECT_TRUE(set.ContainsPoint(100, 100));
366 EXPECT_TRUE(set.ContainsPoint(2000, 4000));
367 EXPECT_FALSE(set.ContainsPoint(1, 50));
368 EXPECT_TRUE(set.ContainsPoint(50, 1));
369 EXPECT_FALSE(set.HasMinHeight());
370 EXPECT_FALSE(set.HasMaxHeight());
371 EXPECT_FALSE(set.HasMinWidth());
372 EXPECT_FALSE(set.HasMaxWidth());
373 EXPECT_FALSE(set.HasMinAspectRatio());
374 EXPECT_TRUE(set.HasMaxAspectRatio());
375 EXPECT_TRUE(set.IsHeightUnconstrained());
376 EXPECT_TRUE(set.IsWidthUnconstrained());
377 EXPECT_FALSE(set.IsAspectRatioUnconstrained());
378 EXPECT_FALSE(set.IsHeightEmpty());
379 EXPECT_FALSE(set.IsWidthEmpty());
380 EXPECT_FALSE(set.IsAspectRatioEmpty());
381
382 set = ResolutionSet::FromAspectRatio(1.0, HUGE_VAL);
383 EXPECT_TRUE(set.ContainsPoint(0, 0));
384 EXPECT_TRUE(set.ContainsPoint(10, 10));
385 EXPECT_TRUE(set.ContainsPoint(10, 20));
386 EXPECT_TRUE(set.ContainsPoint(100, 100));
387 EXPECT_TRUE(set.ContainsPoint(2000, 4000));
388 EXPECT_TRUE(set.ContainsPoint(1, 50));
389 EXPECT_FALSE(set.ContainsPoint(50, 1));
390 EXPECT_FALSE(set.HasMinHeight());
391 EXPECT_FALSE(set.HasMaxHeight());
392 EXPECT_FALSE(set.HasMinWidth());
393 EXPECT_FALSE(set.HasMaxWidth());
394 EXPECT_TRUE(set.HasMinAspectRatio());
395 EXPECT_FALSE(set.HasMaxAspectRatio());
396 EXPECT_TRUE(set.IsHeightUnconstrained());
397 EXPECT_TRUE(set.IsWidthUnconstrained());
398 EXPECT_FALSE(set.IsAspectRatioUnconstrained());
399 EXPECT_FALSE(set.IsHeightEmpty());
400 EXPECT_FALSE(set.IsWidthEmpty());
401 EXPECT_FALSE(set.IsAspectRatioEmpty());
402 }
403
404 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionTrivialEmptiness) {
405 ResolutionSet set = ResolutionSet::FromHeight(100, 10);
406 EXPECT_TRUE(set.IsEmpty());
407 EXPECT_TRUE(set.IsHeightEmpty());
408 EXPECT_FALSE(set.IsWidthEmpty());
409 EXPECT_FALSE(set.IsAspectRatioEmpty());
410
411 set = ResolutionSet::FromWidth(100, 10);
412 EXPECT_TRUE(set.IsEmpty());
413 EXPECT_FALSE(set.IsHeightEmpty());
414 EXPECT_TRUE(set.IsWidthEmpty());
415 EXPECT_FALSE(set.IsAspectRatioEmpty());
416
417 set = ResolutionSet::FromAspectRatio(100.0, 10.0);
418 EXPECT_TRUE(set.IsEmpty());
419 EXPECT_FALSE(set.IsHeightEmpty());
420 EXPECT_FALSE(set.IsWidthEmpty());
421 EXPECT_TRUE(set.IsAspectRatioEmpty());
422 }
423
424 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionLineConstraintsEmptiness) {
425 ResolutionSet set(1, 1, 1, 1, 1, 1);
426 EXPECT_FALSE(set.IsEmpty());
427 EXPECT_FALSE(set.ContainsPoint(0, 0));
428 EXPECT_TRUE(set.ContainsPoint(1, 1));
429 EXPECT_FALSE(set.ContainsPoint(1, 0));
430 EXPECT_FALSE(set.ContainsPoint(0, 1));
431
432 // Three lines that do not intersect in the same point is empty.
433 set = ResolutionSet(1, 1, 1, 1, 0.5, 0.5);
434 EXPECT_TRUE(set.IsEmpty());
435 EXPECT_TRUE(set.IsAspectRatioEmpty());
436 EXPECT_FALSE(set.ContainsPoint(0, 0));
437 EXPECT_FALSE(set.ContainsPoint(1, 1));
438 EXPECT_FALSE(set.ContainsPoint(1, 0));
439 EXPECT_FALSE(set.ContainsPoint(0, 1));
440 }
441
442 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionBoxEmptiness) {
443 int kMin = 100;
444 int kMax = 200;
445 // Max aspect ratio below box.
446 ResolutionSet set(kMin, kMax, kMin, kMax, 0.4, 0.4);
447 EXPECT_TRUE(set.IsEmpty());
448 EXPECT_TRUE(set.IsAspectRatioEmpty());
449
450 // Min aspect ratio above box.
451 set = ResolutionSet(kMin, kMax, kMin, kMax, 3.0, HUGE_VAL);
452 EXPECT_TRUE(set.IsEmpty());
453 EXPECT_TRUE(set.IsAspectRatioEmpty());
454
455 // Min aspect ratio crosses box.
456 set = ResolutionSet(kMin, kMax, kMin, kMax, 1.0, HUGE_VAL);
457 EXPECT_FALSE(set.IsEmpty());
458
459 // Max aspect ratio crosses box.
460 set = ResolutionSet(kMin, kMax, kMin, kMax, 0.0, 1.0);
461 EXPECT_FALSE(set.IsEmpty());
462
463 // Min and max aspect ratios cross box.
464 set = ResolutionSet(kMin, kMax, kMin, kMax, 0.9, 1.1);
465 EXPECT_FALSE(set.IsEmpty());
466
467 // Min and max aspect ratios cover box.
468 set = ResolutionSet(kMin, kMax, kMin, kMax, 0.2, 100);
469 EXPECT_FALSE(set.IsEmpty());
470 }
471
472 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionPointIntersection) {
473 ResolutionSet set1(1, 2, 1, 2, 0.0, HUGE_VAL);
474 ResolutionSet set2 = ResolutionSet::FromExactAspectRatio(0.5);
475
476 // The intersection should contain only the point (h=2, w=1)
477 auto intersection = set1.Intersection(set2);
478 EXPECT_TRUE(intersection.ContainsPoint(2, 1));
479
480 // It should not contain any point in the vicinity of the included point.
481 EXPECT_FALSE(intersection.ContainsPoint(1, 0));
482 EXPECT_FALSE(intersection.ContainsPoint(2, 0));
483 EXPECT_FALSE(intersection.ContainsPoint(3, 0));
484 EXPECT_FALSE(intersection.ContainsPoint(1, 1));
485 EXPECT_FALSE(intersection.ContainsPoint(3, 1));
486 EXPECT_FALSE(intersection.ContainsPoint(1, 2));
487 EXPECT_FALSE(intersection.ContainsPoint(2, 2));
488 EXPECT_FALSE(intersection.ContainsPoint(3, 2));
489
490 // // Intersection with either of the original sets should return the initial
491 // // intersection.
492 // EXPECT_EQ(intersection, intersection.Intersection(set1));
493 // EXPECT_EQ(intersection, intersection.Intersection(set2));
494 }
495
496 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionLineIntersection) {
497 ResolutionSet set1(1, 2, 1, 2, 0.0, HUGE_VAL);
498 ResolutionSet set2 = ResolutionSet::FromExactAspectRatio(1.0);
499
500 // The intersection should contain (1,1) and (2,2)
501 auto intersection = set1.Intersection(set2);
502 EXPECT_TRUE(intersection.ContainsPoint(1, 1));
503 EXPECT_TRUE(intersection.ContainsPoint(2, 2));
504
505 // It should not contain the other points in the bounding box.
506 EXPECT_FALSE(intersection.ContainsPoint(1, 2));
507 EXPECT_FALSE(intersection.ContainsPoint(2, 1));
508
509 // // Intersection with either of the original sets should return the initial
510 // // intersection.
511 // EXPECT_EQ(intersection, intersection.Intersection(set1));
512 // EXPECT_EQ(intersection, intersection.Intersection(set2));
513 }
514
515 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionBoxIntersection) {
516 const int kMin1 = 0;
517 const int kMax1 = 2;
518 ResolutionSet set1(kMin1, kMax1, kMin1, kMax1, 0.0, HUGE_VAL);
519
520 const int kMin2 = 1;
521 const int kMax2 = 3;
522 ResolutionSet set2(kMin2, kMax2, kMin2, kMax2, 0.0, HUGE_VAL);
523
524 auto intersection = set1.Intersection(set2);
525 for (int i = kMin1; i <= kMax2; ++i) {
526 for (int j = kMin1; j <= kMax2; ++j) {
527 if (i >= kMin2 && j >= kMin2 && i <= kMax1 && j <= kMax1)
528 EXPECT_TRUE(intersection.ContainsPoint(i, j));
529 else
530 EXPECT_FALSE(intersection.ContainsPoint(i, j));
531 }
532 }
533 }
534
535 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionClosestPointToPointSet) {
536 const int kHeight = 10;
537 const int kWidth = 10;
538 const double kAspectRatio = 1.0;
539 ResolutionSet set(kHeight, kHeight, kWidth, kWidth, kAspectRatio,
540 kAspectRatio);
541
542 for (int height = 0; height < 100; height += 10) {
543 for (int width = 0; width < 100; width += 10) {
544 EXPECT_EQ(P(kHeight, kWidth), set.ClosestPointTo(P(height, width)));
545 }
546 }
547 }
548
549 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionClosestPointToLineSet) {
550 {
551 const int kHeight = 10;
552 auto set = ResolutionSet::FromExactHeight(kHeight);
553 for (int height = 0; height < 100; height += 10) {
554 for (int width = 0; width < 100; width += 10) {
555 EXPECT_EQ(P(kHeight, width), set.ClosestPointTo(P(height, width)));
556 }
557 }
558 const int kWidth = 10;
559 set = ResolutionSet::FromExactWidth(kWidth);
560 for (int height = 0; height < 100; height += 10) {
561 for (int width = 0; width < 100; width += 10) {
562 EXPECT_EQ(P(height, kWidth), set.ClosestPointTo(P(height, width)));
563 }
564 }
565 }
566
567 {
568 const double kAspectRatios[] = {0.0, 0.1, 0.2, 0.5,
569 1.0, 2.0, 5.0, HUGE_VAL};
570 for (double aspect_ratio : kAspectRatios) {
571 auto set = ResolutionSet::FromExactAspectRatio(aspect_ratio);
572 for (int height = 0; height < 100; height += 10) {
573 for (int width = 0; width < 100; width += 10) {
574 Point point(height, width);
575 Point expected =
576 ProjectionOnSegmentLine(point, P(0, 0), P(1, aspect_ratio));
577 Point actual = set.ClosestPointTo(point);
578 EXPECT_TRUE(expected.IsApproximatelyEqualTo(actual));
579 }
580 }
581 }
582 }
583 }
584
585 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionClosestPointToGeneralSet) {
586 // This set contains the following vertices:
587 // (10, 10), (20, 10), (100, 50), (100, 100), (100/1.5, 100), (10, 15)
588 ResolutionSet set(10, 100, 10, 100, 0.5, 1.5);
589
590 // Check that vertices are the closest points to themselves.
591 std::vector<Point> vertices = {P(10, 10), P(20, 10), P(100, 50),
592 P(100, 100), P(100 / 1.5, 100), P(10, 15)};
593 for (auto& vertex : vertices)
594 EXPECT_EQ(vertex, set.ClosestPointTo(vertex));
595
596 // Point inside the set.
597 EXPECT_EQ(P(11, 11), set.ClosestPointTo(P(11, 11)));
598
599 // Close to horizontal segment (10, 10) (20, 10).
600 EXPECT_EQ(P(15, 10), set.ClosestPointTo(P(15, 9)));
601
602 // Close to horizontal segment (100, 100) (100/1.5, 100).
603 EXPECT_EQ(P(99, 100), set.ClosestPointTo(P(99, 200)));
604
605 // Close to vertical segment (10, 15) (10, 10).
606 EXPECT_EQ(P(10, 12.5), set.ClosestPointTo(P(2, 12.5)));
607
608 // Close to vertical segment (100, 50) (100, 100).
609 EXPECT_EQ(P(100, 75), set.ClosestPointTo(P(120, 75)));
610
611 // Close to oblique segment (20, 10) (100, 50)
612 {
613 Point point(70, 15);
614 Point expected = ProjectionOnSegmentLine(point, P(20, 10), P(100, 50));
615 Point actual = set.ClosestPointTo(point);
616 EXPECT_TRUE(expected.IsApproximatelyEqualTo(actual));
617 }
618
619 // Close to oblique segment (100/1.5, 100) (10, 15)
620 {
621 Point point(12, 70);
622 Point expected =
623 ProjectionOnSegmentLine(point, P(100 / 1.5, 100), P(10, 15));
624 Point actual = set.ClosestPointTo(point);
625 EXPECT_TRUE(expected.IsApproximatelyEqualTo(actual));
626 }
627
628 // Points close to vertices.
629 EXPECT_EQ(P(10, 10), set.ClosestPointTo(P(9, 9)));
630 EXPECT_EQ(P(20, 10), set.ClosestPointTo(P(20, 9)));
631 EXPECT_EQ(P(100, 50), set.ClosestPointTo(P(101, 50)));
632 EXPECT_EQ(P(100, 100), set.ClosestPointTo(P(101, 101)));
633 EXPECT_EQ(P(100 / 1.5, 100), set.ClosestPointTo(P(100 / 1.5, 101)));
634 EXPECT_EQ(P(10, 15), set.ClosestPointTo(P(9, 15)));
635 }
636
637 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionIdealIntersects) {
638 ResolutionSet set(100, 1000, 100, 1000, 0.5, 2.0);
639
640 int kIdealHeight = 500;
641 int kIdealWidth = 1000;
642 double kIdealAspectRatio = 1.5;
643
644 // Ideal height.
645 {
646 factory_.Reset();
647 factory_.basic().height.setIdeal(kIdealHeight);
648 Point point = set.SelectClosestPointToIdeal(
649 factory_.CreateWebMediaConstraints().basic());
650 EXPECT_DOUBLE_EQ(kIdealHeight, point.height());
651 }
652
653 // Ideal width.
654 {
655 factory_.Reset();
656 factory_.basic().width.setIdeal(kIdealWidth);
657 Point point = set.SelectClosestPointToIdeal(
658 factory_.CreateWebMediaConstraints().basic());
659 EXPECT_DOUBLE_EQ(kIdealWidth, point.width());
660 EXPECT_DOUBLE_EQ(kIdealWidth / MediaStreamVideoSource::kDefaultAspectRatio,
661 point.height());
662 }
663
664 // Ideal aspect ratio.
665 {
666 factory_.Reset();
667 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
668 Point point = set.SelectClosestPointToIdeal(
669 factory_.CreateWebMediaConstraints().basic());
670 EXPECT_DOUBLE_EQ(MediaStreamVideoSource::kDefaultHeight, point.height());
671 EXPECT_DOUBLE_EQ(MediaStreamVideoSource::kDefaultHeight * kIdealAspectRatio,
672 point.width());
673 }
674
675 // Ideal height and width.
676 {
677 factory_.Reset();
678 factory_.basic().height.setIdeal(kIdealHeight);
679 factory_.basic().width.setIdeal(kIdealWidth);
680 Point point = set.SelectClosestPointToIdeal(
681 factory_.CreateWebMediaConstraints().basic());
682 EXPECT_DOUBLE_EQ(kIdealHeight, point.height());
683 EXPECT_DOUBLE_EQ(kIdealWidth, point.width());
684 }
685
686 // Ideal height and aspect-ratio.
687 {
688 factory_.Reset();
689 factory_.basic().height.setIdeal(kIdealHeight);
690 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
691 Point point = set.SelectClosestPointToIdeal(
692 factory_.CreateWebMediaConstraints().basic());
693 EXPECT_DOUBLE_EQ(kIdealHeight, point.height());
694 EXPECT_DOUBLE_EQ(kIdealHeight * kIdealAspectRatio, point.width());
695 }
696
697 // Ideal width and aspect-ratio.
698 {
699 factory_.Reset();
700 factory_.basic().width.setIdeal(kIdealWidth);
701 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
702 Point point = set.SelectClosestPointToIdeal(
703 factory_.CreateWebMediaConstraints().basic());
704 EXPECT_DOUBLE_EQ(kIdealWidth, point.width());
705 EXPECT_DOUBLE_EQ(kIdealWidth / kIdealAspectRatio, point.height());
706 }
707
708 // Ideal height, width and aspect-ratio.
709 {
710 factory_.Reset();
711 factory_.basic().height.setIdeal(kIdealHeight);
712 factory_.basic().width.setIdeal(kIdealWidth);
713 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
714 Point point = set.SelectClosestPointToIdeal(
715 factory_.CreateWebMediaConstraints().basic());
716 // Ideal aspect ratio should be ignored.
717 EXPECT_DOUBLE_EQ(kIdealHeight, point.height());
718 EXPECT_DOUBLE_EQ(kIdealWidth, point.width());
719 }
720 }
721
722 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionIdealOutsideSinglePoint) {
723 // This set is a triangle with vertices (100,100), (1000,100) and (1000,1000).
724 ResolutionSet set(100, 1000, 100, 1000, 0.0, 1.0);
725
726 int kIdealHeight = 50;
727 int kIdealWidth = 1100;
728 double kIdealAspectRatio = 0.09;
729 Point kVertex1(100, 100);
730 Point kVertex2(1000, 100);
731 Point kVertex3(1000, 1000);
732
733 // Ideal height.
734 {
735 factory_.Reset();
736 factory_.basic().height.setIdeal(kIdealHeight);
737 Point point = set.SelectClosestPointToIdeal(
738 factory_.CreateWebMediaConstraints().basic());
739 EXPECT_DOUBLE_EQ(kVertex1.height(), point.height());
740 EXPECT_DOUBLE_EQ(kVertex1.width(), point.height());
741 }
742
743 // Ideal width.
744 {
745 factory_.Reset();
746 factory_.basic().width.setIdeal(kIdealWidth);
747 Point point = set.SelectClosestPointToIdeal(
748 factory_.CreateWebMediaConstraints().basic());
749 EXPECT_DOUBLE_EQ(kVertex3.height(), point.height());
750 EXPECT_DOUBLE_EQ(kVertex3.width(), point.width());
751 }
752
753 // Ideal aspect ratio.
754 {
755 factory_.Reset();
756 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
757 Point point = set.SelectClosestPointToIdeal(
758 factory_.CreateWebMediaConstraints().basic());
759 EXPECT_DOUBLE_EQ(kVertex2.height(), point.height());
760 EXPECT_DOUBLE_EQ(kVertex2.width(), point.width());
761 }
762
763 // Ideal height and width.
764 {
765 factory_.Reset();
766 factory_.basic().height.setIdeal(kIdealHeight);
767 factory_.basic().width.setIdeal(kIdealWidth);
768 Point point = set.SelectClosestPointToIdeal(
769 factory_.CreateWebMediaConstraints().basic());
770 Point expected = set.ClosestPointTo(Point(kIdealHeight, kIdealWidth));
771 EXPECT_DOUBLE_EQ(expected.height(), point.height());
772 EXPECT_DOUBLE_EQ(expected.width(), point.width());
773 }
774
775 // Ideal height and aspect-ratio.
776 {
777 factory_.Reset();
778 factory_.basic().height.setIdeal(kIdealHeight);
779 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
780 Point point = set.SelectClosestPointToIdeal(
781 factory_.CreateWebMediaConstraints().basic());
782 Point expected = set.ClosestPointTo(
783 Point(kIdealHeight, kIdealHeight * kIdealAspectRatio));
784 EXPECT_DOUBLE_EQ(expected.height(), point.height());
785 EXPECT_DOUBLE_EQ(expected.width(), point.width());
786 }
787
788 // Ideal width and aspect-ratio.
789 {
790 factory_.Reset();
791 factory_.basic().width.setIdeal(kIdealWidth);
792 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
793 Point point = set.SelectClosestPointToIdeal(
794 factory_.CreateWebMediaConstraints().basic());
795 Point expected =
796 set.ClosestPointTo(Point(kIdealWidth / kIdealAspectRatio, kIdealWidth));
797 EXPECT_DOUBLE_EQ(expected.height(), point.height());
798 EXPECT_DOUBLE_EQ(expected.width(), point.width());
799 }
800
801 // Ideal height, width and aspect-ratio.
802 {
803 factory_.Reset();
804 factory_.basic().height.setIdeal(kIdealHeight);
805 factory_.basic().width.setIdeal(kIdealWidth);
806 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
807 Point point = set.SelectClosestPointToIdeal(
808 factory_.CreateWebMediaConstraints().basic());
809 Point expected = set.ClosestPointTo(Point(kIdealHeight, kIdealWidth));
810 EXPECT_DOUBLE_EQ(expected.height(), point.height());
811 EXPECT_DOUBLE_EQ(expected.width(), point.width());
812 }
813 }
814
815 TEST_F(MediaStreamConstraintsUtilSetsTest,
816 ResolutionIdealOutsideMultiplePoints) {
817 // This set is a triangle with vertices (100,100), (1000,100) and (1000,1000).
818 ResolutionSet set(100, 1000, 100, 1000, 0.0, 1.0);
819
820 int kIdealHeight = 1100;
821 int kIdealWidth = 50;
822 double kIdealAspectRatio = 11.0;
823 Point kVertex1(100, 100);
824 Point kVertex2(1000, 100);
825 Point kVertex3(1000, 1000);
826
827 // Ideal height.
828 {
829 factory_.Reset();
830 factory_.basic().height.setIdeal(kIdealHeight);
831 Point point = set.SelectClosestPointToIdeal(
832 factory_.CreateWebMediaConstraints().basic());
833 // Parallel to the side between kVertex2 and kVertex3. Point closest to
834 // default aspect ratio is kVertex3.
835 EXPECT_DOUBLE_EQ(kVertex3.height(), point.height());
836 EXPECT_DOUBLE_EQ(kVertex3.width(), point.height());
837 }
838
839 // Ideal width.
840 {
841 factory_.Reset();
842 factory_.basic().width.setIdeal(kIdealWidth);
843 Point point = set.SelectClosestPointToIdeal(
844 factory_.CreateWebMediaConstraints().basic());
845 // Parallel to the side between kVertex1 and kVertex2. Point closest to
846 // default aspect ratio is kVertex1.
847 EXPECT_DOUBLE_EQ(kVertex1.height(), point.height());
848 EXPECT_DOUBLE_EQ(kVertex1.width(), point.width());
849 }
850
851 // Ideal aspect ratio.
852 {
853 factory_.Reset();
854 factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
855 Point point = set.SelectClosestPointToIdeal(
856 factory_.CreateWebMediaConstraints().basic());
857 // The side between kVertex1 and kVertex3 is closest. The points closest to
858 // default dimensions are (kDefaultHeight, kDefaultHeight * AR)
859 // and (kDefaultWidth / AR, kDefaultWidth). Since the aspect ratio of the
860 // polygon side is less than the default, the algorithm preserves the
861 // default width.
862 EXPECT_DOUBLE_EQ(
863 MediaStreamVideoSource::kDefaultWidth / kVertex1.AspectRatio(),
864 point.height());
865 EXPECT_DOUBLE_EQ(MediaStreamVideoSource::kDefaultWidth, point.width());
866 }
867 }
868
869 TEST_F(MediaStreamConstraintsUtilSetsTest, ResolutionVertices) {
870 // Empty set.
871 {
872 ResolutionSet set(1000, 100, 1000, 100, 0.5, 1.5);
873 ASSERT_TRUE(set.IsEmpty());
874 auto vertices = set.ComputeVertices();
875 EXPECT_EQ(0U, vertices.size());
876 EXPECT_TRUE(AreValidVertices(vertices));
877 }
878
879 // Three lines that intersect at the same point.
880 {
881 ResolutionSet set(1, 1, 1, 1, 1, 1);
882 EXPECT_FALSE(set.IsEmpty());
883 auto vertices = set.ComputeVertices();
884 EXPECT_EQ(1U, vertices.size());
885 VerticesContain(vertices, Point(1, 1));
886 EXPECT_TRUE(AreValidVertices(vertices));
887 }
888
889 // A line segment with the lower-left and upper-right corner of the box.
890 {
891 ResolutionSet set(0, 100, 0, 100, 1.0, 1.0);
892 EXPECT_FALSE(set.IsEmpty());
893 auto vertices = set.ComputeVertices();
894 EXPECT_EQ(2U, vertices.size());
895 VerticesContain(vertices, Point(0, 0));
896 VerticesContain(vertices, Point(100, 100));
897 EXPECT_TRUE(AreValidVertices(vertices));
898
899 set = ResolutionSet(0, 100, 0, 100, 1.0, HUGE_VAL);
900 EXPECT_FALSE(set.IsEmpty());
901 vertices = set.ComputeVertices();
902 EXPECT_EQ(3U, vertices.size());
903 VerticesContain(vertices, Point(0, 0));
904 VerticesContain(vertices, Point(100, 100));
905 VerticesContain(vertices, Point(0, 100));
906 EXPECT_TRUE(AreValidVertices(vertices));
907
908 set = ResolutionSet(0, 100, 0, 100, 0, 1.0);
909 EXPECT_FALSE(set.IsEmpty());
910 vertices = set.ComputeVertices();
911 EXPECT_EQ(3U, vertices.size());
912 VerticesContain(vertices, Point(0, 0));
913 VerticesContain(vertices, Point(100, 100));
914 VerticesContain(vertices, Point(100, 0));
915 EXPECT_TRUE(AreValidVertices(vertices));
916 }
917
918 // A line segment that crosses the bottom and right sides of the box.
919 {
920 const double kAspectRatio = 50.0 / 75.0;
921 ResolutionSet set(50, 100, 50, 100, kAspectRatio, kAspectRatio);
922 auto vertices = set.ComputeVertices();
923 EXPECT_EQ(2U, vertices.size());
924 VerticesContain(vertices, Point(50 / kAspectRatio, 50));
925 VerticesContain(vertices, Point(100, 100.0 * kAspectRatio));
926 EXPECT_TRUE(AreValidVertices(vertices));
927
928 set = ResolutionSet(50, 100, 50, 100, kAspectRatio, HUGE_VAL);
929 vertices = set.ComputeVertices();
930 EXPECT_EQ(5U, vertices.size());
931 VerticesContain(vertices, Point(50 / kAspectRatio, 50));
932 VerticesContain(vertices, Point(100, 100.0 * kAspectRatio));
933 VerticesContain(vertices, Point(50, 50));
934 VerticesContain(vertices, Point(50, 100));
935 VerticesContain(vertices, Point(100, 100));
936 EXPECT_TRUE(AreValidVertices(vertices));
937
938 set = ResolutionSet(50, 100, 50, 100, 0.0, kAspectRatio);
939 vertices = set.ComputeVertices();
940 EXPECT_EQ(3U, vertices.size());
941 VerticesContain(vertices, Point(50 / kAspectRatio, 50));
942 VerticesContain(vertices, Point(100, 100.0 * kAspectRatio));
943 VerticesContain(vertices, Point(100, 50));
944 EXPECT_TRUE(AreValidVertices(vertices));
945 }
946
947 // A line segment that crosses the left and top sides of the box.
948 {
949 const double kAspectRatio = 75.0 / 50.0;
950 ResolutionSet set(50, 100, 50, 100, kAspectRatio, kAspectRatio);
951 auto vertices = set.ComputeVertices();
952 EXPECT_EQ(2U, vertices.size());
953 VerticesContain(vertices, Point(50, 50 * kAspectRatio));
954 VerticesContain(vertices, Point(100 / kAspectRatio, 100));
955 EXPECT_TRUE(AreValidVertices(vertices));
956
957 set = ResolutionSet(50, 100, 50, 100, kAspectRatio, HUGE_VAL);
958 vertices = set.ComputeVertices();
959 EXPECT_EQ(3U, vertices.size());
960 VerticesContain(vertices, Point(50, 50 * kAspectRatio));
961 VerticesContain(vertices, Point(100 / kAspectRatio, 100));
962 VerticesContain(vertices, Point(50, 100));
963 EXPECT_TRUE(AreValidVertices(vertices));
964
965 set = ResolutionSet(50, 100, 50, 100, 0.0, kAspectRatio);
966 vertices = set.ComputeVertices();
967 EXPECT_EQ(5U, vertices.size());
968 VerticesContain(vertices, Point(50, 50 * kAspectRatio));
969 VerticesContain(vertices, Point(100 / kAspectRatio, 100));
970 VerticesContain(vertices, Point(50, 50));
971 VerticesContain(vertices, Point(100, 100));
972 VerticesContain(vertices, Point(100, 50));
973 EXPECT_TRUE(AreValidVertices(vertices));
974 }
975
976 // An aspect ratio constraint crosses the bottom and top sides of the box.
977 {
978 const double kAspectRatio = 75.0 / 50.0;
979 ResolutionSet set(0, 100, 50, 100, kAspectRatio, kAspectRatio);
980 auto vertices = set.ComputeVertices();
981 EXPECT_EQ(2U, vertices.size());
982 VerticesContain(vertices, Point(50 / kAspectRatio, 50));
983 VerticesContain(vertices, Point(100 / kAspectRatio, 100));
984 EXPECT_TRUE(AreValidVertices(vertices));
985
986 set = ResolutionSet(0, 100, 50, 100, kAspectRatio, HUGE_VAL);
987 vertices = set.ComputeVertices();
988 EXPECT_EQ(4U, vertices.size());
989 VerticesContain(vertices, Point(50 / kAspectRatio, 50));
990 VerticesContain(vertices, Point(100 / kAspectRatio, 100));
991 VerticesContain(vertices, Point(0, 50));
992 VerticesContain(vertices, Point(0, 100));
993 EXPECT_TRUE(AreValidVertices(vertices));
994
995 set = ResolutionSet(0, 100, 50, 100, 0.0, kAspectRatio);
996 vertices = set.ComputeVertices();
997 EXPECT_EQ(4U, vertices.size());
998 VerticesContain(vertices, Point(50 / kAspectRatio, 50));
999 VerticesContain(vertices, Point(100 / kAspectRatio, 100));
1000 VerticesContain(vertices, Point(100, 50));
1001 VerticesContain(vertices, Point(100, 100));
1002 EXPECT_TRUE(AreValidVertices(vertices));
1003 }
1004
1005 // An aspect-ratio constraint crosses the left and right sides of the box.
1006 {
1007 const double kAspectRatio = 75.0 / 50.0;
1008 ResolutionSet set(50, 100, 0, 200, kAspectRatio, kAspectRatio);
1009 auto vertices = set.ComputeVertices();
1010 // This one fails if floating-point precision is too high.
1011 EXPECT_EQ(2U, vertices.size());
1012 VerticesContain(vertices, Point(50, 50 * kAspectRatio));
1013 VerticesContain(vertices, Point(100, 100 * kAspectRatio));
1014 EXPECT_TRUE(AreValidVertices(vertices));
1015
1016 set = ResolutionSet(50, 100, 0, 200, kAspectRatio, HUGE_VAL);
1017 vertices = set.ComputeVertices();
1018 // This one fails if floating-point precision is too high.
1019 EXPECT_EQ(4U, vertices.size());
1020 VerticesContain(vertices, Point(50, 50 * kAspectRatio));
1021 VerticesContain(vertices, Point(100, 100 * kAspectRatio));
1022 VerticesContain(vertices, Point(50, 200));
1023 VerticesContain(vertices, Point(100, 200));
1024 EXPECT_TRUE(AreValidVertices(vertices));
1025
1026 set = ResolutionSet(50, 100, 0, 200, 0.0, kAspectRatio);
1027 vertices = set.ComputeVertices();
1028 EXPECT_EQ(4U, vertices.size());
1029 VerticesContain(vertices, Point(50, 50 * kAspectRatio));
1030 VerticesContain(vertices, Point(100, 100 * kAspectRatio));
1031 VerticesContain(vertices, Point(50, 0));
1032 VerticesContain(vertices, Point(100, 0));
1033 EXPECT_TRUE(AreValidVertices(vertices));
1034 }
1035
1036 // Aspect-ratio lines touch the corners of the box.
1037 {
1038 ResolutionSet set(50, 100, 50, 100, 0.5, 2.0);
1039 auto vertices = set.ComputeVertices();
1040 EXPECT_EQ(4U, vertices.size());
1041 VerticesContain(vertices, Point(50, 50));
1042 VerticesContain(vertices, Point(100, 50));
1043 VerticesContain(vertices, Point(50, 100));
1044 VerticesContain(vertices, Point(100, 100));
1045 EXPECT_TRUE(AreValidVertices(vertices));
1046 }
1047
1048 // Hexagons.
1049 {
1050 ResolutionSet set(10, 100, 10, 100, 0.5, 1.5);
1051 auto vertices = set.ComputeVertices();
1052 EXPECT_EQ(6U, vertices.size());
1053 VerticesContain(vertices, Point(10, 10));
1054 VerticesContain(vertices, Point(100, 100));
1055 VerticesContain(vertices, Point(10, 10 * 1.5));
1056 VerticesContain(vertices, Point(100 / 1.5, 100));
1057 VerticesContain(vertices, Point(10 / 0.5, 10));
1058 VerticesContain(vertices, Point(100, 100 * 0.5));
1059 EXPECT_TRUE(AreValidVertices(vertices));
1060
1061 set = ResolutionSet(50, 100, 50, 100, 50.0 / 75.0, 75.0 / 50.0);
1062 vertices = set.ComputeVertices();
1063 EXPECT_EQ(6U, vertices.size());
1064 VerticesContain(vertices, Point(50, 50));
1065 VerticesContain(vertices, Point(100, 100));
1066 VerticesContain(vertices, Point(75, 50));
1067 VerticesContain(vertices, Point(50, 75));
1068 VerticesContain(vertices, Point(100, 100.0 * 50.0 / 75.0));
1069 VerticesContain(vertices, Point(100 * 50.0 / 75.0, 100.0));
1070 EXPECT_TRUE(AreValidVertices(vertices));
1071 }
1072
1073 // Both aspect-ratio constraints cross the left and top sides of the box.
1074 {
1075 ResolutionSet set(10, 100, 10, 100, 1.5, 1.7);
1076 auto vertices = set.ComputeVertices();
1077 EXPECT_EQ(4U, vertices.size());
1078 VerticesContain(vertices, Point(10, 10 * 1.5));
1079 VerticesContain(vertices, Point(10, 10 * 1.7));
1080 VerticesContain(vertices, Point(100 / 1.5, 100));
1081 VerticesContain(vertices, Point(100 / 1.7, 100));
1082 EXPECT_TRUE(AreValidVertices(vertices));
1083 }
1084
1085 // Both aspect-ratio constraints cross the left and right sides of the box.
1086 {
1087 ResolutionSet set(10, 100, 10, kUnconstrainedMaxDimension, 1.5, 1.7);
1088 auto vertices = set.ComputeVertices();
1089 EXPECT_EQ(4U, vertices.size());
1090 VerticesContain(vertices, Point(10, 10 * 1.5));
1091 VerticesContain(vertices, Point(10, 10 * 1.7));
1092 VerticesContain(vertices, Point(100, 100 * 1.5));
1093 VerticesContain(vertices, Point(100, 100 * 1.7));
1094 EXPECT_TRUE(AreValidVertices(vertices));
1095 }
1096
1097 // Both aspect-ratio constraints cross the bottom and top sides of the box.
1098 {
1099 ResolutionSet set(10, 100, 50, 100, 2.0, 4.0);
1100 auto vertices = set.ComputeVertices();
1101 EXPECT_EQ(4U, vertices.size());
1102 VerticesContain(vertices, Point(50 / 2.0, 50));
1103 VerticesContain(vertices, Point(100 / 2.0, 100));
1104 VerticesContain(vertices, Point(50 / 4.0, 50));
1105 VerticesContain(vertices, Point(100 / 4.0, 100));
1106 EXPECT_TRUE(AreValidVertices(vertices));
1107 }
1108
1109 // Both aspect-ratio constraints cross the bottom and right sides of the box.
1110 {
1111 ResolutionSet set(10, 100, 50, 100, 0.7, 0.9);
1112 auto vertices = set.ComputeVertices();
1113 EXPECT_EQ(4U, vertices.size());
1114 VerticesContain(vertices, Point(50 / 0.7, 50));
1115 VerticesContain(vertices, Point(50 / 0.9, 50));
1116 VerticesContain(vertices, Point(100, 100 * 0.7));
1117 VerticesContain(vertices, Point(100, 100 * 0.9));
1118 EXPECT_TRUE(AreValidVertices(vertices));
1119 }
1120
1121 // Pentagons.
1122 {
1123 ResolutionSet set(10, 100, 50, 100, 0.7, 4.0);
1124 auto vertices = set.ComputeVertices();
1125 EXPECT_EQ(5U, vertices.size());
1126 VerticesContain(vertices, Point(50 / 0.7, 50));
1127 VerticesContain(vertices, Point(100, 100 * 0.7));
1128 VerticesContain(vertices, Point(50 / 4.0, 50));
1129 VerticesContain(vertices, Point(100 / 4.0, 100));
1130 VerticesContain(vertices, Point(100, 100));
1131 EXPECT_TRUE(AreValidVertices(vertices));
1132
1133 set = ResolutionSet(50, 100, 10, 100, 0.7, 1.5);
1134 vertices = set.ComputeVertices();
1135 EXPECT_EQ(5U, vertices.size());
1136 VerticesContain(vertices, Point(50, 50 * 0.7));
1137 VerticesContain(vertices, Point(100, 100 * 0.7));
1138 VerticesContain(vertices, Point(50, 50 * 1.5));
1139 VerticesContain(vertices, Point(100 / 1.5, 100));
1140 VerticesContain(vertices, Point(100, 100));
1141 EXPECT_TRUE(AreValidVertices(vertices));
1142 }
1143
1144 // Extreme aspect ratios, for completeness.
1145 {
1146 ResolutionSet set(0, 100, 0, kUnconstrainedMaxDimension, 0.0, 0.0);
1147 auto vertices = set.ComputeVertices();
1148 EXPECT_EQ(2U, vertices.size());
1149 VerticesContain(vertices, Point(0, 0));
1150 VerticesContain(vertices, Point(100, 0));
1151 EXPECT_TRUE(AreValidVertices(vertices));
1152
1153 set = ResolutionSet(0, kUnconstrainedMaxDimension, 0, 100, HUGE_VAL,
1154 HUGE_VAL);
1155 vertices = set.ComputeVertices();
1156 EXPECT_EQ(2U, vertices.size());
1157 VerticesContain(vertices, Point(0, 0));
1158 VerticesContain(vertices, Point(0, 100));
1159 EXPECT_TRUE(AreValidVertices(vertices));
1160 }
1161 }
1162
1163 TEST_F(MediaStreamConstraintsUtilSetsTest, NumericRangeSetDouble) {
1164 using DoubleRangeSet = NumericRangeSet<double>;
1165 // Unconstrained set.
1166 DoubleRangeSet set;
1167 EXPECT_EQ(0.0, set.Min());
1168 EXPECT_EQ(HUGE_VAL, set.Max());
1169 EXPECT_FALSE(set.IsEmpty());
1170
1171 // Constrained set.
1172 const double kMin = 1.0;
1173 const double kMax = 10.0;
1174 set = DoubleRangeSet(kMin, kMax);
1175 EXPECT_EQ(kMin, set.Min());
1176 EXPECT_EQ(kMax, set.Max());
1177 EXPECT_FALSE(set.IsEmpty());
1178
1179 // Empty set.
1180 set = DoubleRangeSet(kMax, kMin);
1181 EXPECT_TRUE(set.IsEmpty());
1182
1183 // Intersection.
1184 set = DoubleRangeSet(kMin, kMax);
1185 const double kMin2 = 5.0;
1186 const double kMax2 = 20.0;
1187 auto intersection = set.Intersection(DoubleRangeSet(kMin2, kMax2));
1188 EXPECT_EQ(kMin2, intersection.Min());
1189 EXPECT_EQ(kMax, intersection.Max());
1190 EXPECT_FALSE(intersection.IsEmpty());
1191
1192 // Empty intersection.
1193 intersection = set.Intersection(DoubleRangeSet(kMax + 1, HUGE_VAL));
1194 EXPECT_TRUE(intersection.IsEmpty());
1195 }
1196
1197 TEST_F(MediaStreamConstraintsUtilSetsTest, DiscreteSetString) {
1198 // Unconstrained set.
1199 using StringSet = DiscreteSet<std::string>;
1200 StringSet set;
1201 EXPECT_TRUE(set.Contains("arbitrary"));
1202 EXPECT_TRUE(set.Contains("strings"));
1203 EXPECT_FALSE(set.IsEmpty());
1204 EXPECT_TRUE(set.is_unconstrained());
1205 EXPECT_FALSE(set.IsNonEmptyFinite());
1206
1207 // Constrained set.
1208 set = StringSet(std::vector<std::string>({"a", "b", "c"}));
1209 EXPECT_TRUE(set.Contains("a"));
1210 EXPECT_TRUE(set.Contains("b"));
1211 EXPECT_TRUE(set.Contains("c"));
1212 EXPECT_FALSE(set.Contains("d"));
1213 EXPECT_FALSE(set.IsEmpty());
1214 EXPECT_FALSE(set.is_unconstrained());
1215 EXPECT_TRUE(set.IsNonEmptyFinite());
1216 EXPECT_EQ(std::string("a"), set.FirstElement());
1217
1218 // Empty set.
1219 set = StringSet::EmptySet();
1220 EXPECT_FALSE(set.Contains("a"));
1221 EXPECT_FALSE(set.Contains("b"));
1222 EXPECT_TRUE(set.IsEmpty());
1223 EXPECT_FALSE(set.is_unconstrained());
1224 EXPECT_FALSE(set.IsNonEmptyFinite());
1225
1226 // Intersection.
1227 set = StringSet(std::vector<std::string>({"a", "b", "c"}));
1228 StringSet set2 = StringSet(std::vector<std::string>({"b", "c", "d"}));
1229 auto intersection = set.Intersection(set2);
1230 EXPECT_FALSE(intersection.Contains("a"));
1231 EXPECT_TRUE(intersection.Contains("b"));
1232 EXPECT_TRUE(intersection.Contains("c"));
1233 EXPECT_FALSE(intersection.Contains("d"));
1234 EXPECT_FALSE(intersection.IsEmpty());
1235 EXPECT_FALSE(intersection.is_unconstrained());
1236 EXPECT_TRUE(intersection.IsNonEmptyFinite());
1237 EXPECT_EQ(std::string("b"), intersection.FirstElement());
1238
1239 // Empty intersection.
1240 set2 = StringSet(std::vector<std::string>({"d", "e", "f"}));
1241 intersection = set.Intersection(set2);
1242 EXPECT_FALSE(intersection.Contains("a"));
1243 EXPECT_FALSE(intersection.Contains("b"));
1244 EXPECT_FALSE(intersection.Contains("c"));
1245 EXPECT_FALSE(intersection.Contains("d"));
1246 EXPECT_TRUE(intersection.IsEmpty());
1247 EXPECT_FALSE(intersection.is_unconstrained());
1248 EXPECT_FALSE(intersection.IsNonEmptyFinite());
1249 }
1250
1251 TEST_F(MediaStreamConstraintsUtilSetsTest, DiscreteSetBool) {
1252 using BoolSet = DiscreteSet<bool>;
1253 // Unconstrained set.
1254 BoolSet set;
1255 EXPECT_TRUE(set.Contains(true));
1256 EXPECT_TRUE(set.Contains(false));
1257 EXPECT_FALSE(set.IsEmpty());
1258 EXPECT_TRUE(set.is_unconstrained());
1259 EXPECT_FALSE(set.IsNonEmptyFinite());
1260
1261 // Constrained set.
1262 set = BoolSet({true});
1263 EXPECT_TRUE(set.Contains(true));
1264 EXPECT_FALSE(set.Contains(false));
1265 EXPECT_FALSE(set.IsEmpty());
1266 EXPECT_FALSE(set.is_unconstrained());
1267 EXPECT_TRUE(set.IsNonEmptyFinite());
1268 EXPECT_TRUE(set.FirstElement());
1269
1270 set = BoolSet({false});
1271 EXPECT_FALSE(set.Contains(true));
1272 EXPECT_TRUE(set.Contains(false));
1273 EXPECT_FALSE(set.IsEmpty());
1274 EXPECT_FALSE(set.is_unconstrained());
1275 EXPECT_TRUE(set.IsNonEmptyFinite());
1276 EXPECT_FALSE(set.FirstElement());
1277
1278 // Empty set.
1279 set = BoolSet::EmptySet();
1280 EXPECT_FALSE(set.Contains(true));
1281 EXPECT_FALSE(set.Contains(false));
1282 EXPECT_TRUE(set.IsEmpty());
1283 EXPECT_FALSE(set.is_unconstrained());
1284 EXPECT_FALSE(set.IsNonEmptyFinite());
1285
1286 // Intersection.
1287 set = BoolSet();
1288 auto intersection = set.Intersection(BoolSet({true}));
1289 EXPECT_TRUE(intersection.Contains(true));
1290 EXPECT_FALSE(intersection.Contains(false));
1291 intersection = set.Intersection(set);
1292 EXPECT_TRUE(intersection.Contains(true));
1293 EXPECT_TRUE(intersection.Contains(true));
1294
1295 // Empty intersection.
1296 set = BoolSet({true});
1297 intersection = set.Intersection(BoolSet({false}));
1298 EXPECT_TRUE(intersection.IsEmpty());
1299 }
1300
1301 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698