• 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     EXPECT_FALSE(sigData);
203 }
204 
205 /**
206  * @tc.name: AotCompilerServiceTest_010
207  * @tc.desc: AotCompilerService::RemoveUnloadTask(TASK_ID)
208  * @tc.type: Func
209 */
210 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_010, TestSize.Level0)
211 {
212     AotCompilerService aotService;
213     aotService.unLoadHandler_ = nullptr;
214     aotService.RemoveUnloadTask(TASK_ID);
215     aotService.DelayUnloadTask(TASK_ID, NO_DELAY_TIME);
216     bool ret = aotService.Init();
217     EXPECT_TRUE(ret);
218     aotService.RemoveUnloadTask(TASK_ID);
219     EXPECT_NE(aotService.unLoadHandler_, nullptr);
220 }
221 
222 /**
223  * @tc.name: AotCompilerServiceTest_011
224  * @tc.desc: AotCompilerService::RegisterPowerDisconnectedListener()
225  * @tc.type: Func
226 */
227 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_011, TestSize.Level0)
228 {
229     AotCompilerService aotService;
230     bool viewData1 = true;
231     int32_t viewData2 = 101010;
232     std::string viewData3 = "101010";
233     aotService.RegisterPowerDisconnectedListener();
234     aotService.UnRegisterPowerDisconnectedListener();
235     aotService.UnRegisterPowerDisconnectedListener();
236     EXPECT_TRUE(viewData1);
237     EXPECT_EQ(viewData2, 101010);
238     EXPECT_STREQ(viewData3.c_str(), "101010");
239 }
240 
241 /**
242  * @tc.name: AotCompilerServiceTest_012
243  * @tc.desc: AotCompilerService::RegisterScreenStatusSubscriber()
244  * @tc.type: Func
245 */
246 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_012, TestSize.Level0)
247 {
248     AotCompilerService aotService;
249     bool viewData1 = true;
250     int32_t viewData2 = 101010;
251     std::string viewData3 = "101010";
252     aotService.RegisterScreenStatusSubscriber();
253     aotService.UnRegisterScreenStatusSubscriber();
254     aotService.UnRegisterScreenStatusSubscriber();
255     EXPECT_TRUE(viewData1);
256     EXPECT_EQ(viewData2, 101010);
257     EXPECT_STREQ(viewData3.c_str(), "101010");
258 }
259 
260 /**
261  * @tc.name: AotCompilerServiceTest_013
262  * @tc.desc: AotCompilerService::RegisterThermalMgrListener()
263  * @tc.type: Func
264 */
265 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_013, TestSize.Level0)
266 {
267     AotCompilerService aotService;
268     bool viewData1 = true;
269     int32_t viewData2 = 101010;
270     std::string viewData3 = "101010";
271     aotService.RegisterThermalMgrListener();
272     aotService.UnRegisterThermalMgrListener();
273     aotService.UnRegisterThermalMgrListener();
274     EXPECT_TRUE(viewData1);
275     EXPECT_EQ(viewData2, 101010);
276     EXPECT_STREQ(viewData3.c_str(), "101010");
277 }
278 
279 /**
280  * @tc.name: AotCompilerServiceTest_014
281  * @tc.desc: PowerDisconnectedListener::OnReceiveEvent(data)
282  * @tc.type: Func
283 */
284 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_014, TestSize.Level0)
285 {
286     bool viewData = true;
287     EventFwk::MatchingSkills matchingSkills;
288     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
289     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
290     auto powerDisconnectedListener_ = std::make_shared<PowerDisconnectedListener>(subscribeInfo);
291     EventFwk::CommonEventData data;
292     powerDisconnectedListener_->OnReceiveEvent(data);
293     EXPECT_TRUE(viewData);
294 }
295 
296 /**
297  * @tc.name: AotCompilerServiceTest_015
298  * @tc.desc: ScreenStatusSubscriber::OnReceiveEvent(data)
299  * @tc.type: Func
300 */
301 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_015, TestSize.Level0)
302 {
303     bool viewData = true;
304     EventFwk::MatchingSkills matchingSkills;
305     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
306     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
307     auto screenStatusSubscriber_ = std::make_shared<ScreenStatusSubscriber>(subscribeInfo);
308     EventFwk::CommonEventData data;
309     screenStatusSubscriber_->OnReceiveEvent(data);
310     EXPECT_TRUE(viewData);
311 }
312 
313 /**
314  * @tc.name: AotCompilerServiceTest_016
315  * @tc.desc: ThermalMgrListener::OnReceiveEvent(data)
316  * @tc.type: Func
317 */
318 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_016, TestSize.Level0)
319 {
320     bool viewData = true;
321     EventFwk::MatchingSkills matchingSkills;
322     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
323     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
324     auto thermalMgrListener_ = std::make_shared<ThermalMgrListener>(subscribeInfo);
325     EventFwk::CommonEventData data;
326     thermalMgrListener_->OnReceiveEvent(data);
327     EXPECT_TRUE(viewData);
328 }
329 } // namespace OHOS::ArkCompiler