1 /*
2  * Copyright (c) 2023 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 
18 #define protected public
19 #include "js_child_process.h"
20 #undef protected
21 #include "hilog_tag_wrapper.h"
22 #include "js_runtime.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 class JsChildProcessTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp() override;
34     void TearDown() override;
35 };
36 
SetUpTestCase()37 void JsChildProcessTest::SetUpTestCase()
38 {}
39 
TearDownTestCase()40 void JsChildProcessTest::TearDownTestCase()
41 {}
42 
SetUp()43 void JsChildProcessTest::SetUp()
44 {}
45 
TearDown()46 void JsChildProcessTest::TearDown()
47 {}
48 
49 /**
50  * @tc.number: JsChildProcessCreate_0100
51  * @tc.desc: Test JsChildProcessTest Create works
52  * @tc.type: FUNC
53  */
54 HWTEST_F(JsChildProcessTest, JsChildProcessCreate_0100, TestSize.Level0)
55 {
56     TAG_LOGD(AAFwkTag::TEST, "JsChildProcessCreate_0100 called.");
57     std::unique_ptr<Runtime> runtime = std::make_unique<JsRuntime>();
58     auto process = JsChildProcess::Create(runtime);
59     EXPECT_TRUE(process != nullptr);
60 }
61 
62 /**
63  * @tc.number: JsChildProcessInit_0100
64  * @tc.desc: Test JsChildProcess Init works
65  * @tc.type: FUNC
66  */
67 HWTEST_F(JsChildProcessTest, JsChildProcessInit_0100, TestSize.Level0)
68 {
69     TAG_LOGD(AAFwkTag::TEST, "JsChildProcessInit_0100 called.");
70     std::unique_ptr<Runtime> runtime = std::make_unique<JsRuntime>();
71     auto process = JsChildProcess::Create(runtime);
72     EXPECT_TRUE(process != nullptr);
73 
74     std::shared_ptr<ChildProcessStartInfo> info = std::make_shared<ChildProcessStartInfo>();
75     info->name = "AProcess";
76     info->srcEntry = "entry/./ets/process/AProcess.ts";
77     info->moduleName = "entry";
78 
79     process->Init(info);
80     EXPECT_TRUE(process->processStartInfo_ != nullptr);
81 }
82 
83 /**
84  * @tc.number: JsChildProcessInit_0200
85  * @tc.desc: Test JsChildProcess Init works
86  * @tc.type: FUNC
87  */
88 HWTEST_F(JsChildProcessTest, JsChildProcessInit_0200, TestSize.Level0)
89 {
90     TAG_LOGD(AAFwkTag::TEST, "JsChildProcessInit_0200 called.");
91     std::unique_ptr<Runtime> runtime = std::make_unique<JsRuntime>();
92     auto process = JsChildProcess::Create(runtime);
93     EXPECT_TRUE(process != nullptr);
94 
95     auto ret = process->Init(nullptr);
96     EXPECT_FALSE(ret);
97 }
98 
99 /**
100  * @tc.number: JsChildProcessInit_0300
101  * @tc.desc: Test JsChildProcess Init works
102  * @tc.type: FUNC
103  */
104 HWTEST_F(JsChildProcessTest, JsChildProcessInit_0300, TestSize.Level0)
105 {
106     TAG_LOGD(AAFwkTag::TEST, "JsChildProcessInit_0300 called.");
107     std::unique_ptr<Runtime> runtime = std::make_unique<JsRuntime>();
108     auto process = JsChildProcess::Create(runtime);
109     EXPECT_TRUE(process != nullptr);
110 
111     std::shared_ptr<ChildProcessStartInfo> info = std::make_shared<ChildProcessStartInfo>();
112     info->name = "AProcess";
113     info->srcEntry = "";
114     info->moduleName = "entry";
115 
116     auto ret = process->Init(info);
117     EXPECT_FALSE(ret);
118 }
119 
120 /**
121  * @tc.number: JsChildProcessOnStart_0100
122  * @tc.desc: Test JsChildProcess OnStart works
123  * @tc.type: FUNC
124  */
125 HWTEST_F(JsChildProcessTest, JsChildProcessOnStart_0100, TestSize.Level0)
126 {
127     TAG_LOGD(AAFwkTag::TEST, "JsChildProcessOnStart_0100 called.");
128     std::unique_ptr<Runtime> runtime = std::make_unique<JsRuntime>();
129     auto process = JsChildProcess::Create(runtime);
130     EXPECT_TRUE(process != nullptr);
131 
132     std::shared_ptr<ChildProcessStartInfo> info = std::make_shared<ChildProcessStartInfo>();
133     info->name = "AProcess";
134     info->srcEntry = "entry/./ets/process/AProcess.ts";
135     info->moduleName = "entry";
136 
137     process->Init(info);
138     process->OnStart();
139     EXPECT_TRUE(process->processStartInfo_ != nullptr);
140 }
141 
142 /**
143  * @tc.number: JsChildProcessOnStart_0200
144  * @tc.desc: Test JsChildProcess OnStart works
145  * @tc.type: FUNC
146  */
147 HWTEST_F(JsChildProcessTest, JsChildProcessOnStart_0200, TestSize.Level0)
148 {
149     TAG_LOGD(AAFwkTag::TEST, "JsChildProcessOnStart_0200 called.");
150     std::unique_ptr<Runtime> runtime = std::make_unique<JsRuntime>();
151     auto process = JsChildProcess::Create(runtime);
152     EXPECT_TRUE(process != nullptr);
153 
154     std::shared_ptr<ChildProcessStartInfo> info = std::make_shared<ChildProcessStartInfo>();
155     info->name = "AProcess";
156     info->srcEntry = "entry/./ets/process/AProcess.ts";
157     info->moduleName = "entry";
158 
159     process->Init(info);
160     auto args = std::make_shared<AppExecFwk::ChildProcessArgs>();
161     process->OnStart(args);
162     EXPECT_TRUE(process->processStartInfo_ != nullptr);
163 }
164 }  // namespace AbilityRuntime
165 }  // namespace OHOS