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 #include "xpower_jsevent_test.h"
16 #include "xpower_event_js.h"
17 #include "xpower_event_jsvm.h"
18 #include "native_engine/impl/ark/ark_native_engine.h"
19 #include "xpower_event_common.h"
20 #include <parameters.h>
21
22 using namespace testing::ext;
23 using namespace OHOS::HiviewDFX;
24 using panda::RuntimeOption;
25
SetUp()26 void NapiXPowerEventTest::SetUp()
27 {
28 printf("SetUp ArkNativeEngine.\n");
29 RuntimeOption option;
30 option.SetGcType(RuntimeOption::GC_TYPE::GEN_GC);
31 const int64_t poolSize = 0x1000000; // 16M
32 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
33 option.SetGcPoolSize(poolSize);
34 option.SetDebuggerLibraryPath("");
35 EcmaVM *vm = panda::JSNApi::CreateJSVM(option);
36 if (vm == nullptr) {
37 printf("Failed to CreateJSVM.\n");
38 return;
39 }
40 engine_ = new ArkNativeEngine(vm, nullptr);
41 }
42
SetUpTestCase()43 void NapiXPowerEventTest::SetUpTestCase()
44 {
45 printf("SetUpTestCase.\n");
46 }
47
TearDownTestCase()48 void NapiXPowerEventTest::TearDownTestCase()
49 {
50 printf("TearDownTestCase.\n");
51 }
52
TearDown()53 void NapiXPowerEventTest::TearDown()
54 {
55 printf("TearDown.\n");
56 }
57
58 /**
59 * @tc.name: ReportXPowerJsEventTest001
60 * @tc.desc: Test undefined type.
61 * @tc.type: FUNC
62 */
63 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest001, testing::ext::TestSize.Level0)
64 {
65 napi_env env = (napi_env)engine_;
66 napi_value result = nullptr;
67 ASSERT_EQ(napi_get_undefined(env, &result), napi_ok);
68 napi_valuetype valueType = napi_undefined;
69 ASSERT_TRUE(result != nullptr);
70 ASSERT_EQ(napi_typeof(env, result, &valueType), napi_ok);
71 ASSERT_EQ(valueType, napi_undefined);
72 }
73
74 /**
75 * @tc.name: ReportXPowerJsEventTest002
76 * @tc.desc: used to test ReportXPowerJsEventTest
77 * @tc.type: FUNC
78 */
79 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest002, testing::ext::TestSize.Level0)
80 {
81 printf("ReportXPowerJsEventTest002.\n");
82 napi_env env = (napi_env)engine_;
83 bool succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "0");
84 ASSERT_TRUE(succ);
85 sleep(1);
86 int param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
87 ASSERT_EQ(param, 0);
88 int ret = ReportXPowerJsStackSysEvent(env, "XPOWER_HIVIEW_JSAPI_TEST", "info=1,succ=true");
89 ASSERT_EQ(ret, ERR_PROP_NOT_ENABLE);
90
91 printf("ReportXPowerJsEventTest002.\n");
92 succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "1");
93 ASSERT_TRUE(succ);
94 sleep(1);
95 param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
96 ASSERT_EQ(param, 1);
97 ret = ReportXPowerJsStackSysEvent(env, "XPOWER_HIVIEW_JSAPI_TEST", "info=2,succ=true");
98 if (ret == ERR_DUMP_STACK_FAILED) {
99 GTEST_SKIP() << "No JS stack exists in C++";
100 }
101 ASSERT_EQ(ret, ERR_SUCCESS);
102
103 printf("enable parameter and test default info.\n");
104 ret = ReportXPowerJsStackSysEvent(env, "XPOWER_HIVIEW_JSAPI_TEST");
105 ASSERT_EQ(ret, ERR_SUCCESS);
106 }
107
108 /**
109 * @tc.name: ReportXPowerJsEventTest003
110 * @tc.desc: used to test ReportXPowerJsEventTest
111 * @tc.type: FUNC
112 */
113 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest003, testing::ext::TestSize.Level0)
114 {
115 printf("ReportXPowerJsEventTest003 nullptr.\n");
116 NativeEngine *engine = nullptr;
117 int ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST", "info=5,succ=true");
118 ASSERT_EQ(ret, ERR_PARAM_INVALID);
119
120 printf("ReportXPowerJsEventTest003 test when disabled.\n");
121 napi_env env = (napi_env)engine_;
122 bool succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "0");
123 ASSERT_TRUE(succ);
124 sleep(1);
125 int param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
126 ASSERT_EQ(param, 0);
127 engine = reinterpret_cast<NativeEngine*>(env);
128 ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST", "info=6,succ=true");
129 ASSERT_EQ(ret, ERR_PROP_NOT_ENABLE);
130
131 printf("ReportXPowerJsEventTest003.\n");
132 succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "1");
133 ASSERT_TRUE(succ);
134 sleep(1);
135 param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
136 ASSERT_EQ(param, 1);
137 ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST", "info=7,succ=true");
138 if (ret == ERR_DUMP_STACK_FAILED) {
139 GTEST_SKIP() << "No JS stack exists in C++";
140 }
141 ASSERT_EQ(ret, ERR_SUCCESS);
142
143 printf("enable parameter and test default info.\n");
144 ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST");
145 ASSERT_EQ(ret, ERR_SUCCESS);
146 }
147
148 /**
149 * @tc.name: ReportXPowerJsEventTest004
150 * @tc.desc: used to test ReportXPowerJsEventTest
151 * @tc.type: FUNC
152 */
153 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest004, testing::ext::TestSize.Level0)
154 {
155 ArkNativeEngine* arkEngine = reinterpret_cast<ArkNativeEngine*>(engine_);
156 EcmaVM* vm = const_cast<EcmaVM*>(arkEngine->GetEcmaVm());
157
158 printf("ReportXPowerJsEventTest003.\n");
159 bool succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "1");
160 ASSERT_TRUE(succ);
161 sleep(1);
162 int param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
163 ASSERT_EQ(param, 1);
164 int ret = ReportXPowerJsStackSysEvent(vm, "XPOWER_HIVIEW_JSAPI2_TEST", "info=7,succ=true");
165 if (ret == ERR_DUMP_STACK_FAILED) {
166 GTEST_SKIP() << "No JS stack exists in C++";
167 }
168 ASSERT_EQ(ret, ERR_SUCCESS);
169
170 printf("enable parameter and test default info.\n");
171 ret = ReportXPowerJsStackSysEvent(vm, "XPOWER_HIVIEW_JSAPI2_TEST");
172 ASSERT_EQ(ret, ERR_SUCCESS);
173 }