1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/condition/ConditionTimer.h"
16
17 #include <gtest/gtest.h>
18 #include <stdio.h>
19
20 #ifdef __ANDROID__
21
22 namespace android {
23 namespace os {
24 namespace statsd {
25
26 namespace {
27
28 constexpr int64_t time_base = 10;
29 constexpr int64_t ct_start_time = 200;
30
assertConditionDurationInfo(const ConditionTimer::ConditionDurationInfo & info,ConditionTimer::ConditionDurationInfo expectedInfo)31 static void assertConditionDurationInfo(const ConditionTimer::ConditionDurationInfo& info,
32 ConditionTimer::ConditionDurationInfo expectedInfo) {
33 // TODO(b/195646451): improve debuggability invoking macros directly in the test
34 EXPECT_EQ(info, expectedInfo);
35 }
36
37 } // namespace
38
TEST(ConditionTimerTest,TestTimer_Inital_False)39 TEST(ConditionTimerTest, TestTimer_Inital_False) {
40 ConditionTimer timer(false, time_base);
41 EXPECT_EQ(false, timer.mCondition);
42 EXPECT_EQ(0, timer.mTimerNs);
43
44 assertConditionDurationInfo(timer.newBucketStart(ct_start_time, ct_start_time),
45 {.mDurationNs = 0, .mCorrectionNs = 0});
46 EXPECT_EQ(0, timer.mTimerNs);
47
48 timer.onConditionChanged(true, ct_start_time + 5);
49 EXPECT_EQ(ct_start_time + 5, timer.mLastConditionChangeTimestampNs);
50 EXPECT_EQ(true, timer.mCondition);
51
52 assertConditionDurationInfo(timer.newBucketStart(ct_start_time + 100, ct_start_time + 100),
53 {.mDurationNs = 95, .mCorrectionNs = 0});
54 EXPECT_EQ(ct_start_time + 100, timer.mLastConditionChangeTimestampNs);
55 EXPECT_EQ(true, timer.mCondition);
56 }
57
TEST(ConditionTimerTest,TestTimer_Inital_True)58 TEST(ConditionTimerTest, TestTimer_Inital_True) {
59 ConditionTimer timer(true, time_base);
60 EXPECT_EQ(true, timer.mCondition);
61 EXPECT_EQ(0, timer.mTimerNs);
62
63 assertConditionDurationInfo(timer.newBucketStart(ct_start_time, ct_start_time),
64 {.mDurationNs = ct_start_time - time_base, .mCorrectionNs = 0});
65 EXPECT_EQ(true, timer.mCondition);
66 EXPECT_EQ(0, timer.mTimerNs);
67 EXPECT_EQ(ct_start_time, timer.mLastConditionChangeTimestampNs);
68
69 timer.onConditionChanged(false, ct_start_time + 5);
70 EXPECT_EQ(5, timer.mTimerNs);
71
72 assertConditionDurationInfo(timer.newBucketStart(ct_start_time + 100, ct_start_time + 100),
73 {.mDurationNs = 5, .mCorrectionNs = 0});
74 EXPECT_EQ(0, timer.mTimerNs);
75 }
76
TEST(ConditionTimerTest,TestTimer_Correction_DelayedChangeToFalse)77 TEST(ConditionTimerTest, TestTimer_Correction_DelayedChangeToFalse) {
78 ConditionTimer timer(true, time_base);
79 EXPECT_EQ(true, timer.mCondition);
80 EXPECT_EQ(0, timer.mTimerNs);
81
82 assertConditionDurationInfo(timer.newBucketStart(ct_start_time, ct_start_time),
83 {.mDurationNs = ct_start_time - time_base, .mCorrectionNs = 0});
84 EXPECT_EQ(true, timer.mCondition);
85 EXPECT_EQ(0, timer.mTimerNs);
86 EXPECT_EQ(ct_start_time, timer.mLastConditionChangeTimestampNs);
87
88 timer.onConditionChanged(false, ct_start_time + 7);
89 EXPECT_EQ(7, timer.mTimerNs);
90
91 assertConditionDurationInfo(timer.newBucketStart(ct_start_time + 7, ct_start_time + 5),
92 {.mDurationNs = 5, .mCorrectionNs = 2});
93 EXPECT_EQ(2, timer.mTimerNs);
94 EXPECT_EQ(2, timer.mCurrentBucketStartDelayNs);
95 }
96
TEST(ConditionTimerTest,TestTimer_Correction_DelayedChangeToTrue)97 TEST(ConditionTimerTest, TestTimer_Correction_DelayedChangeToTrue) {
98 ConditionTimer timer(false, time_base);
99 EXPECT_EQ(false, timer.mCondition);
100 EXPECT_EQ(0, timer.mTimerNs);
101
102 assertConditionDurationInfo(timer.newBucketStart(ct_start_time, ct_start_time),
103 {.mDurationNs = 0, .mCorrectionNs = 0});
104 EXPECT_EQ(0, timer.mTimerNs);
105
106 timer.onConditionChanged(true, ct_start_time + 7);
107 EXPECT_EQ(ct_start_time + 7, timer.mLastConditionChangeTimestampNs);
108
109 assertConditionDurationInfo(timer.newBucketStart(ct_start_time + 7, ct_start_time + 5),
110 {.mDurationNs = 0, .mCorrectionNs = 0});
111 EXPECT_EQ(0, timer.mTimerNs);
112 EXPECT_EQ(0, timer.mCurrentBucketStartDelayNs);
113 }
114
TEST(ConditionTimerTest,TestTimer_Correction_DelayedWithInitialFalse)115 TEST(ConditionTimerTest, TestTimer_Correction_DelayedWithInitialFalse) {
116 ConditionTimer timer(false, time_base);
117 EXPECT_EQ(false, timer.mCondition);
118 EXPECT_EQ(0, timer.mTimerNs);
119
120 assertConditionDurationInfo(timer.newBucketStart(ct_start_time, ct_start_time),
121 {.mDurationNs = 0, .mCorrectionNs = 0});
122 EXPECT_EQ(0, timer.mTimerNs);
123
124 assertConditionDurationInfo(timer.newBucketStart(ct_start_time + 7, ct_start_time + 5),
125 {.mDurationNs = 0, .mCorrectionNs = 0});
126 EXPECT_EQ(0, timer.mTimerNs);
127 EXPECT_EQ(0, timer.mCurrentBucketStartDelayNs);
128 }
129
TEST(ConditionTimerTest,TestTimer_Correction_DelayedWithInitialTrue)130 TEST(ConditionTimerTest, TestTimer_Correction_DelayedWithInitialTrue) {
131 ConditionTimer timer(true, time_base);
132 EXPECT_EQ(true, timer.mCondition);
133 EXPECT_EQ(0, timer.mTimerNs);
134
135 assertConditionDurationInfo(timer.newBucketStart(ct_start_time, ct_start_time),
136 {.mDurationNs = ct_start_time - time_base, .mCorrectionNs = 0});
137 EXPECT_EQ(0, timer.mTimerNs);
138
139 assertConditionDurationInfo(timer.newBucketStart(ct_start_time + 7, ct_start_time + 5),
140 {.mDurationNs = 5, .mCorrectionNs = 2});
141 EXPECT_EQ(0, timer.mTimerNs);
142 EXPECT_EQ(2, timer.mCurrentBucketStartDelayNs);
143 }
144
145 } // namespace statsd
146 } // namespace os
147 } // namespace android
148 #else
149 GTEST_LOG_(INFO) << "This test does nothing.\n";
150 #endif
151