1 #include <gtest/gtest.h>
2 #include <chrono>
3 #include <thread>
4 #include <ImsMediaTimer.h>
5 #include <ImsMediaTrace.h>
6 #include <limits.h>
7 
8 class ImsMediaTimerTest : public ::testing::Test
9 {
10 public:
11     bool mCallbackCalled = false;
12     hTimerHandler mTimer = nullptr;
13 
SetUp()14     virtual void SetUp() override
15     {
16         mCallbackCalled = false;
17         mTimer = nullptr;
18     }
19 
TearDown()20     virtual void TearDown() override {}
21 
TimerCallback(hTimerHandler,void * data)22     static void TimerCallback(hTimerHandler, void* data)
23     {
24         ImsMediaTimerTest* pImsMediaTimerTest = (ImsMediaTimerTest*)data;
25         pImsMediaTimerTest->mCallbackCalled = true;
26     }
27 };
28 
TEST_F(ImsMediaTimerTest,StopTimerBeforeExpiry1)29 TEST_F(ImsMediaTimerTest, StopTimerBeforeExpiry1)
30 {
31     hTimerHandler hTimer = ImsMediaTimer::TimerStart(1, false, TimerCallback, this);
32     EXPECT_EQ(ImsMediaTimer::TimerStop(hTimer, nullptr), true);
33     EXPECT_EQ(mCallbackCalled, false);
34 }
35 
TEST_F(ImsMediaTimerTest,StopTimerBeforeExpiry2)36 TEST_F(ImsMediaTimerTest, StopTimerBeforeExpiry2)
37 {
38     hTimerHandler hTimer = ImsMediaTimer::TimerStart(10, false, TimerCallback, this);
39     EXPECT_EQ(ImsMediaTimer::TimerStop(hTimer, nullptr), true);
40     EXPECT_EQ(mCallbackCalled, false);
41 }
42 
TEST_F(ImsMediaTimerTest,StopTimerBeforeExpiry3)43 TEST_F(ImsMediaTimerTest, StopTimerBeforeExpiry3)
44 {
45     hTimerHandler hTimer = ImsMediaTimer::TimerStart(100, false, TimerCallback, this);
46     EXPECT_EQ(ImsMediaTimer::TimerStop(hTimer, nullptr), true);
47     EXPECT_EQ(mCallbackCalled, false);
48 }
49 
TEST_F(ImsMediaTimerTest,StopTimerBeforeExpiry4)50 TEST_F(ImsMediaTimerTest, StopTimerBeforeExpiry4)
51 {
52     hTimerHandler hTimer = ImsMediaTimer::TimerStart(1000, false, TimerCallback, this);
53     EXPECT_EQ(ImsMediaTimer::TimerStop(hTimer, nullptr), true);
54     EXPECT_EQ(mCallbackCalled, false);
55 }
56 
TEST_F(ImsMediaTimerTest,StopTimerAfterExpiry)57 TEST_F(ImsMediaTimerTest, StopTimerAfterExpiry)
58 {
59     hTimerHandler hTimer = ImsMediaTimer::TimerStart(200, false, TimerCallback, this);
60     std::this_thread::sleep_for(std::chrono::milliseconds(300));
61     EXPECT_EQ(ImsMediaTimer::TimerStop(hTimer, nullptr), false);
62     EXPECT_EQ(mCallbackCalled, true);
63 }
64 
TEST_F(ImsMediaTimerTest,MicrosecTimeDifference)65 TEST_F(ImsMediaTimerTest, MicrosecTimeDifference)
66 {
67     const uint32_t timeDiff = 100000;
68     uint64_t timeStart = ImsMediaTimer::GetTimeInMicroSeconds();
69     std::this_thread::sleep_for(std::chrono::microseconds(timeDiff));
70     uint64_t timeEnd = ImsMediaTimer::GetTimeInMicroSeconds();
71     EXPECT_TRUE(timeEnd - timeStart >= timeDiff);
72 }
73 
TEST_F(ImsMediaTimerTest,MillisecTimeDifference)74 TEST_F(ImsMediaTimerTest, MillisecTimeDifference)
75 {
76     const uint32_t timeDiff = 100;
77     uint32_t timeStart = ImsMediaTimer::GetTimeInMilliSeconds();
78     std::this_thread::sleep_for(std::chrono::milliseconds(timeDiff));
79     uint32_t timeEnd = ImsMediaTimer::GetTimeInMilliSeconds();
80     EXPECT_TRUE(timeEnd - timeStart >= timeDiff);
81 }
82 
TEST_F(ImsMediaTimerTest,CheckMillisecTimeOverflow)83 TEST_F(ImsMediaTimerTest, CheckMillisecTimeOverflow)
84 {
85     const uint32_t timeDiff = 100;
86     ImsMediaTimer::SetStartTimeInMicroSeconds(static_cast<uint64_t>(UINT_MAX) * 1000);
87     const uint32_t timeStart = ImsMediaTimer::GetTimeInMilliSeconds();
88     std::this_thread::sleep_for(std::chrono::milliseconds(timeDiff));
89     const uint32_t timeEnd = ImsMediaTimer::GetTimeInMilliSeconds();
90 
91     EXPECT_TRUE(timeEnd - timeStart <= timeDiff * 1.05f);
92 }