forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smoothness_helper_unittest.cc
208 lines (172 loc) · 7.87 KB
/
smoothness_helper_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/blink/smoothness_helper.h"
#include "base/run_loop.h"
#include "media/blink/blink_platform_with_task_environment.h"
#include "media/learning/common/labelled_example.h"
#include "media/learning/common/learning_task_controller.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
using learning::FeatureValue;
using learning::FeatureVector;
using learning::LearningTask;
using learning::LearningTaskController;
using learning::ObservationCompletion;
using learning::TargetValue;
using testing::_;
using testing::AnyNumber;
using testing::Eq;
using testing::Gt;
using testing::Lt;
using testing::ResultOf;
using testing::Return;
// Helper for EXPECT_CALL argument matching on Optional<TargetValue>. Applies
// matcher |m| to the TargetValue as a double. For example:
// void Foo(absl::optional<TargetValue>);
// EXPECT_CALL(..., Foo(OPT_TARGET(Gt(0.9)))) will expect that the value of the
// Optional<TargetValue> passed to Foo() to be greather than 0.9 .
#define OPT_TARGET(m) \
ResultOf([](const absl::optional<TargetValue>& v) { return (*v).value(); }, m)
// Same as above, but expects an ObservationCompletion.
#define COMPLETION_TARGET(m) \
ResultOf( \
[](const ObservationCompletion& x) { return x.target_value.value(); }, \
m)
class SmoothnessHelperTest : public testing::Test {
class MockLearningTaskController : public LearningTaskController {
public:
MOCK_METHOD4(BeginObservation,
void(base::UnguessableToken id,
const FeatureVector& features,
const absl::optional<TargetValue>& default_target,
const absl::optional<ukm::SourceId>& source_id));
MOCK_METHOD2(CompleteObservation,
void(base::UnguessableToken id,
const ObservationCompletion& completion));
MOCK_METHOD1(CancelObservation, void(base::UnguessableToken id));
MOCK_METHOD2(UpdateDefaultTarget,
void(base::UnguessableToken id,
const absl::optional<TargetValue>& default_target));
MOCK_METHOD0(GetLearningTask, const LearningTask&());
MOCK_METHOD2(PredictDistribution,
void(const FeatureVector& features, PredictionCB callback));
};
class MockClient : public SmoothnessHelper::Client {
public:
~MockClient() override = default;
MOCK_CONST_METHOD0(DecodedFrameCount, unsigned(void));
MOCK_CONST_METHOD0(DroppedFrameCount, unsigned(void));
};
public:
void SetUp() override {
auto bad_ltc = std::make_unique<MockLearningTaskController>();
bad_ltc_ = bad_ltc.get();
auto nnr_ltc = std::make_unique<MockLearningTaskController>();
nnr_ltc_ = nnr_ltc.get();
features_.push_back(FeatureValue(123));
helper_ = SmoothnessHelper::Create(std::move(bad_ltc), std::move(nnr_ltc),
features_, &client_);
segment_size_ = SmoothnessHelper::SegmentSizeForTesting();
}
// Helper for EXPECT_CALL.
absl::optional<TargetValue> Opt(double x) {
return absl::optional<TargetValue>(TargetValue(x));
}
void FastForwardBy(base::TimeDelta amount) {
BlinkPlatformWithTaskEnvironment::GetTaskEnvironment()->FastForwardBy(
amount);
}
// Set the dropped / decoded totals that will be returned by the mock client.
void SetFrameCounters(int dropped, int decoded) {
ON_CALL(client_, DroppedFrameCount()).WillByDefault(Return(dropped));
ON_CALL(client_, DecodedFrameCount()).WillByDefault(Return(decoded));
}
// Helper under test
std::unique_ptr<SmoothnessHelper> helper_;
// Max bad consecutive windows by frame drop LTC.
MockLearningTaskController* bad_ltc_;
// Max consecutive NNRs LTC.
MockLearningTaskController* nnr_ltc_;
MockClient client_;
FeatureVector features_;
base::TimeDelta segment_size_;
};
TEST_F(SmoothnessHelperTest, FeaturesAreReturned) {
EXPECT_EQ(features_, helper_->features());
}
TEST_F(SmoothnessHelperTest, MaxBadWindowsRecordsTrue) {
// Record three bad segments, and verify that it records 'true'.
SetFrameCounters(0, 0);
base::RunLoop().RunUntilIdle();
int dropped_frames = 0;
int total_frames = 0;
// First segment has no dropped frames. Should record 0.
EXPECT_CALL(*bad_ltc_, BeginObservation(_, _, OPT_TARGET(0.0), _)).Times(1);
SetFrameCounters(dropped_frames += 0, total_frames += 1000);
FastForwardBy(segment_size_);
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(bad_ltc_);
// Second segment has a lot of dropped frames, so the target should increase.
EXPECT_CALL(*bad_ltc_, UpdateDefaultTarget(_, OPT_TARGET(1.0))).Times(1);
SetFrameCounters(dropped_frames += 999, total_frames += 1000);
FastForwardBy(segment_size_);
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(bad_ltc_);
// Third segment looks nice, so nothing should update.
EXPECT_CALL(*bad_ltc_, UpdateDefaultTarget(_, OPT_TARGET(_))).Times(0);
SetFrameCounters(dropped_frames += 0, total_frames += 1000);
FastForwardBy(segment_size_);
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(bad_ltc_);
// Fourth segment has dropped frames, but the default shouldn't change.
// It's okay if it changes to the same value, but we just memorize that it
// won't change at all.
EXPECT_CALL(*bad_ltc_, UpdateDefaultTarget(_, OPT_TARGET(_))).Times(0);
SetFrameCounters(dropped_frames += 999, total_frames += 1000);
FastForwardBy(segment_size_);
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(bad_ltc_);
// The last segment is also bad, and should increase the max.
EXPECT_CALL(*bad_ltc_, UpdateDefaultTarget(_, OPT_TARGET(2.0))).Times(1);
SetFrameCounters(dropped_frames += 999, total_frames += 1000);
FastForwardBy(segment_size_);
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(bad_ltc_);
}
TEST_F(SmoothnessHelperTest, NNRTaskRecordsMaxNNRs) {
// We should get the first target once a window has elapsed. We need some
// decoded frames before anything happens.
SetFrameCounters(0, 1);
EXPECT_CALL(*nnr_ltc_, BeginObservation(_, _, OPT_TARGET(0.0), _)).Times(1);
FastForwardBy(segment_size_);
base::RunLoop().RunUntilIdle();
FastForwardBy(segment_size_);
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(nnr_ltc_);
// Add some NNRs, which should be reported immediately now that a segment
// has started. Note that we don't care if NNRs are reported before a segment
// is started, because it's not really clear which behavior is right anyway.
EXPECT_CALL(*nnr_ltc_, UpdateDefaultTarget(_, OPT_TARGET(1))).Times(1);
helper_->NotifyNNR();
testing::Mock::VerifyAndClearExpectations(nnr_ltc_);
// Advance time by one window, and add an NNR. It's close enough that we
// should be notified that the max went up.
FastForwardBy(segment_size_);
EXPECT_CALL(*nnr_ltc_, UpdateDefaultTarget(_, OPT_TARGET(2))).Times(1);
helper_->NotifyNNR();
testing::Mock::VerifyAndClearExpectations(nnr_ltc_);
// Fast forward by a lot, so that the next NNR isn't consecutive. Nothing
// should be reported, because it's less than the current maximum.
EXPECT_CALL(*nnr_ltc_, UpdateDefaultTarget(_, OPT_TARGET(_))).Times(0);
FastForwardBy(base::TimeDelta::FromSeconds(1000));
helper_->NotifyNNR();
// It might be okay if this reported 2, since it's a tie.
helper_->NotifyNNR();
testing::Mock::VerifyAndClearExpectations(nnr_ltc_);
// The next NNR should advance the maximum to 3.
EXPECT_CALL(*nnr_ltc_, UpdateDefaultTarget(_, OPT_TARGET(3))).Times(1);
helper_->NotifyNNR();
}
} // namespace media