• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <vector>
20 
21 #define private public
22 #define protected public
23 #include "aot_compiler_client.h"
24 #include "aot_compiler_service.h"
25 #include "aot_compiler_error_utils.h"
26 #include "aot_compiler_load_callback.h"
27 #undef protected
28 #undef private
29 #include "common_event_data.h"
30 #include "common_event_manager.h"
31 #include "common_event_support.h"
32 #include "common_event_subscriber.h"
33 #include "iservice_registry.h"
34 #include "power_disconnected_listener.h"
35 #include "system_ability_definition.h"
36 
37 using namespace testing::ext;
38 
39 namespace OHOS::ArkCompiler {
40 namespace {
41 const std::string TASK_ID = "UnLoadSA";
42 constexpr int32_t NO_DELAY_TIME = 0;
43 constexpr int32_t SLEEP_TIME_FOR_WAITTING_UNLOAD_SA = 5 * 1000; // 5 ms
44 }
45 
46 class AotCompilerServiceTest : public testing::Test {
47 public:
AotCompilerServiceTest()48     AotCompilerServiceTest() {}
~AotCompilerServiceTest()49     virtual ~AotCompilerServiceTest() {}
50 
SetUpTestCase()51     static void SetUpTestCase() {}
TearDownTestCase()52     static void TearDownTestCase() {}
SetUp()53     void SetUp() override {}
TearDown()54     void TearDown() override {}
55     void OnStart(AotCompilerService &aotService);
56 };
57 
OnStart(AotCompilerService & aotService)58 void AotCompilerServiceTest::OnStart(AotCompilerService &aotService)
59 {
60     if (aotService.state_ == ServiceRunningState::STATE_RUNNING) {
61         return;
62     }
63     if (!aotService.Init()) {
64         return;
65     }
66     aotService.state_ = ServiceRunningState::STATE_RUNNING;
67     aotService.RegisterPowerDisconnectedListener();
68     aotService.RegisterScreenStatusSubscriber();
69     aotService.RegisterThermalMgrListener();
70 }
71 
72 /**
73  * @tc.name: AotCompilerServiceTest_001
74  * @tc.desc: AotCompilerService instance initialization
75  * @tc.type: Func
76 */
77 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_001, TestSize.Level0)
78 {
79     AotCompilerService aotService;
80     EXPECT_EQ(aotService.state_, ServiceRunningState::STATE_NOT_START);
81 }
82 
83 /**
84  * @tc.name: AotCompilerServiceTest_002
85  * @tc.desc: Init() in AotCompilerService
86  * @tc.type: Func
87 */
88 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_002, TestSize.Level0)
89 {
90     AotCompilerService aotService;
91     EXPECT_TRUE(aotService.Init());
92     EXPECT_NE(aotService.unLoadHandler_, nullptr);
93 }
94 
95 /**
96  * @tc.name: AotCompilerServiceTest_003
97  * @tc.desc: RegisterPowerDisconnectedListener() && UnRegisterPowerDisconnectedListener()
98  * @tc.type: Func
99 */
100 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_003, TestSize.Level0)
101 {
102     AotCompilerService aotService;
103     aotService.RegisterPowerDisconnectedListener();
104     EXPECT_TRUE(aotService.isPowerEventSubscribered_);
105 
106     aotService.UnRegisterPowerDisconnectedListener();
107     EXPECT_FALSE(aotService.isPowerEventSubscribered_);
108 }
109 
110 /**
111  * @tc.name: AotCompilerServiceTest_004
112  * @tc.desc: OnStart()/OnStop() in AotCompilerService
113  * @tc.type: Func
114 */
115 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_004, TestSize.Level0)
116 {
117     AotCompilerService aotService;
118     OnStart(aotService);
119     EXPECT_EQ(aotService.state_, ServiceRunningState::STATE_RUNNING);
120 
121     aotService.OnStop();
122     EXPECT_EQ(aotService.state_, ServiceRunningState::STATE_NOT_START);
123 }
124 
125 /**
126  * @tc.name: AotCompilerServiceTest_005
127  * @tc.desc: DelayUnloadTask(TASK_ID, DELAY_TIME)
128  * @tc.type: Func
129 */
130 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_005, TestSize.Level0)
131 {
132     AotCompilerService aotService;
133     EXPECT_TRUE(aotService.Init());
134     EXPECT_NE(aotService.unLoadHandler_, nullptr);
135 
136     AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
137     EXPECT_NE(aotClient.GetAotCompilerProxy(), nullptr);
138     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
139     ASSERT_NE(systemAbilityMgr, nullptr);
140     auto remoteObject = systemAbilityMgr->CheckSystemAbility(AOT_COMPILER_SERVICE_ID);
141     EXPECT_NE(remoteObject, nullptr);
142 
143     aotService.DelayUnloadTask(TASK_ID, NO_DELAY_TIME);
144     usleep(SLEEP_TIME_FOR_WAITTING_UNLOAD_SA);
145     aotService.OnStop();
146     EXPECT_EQ(aotService.state_, ServiceRunningState::STATE_NOT_START);
147 }
148 
149 /**
150  * @tc.name: AotCompilerServiceTest_006
151  * @tc.desc: AotCompilerService::AotCompiler(argsMap, fileData)
152  * @tc.type: Func
153 */
154 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_006, TestSize.Level0)
155 {
156     AotCompilerService aotService;
157     std::unordered_map<std::string, std::string> argsMap;
158     std::vector<int16_t> fileData;
159     int32_t ret = aotService.AotCompiler(argsMap, fileData);
160     EXPECT_NE(ret, ERR_OK);
161 }
162 
163 /**
164  * @tc.name: AotCompilerServiceTest_007
165  * @tc.desc: AotCompilerService::StopAotCompiler()
166  * @tc.type: Func
167 */
168 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_007, TestSize.Level0)
169 {
170     AotCompilerService aotService;
171     int32_t ret = aotService.StopAotCompiler();
172     EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
173 }
174 
175 /**
176  * @tc.name: AotCompilerServiceTest_008
177  * @tc.desc: AotCompilerService::GetAOTVersion(sigData)
178  * @tc.type: Func
179 */
180 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_008, TestSize.Level0)
181 {
182     AotCompilerService aotService;
183     std::string sigData = "test_string";
184     int32_t ret = aotService.GetAOTVersion(sigData);
185     EXPECT_EQ(ret, ERR_OK);
186     EXPECT_STRNE(sigData.c_str(), "test_string");
187 }
188 
189 /**
190  * @tc.name: AotCompilerServiceTest_009
191  * @tc.desc: AotCompilerService::NeedReCompile(oldVersion, sigData)
192  * @tc.type: Func
193 */
194 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_009, TestSize.Level0)
195 {
196     AotCompilerService aotService;
197     std::string oldVersion = "4.0.0.0";
198     bool sigData = true;
199     int32_t ret = aotService.NeedReCompile(oldVersion, sigData);
200     EXPECT_EQ(ret, ERR_OK);
201     EXPECT_STREQ(oldVersion.c_str(), "4.0.0.0");
202 }
203 
204 /**
205  * @tc.name: AotCompilerServiceTest_010
206  * @tc.desc: AotCompilerService::RemoveUnloadTask(TASK_ID)
207  * @tc.type: Func
208 */
209 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_010, TestSize.Level0)
210 {
211     AotCompilerService aotService;
212     aotService.unLoadHandler_ = nullptr;
213     aotService.RemoveUnloadTask(TASK_ID);
214     aotService.DelayUnloadTask(TASK_ID, NO_DELAY_TIME);
215     bool ret = aotService.Init();
216     EXPECT_TRUE(ret);
217     aotService.RemoveUnloadTask(TASK_ID);
218     EXPECT_NE(aotService.unLoadHandler_, nullptr);
219 }
220 
221 /**
222  * @tc.name: AotCompilerServiceTest_011
223  * @tc.desc: AotCompilerService::RegisterPowerDisconnectedListener()
224  * @tc.type: Func
225 */
226 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_011, TestSize.Level0)
227 {
228     AotCompilerService aotService;
229     bool viewData1 = true;
230     int32_t viewData2 = 101010;
231     std::string viewData3 = "101010";
232     aotService.RegisterPowerDisconnectedListener();
233     aotService.UnRegisterPowerDisconnectedListener();
234     aotService.UnRegisterPowerDisconnectedListener();
235     EXPECT_TRUE(viewData1);
236     EXPECT_EQ(viewData2, 101010);
237     EXPECT_STREQ(viewData3.c_str(), "101010");
238 }
239 
240 /**
241  * @tc.name: AotCompilerServiceTest_012
242  * @tc.desc: AotCompilerService::RegisterScreenStatusSubscriber()
243  * @tc.type: Func
244 */
245 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_012, TestSize.Level0)
246 {
247     AotCompilerService aotService;
248     bool viewData1 = true;
249     int32_t viewData2 = 101010;
250     std::string viewData3 = "101010";
251     aotService.RegisterScreenStatusSubscriber();
252     aotService.UnRegisterScreenStatusSubscriber();
253     aotService.UnRegisterScreenStatusSubscriber();
254     EXPECT_TRUE(viewData1);
255     EXPECT_EQ(viewData2, 101010);
256     EXPECT_STREQ(viewData3.c_str(), "101010");
257 }
258 
259 /**
260  * @tc.name: AotCompilerServiceTest_013
261  * @tc.desc: AotCompilerService::RegisterThermalMgrListener()
262  * @tc.type: Func
263 */
264 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_013, TestSize.Level0)
265 {
266     AotCompilerService aotService;
267     bool viewData1 = true;
268     int32_t viewData2 = 101010;
269     std::string viewData3 = "101010";
270     aotService.RegisterThermalMgrListener();
271     aotService.UnRegisterThermalMgrListener();
272     aotService.UnRegisterThermalMgrListener();
273     EXPECT_TRUE(viewData1);
274     EXPECT_EQ(viewData2, 101010);
275     EXPECT_STREQ(viewData3.c_str(), "101010");
276 }
277 
278 /**
279  * @tc.name: AotCompilerServiceTest_014
280  * @tc.desc: PowerDisconnectedListener::OnReceiveEvent(data)
281  * @tc.type: Func
282 */
283 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_014, TestSize.Level0)
284 {
285     bool viewData = true;
286     EventFwk::MatchingSkills matchingSkills;
287     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
288     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
289     auto powerDisconnectedListener_ = std::make_shared<PowerDisconnectedListener>(subscribeInfo);
290     EventFwk::CommonEventData data;
291     powerDisconnectedListener_->OnReceiveEvent(data);
292     EXPECT_TRUE(viewData);
293 }
294 
295 /**
296  * @tc.name: AotCompilerServiceTest_015
297  * @tc.desc: ScreenStatusSubscriber::OnReceiveEvent(data)
298  * @tc.type: Func
299 */
300 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_015, TestSize.Level0)
301 {
302     bool viewData = true;
303     EventFwk::MatchingSkills matchingSkills;
304     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
305     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
306     auto screenStatusSubscriber_ = std::make_shared<ScreenStatusSubscriber>(subscribeInfo);
307     EventFwk::CommonEventData data;
308     screenStatusSubscriber_->OnReceiveEvent(data);
309     EXPECT_TRUE(viewData);
310 }
311 
312 /**
313  * @tc.name: AotCompilerServiceTest_016
314  * @tc.desc: ThermalMgrListener::OnReceiveEvent(data)
315  * @tc.type: Func
316 */
317 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_016, TestSize.Level0)
318 {
319     bool viewData = true;
320     EventFwk::MatchingSkills matchingSkills;
321     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
322     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
323     auto thermalMgrListener_ = std::make_shared<ThermalMgrListener>(subscribeInfo);
324     EventFwk::CommonEventData data;
325     thermalMgrListener_->OnReceiveEvent(data);
326     EXPECT_TRUE(viewData);
327 }
328 } // namespace OHOS::ArkCompiler