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 "ohos_js_environment_impl.h"
17
18 #include <gtest/gtest.h>
19 #include <cstdarg>
20 #include <string>
21
22 #include "hilog_wrapper.h"
23 #include "js_runtime.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace AbilityRuntime {
30 class OHOSJsEnvironmentTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void OHOSJsEnvironmentTest::SetUpTestCase()
39 {}
40
TearDownTestCase()41 void OHOSJsEnvironmentTest::TearDownTestCase()
42 {}
43
SetUp()44 void OHOSJsEnvironmentTest::SetUp()
45 {}
46
TearDown()47 void OHOSJsEnvironmentTest::TearDown()
48 {}
49
50 /**
51 * @tc.name: PostTask_0100
52 * @tc.desc: Js environment post and remove task.
53 * @tc.type: FUNC
54 * @tc.require: issueI6KODF
55 */
56 HWTEST_F(OHOSJsEnvironmentTest, PostTask_0100, TestSize.Level0)
57 {
58 auto jsEnvImpl = std::make_shared<OHOSJsEnvironmentImpl>();
59 ASSERT_NE(jsEnvImpl, nullptr);
60
61 std::string taskName = "task001";
__anon8c0bb9550102() 62 auto task = [name = taskName]() {
63 HILOG_INFO("%{public}s called.", name.c_str());
64 };
65 int64_t delayTime = 1000;
66 jsEnvImpl->PostTask(task, taskName, delayTime);
67 jsEnvImpl->RemoveTask(taskName);
68 }
69
70 /**
71 * @tc.name: PostSyncTask_0100
72 * @tc.desc: Js environment post sync task.
73 * @tc.type: FUNC
74 * @tc.require: issueI7C87T
75 */
76 HWTEST_F(OHOSJsEnvironmentTest, PostSyncTask_0100, TestSize.Level0)
77 {
78 auto runner = AppExecFwk::EventRunner::Create("TASK_RUNNER");
79 ASSERT_NE(runner, nullptr);
80 auto jsEnvImpl = std::make_shared<OHOSJsEnvironmentImpl>(runner);
81 ASSERT_NE(jsEnvImpl, nullptr);
82
83 AbilityRuntime::Runtime::Options options;
84 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
85 ASSERT_NE(jsRuntime, nullptr);
86 auto ret = jsEnvImpl->InitLoop(jsRuntime->GetNativeEnginePointer());
87 ASSERT_EQ(ret, true);
88
89 std::string taskName = "syncTask001";
90 bool taskExecuted = false;
__anon8c0bb9550202() 91 auto task = [taskName, &taskExecuted]() {
92 HILOG_INFO("%{public}s called.", taskName.c_str());
93 taskExecuted = true;
94 };
95 jsEnvImpl->PostSyncTask(task, taskName);
96 EXPECT_EQ(taskExecuted, true);
97 }
98
99 /**
100 * @tc.name: InitTimerModule_0100
101 * @tc.desc: Js environment init timer.
102 * @tc.type: FUNC
103 * @tc.require: issueI6Z5M5
104 */
105 HWTEST_F(OHOSJsEnvironmentTest, InitTimerModule_0100, TestSize.Level0)
106 {
107 auto jsEnvImpl = std::make_unique<OHOSJsEnvironmentImpl>();
108 ASSERT_NE(jsEnvImpl, nullptr);
109
110 // Init timer module when native engine is invalid.
111 jsEnvImpl->InitTimerModule(nullptr);
112
113 AbilityRuntime::Runtime::Options options;
114 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
115 ASSERT_NE(jsRuntime, nullptr);
116
117 // Init timer module when native engine has created.
118 jsEnvImpl->InitTimerModule(jsRuntime->GetNativeEnginePointer());
119 }
120
121 /**
122 * @tc.name: InitWorkerModule_0100
123 * @tc.desc: Js environment init worker.
124 * @tc.type: FUNC
125 * @tc.require: issueI6KODF
126 */
127 HWTEST_F(OHOSJsEnvironmentTest, InitWorkerModule_0100, TestSize.Level0)
128 {
129 auto jsEnvImpl = std::make_shared<OHOSJsEnvironmentImpl>();
130 ASSERT_NE(jsEnvImpl, nullptr);
131 AbilityRuntime::Runtime::Options options;
132 auto runtime = AbilityRuntime::Runtime::Create(options);
133 auto& jsEngine = (static_cast<AbilityRuntime::JsRuntime&>(*runtime)).GetNativeEngine();
134 std::shared_ptr<JsEnv::WorkerInfo> workerInfo = std::make_shared<JsEnv::WorkerInfo>();
135 jsEnvImpl->InitWorkerModule(jsEngine, workerInfo);
136 }
137
138 /**
139 * @tc.name: InitSyscapModule_0100
140 * @tc.desc: Js environment init syscap.
141 * @tc.type: FUNC
142 * @tc.require: issueI6KODF
143 */
144 HWTEST_F(OHOSJsEnvironmentTest, InitSyscapModule_0100, TestSize.Level0)
145 {
146 auto jsEnvImpl = std::make_shared<OHOSJsEnvironmentImpl>();
147 ASSERT_NE(jsEnvImpl, nullptr);
148
149 jsEnvImpl->InitSyscapModule();
150 }
151 } // namespace AbilityRuntime
152 } // namespace OHOS
153