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 <gtest/hwext/gtest-multithread.h>
18 
19 #define private public
20 #define protected public
21 #include "js_environment.h"
22 #include "js_runtime.h"
23 #include "js_runtime_utils.h"
24 #include "js_worker.h"
25 #undef private
26 #undef protected
27 #include "event_runner.h"
28 #include "hilog_tag_wrapper.h"
29 #include "mock_js_runtime.h"
30 #include "mock_jsnapi.h"
31 
32 using namespace testing;
33 using namespace testing::ext;
34 using namespace testing::mt;
35 
36 namespace OHOS {
37 namespace AbilityRuntime {
38 namespace {
39 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
40 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
41 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
42 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
43 const std::string TEST_HAP_PATH =
44     "/system/app/com.ohos.contactsdataability/Contacts_DataAbility.hap";
45 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
46 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curJsModulePath";
47 }  // namespace
48 class JsRuntimeTest : public testing::Test {
49 public:
50     static void SetUpTestCase();
51     static void TearDownTestCase();
52     void SetUp() override;
53     void TearDown() override;
54     Runtime::Options options_;
55 };
56 
SetUpTestCase()57 void JsRuntimeTest::SetUpTestCase() {}
58 
TearDownTestCase()59 void JsRuntimeTest::TearDownTestCase() {}
60 
SetUp()61 void JsRuntimeTest::SetUp()
62 {
63     Runtime::Options newOptions;
64     options_ = newOptions;
65     options_.bundleName = TEST_BUNDLE_NAME;
66     options_.codePath = TEST_CODE_PATH;
67     options_.loadAce = false;
68     options_.isBundle = true;
69     options_.preload = false;
70     std::shared_ptr<AppExecFwk::EventRunner> eventRunner =
71         AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
72     options_.eventRunner = eventRunner;
73 }
74 
TearDown()75 void JsRuntimeTest::TearDown() {}
76 
77 /**
78  * @tc.name: DebuggerConnectionHandler_0100
79  * @tc.desc: JsRuntime test for DebuggerConnectionHandler.
80  * @tc.type: FUNC
81  */
HWTEST_F(JsRuntimeTest, UpdatePkgContextInfoJson_0100, TestSize.Level1)82 HWTEST_F(JsRuntimeTest, UpdatePkgContextInfoJson_0100, TestSize.Level1)
83 {
84     AbilityRuntime::Runtime::Options options;
85     options.preload = true;
86     auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
87     ASSERT_NE(jsRuntime, nullptr);
88     bool isDebugApp = true;
89     bool isStartWithDebug = false;
90     jsRuntime->jsEnv_->vm_ = nullptr;
91     jsRuntime->DebuggerConnectionHandler(isDebugApp, isStartWithDebug);
92     EXPECT_NE(jsRuntime, nullptr);
93     jsRuntime.reset();
94 }
95 
96 /**
97  * @tc.name: DebuggerConnectionHandler_0200
98  * @tc.desc: JsRuntime test for DebuggerConnectionHandler.
99  * @tc.type: FUNC
100  */
HWTEST_F(JsRuntimeTest, UpdatePkgContextInfoJson_0200, TestSize.Level1)101 HWTEST_F(JsRuntimeTest, UpdatePkgContextInfoJson_0200, TestSize.Level1)
102 {
103     AbilityRuntime::Runtime::Options options;
104     options.preload = true;
105     auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
106     jsRuntime->jsEnv_->vm_ = nullptr;
107     ASSERT_NE(jsRuntime, nullptr);
108     bool isDebugApp = true;
109     bool isStartWithDebug = false;
110     jsRuntime->DebuggerConnectionHandler(isDebugApp, isStartWithDebug);
111     EXPECT_NE(jsRuntime, nullptr);
112     jsRuntime.reset();
113 }
114 
115 /**
116  * @tc.name: GetSafeData_0100
117  * @tc.desc: JsRuntime test for GetSafeData.
118  * @tc.type: FUNC
119  */
HWTEST_F(JsRuntimeTest, GetSafeData_0100, TestSize.Level1)120 HWTEST_F(JsRuntimeTest, GetSafeData_0100, TestSize.Level1)
121 {
122     TAG_LOGI(AAFwkTag::TEST, "GetSafeData_0100 start");
123     AbilityRuntime::Runtime::Options options;
124     options.preload = true;
125     auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
126     ASSERT_NE(jsRuntime, nullptr);
127     std::string Path = "";
128     std::string fileFullName = "";
129     jsRuntime->jsEnv_->vm_ = nullptr;
130     auto result = jsRuntime->GetSafeData(Path, fileFullName);
131     EXPECT_EQ(result, nullptr);
132     jsRuntime.reset();
133     std::this_thread::sleep_for(std::chrono::milliseconds(200));
134     TAG_LOGI(AAFwkTag::TEST, "GetSafeData_0100 end");
135 }
136 
137 /**
138  * @tc.name: DebuggerConnectionManager_0100
139  * @tc.desc: JsRuntime test for DebuggerConnectionManager.
140  * @tc.type: FUNC
141  */
HWTEST_F(JsRuntimeTest, DebuggerConnectionManager_0100, TestSize.Level1)142 HWTEST_F(JsRuntimeTest, DebuggerConnectionManager_0100, TestSize.Level1)
143 {
144     TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0100 start");
145     AbilityRuntime::Runtime::Options options;
146     options.preload = true;
147     auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
148     ASSERT_NE(jsRuntime, nullptr);
149     bool isDebugApp = true;
150     bool isStartWithDebug = true;
151     AbilityRuntime::Runtime::DebugOption dOption;
152     dOption.perfCmd = "profile jsperf 100";
153     dOption.isStartWithDebug = false;
154     dOption.processName = "test";
155     dOption.isDebugApp = true;
156     dOption.isStartWithNative = false;
157     jsRuntime->jsEnv_->vm_ = nullptr;
158     jsRuntime->DebuggerConnectionManager(isDebugApp, isStartWithDebug, dOption);
159     EXPECT_NE(jsRuntime, nullptr);
160     jsRuntime.reset();
161     std::this_thread::sleep_for(std::chrono::milliseconds(200));
162     TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0100 end");
163 }
164 
165 /**
166  * @tc.name: DebuggerConnectionManager_0200
167  * @tc.desc: JsRuntime test for DebuggerConnectionManager.
168  * @tc.type: FUNC
169  */
HWTEST_F(JsRuntimeTest, DebuggerConnectionManager_0200, TestSize.Level1)170 HWTEST_F(JsRuntimeTest, DebuggerConnectionManager_0200, TestSize.Level1)
171 {
172     TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0200 start");
173     AbilityRuntime::Runtime::Options options;
174     options.preload = true;
175     auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
176     ASSERT_NE(jsRuntime, nullptr);
177     bool isDebugApp = false;
178     bool isStartWithDebug = true;
179     AbilityRuntime::Runtime::DebugOption dOption;
180     dOption.perfCmd = "profile jsperf 100";
181     dOption.isStartWithDebug = false;
182     dOption.processName = "test";
183     dOption.isDebugApp = true;
184     dOption.isStartWithNative = false;
185     jsRuntime->jsEnv_->vm_ = nullptr;
186     jsRuntime->DebuggerConnectionManager(isDebugApp, isStartWithDebug, dOption);
187     EXPECT_NE(jsRuntime, nullptr);
188     jsRuntime.reset();
189     std::this_thread::sleep_for(std::chrono::milliseconds(200));
190     TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0200 end");
191 }
192 }  // namespace AbilityRuntime
193 }  // namespace OHOS