1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17
18 #include "ability_manager_xcollie.h"
19 #include "xcollie/xcollie.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace HiviewDFX {
25 std::shared_ptr<XCollie> XCollie::instance;
GetInstance()26 XCollie &XCollie::GetInstance()
27 {
28 if (instance == nullptr) {
29 instance = std::make_shared<XCollie>();
30 }
31 return *instance;
32 }
33 } // namespace HiviewDFX
34
35 namespace OHOS {
36 namespace AbilityRuntime {
37 using namespace HiviewDFX;
38 class AbilityManagerXcollieTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp() override;
43 void TearDown() override;
44 };
45
SetUpTestCase(void)46 void AbilityManagerXcollieTest::SetUpTestCase(void)
47 {}
TearDownTestCase(void)48 void AbilityManagerXcollieTest::TearDownTestCase(void)
49 {}
SetUp()50 void AbilityManagerXcollieTest::SetUp()
51 {
52 XCollie::instance = std::make_shared<XCollie>();
53 }
TearDown()54 void AbilityManagerXcollieTest::TearDown()
55 {
56 XCollie::instance.reset();
57 }
58
59 /**
60 * @tc.name: AbilityManagerXCollie_0010
61 * @tc.desc: set timer normally.
62 * @tc.type: FUNC
63 */
64 HWTEST_F(AbilityManagerXcollieTest, AbilityManagerXCollie_0010, TestSize.Level1)
65 {
66 std::string tag = "tag";
67 uint32_t timeoutSeconds = 0;
68 bool ignore = false;
69 EXPECT_CALL(*XCollie::instance, SetTimer).Times(1)
70 .WillOnce(Return(1));
71 AbilityManagerXCollie testObj(tag, timeoutSeconds, ignore);
72 EXPECT_EQ(testObj.id_, 1);
73 EXPECT_FALSE(testObj.isCanceled_);
74 EXPECT_CALL(*XCollie::instance, CancelTimer).Times(1);
75 }
76
77 /**
78 * @tc.name: AbilityManagerXCollie_0020
79 * @tc.desc: ignore and no set timer.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(AbilityManagerXcollieTest, AbilityManagerXCollie_0020, TestSize.Level1)
83 {
84 std::string tag = "tag";
85 uint32_t timeoutSeconds = 0;
86 bool ignore = true;
87 EXPECT_CALL(*XCollie::instance, SetTimer).Times(0);
88 AbilityManagerXCollie testObj(tag, timeoutSeconds, ignore);
89 EXPECT_EQ(testObj.id_, -1);
90 EXPECT_TRUE(testObj.isCanceled_);
91 EXPECT_CALL(*XCollie::instance, CancelTimer).Times(0);
92 }
93
94 /**
95 * @tc.name: CancelAbilityManagerXCollie_0010
96 * @tc.desc: cancel normal timer.
97 * @tc.type: FUNC
98 */
99 HWTEST_F(AbilityManagerXcollieTest, CancelAbilityManagerXCollie_0010, TestSize.Level1)
100 {
101 std::string tag = "tag";
102 uint32_t timeoutSeconds = 0;
103 bool ignore = false;
104 EXPECT_CALL(*XCollie::instance, SetTimer).Times(1)
105 .WillOnce(Return(1));
106 AbilityManagerXCollie testObj(tag, timeoutSeconds, ignore);
107 EXPECT_CALL(*XCollie::instance, CancelTimer).Times(1);
108 testObj.CancelAbilityManagerXCollie();
109 EXPECT_TRUE(testObj.isCanceled_);
110
111 EXPECT_CALL(*XCollie::instance, CancelTimer).Times(0);
112 testObj.CancelAbilityManagerXCollie();
113 }
114 } // namespace AbilityRuntime
115 } // namespace OHOS
116