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