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.
4
5#include <string>
6
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"
12
13namespace content {
14
15class MediaStreamConstraintsUtilTest : public testing::Test {
16};
17
18TEST_F(MediaStreamConstraintsUtilTest, BooleanConstraints) {
19  static const std::string kValueTrue = "true";
20  static const std::string kValueFalse = "false";
21
22  MockMediaConstraintFactory constraint_factory;
23  // Mandatory constraints.
24  constraint_factory.AddMandatory(MediaAudioConstraints::kEchoCancellation,
25                                  kValueTrue);
26  constraint_factory.AddMandatory(MediaAudioConstraints::kGoogEchoCancellation,
27                                  kValueFalse);
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);
38
39  // Optional constraints.
40  constraint_factory.AddOptional(MediaAudioConstraints::kEchoCancellation,
41                                 kValueFalse);
42  constraint_factory.AddOptional(MediaAudioConstraints::kGoogEchoCancellation,
43                                 kValueTrue);
44  constraints = constraint_factory.CreateWebMediaConstraints();
45  EXPECT_TRUE(GetOptionalConstraintValueAsBoolean(
46      constraints, MediaAudioConstraints::kEchoCancellation, &value_false));
47  EXPECT_TRUE(GetOptionalConstraintValueAsBoolean(
48      constraints, MediaAudioConstraints::kGoogEchoCancellation,
49      &value_true));
50  EXPECT_TRUE(value_true);
51  EXPECT_FALSE(value_false);
52}
53
54TEST_F(MediaStreamConstraintsUtilTest, MandatoryDoubleConstraints) {
55  MockMediaConstraintFactory constraint_factory;
56  const std::string test_key = "test key";
57  const double test_value= 0.01f;
58
59  constraint_factory.AddMandatory(test_key, test_value);
60  blink::WebMediaConstraints constraints =
61      constraint_factory.CreateWebMediaConstraints();
62
63  double value;
64  EXPECT_FALSE(GetOptionalConstraintValueAsDouble(constraints, test_key,
65                                                  &value));
66  EXPECT_TRUE(GetMandatoryConstraintValueAsDouble(constraints, test_key,
67                                                  &value));
68  EXPECT_EQ(test_value, value);
69
70  value = 0;
71  EXPECT_TRUE(GetConstraintValueAsDouble(constraints, test_key, &value));
72  EXPECT_EQ(test_value, value);
73}
74
75TEST_F(MediaStreamConstraintsUtilTest, OptionalDoubleConstraints) {
76  MockMediaConstraintFactory constraint_factory;
77  const std::string test_key = "test key";
78  const double test_value= 0.01f;
79
80  constraint_factory.AddOptional(test_key, test_value);
81  blink::WebMediaConstraints constraints =
82      constraint_factory.CreateWebMediaConstraints();
83
84  double value;
85  EXPECT_FALSE(GetMandatoryConstraintValueAsDouble(constraints, test_key,
86                                                   &value));
87  EXPECT_TRUE(GetOptionalConstraintValueAsDouble(constraints, test_key,
88                                                 &value));
89  EXPECT_EQ(test_value, value);
90
91  value = 0;
92  EXPECT_TRUE(GetConstraintValueAsDouble(constraints, test_key, &value));
93  EXPECT_EQ(test_value, value);
94}
95
96TEST_F(MediaStreamConstraintsUtilTest, IntConstraints) {
97  MockMediaConstraintFactory constraint_factory;
98  int width = 600;
99  int height = 480;
100  constraint_factory.AddMandatory(MediaStreamVideoSource::kMaxWidth, width);
101  constraint_factory.AddMandatory(MediaStreamVideoSource::kMaxHeight, height);
102  blink::WebMediaConstraints constraints =
103      constraint_factory.CreateWebMediaConstraints();
104  int value_width = 0;
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);
112
113  width = 720;
114  height = 600;
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);
124}
125
126TEST_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,
131                                  kWrongValueTrue);
132  constraint_factory.AddMandatory(MediaAudioConstraints::kGoogEchoCancellation,
133                                  kWrongValueFalse);
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);
143}
144
145}  // namespace content
146