1 // Copyright 2014 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.
7 #include "content/renderer/media/media_stream_audio_processor_options.h"
8 #include "content/renderer/media/media_stream_constraints_util.h"
9 #include "content/renderer/media/media_stream_video_source.h"
10 #include "content/renderer/media/mock_media_constraint_factory.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class MediaStreamConstraintsUtilTest
: public testing::Test
{
18 TEST_F(MediaStreamConstraintsUtilTest
, BooleanConstraints
) {
19 static const std::string kValueTrue
= "true";
20 static const std::string kValueFalse
= "false";
22 MockMediaConstraintFactory constraint_factory
;
23 // Mandatory constraints.
24 constraint_factory
.AddMandatory(MediaAudioConstraints::kEchoCancellation
,
26 constraint_factory
.AddMandatory(MediaAudioConstraints::kGoogEchoCancellation
,
28 blink::WebMediaConstraints constraints
=
29 constraint_factory
.CreateWebMediaConstraints();
30 bool value_true
= false;
31 bool value_false
= false;
32 EXPECT_TRUE(GetMandatoryConstraintValueAsBoolean(
33 constraints
, MediaAudioConstraints::kEchoCancellation
, &value_true
));
34 EXPECT_TRUE(GetMandatoryConstraintValueAsBoolean(
35 constraints
, MediaAudioConstraints::kGoogEchoCancellation
, &value_false
));
36 EXPECT_TRUE(value_true
);
37 EXPECT_FALSE(value_false
);
39 // Optional constraints.
40 constraint_factory
.AddOptional(MediaAudioConstraints::kEchoCancellation
,
42 constraint_factory
.AddOptional(MediaAudioConstraints::kGoogEchoCancellation
,
44 constraints
= constraint_factory
.CreateWebMediaConstraints();
45 EXPECT_TRUE(GetOptionalConstraintValueAsBoolean(
46 constraints
, MediaAudioConstraints::kEchoCancellation
, &value_false
));
47 EXPECT_TRUE(GetOptionalConstraintValueAsBoolean(
48 constraints
, MediaAudioConstraints::kGoogEchoCancellation
,
50 EXPECT_TRUE(value_true
);
51 EXPECT_FALSE(value_false
);
54 TEST_F(MediaStreamConstraintsUtilTest
, MandatoryDoubleConstraints
) {
55 MockMediaConstraintFactory constraint_factory
;
56 const std::string test_key
= "test key";
57 const double test_value
= 0.01f
;
59 constraint_factory
.AddMandatory(test_key
, test_value
);
60 blink::WebMediaConstraints constraints
=
61 constraint_factory
.CreateWebMediaConstraints();
64 EXPECT_FALSE(GetOptionalConstraintValueAsDouble(constraints
, test_key
,
66 EXPECT_TRUE(GetMandatoryConstraintValueAsDouble(constraints
, test_key
,
68 EXPECT_EQ(test_value
, value
);
71 EXPECT_TRUE(GetConstraintValueAsDouble(constraints
, test_key
, &value
));
72 EXPECT_EQ(test_value
, value
);
75 TEST_F(MediaStreamConstraintsUtilTest
, OptionalDoubleConstraints
) {
76 MockMediaConstraintFactory constraint_factory
;
77 const std::string test_key
= "test key";
78 const double test_value
= 0.01f
;
80 constraint_factory
.AddOptional(test_key
, test_value
);
81 blink::WebMediaConstraints constraints
=
82 constraint_factory
.CreateWebMediaConstraints();
85 EXPECT_FALSE(GetMandatoryConstraintValueAsDouble(constraints
, test_key
,
87 EXPECT_TRUE(GetOptionalConstraintValueAsDouble(constraints
, test_key
,
89 EXPECT_EQ(test_value
, value
);
92 EXPECT_TRUE(GetConstraintValueAsDouble(constraints
, test_key
, &value
));
93 EXPECT_EQ(test_value
, value
);
96 TEST_F(MediaStreamConstraintsUtilTest
, IntConstraints
) {
97 MockMediaConstraintFactory constraint_factory
;
100 constraint_factory
.AddMandatory(MediaStreamVideoSource::kMaxWidth
, width
);
101 constraint_factory
.AddMandatory(MediaStreamVideoSource::kMaxHeight
, height
);
102 blink::WebMediaConstraints constraints
=
103 constraint_factory
.CreateWebMediaConstraints();
105 int value_height
= 0;
106 EXPECT_TRUE(GetMandatoryConstraintValueAsInteger(
107 constraints
, MediaStreamVideoSource::kMaxWidth
, &value_width
));
108 EXPECT_TRUE(GetMandatoryConstraintValueAsInteger(
109 constraints
, MediaStreamVideoSource::kMaxHeight
, &value_height
));
110 EXPECT_EQ(width
, value_width
);
111 EXPECT_EQ(height
, value_height
);
115 constraint_factory
.AddOptional(MediaStreamVideoSource::kMaxWidth
, width
);
116 constraint_factory
.AddOptional(MediaStreamVideoSource::kMaxHeight
, height
);
117 constraints
= constraint_factory
.CreateWebMediaConstraints();
118 EXPECT_TRUE(GetOptionalConstraintValueAsInteger(
119 constraints
, MediaStreamVideoSource::kMaxWidth
, &value_width
));
120 EXPECT_TRUE(GetOptionalConstraintValueAsInteger(
121 constraints
, MediaStreamVideoSource::kMaxHeight
, &value_height
));
122 EXPECT_EQ(width
, value_width
);
123 EXPECT_EQ(height
, value_height
);
126 TEST_F(MediaStreamConstraintsUtilTest
, WrongBooleanConstraints
) {
127 static const std::string kWrongValueTrue
= "True";
128 static const std::string kWrongValueFalse
= "False";
129 MockMediaConstraintFactory constraint_factory
;
130 constraint_factory
.AddMandatory(MediaAudioConstraints::kEchoCancellation
,
132 constraint_factory
.AddMandatory(MediaAudioConstraints::kGoogEchoCancellation
,
134 blink::WebMediaConstraints constraints
=
135 constraint_factory
.CreateWebMediaConstraints();
136 bool value_false
= false;
137 EXPECT_FALSE(GetMandatoryConstraintValueAsBoolean(
138 constraints
, MediaAudioConstraints::kEchoCancellation
, &value_false
));
139 EXPECT_FALSE(value_false
);
140 EXPECT_FALSE(GetMandatoryConstraintValueAsBoolean(
141 constraints
, MediaAudioConstraints::kGoogEchoCancellation
, &value_false
));
142 EXPECT_FALSE(value_false
);
145 } // namespace content