1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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 
16 #include "graphic_timer.h"
17 
18 #include <ctime>
19 #include <gtest/gtest.h>
20 #include <unistd.h>
21 
22 #include "hal_tick.h"
23 
24 using namespace testing::ext;
25 namespace {
26 constexpr int32_t NORMAL_PERIORD = 50;  // 50ms
27 constexpr int32_t NEGTIVE_PERIORD = -1; // -1ms
28 constexpr int32_t NONSENSE_PERIORD = 0; // 0ms
29 constexpr int32_t OVERFLOW_PERIORD = OHOS::GraphicTimer::MAX_PERIOD_MS + 1;
30 constexpr uint32_t NEAR_PEROID = 20; // 20ms
31 
GetRandonPeriod()32 int32_t GetRandonPeriod()
33 {
34     return rand() % OVERFLOW_PERIORD;
35 }
36 } // namespace
37 
38 namespace OHOS {
39 class GraphicTimerTest : public testing::Test {
40 public:
GraphicTimerTest()41     GraphicTimerTest() {}
~GraphicTimerTest()42     ~GraphicTimerTest() {}
SetUpTestCase(void)43     static void SetUpTestCase(void)
44     {
45         srand(time(nullptr));
46     }
TearDownTestCase(void)47     static void TearDownTestCase(void) {}
48     void SetUp();
49     void TearDown();
TimerCb(void * arg)50     static void TimerCb(void* arg)
51     {
52         GraphicTimerTest* t = reinterpret_cast<GraphicTimerTest*>(arg);
53         t->cbCnt_++;
54         t->lastPeriod_ = HALTick::GetInstance().GetElapseTime(t->lastStampMs_);
55         t->lastStampMs_ = HALTick::GetInstance().GetTime();
56     }
57 
58     int32_t cbCnt_ = 0;
59     uint32_t lastStampMs_ = 0;
60     uint32_t lastPeriod_ = 0;
61 };
62 
SetUp()63 void GraphicTimerTest::SetUp() {}
64 
TearDown()65 void GraphicTimerTest::TearDown() {}
66 
67 HWTEST_F(GraphicTimerTest, GraphicTimerConstructTest, TestSize.Level0)
68 {
69     GraphicTimer timer1(NEGTIVE_PERIORD, GraphicTimerTest::TimerCb, &timer1, false);
70     EXPECT_EQ(timer1.Start(), false);
71 
72     GraphicTimer timer2(NONSENSE_PERIORD, GraphicTimerTest::TimerCb, &timer2, false);
73     EXPECT_EQ(timer2.SetPeriod(NORMAL_PERIORD), false);
74 
75     GraphicTimer timer3(OVERFLOW_PERIORD, GraphicTimerTest::TimerCb, &timer3, false);
76     EXPECT_EQ(timer3.Start(), false);
77 
78     GraphicTimer timer4(GetRandonPeriod(), GraphicTimerTest::TimerCb, &timer4);
79     EXPECT_EQ(timer4.SetPeriod(NORMAL_PERIORD), true);
80     EXPECT_EQ(timer4.Start(), true);
81 }
82 
83 HWTEST_F(GraphicTimerTest, GraphicTimerIsPeriodTest, TestSize.Level0)
84 {
85     GraphicTimer timer1(NORMAL_PERIORD, GraphicTimerTest::TimerCb, &timer1, true);
86     EXPECT_EQ(timer1.IsPeriodic(), true);
87 
88     GraphicTimer timer2(NORMAL_PERIORD, GraphicTimerTest::TimerCb, &timer2, false);
89     EXPECT_EQ(timer2.IsPeriodic(), false);
90 }
91 
92 HWTEST_F(GraphicTimerTest, GraphicTimerSetPeriodTest, TestSize.Level0)
93 {
94     GraphicTimer timer(NORMAL_PERIORD, GraphicTimerTest::TimerCb, &timer, false);
95     EXPECT_EQ(timer.SetPeriod(OVERFLOW_PERIORD), false);
96 }
97 } // namespace OHOS
98