1 // Copyright (c) 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/content/feedback_signal_accumulator.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 class FeedbackSignalAccumulatorTest
: public ::testing::Test
{
13 FeedbackSignalAccumulatorTest()
14 : half_life_(base::TimeDelta::FromSeconds(1)),
16 t_(base::TimeTicks() + base::TimeDelta::FromSeconds(120)) {
21 const base::TimeDelta half_life_
;
22 FeedbackSignalAccumulator acc_
;
26 TEST_F(FeedbackSignalAccumulatorTest
, HasCorrectStartingValueAfterReset
) {
27 ASSERT_EQ(0.0, acc_
.current());
28 ASSERT_EQ(t_
, acc_
.reset_time());
29 ASSERT_EQ(t_
, acc_
.update_time());
32 ASSERT_EQ(1.0, acc_
.current());
33 ASSERT_EQ(t_
, acc_
.reset_time());
34 ASSERT_EQ(t_
, acc_
.update_time());
38 ASSERT_EQ(2.0, acc_
.current());
39 ASSERT_EQ(t_
, acc_
.reset_time());
40 ASSERT_EQ(t_
, acc_
.update_time());
43 TEST_F(FeedbackSignalAccumulatorTest
, DoesNotUpdateIfBeforeResetTime
) {
45 ASSERT_EQ(0.0, acc_
.current());
46 ASSERT_EQ(t_
, acc_
.update_time());
48 const base::TimeTicks one_usec_before
=
49 t_
- base::TimeDelta::FromMicroseconds(1);
50 ASSERT_FALSE(acc_
.Update(1.0, one_usec_before
));
51 ASSERT_EQ(0.0, acc_
.current());
52 ASSERT_EQ(t_
, acc_
.update_time());
54 const base::TimeTicks one_usec_after
=
55 t_
+ base::TimeDelta::FromMicroseconds(1);
56 ASSERT_TRUE(acc_
.Update(1.0, one_usec_after
));
57 ASSERT_LT(0.0, acc_
.current());
58 ASSERT_EQ(one_usec_after
, acc_
.update_time());
61 TEST_F(FeedbackSignalAccumulatorTest
, TakesMaxOfUpdatesAtResetTime
) {
63 ASSERT_EQ(0.0, acc_
.current());
64 ASSERT_EQ(t_
, acc_
.update_time());
66 ASSERT_TRUE(acc_
.Update(1.0, t_
));
67 ASSERT_EQ(1.0, acc_
.current());
68 ASSERT_EQ(t_
, acc_
.update_time());
70 ASSERT_TRUE(acc_
.Update(2.0, t_
));
71 ASSERT_EQ(2.0, acc_
.current());
72 ASSERT_EQ(t_
, acc_
.update_time());
74 ASSERT_TRUE(acc_
.Update(1.0, t_
));
75 ASSERT_EQ(2.0, acc_
.current());
76 ASSERT_EQ(t_
, acc_
.update_time());
79 TEST_F(FeedbackSignalAccumulatorTest
, AppliesMaxOfUpdatesWithSameTimestamp
) {
81 ASSERT_EQ(0.0, acc_
.current());
82 ASSERT_EQ(t_
, acc_
.update_time());
85 // Update with an identical value at the same timestamp.
86 for (int i
= 0; i
< 3; ++i
) {
87 ASSERT_TRUE(acc_
.Update(1.0, t_
));
88 ASSERT_EQ(0.5, acc_
.current());
89 ASSERT_EQ(t_
, acc_
.update_time());
92 // Now continue updating with different values at the same timestamp.
93 ASSERT_TRUE(acc_
.Update(2.0, t_
));
94 ASSERT_EQ(1.0, acc_
.current());
95 ASSERT_EQ(t_
, acc_
.update_time());
96 ASSERT_TRUE(acc_
.Update(3.0, t_
));
97 ASSERT_EQ(1.5, acc_
.current());
98 ASSERT_EQ(t_
, acc_
.update_time());
99 ASSERT_TRUE(acc_
.Update(1.0, t_
));
100 ASSERT_EQ(1.5, acc_
.current());
101 ASSERT_EQ(t_
, acc_
.update_time());
104 TEST_F(FeedbackSignalAccumulatorTest
, ProvidesExpectedHoldResponse
) {
105 // Step one half-life interval per update.
107 ASSERT_EQ(0.0, acc_
.current());
108 ASSERT_EQ(t_
, acc_
.update_time());
109 t_
+= 1 * half_life_
;
110 ASSERT_TRUE(acc_
.Update(1.0, t_
));
111 ASSERT_EQ(0.5, acc_
.current());
112 ASSERT_EQ(t_
, acc_
.update_time());
113 t_
+= 1 * half_life_
;
114 ASSERT_TRUE(acc_
.Update(1.0, t_
));
115 ASSERT_EQ(0.75, acc_
.current());
116 ASSERT_EQ(t_
, acc_
.update_time());
117 t_
+= 1 * half_life_
;
118 ASSERT_TRUE(acc_
.Update(1.0, t_
));
119 ASSERT_EQ(0.875, acc_
.current());
120 ASSERT_EQ(t_
, acc_
.update_time());
121 t_
+= 1 * half_life_
;
122 ASSERT_TRUE(acc_
.Update(1.0, t_
));
123 ASSERT_EQ(0.9375, acc_
.current());
124 ASSERT_EQ(t_
, acc_
.update_time());
126 // Step two half-life intervals per update.
128 ASSERT_EQ(0.0, acc_
.current());
129 ASSERT_EQ(t_
, acc_
.update_time());
130 t_
+= 2 * half_life_
;
131 ASSERT_TRUE(acc_
.Update(1.0, t_
));
132 ASSERT_NEAR(0.666666667, acc_
.current(), 0.000000001);
133 ASSERT_EQ(t_
, acc_
.update_time());
134 t_
+= 2 * half_life_
;
135 ASSERT_TRUE(acc_
.Update(1.0, t_
));
136 ASSERT_NEAR(0.888888889, acc_
.current(), 0.000000001);
137 ASSERT_EQ(t_
, acc_
.update_time());
138 t_
+= 2 * half_life_
;
139 ASSERT_TRUE(acc_
.Update(1.0, t_
));
140 ASSERT_NEAR(0.962962963, acc_
.current(), 0.000000001);
141 ASSERT_EQ(t_
, acc_
.update_time());
142 t_
+= 2 * half_life_
;
143 ASSERT_TRUE(acc_
.Update(1.0, t_
));
144 ASSERT_NEAR(0.987654321, acc_
.current(), 0.000000001);
145 ASSERT_EQ(t_
, acc_
.update_time());
147 // Step three half-life intervals per update.
149 ASSERT_EQ(0.0, acc_
.current());
150 ASSERT_EQ(t_
, acc_
.update_time());
151 t_
+= 3 * half_life_
;
152 ASSERT_TRUE(acc_
.Update(1.0, t_
));
153 ASSERT_EQ(0.75, acc_
.current());
154 ASSERT_EQ(t_
, acc_
.update_time());
155 t_
+= 3 * half_life_
;
156 ASSERT_TRUE(acc_
.Update(1.0, t_
));
157 ASSERT_EQ(0.9375, acc_
.current());
158 ASSERT_EQ(t_
, acc_
.update_time());
159 t_
+= 3 * half_life_
;
160 ASSERT_TRUE(acc_
.Update(1.0, t_
));
161 ASSERT_EQ(0.984375, acc_
.current());
162 ASSERT_EQ(t_
, acc_
.update_time());
163 t_
+= 3 * half_life_
;
164 ASSERT_TRUE(acc_
.Update(1.0, t_
));
165 ASSERT_EQ(0.99609375, acc_
.current());
166 ASSERT_EQ(t_
, acc_
.update_time());
169 TEST_F(FeedbackSignalAccumulatorTest
, IgnoresUpdatesThatAreOutOfOrder
) {
170 // First, go forward several steps, in order.
172 ASSERT_EQ(0.0, acc_
.current());
173 ASSERT_EQ(t_
, acc_
.update_time());
174 t_
+= 1 * half_life_
;
175 ASSERT_TRUE(acc_
.Update(2.0, t_
));
176 ASSERT_EQ(1.0, acc_
.current());
177 ASSERT_EQ(t_
, acc_
.update_time());
178 t_
+= 1 * half_life_
;
179 ASSERT_TRUE(acc_
.Update(2.0, t_
));
180 ASSERT_EQ(1.5, acc_
.current());
181 ASSERT_EQ(t_
, acc_
.update_time());
182 t_
+= 1 * half_life_
;
183 ASSERT_TRUE(acc_
.Update(2.0, t_
));
184 ASSERT_EQ(1.75, acc_
.current());
185 ASSERT_EQ(t_
, acc_
.update_time());
186 t_
+= 1 * half_life_
;
187 ASSERT_TRUE(acc_
.Update(2.0, t_
));
188 ASSERT_EQ(1.875, acc_
.current());
189 ASSERT_EQ(t_
, acc_
.update_time());
191 // Go back 1 steps, then 1.5, then 2, then 2.5, etc. and expect the update to
193 base::TimeTicks earlier
= t_
- 1 * half_life_
;
194 for (int i
= 0; i
< 5; ++i
) {
195 ASSERT_FALSE(acc_
.Update(999.0, earlier
));
196 ASSERT_EQ(1.875, acc_
.current());
197 ASSERT_EQ(t_
, acc_
.update_time());
198 earlier
-= half_life_
/ 2;