1 // Copyright 2015 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 "media/capture/capture_resolution_chooser.h"
7 #include "base/location.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using tracked_objects::Location
;
16 // 16:9 maximum and minimum frame sizes.
17 const int kMaxFrameWidth
= 3840;
18 const int kMaxFrameHeight
= 2160;
19 const int kMinFrameWidth
= 320;
20 const int kMinFrameHeight
= 180;
22 // Checks whether |size| is strictly between (inclusive) |min_size| and
23 // |max_size| and has the same aspect ratio as |max_size|.
24 void ExpectIsWithinBoundsAndSameAspectRatio(const Location
& location
,
25 const gfx::Size
& min_size
,
26 const gfx::Size
& max_size
,
27 const gfx::Size
& size
) {
28 SCOPED_TRACE(::testing::Message() << "From here: " << location
.ToString());
29 EXPECT_LE(min_size
.width(), size
.width());
30 EXPECT_LE(min_size
.height(), size
.height());
31 EXPECT_GE(max_size
.width(), size
.width());
32 EXPECT_GE(max_size
.height(), size
.height());
33 EXPECT_NEAR(static_cast<double>(max_size
.width()) / max_size
.height(),
34 static_cast<double>(size
.width()) / size
.height(),
40 TEST(CaptureResolutionChooserTest
,
41 FixedResolutionPolicy_CaptureSizeAlwaysFixed
) {
42 const gfx::Size
the_one_frame_size(kMaxFrameWidth
, kMaxFrameHeight
);
43 CaptureResolutionChooser
chooser(the_one_frame_size
,
44 RESOLUTION_POLICY_FIXED_RESOLUTION
);
45 EXPECT_EQ(the_one_frame_size
, chooser
.capture_size());
47 chooser
.SetSourceSize(the_one_frame_size
);
48 EXPECT_EQ(the_one_frame_size
, chooser
.capture_size());
50 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
+ 424, kMaxFrameHeight
- 101));
51 EXPECT_EQ(the_one_frame_size
, chooser
.capture_size());
53 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
- 202, kMaxFrameHeight
+ 56));
54 EXPECT_EQ(the_one_frame_size
, chooser
.capture_size());
56 chooser
.SetSourceSize(gfx::Size(kMinFrameWidth
, kMinFrameHeight
));
57 EXPECT_EQ(the_one_frame_size
, chooser
.capture_size());
60 TEST(CaptureResolutionChooserTest
,
61 FixedAspectRatioPolicy_CaptureSizeHasSameAspectRatio
) {
62 CaptureResolutionChooser
chooser(
63 gfx::Size(kMaxFrameWidth
, kMaxFrameHeight
),
64 RESOLUTION_POLICY_FIXED_ASPECT_RATIO
);
66 // Starting condition.
67 const gfx::Size
min_size(kMinFrameWidth
, kMinFrameHeight
);
68 const gfx::Size
max_size(kMaxFrameWidth
, kMaxFrameHeight
);
69 ExpectIsWithinBoundsAndSameAspectRatio(
70 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
72 // Max size in --> max size out.
73 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
, kMaxFrameHeight
));
74 ExpectIsWithinBoundsAndSameAspectRatio(
75 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
77 // Various source sizes within bounds.
78 chooser
.SetSourceSize(gfx::Size(640, 480));
79 ExpectIsWithinBoundsAndSameAspectRatio(
80 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
82 chooser
.SetSourceSize(gfx::Size(480, 640));
83 ExpectIsWithinBoundsAndSameAspectRatio(
84 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
86 chooser
.SetSourceSize(gfx::Size(640, 640));
87 ExpectIsWithinBoundsAndSameAspectRatio(
88 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
90 // Bad source size results in no update.
91 const gfx::Size unchanged_size
= chooser
.capture_size();
92 chooser
.SetSourceSize(gfx::Size(0, 0));
93 EXPECT_EQ(unchanged_size
, chooser
.capture_size());
95 // Downscaling size (preserving aspect ratio) when source size exceeds the
97 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
* 2, kMaxFrameHeight
* 2));
98 ExpectIsWithinBoundsAndSameAspectRatio(
99 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
101 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
* 2, kMaxFrameHeight
));
102 ExpectIsWithinBoundsAndSameAspectRatio(
103 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
105 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
, kMaxFrameHeight
* 2));
106 ExpectIsWithinBoundsAndSameAspectRatio(
107 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
109 // Upscaling size (preserving aspect ratio) when source size is under the
111 chooser
.SetSourceSize(gfx::Size(kMinFrameWidth
/ 2, kMinFrameHeight
/ 2));
112 ExpectIsWithinBoundsAndSameAspectRatio(
113 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
115 chooser
.SetSourceSize(gfx::Size(kMinFrameWidth
/ 2, kMaxFrameHeight
));
116 ExpectIsWithinBoundsAndSameAspectRatio(
117 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
119 chooser
.SetSourceSize(gfx::Size(kMinFrameWidth
, kMinFrameHeight
/ 2));
120 ExpectIsWithinBoundsAndSameAspectRatio(
121 FROM_HERE
, min_size
, max_size
, chooser
.capture_size());
124 TEST(CaptureResolutionChooserTest
,
125 AnyWithinLimitPolicy_CaptureSizeIsAnythingWithinLimits
) {
126 const gfx::Size
max_size(kMaxFrameWidth
, kMaxFrameHeight
);
127 CaptureResolutionChooser
chooser(
128 max_size
, RESOLUTION_POLICY_ANY_WITHIN_LIMIT
);
130 // Starting condition.
131 EXPECT_EQ(max_size
, chooser
.capture_size());
133 // Max size in --> max size out.
134 chooser
.SetSourceSize(max_size
);
135 EXPECT_EQ(max_size
, chooser
.capture_size());
137 // Various source sizes within bounds.
138 chooser
.SetSourceSize(gfx::Size(640, 480));
139 EXPECT_EQ(gfx::Size(640, 480), chooser
.capture_size());
141 chooser
.SetSourceSize(gfx::Size(480, 640));
142 EXPECT_EQ(gfx::Size(480, 640), chooser
.capture_size());
144 chooser
.SetSourceSize(gfx::Size(640, 640));
145 EXPECT_EQ(gfx::Size(640, 640), chooser
.capture_size());
147 chooser
.SetSourceSize(gfx::Size(2, 2));
148 EXPECT_EQ(gfx::Size(2, 2), chooser
.capture_size());
150 // Bad source size results in no update.
151 const gfx::Size unchanged_size
= chooser
.capture_size();
152 chooser
.SetSourceSize(gfx::Size(0, 0));
153 EXPECT_EQ(unchanged_size
, chooser
.capture_size());
155 // Downscaling size (preserving aspect ratio) when source size exceeds the
157 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
* 2, kMaxFrameHeight
* 2));
158 EXPECT_EQ(max_size
, chooser
.capture_size());
160 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
* 2, kMaxFrameHeight
));
161 EXPECT_EQ(gfx::Size(kMaxFrameWidth
, kMaxFrameHeight
/ 2),
162 chooser
.capture_size());
164 chooser
.SetSourceSize(gfx::Size(kMaxFrameWidth
, kMaxFrameHeight
* 2));
165 EXPECT_EQ(gfx::Size(kMaxFrameWidth
/ 2, kMaxFrameHeight
),
166 chooser
.capture_size());