Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / capture / content / feedback_signal_accumulator_unittest.cc
blob9d0f925adcdae14ec8f0ce004147e3f03ec67265
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"
9 namespace media {
11 class FeedbackSignalAccumulatorTest : public ::testing::Test {
12 public:
13 FeedbackSignalAccumulatorTest()
14 : half_life_(base::TimeDelta::FromSeconds(1)),
15 acc_(half_life_),
16 t_(base::TimeTicks() + base::TimeDelta::FromSeconds(120)) {
17 acc_.Reset(0.0, t_);
20 protected:
21 const base::TimeDelta half_life_;
22 FeedbackSignalAccumulator acc_;
23 base::TimeTicks t_;
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());
31 acc_.Reset(1.0, t_);
32 ASSERT_EQ(1.0, acc_.current());
33 ASSERT_EQ(t_, acc_.reset_time());
34 ASSERT_EQ(t_, acc_.update_time());
36 t_ += half_life_;
37 acc_.Reset(2.0, t_);
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) {
44 acc_.Reset(0.0, t_);
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) {
62 acc_.Reset(0.0, t_);
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) {
80 acc_.Reset(0.0, t_);
81 ASSERT_EQ(0.0, acc_.current());
82 ASSERT_EQ(t_, acc_.update_time());
83 t_ += 1 * half_life_;
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.
106 acc_.Reset(0.0, t_);
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.
127 acc_.Reset(0.0, t_);
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.
148 acc_.Reset(0.0, t_);
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.
171 acc_.Reset(0.0, t_);
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
192 // fail each time.
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;
202 } // namespace media