Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / cast / sender / congestion_control_unittest.cc
blobbf76a35c27d654451fbb4ae1ac1fe58e75c402b9
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.
5 #include <stdint.h>
7 #include "base/bind.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "media/cast/cast_defines.h"
10 #include "media/cast/sender/congestion_control.h"
11 #include "media/cast/test/fake_single_thread_task_runner.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace media {
15 namespace cast {
17 static const uint32 kMaxBitrateConfigured = 5000000;
18 static const uint32 kMinBitrateConfigured = 500000;
19 static const int64 kFrameDelayMs = 33;
20 static const double kMaxFrameRate = 1000.0 / kFrameDelayMs;
21 static const int64 kStartMillisecond = INT64_C(12345678900000);
22 static const double kTargetEmptyBufferFraction = 0.9;
24 class CongestionControlTest : public ::testing::Test {
25 protected:
26 CongestionControlTest()
27 : task_runner_(new test::FakeSingleThreadTaskRunner(&testing_clock_)) {
28 testing_clock_.Advance(
29 base::TimeDelta::FromMilliseconds(kStartMillisecond));
30 congestion_control_.reset(NewAdaptiveCongestionControl(
31 &testing_clock_, kMaxBitrateConfigured, kMinBitrateConfigured,
32 kMaxFrameRate));
33 const int max_unacked_frames = 10;
34 const base::TimeDelta target_playout_delay =
35 (max_unacked_frames - 1) * base::TimeDelta::FromSeconds(1) /
36 kMaxFrameRate;
37 congestion_control_->UpdateTargetPlayoutDelay(target_playout_delay);
40 void AckFrame(uint32 frame_id) {
41 congestion_control_->AckFrame(frame_id, testing_clock_.NowTicks());
44 void Run(uint32 frames,
45 size_t frame_size,
46 base::TimeDelta rtt,
47 base::TimeDelta frame_delay,
48 base::TimeDelta ack_time) {
49 for (frame_id_ = 0; frame_id_ < frames; frame_id_++) {
50 congestion_control_->UpdateRtt(rtt);
51 congestion_control_->SendFrameToTransport(
52 frame_id_, frame_size, testing_clock_.NowTicks());
53 task_runner_->PostDelayedTask(FROM_HERE,
54 base::Bind(&CongestionControlTest::AckFrame,
55 base::Unretained(this),
56 frame_id_),
57 ack_time);
58 task_runner_->Sleep(frame_delay);
62 base::SimpleTestTickClock testing_clock_;
63 scoped_ptr<CongestionControl> congestion_control_;
64 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
65 uint32 frame_id_;
67 DISALLOW_COPY_AND_ASSIGN(CongestionControlTest);
70 TEST_F(CongestionControlTest, SimpleRun) {
71 uint32 frame_size = 10000 * 8;
72 Run(500,
73 frame_size,
74 base::TimeDelta::FromMilliseconds(10),
75 base::TimeDelta::FromMilliseconds(kFrameDelayMs),
76 base::TimeDelta::FromMilliseconds(45));
77 // Empty the buffer.
78 task_runner_->Sleep(base::TimeDelta::FromMilliseconds(100));
80 uint32 safe_bitrate = frame_size * 1000 / kFrameDelayMs;
81 uint32 bitrate = congestion_control_->GetBitrate(
82 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(300),
83 base::TimeDelta::FromMilliseconds(300));
84 EXPECT_NEAR(
85 safe_bitrate / kTargetEmptyBufferFraction, bitrate, safe_bitrate * 0.05);
87 bitrate = congestion_control_->GetBitrate(
88 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(200),
89 base::TimeDelta::FromMilliseconds(300));
90 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 2 / 3,
91 bitrate,
92 safe_bitrate * 0.05);
94 bitrate = congestion_control_->GetBitrate(
95 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(100),
96 base::TimeDelta::FromMilliseconds(300));
97 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 1 / 3,
98 bitrate,
99 safe_bitrate * 0.05);
101 // Add a large (100ms) frame.
102 congestion_control_->SendFrameToTransport(
103 frame_id_++, safe_bitrate * 100 / 1000, testing_clock_.NowTicks());
105 // Results should show that we have ~200ms to send
106 bitrate = congestion_control_->GetBitrate(
107 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(300),
108 base::TimeDelta::FromMilliseconds(300));
109 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 2 / 3,
110 bitrate,
111 safe_bitrate * 0.05);
113 // Add another large (100ms) frame.
114 congestion_control_->SendFrameToTransport(
115 frame_id_++, safe_bitrate * 100 / 1000, testing_clock_.NowTicks());
117 // Resulst should show that we have ~100ms to send
118 bitrate = congestion_control_->GetBitrate(
119 testing_clock_.NowTicks() + base::TimeDelta::FromMilliseconds(300),
120 base::TimeDelta::FromMilliseconds(300));
121 EXPECT_NEAR(safe_bitrate / kTargetEmptyBufferFraction * 1 / 3,
122 bitrate,
123 safe_bitrate * 0.05);
127 } // namespace cast
128 } // namespace media