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 <chrono>
17 #include <thread>
18 #include <gtest/hwext/gtest-multithread.h>
19 #include <unistd.h>
20 #include "gtest/gtest.h"
21 #include "system_ability_definition.h"
22 #include "system_ability.h"
23 #include "iservice_registry.h"
24 #include "start_test_server.h"
25 #include "mock_permission.h"
26
27 using namespace std;
28 using namespace testing::ext;
29 using namespace testing::mt;
30 using namespace OHOS;
31 using namespace OHOS::testserver;
32
33 const int32_t SYSTEM_ABILITY_ID = TEST_SERVER_SA_ID;
34 const int32_t UNLOAD_SYSTEMABILITY_WAITTIME = 2000;
35
36 HWTEST(ClientTest, testLoadTestServer, TestSize.Level1)
37 {
38 TestServerMockPermission::MockProcess("testserver");
39 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 sptr<ITestServerInterface> iTestServerInterface = StartTestServer::GetInstance().LoadTestServer();
41 EXPECT_NE(iTestServerInterface, nullptr);
42 EXPECT_NE(samgr->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr);
43 samgr->UnloadSystemAbility(SYSTEM_ABILITY_ID);
44 std::this_thread::sleep_for(std::chrono::milliseconds(UNLOAD_SYSTEMABILITY_WAITTIME));
45 }
46
load_test_server_test(sptr<ISystemAbilityManager> samgr)47 void load_test_server_test(sptr<ISystemAbilityManager> samgr)
48 {
49 TestServerMockPermission::MockProcess("testserver");
50 sptr<ITestServerInterface> iTestServerInterface = StartTestServer::GetInstance().LoadTestServer();
51 EXPECT_NE(iTestServerInterface, nullptr);
52 EXPECT_NE(samgr->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr);
53 }
54
55 HWMTEST(ClientTest, testMutilLoadTestServer, TestSize.Level1, 3)
56 {
57 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 EXPECT_EQ(samgr->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr);
59 load_test_server_test(samgr);
60 EXPECT_NE(samgr->CheckSystemAbility(SYSTEM_ABILITY_ID), nullptr);
61 samgr->UnloadSystemAbility(SYSTEM_ABILITY_ID);
62 std::this_thread::sleep_for(std::chrono::milliseconds(UNLOAD_SYSTEMABILITY_WAITTIME));
63 }