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 <gtest/gtest.h>
17 #include "ability_manager_errors.h"
18 #include "app_native_spawn_manager.h"
19 #include "native_child_notify_proxy.h"
20 #include "hilog_tag_wrapper.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 class AppNativeSpawnManagerTest : public testing::Test {
28 public:
29 void SetUp();
30 void TearDown();
31
32 protected:
33 std::shared_ptr<AppRunningManager> appRunningManager_ = nullptr;
34 };
35
SetUp()36 void AppNativeSpawnManagerTest::SetUp()
37 {
38 appRunningManager_ = std::make_shared<AppRunningManager>();
39 AppNativeSpawnManager::GetInstance().InitNativeSpawnMsgPipe(appRunningManager_);
40 }
41
TearDown()42 void AppNativeSpawnManagerTest::TearDown()
43 {
44 }
45
46 /**
47 * @tc.number: AppNativeSpawnManagerTest_RegisterNativeChildExitNotify_0100
48 * @tc.desc: Test RegisterNativeChildExitNotify works
49 * @tc.type: FUNC
50 * @tc.Function: RegisterNativeChildExitNotify
51 * @tc.SubFunction: NA
52 * @tc.EnvConditions: NA
53 */
54 HWTEST_F(AppNativeSpawnManagerTest, AppNativeSpawnManagerTest_RegisterNativeChildExitNotify_0100, TestSize.Level0)
55 {
56 TAG_LOGD(AAFwkTag::TEST, "AppNativeSpawnManagerTest_RegisterNativeChildExitNotify_0100 start.");
57 sptr<INativeChildNotify> notify;
58 auto ret = AppNativeSpawnManager::GetInstance().RegisterNativeChildExitNotify(notify);
59 EXPECT_EQ(ret, ERR_INVALID_VALUE);
60 notify = sptr<NativeChildNotifyProxy>::MakeSptr(nullptr);
61 ret = AppNativeSpawnManager::GetInstance().RegisterNativeChildExitNotify(notify);
62 EXPECT_EQ(ret, OHOS::AAFwk::ERR_CALLER_NOT_EXISTS);
63 }
64
65 /**
66 * @tc.number: AppNativeSpawnManagerTest_UnregisterNativeChildExitNotify_0200
67 * @tc.desc: Test UnregisterNativeChildExitNotify works
68 * @tc.type: FUNC
69 * @tc.Function: UnregisterNativeChildExitNotify
70 * @tc.SubFunction: NA
71 * @tc.EnvConditions: NA
72 */
73 HWTEST_F(AppNativeSpawnManagerTest, AppNativeSpawnManagerTest_UnregisterNativeChildExitNotify_0200, TestSize.Level0)
74 {
75 TAG_LOGD(AAFwkTag::TEST, "AppNativeSpawnManagerTest_UnregisterNativeChildExitNotify_0200 start.");
76 sptr<INativeChildNotify> notify;
77 auto ret = AppNativeSpawnManager::GetInstance().UnregisterNativeChildExitNotify(notify);
78 EXPECT_EQ(ret, ERR_INVALID_VALUE);
79 notify = sptr<NativeChildNotifyProxy>::MakeSptr(nullptr);
80 ret = AppNativeSpawnManager::GetInstance().UnregisterNativeChildExitNotify(notify);
81 EXPECT_EQ(ret, OHOS::AAFwk::ERR_INVALID_CALLER);
82 }
83
84 /**
85 * @tc.number: AppNativeSpawnManagerTest_GetNativeChildCallbackByPid_0300
86 * @tc.desc: Test UnregisterNativeChildExitNotify works
87 * @tc.type: FUNC
88 * @tc.Function: UnregisterNativeChildExitNotify
89 * @tc.SubFunction: NA
90 * @tc.EnvConditions: NA
91 */
92 HWTEST_F(AppNativeSpawnManagerTest, AppNativeSpawnManagerTest_GetNativeChildCallbackByPid_0300, TestSize.Level0)
93 {
94 TAG_LOGD(AAFwkTag::TEST, "AppNativeSpawnManagerTest_GetNativeChildCallbackByPid_0300 start.");
95 int32_t pid = 1;
96 auto notify = AppNativeSpawnManager::GetInstance().GetNativeChildCallbackByPid(pid);
97 EXPECT_EQ(notify, nullptr);
98 }
99
100
101 /**
102 * @tc.number: AppNativeSpawnManagerTest_RemoveNativeChildCallbackByPid_0400
103 * @tc.desc: Test RemoveNativeChildCallbackByPid works
104 * @tc.type: FUNC
105 * @tc.Function: RemoveNativeChildCallbackByPid
106 * @tc.SubFunction: NA
107 * @tc.EnvConditions: NA
108 */
109 HWTEST_F(AppNativeSpawnManagerTest, AppNativeSpawnManagerTest_RemoveNativeChildCallbackByPid_0400, TestSize.Level0)
110 {
111 TAG_LOGD(AAFwkTag::TEST, "AppNativeSpawnManagerTest_RemoveNativeChildCallbackByPid_0400 start.");
112 int32_t pid = 1;
113 AppNativeSpawnManager::GetInstance().RemoveNativeChildCallbackByPid(pid);
114 auto notify = AppNativeSpawnManager::GetInstance().GetNativeChildCallbackByPid(pid);
115 EXPECT_EQ(notify, nullptr);
116 }
117
118 /**
119 * @tc.number: AppNativeSpawnManagerTest_ChildRelation_0500
120 * @tc.desc: Test ChildRelation works
121 * @tc.type: FUNC
122 * @tc.Function: ChildRelation
123 * @tc.SubFunction: NA
124 * @tc.EnvConditions: NA
125 */
126 HWTEST_F(AppNativeSpawnManagerTest, AppNativeSpawnManagerTest_ChildRelation_0500, TestSize.Level0)
127 {
128 TAG_LOGD(AAFwkTag::TEST, "AppNativeSpawnManagerTest_ChildRelation_0500 start.");
129 int32_t childPid = 1;
130 int32_t parentPid = 2;
131 AppNativeSpawnManager::GetInstance().AddChildRelation(childPid, parentPid);
132 auto parent = AppNativeSpawnManager::GetInstance().GetChildRelation(childPid);
133 EXPECT_EQ(parent, 2);
134 AppNativeSpawnManager::GetInstance().RemoveChildRelation(childPid);
135 parent = AppNativeSpawnManager::GetInstance().GetChildRelation(childPid);
136 EXPECT_EQ(parent, 0);
137 }
138
139
140 } // namespace AppExecFwk
141 } // namespace OHOS
142