Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2025 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <lib/core/CHIPError.h>
21 : #include <lib/support/TimerDelegate.h>
22 : #include <system/SystemClock.h>
23 :
24 : namespace chip {
25 :
26 : class TimerDelegateMock : public TimerDelegate
27 : {
28 : public:
29 37 : CriticalFailure StartTimer(TimerContext * context, chip::System::Clock::Timeout aTimeout) override
30 : {
31 37 : mTimerContext = context;
32 37 : mTimerTimeout = mMockSystemTimestamp + aTimeout;
33 37 : return CHIP_NO_ERROR;
34 : }
35 :
36 17 : void CancelTimer(TimerContext * context) override
37 : {
38 17 : mTimerContext = nullptr;
39 17 : mTimerTimeout = chip::System::Clock::Milliseconds64(0x7FFFFFFFFFFFFFFF);
40 17 : }
41 :
42 21 : bool IsTimerActive(TimerContext * context) override { return mTimerContext != nullptr && mTimerTimeout > mMockSystemTimestamp; }
43 :
44 9 : chip::System::Clock::Timestamp GetCurrentMonotonicTimestamp() override { return mMockSystemTimestamp; }
45 :
46 26 : void AdvanceClock(chip::System::Clock::Timeout aTimeout)
47 : {
48 26 : mMockSystemTimestamp += aTimeout;
49 26 : if (mTimerContext && mMockSystemTimestamp >= mTimerTimeout)
50 : {
51 21 : mTimerContext->TimerFired();
52 : }
53 26 : }
54 :
55 : private:
56 : TimerContext * mTimerContext = nullptr;
57 : chip::System::Clock::Timestamp mTimerTimeout = chip::System::Clock::Milliseconds64(0x7FFFFFFFFFFFFFFF);
58 : chip::System::Clock::Timestamp mMockSystemTimestamp = chip::System::Clock::Milliseconds64(0);
59 : };
60 :
61 : } // namespace chip
|