1 /*
2 * Copyright (c) 2024 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 #define private public
19 #include "recovery_info_timer.h"
20 #undef private
21
22 using namespace testing::ext;
23 using namespace testing;
24
25 namespace OHOS {
26 namespace AAFwk {
27 class RecoveryInfoTimerTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase(void)35 void RecoveryInfoTimerTest::SetUpTestCase(void)
36 {}
TearDownTestCase(void)37 void RecoveryInfoTimerTest::TearDownTestCase(void)
38 {}
TearDown(void)39 void RecoveryInfoTimerTest::TearDown(void)
40 {}
SetUp()41 void RecoveryInfoTimerTest::SetUp()
42 {}
43
44 /**
45 * @tc.number: SubmitSaveRecoveryInfo_001
46 * @tc.name: SubmitSaveRecoveryInfo
47 * @tc.desc: Test whether SubmitSaveRecoveryInfo is called normally.
48 * @tc.type: FUNC
49 */
50 HWTEST_F(RecoveryInfoTimerTest, SubmitSaveRecoveryInfo_001, TestSize.Level1)
51 {
52 RecoveryInfoTimer &instance = RecoveryInfoTimer::GetInstance();
53 RecoveryInfo recoveryInfo;
54 recoveryInfo.bundleName = "testBundleName";
55 recoveryInfo.moduleName = "testModuleName";
56 recoveryInfo.abilityName = "testAbilityName";
57 instance.SubmitSaveRecoveryInfo(recoveryInfo);
__anon44ae4bfe0102(const RecoveryInfo &item) 58 auto findByInfo = [&recoveryInfo](const RecoveryInfo &item) {
59 return item.abilityName == recoveryInfo.abilityName && item.bundleName == recoveryInfo.bundleName &&
60 item.moduleName == recoveryInfo.moduleName;
61 };
62 auto i = find_if(instance.recoveryInfoQueue_.begin(), instance.recoveryInfoQueue_.end(), findByInfo);
63 EXPECT_TRUE(i != instance.recoveryInfoQueue_.end());
64 }
65 /**
66 * @tc.number: SubmitSaveRecoveryInfo_002
67 * @tc.name: SubmitSaveRecoveryInfo
68 * @tc.desc: Test that submitting a recovery info with the same identifiers replaces the existing one.
69 * @tc.type: FUNC
70 */
71 HWTEST_F(RecoveryInfoTimerTest, SubmitSaveRecoveryInfo_002, TestSize.Level1)
72 {
73 RecoveryInfoTimer &instance = RecoveryInfoTimer::GetInstance();
74 instance.recoveryInfoQueue_.clear();
75
76 RecoveryInfo recoveryInfo1;
77 recoveryInfo1.bundleName = "testBundleName";
78 recoveryInfo1.moduleName = "testModuleName";
79 recoveryInfo1.abilityName = "testAbilityName";
80 recoveryInfo1.time = 1000;
81 instance.SubmitSaveRecoveryInfo(recoveryInfo1);
82
83 RecoveryInfo recoveryInfo2;
84 recoveryInfo2.bundleName = "testBundleName";
85 recoveryInfo2.moduleName = "testModuleName";
86 recoveryInfo2.abilityName = "testAbilityName";
87 recoveryInfo2.time = 2000;
88 instance.SubmitSaveRecoveryInfo(recoveryInfo2);
89
90 EXPECT_EQ(instance.recoveryInfoQueue_.size(), 1);
91 EXPECT_EQ(instance.recoveryInfoQueue_.front().time, 2000);
92 }
93
94 /**
95 * @tc.number: SubmitSaveRecoveryInfo_003
96 * @tc.name: SubmitSaveRecoveryInfo_TimeoutDeletion
97 * @tc.desc: Test that old recovery infos are deleted when they exceed the reserve number.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(RecoveryInfoTimerTest, SubmitSaveRecoveryInfo_003, TestSize.Level1)
101 {
102 RecoveryInfoTimer &instance = RecoveryInfoTimer::GetInstance();
103 instance.recoveryInfoQueue_.clear();
104
105 int64_t currentTime = 1000000;
106
107 for (int i = 0; i < 7; i++) {
108 RecoveryInfo oldInfo;
109 oldInfo.bundleName = "oldBundle" + std::to_string(i);
110 oldInfo.moduleName = "oldModule" + std::to_string(i);
111 oldInfo.abilityName = "oldAbility" + std::to_string(i);
112 oldInfo.time = currentTime - (168 * 60 * 60 + 1);
113 oldInfo.tokenId = i;
114 instance.recoveryInfoQueue_.push_back(oldInfo);
115 }
116
117 RecoveryInfo newInfo;
118 newInfo.bundleName = "newBundle";
119 newInfo.moduleName = "newModule";
120 newInfo.abilityName = "newAbility";
121 newInfo.time = currentTime;
122 instance.SubmitSaveRecoveryInfo(newInfo);
123
124 EXPECT_EQ(instance.recoveryInfoQueue_.size(), 6);
125 EXPECT_NE(instance.recoveryInfoQueue_.front().bundleName, "oldBundle0");
126 EXPECT_NE(instance.recoveryInfoQueue_.front().bundleName, "oldBundle1");
127 EXPECT_EQ(instance.recoveryInfoQueue_.back().bundleName, "newBundle");
128 }
129
130 /**
131 * @tc.number: SubmitSaveRecoveryInfo_004
132 * @tc.name: SubmitSaveRecoveryInfo_FewerTimeoutsThanReserve
133 * @tc.desc: Test case when there are timed out items but fewer than the reserve limit.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(RecoveryInfoTimerTest, SubmitSaveRecoveryInfo_004, TestSize.Level1)
137 {
138 RecoveryInfoTimer &instance = RecoveryInfoTimer::GetInstance();
139 instance.recoveryInfoQueue_.clear();
140
141 int64_t currentTime = 1000000;
142
143 for (int i = 0; i < 3; i++) {
144 RecoveryInfo oldInfo;
145 oldInfo.bundleName = "oldBundle" + std::to_string(i);
146 oldInfo.moduleName = "oldModule" + std::to_string(i);
147 oldInfo.abilityName = "oldAbility" + std::to_string(i);
148 oldInfo.time = currentTime - (168 * 60 * 60 + 1);
149 oldInfo.tokenId = i;
150 instance.recoveryInfoQueue_.push_back(oldInfo);
151 }
152
153 for (int i = 0; i < 2; i++) {
154 RecoveryInfo recentInfo;
155 recentInfo.bundleName = "recentBundle" + std::to_string(i);
156 recentInfo.moduleName = "recentModule" + std::to_string(i);
157 recentInfo.abilityName = "recentAbility" + std::to_string(i);
158 recentInfo.time = currentTime - 1000;
159 recentInfo.tokenId = i + 10;
160 instance.recoveryInfoQueue_.push_back(recentInfo);
161 }
162
163 int originalSize = instance.recoveryInfoQueue_.size();
164
165 RecoveryInfo newInfo;
166 newInfo.bundleName = "newBundle";
167 newInfo.moduleName = "newModule";
168 newInfo.abilityName = "newAbility";
169 newInfo.time = currentTime;
170 instance.SubmitSaveRecoveryInfo(newInfo);
171 EXPECT_EQ(instance.recoveryInfoQueue_.size(), originalSize + 1);
172 bool foundFirstOld = false;
173 for (const auto& info : instance.recoveryInfoQueue_) {
174 if (info.bundleName == "oldBundle0") {
175 foundFirstOld = true;
176 break;
177 }
178 }
179 EXPECT_TRUE(foundFirstOld);
180 EXPECT_EQ(instance.recoveryInfoQueue_.back().bundleName, "newBundle");
181 }
182 } // namespace AAFwk
183 } // namespace OHOS
184