• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 
27 class NapiXPowerEventTest : public NativeEngineTest {
28 public:
SetUpTestCase()29     static void SetUpTestCase()
30     {
31         GTEST_LOG_(INFO) << "NapiXPowerEventTest SetUpTestCase";
32     }
33 
TearDownTestCase()34     static void TearDownTestCase()
35     {
36         GTEST_LOG_(INFO) << "NapiXPowerEventTest TearDownTestCase";
37     }
38 
SetUp()39     void SetUp() override {}
TearDown()40     void TearDown() override {}
41 };
42 
43 /**
44  * @tc.name: ReportXPowerJsEventTest001
45  * @tc.desc: Test undefined type.
46  * @tc.type: FUNC
47  */
48 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest001, testing::ext::TestSize.Level1)
49 {
50     napi_env env = (napi_env)engine_;
51     napi_value result = nullptr;
52     ASSERT_CHECK_CALL(napi_get_undefined(env, &result));
53     ASSERT_CHECK_VALUE_TYPE(env, result, napi_undefined);
54 }
55 
56 /**
57  * @tc.name: ReportXPowerJsEventTest002
58  * @tc.desc: used to test ReportXPowerJsEventTest
59  * @tc.type: FUNC
60  */
61 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest002, testing::ext::TestSize.Level1)
62 {
63     printf("ReportXPowerJsEventTest002.\n");
64     napi_env env = (napi_env)engine_;
65     bool succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "0");
66     ASSERT_TRUE(succ);
67     sleep(1);
68     int param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
69     ASSERT_EQ(param, 0);
70     int ret = ReportXPowerJsStackSysEvent(env, "XPOWER_HIVIEW_JSAPI_TEST", "info=1,succ=true");
71     ASSERT_EQ(ret, ERR_PROP_NOT_ENABLE);
72 
73     printf("ReportXPowerJsEventTest002.\n");
74     succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "1");
75     ASSERT_TRUE(succ);
76     sleep(1);
77     param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
78     ASSERT_EQ(param, 1);
79     ret = ReportXPowerJsStackSysEvent(env, "XPOWER_HIVIEW_JSAPI_TEST", "info=2,succ=true");
80     ASSERT_EQ(ret, ERR_SUCCESS);
81 
82     printf("enable parameter and test default info.\n");
83     ret = ReportXPowerJsStackSysEvent(env, "XPOWER_HIVIEW_JSAPI_TEST");
84     ASSERT_EQ(ret, ERR_SUCCESS);
85 }
86 
87 /**
88  * @tc.name: ReportXPowerJsEventTest003
89  * @tc.desc: used to test ReportXPowerJsEventTest
90  * @tc.type: FUNC
91  */
92 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest003, testing::ext::TestSize.Level1)
93 {
94     printf("ReportXPowerJsEventTest003 nullptr.\n");
95     NativeEngine *engine = nullptr;
96     int ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST", "info=5,succ=true");
97     ASSERT_EQ(ret, ERR_PARAM_INVALID);
98 
99     printf("ReportXPowerJsEventTest003 test when disabled.\n");
100     napi_env env = (napi_env)engine_;
101     bool succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "0");
102     ASSERT_TRUE(succ);
103     sleep(1);
104     int param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
105     ASSERT_EQ(param, 0);
106     engine = reinterpret_cast<NativeEngine*>(env);
107     ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST", "info=6,succ=true");
108     ASSERT_EQ(ret, ERR_PROP_NOT_ENABLE);
109 
110     printf("ReportXPowerJsEventTest003.\n");
111     succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "1");
112     ASSERT_TRUE(succ);
113     sleep(1);
114     param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
115     ASSERT_EQ(param, 1);
116     ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST", "info=7,succ=true");
117     ASSERT_EQ(ret, ERR_SUCCESS);
118 
119     printf("enable parameter and test default info.\n");
120     ret = ReportXPowerJsStackSysEvent(engine, "XPOWER_HIVIEW_JSAPI2_TEST");
121     ASSERT_EQ(ret, ERR_SUCCESS);
122 }
123 
124 /**
125  * @tc.name: ReportXPowerJsEventTest004
126  * @tc.desc: used to test ReportXPowerJsEventTest
127  * @tc.type: FUNC
128  */
129 HWTEST_F(NapiXPowerEventTest, ReportXPowerJsEventTest004, testing::ext::TestSize.Level1)
130 {
131     ArkNativeEngine* arkEngine = reinterpret_cast<ArkNativeEngine*>(engine_);
132     EcmaVM* vm = const_cast<EcmaVM*>(arkEngine->GetEcmaVm());
133 
134     printf("ReportXPowerJsEventTest003.\n");
135     bool succ = OHOS::system::SetParameter(PROP_XPOWER_OPTIMIZE_ENABLE, "1");
136     ASSERT_TRUE(succ);
137     sleep(1);
138     int param = OHOS::system::GetIntParameter(PROP_XPOWER_OPTIMIZE_ENABLE, 0);
139     ASSERT_EQ(param, 1);
140     int ret = ReportXPowerJsStackSysEvent(vm, "XPOWER_HIVIEW_JSAPI2_TEST", "info=7,succ=true");
141     ASSERT_EQ(ret, ERR_SUCCESS);
142 
143     printf("enable parameter and test default info.\n");
144     ret = ReportXPowerJsStackSysEvent(vm, "XPOWER_HIVIEW_JSAPI2_TEST");
145     ASSERT_EQ(ret, ERR_SUCCESS);
146 }