1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // #define LOG_NDEBUG 0
18 #define LOG_TAG "monotonicframecounter_tests"
19
20 #include "../MonotonicFrameCounter.h"
21
22 #include <gtest/gtest.h>
23
24 using namespace android::audioflinger;
25
26 namespace {
27
TEST(MonotonicFrameCounterTest,SimpleProgression)28 TEST(MonotonicFrameCounterTest, SimpleProgression) {
29 MonotonicFrameCounter monotonicFrameCounter;
30
31 const std::vector<std::pair<int64_t, int64_t>> frametimes{
32 {0, 0}, {100, 100}, {200, 200},
33 };
34
35 int64_t maxReceivedFrameCount = 0;
36 for (const auto& p : frametimes) {
37 maxReceivedFrameCount = std::max(maxReceivedFrameCount, p.first);
38 ASSERT_EQ(p.first,
39 monotonicFrameCounter.updateAndGetMonotonicFrameCount(p.first, p.second));
40 }
41 ASSERT_EQ(maxReceivedFrameCount, monotonicFrameCounter.getLastReportedFrameCount());
42 }
43
TEST(MonotonicFrameCounterTest,InvalidData)44 TEST(MonotonicFrameCounterTest, InvalidData) {
45 MonotonicFrameCounter monotonicFrameCounter;
46
47 const std::vector<std::pair<int64_t, int64_t>> frametimes{
48 {-1, -1}, {100, 100}, {-1, -1}, {90, 90}, {200, 200},
49 };
50
51 int64_t prevFrameCount = 0;
52 int64_t maxReceivedFrameCount = 0;
53 for (const auto& p : frametimes) {
54 maxReceivedFrameCount = std::max(maxReceivedFrameCount, p.first);
55 const int64_t frameCount =
56 monotonicFrameCounter.updateAndGetMonotonicFrameCount(p.first, p.second);
57 // we must be monotonic
58 ASSERT_GE(frameCount, prevFrameCount);
59 prevFrameCount = frameCount;
60 }
61 ASSERT_EQ(maxReceivedFrameCount, monotonicFrameCounter.getLastReportedFrameCount());
62 }
63
TEST(MonotonicFrameCounterTest,Flush)64 TEST(MonotonicFrameCounterTest, Flush) {
65 MonotonicFrameCounter monotonicFrameCounter;
66
67 // Different playback sequences are separated by a flush.
68 const std::vector<std::vector<std::pair<int64_t, int64_t>>> frameset{
69 {{-1, -1}, {100, 10}, {200, 20}, {300, 30},},
70 {{-1, -1}, {100, 10}, {200, 20}, {300, 30},},
71 {{-1, -1}, {100, 100}, {-1, -1}, {90, 90}, {200, 200},},
72 };
73
74 int64_t prevFrameCount = 0;
75 int64_t maxReceivedFrameCount = 0;
76 int64_t sumMaxReceivedFrameCount = 0;
77 for (const auto& v : frameset) {
78 for (const auto& p : v) {
79 maxReceivedFrameCount = std::max(maxReceivedFrameCount, p.first);
80 const int64_t frameCount =
81 monotonicFrameCounter.updateAndGetMonotonicFrameCount(p.first, p.second);
82 // we must be monotonic
83 ASSERT_GE(frameCount, prevFrameCount);
84 prevFrameCount = frameCount;
85 }
86 monotonicFrameCounter.onFlush();
87 sumMaxReceivedFrameCount += maxReceivedFrameCount;
88 maxReceivedFrameCount = 0;
89 }
90
91 // On flush we keep a monotonic reported framecount
92 // even though the received framecount resets to 0.
93 // The requirement of equality here is implementation dependent.
94 ASSERT_EQ(sumMaxReceivedFrameCount, monotonicFrameCounter.getLastReportedFrameCount());
95 }
96
97 } // namespace
98