Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_timer_unittest.cpp
Go to the documentation of this file.
1// Copyright 2019 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fxcrt/cfx_timer.h"
6
7#include <memory>
8
9#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12using testing::_;
13using testing::DoAll;
14using testing::Return;
15using testing::SaveArg;
16
17namespace {
18
19class MockTimerScheduler : public CFX_Timer::HandlerIface {
20 public:
21 MOCK_METHOD2(SetTimer, int(int32_t uElapse, TimerCallback lpTimerFunc));
22 MOCK_METHOD1(KillTimer, void(int32_t nID));
23};
24
25class MockTimerCallback : public CFX_Timer::CallbackIface {
26 public:
27 MOCK_METHOD0(OnTimerFired, void());
28};
29
30} // namespace
31
36
38 CFX_Timer::HandlerIface::TimerCallback fn1 = nullptr;
39 CFX_Timer::HandlerIface::TimerCallback fn2 = nullptr;
40
41 MockTimerScheduler scheduler;
42 EXPECT_CALL(scheduler, SetTimer(100, _))
43 .WillOnce(DoAll(SaveArg<1>(&fn1), Return(1001)));
44 EXPECT_CALL(scheduler, SetTimer(200, _))
45 .WillOnce(DoAll(SaveArg<1>(&fn2), Return(1002)));
46 EXPECT_CALL(scheduler, KillTimer(1001));
47 EXPECT_CALL(scheduler, KillTimer(1002));
48
49 MockTimerCallback cb1;
50 EXPECT_CALL(cb1, OnTimerFired()).Times(1);
51
52 MockTimerCallback cb2;
53 EXPECT_CALL(cb2, OnTimerFired()).Times(2);
54
55 auto timer1 = std::make_unique<CFX_Timer>(&scheduler, &cb1, 100);
56 auto timer2 = std::make_unique<CFX_Timer>(&scheduler, &cb2, 200);
57 EXPECT_TRUE(timer1->HasValidID());
58 EXPECT_TRUE(timer2->HasValidID());
59
60 // Fire some timers.
61 ASSERT_TRUE(fn1);
62 ASSERT_TRUE(fn2);
63 (*fn1)(1001);
64 (*fn1)(1002);
65 (*fn1)(1002);
66}
67
69 CFX_Timer::HandlerIface::TimerCallback fn1 = nullptr;
70
71 MockTimerScheduler scheduler;
72 EXPECT_CALL(scheduler, SetTimer(100, _))
73 .WillOnce(DoAll(SaveArg<1>(&fn1), Return(1001)));
74 EXPECT_CALL(scheduler, KillTimer(1001));
75
76 MockTimerCallback cb1;
77 EXPECT_CALL(cb1, OnTimerFired()).Times(0);
78
79 {
80 auto timer1 = std::make_unique<CFX_Timer>(&scheduler, &cb1, 100);
81 EXPECT_TRUE(timer1->HasValidID());
82
83 // Fire callback with bad arguments.
84 ASSERT_TRUE(fn1);
85 (*fn1)(-1);
86 (*fn1)(0);
87 (*fn1)(1002);
88 }
89
90 // Fire callback against stale timer.
91 (*fn1)(1001);
92}
TEST_F(CFXTimer, ValidTimers)
void SetUp() override
void TearDown() override
static void DestroyGlobals()
Definition cfx_timer.cpp:27
static void InitializeGlobals()
Definition cfx_timer.cpp:21