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