blob: 568a3b001a4399fd21fc095827f6d1a8f1743520 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
2 Copyright (c) 2018-2019 Nokia.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#include <type_traits>
18#include <memory>
19#include <gmock/gmock.h>
20#include "private/timer.hpp"
21#include "private/tst/enginemock.hpp"
22
23using namespace shareddatalayer;
24using namespace shareddatalayer::tst;
25using namespace testing;
26
27namespace
28{
29 class TimerTest: public testing::Test
30 {
31 public:
32 NiceMock<EngineMock> engineMock;
33 const Timer::Duration duration;
34 std::unique_ptr<Timer> timer;
35
36 TimerTest():
37 duration(std::chrono::duration_cast<Timer::Duration>(std::chrono::seconds(12))),
38 timer(new Timer(engineMock))
39 {
40 }
41 };
42
43 void nop() { }
44}
45
46TEST_F(TimerTest, IsNotCopyableAndIsNotMovable)
47{
48 EXPECT_FALSE(std::is_copy_assignable<Timer>::value);
49 EXPECT_FALSE(std::is_move_assignable<Timer>::value);
50 EXPECT_FALSE(std::is_copy_constructible<Timer>::value);
51 EXPECT_FALSE(std::is_move_constructible<Timer>::value);
52}
53
54TEST_F(TimerTest, ArmCallsArmOfAssociatedTimerService)
55{
56 EXPECT_CALL(engineMock, armTimer(_, duration, _))
57 .Times(1);
58 timer->arm(duration, nop);
59}
60
61TEST_F(TimerTest, DisarmingCallsDisarmOfAssociatedTimerService)
62{
63 timer->arm(duration, nop);
64 EXPECT_CALL(engineMock, disarmTimer(_))
65 .Times(1);
66 timer->disarm();
67}
68
69TEST_F(TimerTest, DisarmingUnArmedTimerDoesNothing)
70{
71 EXPECT_CALL(engineMock, disarmTimer(_))
72 .Times(0);
73 timer->disarm();
74}
75
76TEST_F(TimerTest, DoubleDisarmingDoesNothing)
77{
78 timer->arm(duration, nop);
79 EXPECT_CALL(engineMock, disarmTimer(_))
80 .Times(1);
81 timer->disarm();
82 timer->disarm();
83}
84
85TEST_F(TimerTest, DoubleArmingDisarmsFirst)
86{
87 InSequence dummy;
88 EXPECT_CALL(engineMock, armTimer(_, duration, _))
89 .Times(1);
90 timer->arm(duration, nop);
91 EXPECT_CALL(engineMock, disarmTimer(_))
92 .Times(1);
93 EXPECT_CALL(engineMock, armTimer(_, duration, _))
94 .Times(1);
95 timer->arm(duration, nop);
96 Mock::VerifyAndClear(&engineMock);
97}
98
99TEST_F(TimerTest, DestructorDisarms)
100{
101 timer->arm(duration, nop);
102 EXPECT_CALL(engineMock, disarmTimer(_))
103 .Times(1);
104 timer.reset();
105}
106
107TEST_F(TimerTest, AfterTimerHasExpiredItIsNotArmedAnymore)
108{
109 Timer::Callback savedCb;
110 ON_CALL(engineMock, armTimer(_, _, _))
111 .WillByDefault(SaveArg<2>(&savedCb));
112 timer->arm(duration, nop);
113 savedCb();
114 EXPECT_FALSE(timer->isArmed());
115}
116
117TEST_F(TimerTest, ArmingNullCallbackCallsSHAREDDATALAYER_ABORT)
118{
119 EXPECT_EXIT(timer->arm(duration, Timer::Callback()),
120 KilledBySignal(SIGABRT), "timer\\.cpp");
121}