1 // Copyright 2019 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 "components/viz/service/frame_sinks/begin_frame_tracker.h"
6
7 #include <queue>
8
9 #include "base/containers/queue.h"
10 #include "components/viz/test/begin_frame_args_test.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace viz {
14 namespace {
15
16 class BeginFrameTrackerTest : public testing::Test {
17 public:
SendNextBeginFrame()18 void SendNextBeginFrame() {
19 BeginFrameArgs args = CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE,
20 0, sequence_number_++);
21 pending_acks_.push(BeginFrameAck(args, true));
22 tracker_.SentBeginFrame(args);
23 }
24
SendAck()25 void SendAck() {
26 tracker_.ReceivedAck(pending_acks_.front());
27 pending_acks_.pop();
28 }
29
DropAck()30 void DropAck() { pending_acks_.pop(); }
31
32 protected:
33 BeginFrameTracker tracker_;
34 uint64_t sequence_number_ = 1;
35 std::queue<BeginFrameAck> pending_acks_;
36 };
37
38 // Verify that BeginFrameTracker throttles and unthrottles correctly.
TEST_F(BeginFrameTrackerTest,Throttle)39 TEST_F(BeginFrameTrackerTest, Throttle) {
40 for (int i = 0; i < BeginFrameTracker::kLimitThrottle; ++i) {
41 EXPECT_FALSE(tracker_.ShouldThrottleBeginFrame());
42 EXPECT_FALSE(tracker_.ShouldStopBeginFrame());
43 SendNextBeginFrame();
44 }
45
46 EXPECT_TRUE(tracker_.ShouldThrottleBeginFrame());
47 EXPECT_FALSE(tracker_.ShouldStopBeginFrame());
48
49 SendAck();
50
51 EXPECT_FALSE(tracker_.ShouldThrottleBeginFrame());
52 EXPECT_FALSE(tracker_.ShouldStopBeginFrame());
53 }
54
55 // Verify that BeginFrameTracker stops sending begin frames after kLimitStop.
TEST_F(BeginFrameTrackerTest,Stop)56 TEST_F(BeginFrameTrackerTest, Stop) {
57 for (int i = 0; i < BeginFrameTracker::kLimitStop; ++i) {
58 EXPECT_FALSE(tracker_.ShouldStopBeginFrame());
59 SendNextBeginFrame();
60 }
61
62 EXPECT_FALSE(tracker_.ShouldThrottleBeginFrame());
63 EXPECT_TRUE(tracker_.ShouldStopBeginFrame());
64
65 SendAck();
66
67 EXPECT_TRUE(tracker_.ShouldThrottleBeginFrame());
68 EXPECT_FALSE(tracker_.ShouldStopBeginFrame());
69 }
70
71 // Verify that BeginFrameTracker doesn't throttle a client that only acks half
72 // the time, as long as they ack the latest BeginFrameArgs.
TEST_F(BeginFrameTrackerTest,AllowDroppedAcks)73 TEST_F(BeginFrameTrackerTest, AllowDroppedAcks) {
74 for (int i = 0; i < BeginFrameTracker::kLimitThrottle * 4; ++i) {
75 EXPECT_FALSE(tracker_.ShouldThrottleBeginFrame());
76 EXPECT_FALSE(tracker_.ShouldStopBeginFrame());
77 SendNextBeginFrame();
78
79 if (i % 2)
80 SendAck();
81 else
82 DropAck();
83 }
84 }
85
86 } // namespace
87 } // namespace viz
88