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 "iservice_registry.h"
30 #include "system_ability_definition.h"
31
32 using namespace testing::ext;
33
34 namespace OHOS::ArkCompiler {
35 namespace {
36 const std::string TASK_ID = "UnLoadSA";
37 constexpr int32_t NO_DELAY_TIME = 0;
38 constexpr int32_t SLEEP_TIME_FOR_WAITTING_UNLOAD_SA = 5 * 1000; // 5 ms
39 }
40
41 class AotCompilerServiceTest : public testing::Test {
42 public:
AotCompilerServiceTest()43 AotCompilerServiceTest() {}
~AotCompilerServiceTest()44 virtual ~AotCompilerServiceTest() {}
45
SetUpTestCase()46 static void SetUpTestCase() {}
TearDownTestCase()47 static void TearDownTestCase() {}
SetUp()48 void SetUp() override {}
TearDown()49 void TearDown() override {}
50 void OnStart(AotCompilerService &aotService);
51 };
52
OnStart(AotCompilerService & aotService)53 void AotCompilerServiceTest::OnStart(AotCompilerService &aotService)
54 {
55 if (aotService.state_ == ServiceRunningState::STATE_RUNNING) {
56 return;
57 }
58 if (!aotService.Init()) {
59 return;
60 }
61 aotService.state_ = ServiceRunningState::STATE_RUNNING;
62 aotService.RegisterPowerDisconnectedListener();
63 }
64
65 /**
66 * @tc.name: AotCompilerServiceTest_001
67 * @tc.desc: AotCompilerService instance initialization
68 * @tc.type: Func
69 * @tc.require: IR/AR/SR
70 */
71 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_001, TestSize.Level0)
72 {
73 AotCompilerService aotService;
74 EXPECT_EQ(aotService.state_, ServiceRunningState::STATE_NOT_START);
75 }
76
77 /**
78 * @tc.name: AotCompilerServiceTest_002
79 * @tc.desc: Init() in AotCompilerService
80 * @tc.type: Func
81 * @tc.require: IR/AR/SR
82 */
83 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_002, TestSize.Level0)
84 {
85 AotCompilerService aotService;
86 EXPECT_TRUE(aotService.Init());
87 EXPECT_NE(aotService.unLoadHandler_, nullptr);
88 }
89
90 /**
91 * @tc.name: AotCompilerServiceTest_003
92 * @tc.desc: RegisterPowerDisconnectedListener() && UnRegisterPowerDisconnectedListener()
93 * @tc.type: Func
94 * @tc.require: IR/AR/SR
95 */
96 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_003, TestSize.Level0)
97 {
98 AotCompilerService aotService;
99 aotService.RegisterPowerDisconnectedListener();
100 EXPECT_TRUE(aotService.isPowerEventSubscribered_);
101
102 aotService.UnRegisterPowerDisconnectedListener();
103 EXPECT_FALSE(aotService.isPowerEventSubscribered_);
104 }
105
106 /**
107 * @tc.name: AotCompilerServiceTest_004
108 * @tc.desc: OnStart()/OnStop() in AotCompilerService
109 * @tc.type: Func
110 * @tc.require: IR/AR/SR
111 */
112 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_004, TestSize.Level0)
113 {
114 AotCompilerService aotService;
115 OnStart(aotService);
116 EXPECT_EQ(aotService.state_, ServiceRunningState::STATE_RUNNING);
117
118 aotService.OnStop();
119 EXPECT_EQ(aotService.state_, ServiceRunningState::STATE_NOT_START);
120 }
121
122 /**
123 * @tc.name: AotCompilerServiceTest_005
124 * @tc.desc: DelayUnloadTask(TASK_ID, DELAY_TIME)
125 * @tc.type: Func
126 * @tc.require: IR/AR/SR
127 */
128 HWTEST_F(AotCompilerServiceTest, AotCompilerServiceTest_005, TestSize.Level0)
129 {
130 AotCompilerService aotService;
131 EXPECT_TRUE(aotService.Init());
132 EXPECT_NE(aotService.unLoadHandler_, nullptr);
133
134 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
135 EXPECT_NE(aotClient.GetAotCompilerProxy(), nullptr);
136 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
137 ASSERT_NE(systemAbilityMgr, nullptr);
138 auto remoteObject = systemAbilityMgr->CheckSystemAbility(AOT_COMPILER_SERVICE_ID);
139 EXPECT_NE(remoteObject, nullptr);
140
141 aotService.DelayUnloadTask(TASK_ID, NO_DELAY_TIME);
142 usleep(SLEEP_TIME_FOR_WAITTING_UNLOAD_SA);
143 aotService.OnStop();
144 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
145 }
146 } // namespace OHOS::ArkCompiler