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 "interface_loader_test.h"
17 #include "interface_loader.h"
18 #include "power_log.h"
19
20 namespace OHOS {
21 namespace PowerMgr {
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace {
26 const std::string TEST_LIB_PATH = "libpower_ability.z.so";
27 const std::vector<std::string> TEST_LIB_SYMBOLS = {
28 "PowerConnectAbility",
29 "PowerStartAbility",
30 "UnExistedSymbolPowerAbc",
31 "PowerGetForegroundApplications",
32 "PowerIsForegroundApplication",
33 "UnExistedSymbolPowerXyz",
34 };
35 InterfaceLoader g_loader(TEST_LIB_PATH, TEST_LIB_SYMBOLS);
36 }
37
SetUpTestCase()38 void InterfaceLoaderTest::SetUpTestCase()
39 {
40 bool res = g_loader.Init();
41 EXPECT_TRUE(res);
42 }
43
TearDownTestCase()44 void InterfaceLoaderTest::TearDownTestCase()
45 {
46 g_loader.DeInit();
47 }
48
49 /**
50 * @tc.name: InterfaceLoaderTest001
51 * @tc.desc: test QueryInterface, symbol exists
52 * @tc.type: FUNC
53 */
54 HWTEST_F(InterfaceLoaderTest, InterfaceLoaderTest001, TestSize.Level0)
55 {
56 POWER_HILOGI(LABEL_TEST, "InterfaceLoaderTest001 function start!");
57 void* connectAbilityFunc = g_loader.QueryInterface("PowerConnectAbility");
58 EXPECT_NE(connectAbilityFunc, nullptr);
59 void* isForegroundAppFunc = g_loader.QueryInterface("PowerIsForegroundApplication");
60 EXPECT_NE(isForegroundAppFunc, nullptr);
61 POWER_HILOGI(LABEL_TEST, "InterfaceLoaderTest001 function end!");
62 }
63
64 /**
65 * @tc.name: InterfaceLoaderTest002
66 * @tc.desc: test QueryInterface, symbol don't exist
67 * @tc.type: FUNC
68 */
69 HWTEST_F(InterfaceLoaderTest, InterfaceLoaderTest002, TestSize.Level0)
70 {
71 POWER_HILOGI(LABEL_TEST, "InterfaceLoaderTest002 function start!");
72 void* powerAbcFunc = g_loader.QueryInterface("UnExistedSymbolPowerAbc");
73 EXPECT_EQ(powerAbcFunc, nullptr);
74 void* powerXyzFunc = g_loader.QueryInterface("UnExistedSymbolPowerXyz");
75 EXPECT_EQ(powerXyzFunc, nullptr);
76 POWER_HILOGI(LABEL_TEST, "InterfaceLoaderTest002 function end!");
77 }
78 } // PowerMgr
79 } // OHOS