Home | History | Annotate | Download | only in condition
      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 static int64_t time_base = 10;
     27 static int64_t ct_start_time = 200;
     28 
     29 TEST(ConditionTimerTest, TestTimer_Inital_False) {
     30     ConditionTimer timer(false, time_base);
     31     EXPECT_EQ(false, timer.mCondition);
     32     EXPECT_EQ(0, timer.mTimerNs);
     33 
     34     EXPECT_EQ(0, timer.newBucketStart(ct_start_time));
     35     EXPECT_EQ(0, timer.mTimerNs);
     36 
     37     timer.onConditionChanged(true, ct_start_time + 5);
     38     EXPECT_EQ(ct_start_time + 5, timer.mLastConditionTrueTimestampNs);
     39     EXPECT_EQ(true, timer.mCondition);
     40 
     41     EXPECT_EQ(95, timer.newBucketStart(ct_start_time + 100));
     42     EXPECT_EQ(ct_start_time + 100, timer.mLastConditionTrueTimestampNs);
     43     EXPECT_EQ(true, timer.mCondition);
     44 }
     45 
     46 TEST(ConditionTimerTest, TestTimer_Inital_True) {
     47     ConditionTimer timer(true, time_base);
     48     EXPECT_EQ(true, timer.mCondition);
     49     EXPECT_EQ(0, timer.mTimerNs);
     50 
     51     EXPECT_EQ(ct_start_time - time_base, timer.newBucketStart(ct_start_time));
     52     EXPECT_EQ(true, timer.mCondition);
     53     EXPECT_EQ(0, timer.mTimerNs);
     54     EXPECT_EQ(ct_start_time, timer.mLastConditionTrueTimestampNs);
     55 
     56     timer.onConditionChanged(false, ct_start_time + 5);
     57     EXPECT_EQ(5, timer.mTimerNs);
     58 
     59     EXPECT_EQ(5, timer.newBucketStart(ct_start_time + 100));
     60     EXPECT_EQ(0, timer.mTimerNs);
     61 }
     62 
     63 }  // namespace statsd
     64 }  // namespace os
     65 }  // namespace android
     66 #else
     67 GTEST_LOG_(INFO) << "This test does nothing.\n";
     68 #endif
     69