1 /*
2 * Copyright (c) 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 <gtest/gtest.h>
17
18 #include "ohos.power.ani.hpp"
19 #include "power_log.h"
20
21 using namespace OHOS::PowerMgr;
22
23 using namespace testing;
24 using namespace testing::ext;
25 namespace {
26 ani_status g_status1 = ANI_OK;
27 ani_status g_status2 = ANI_OK;
28 }
29
30 namespace ohos::power {
ANIRegister(ani_env * env)31 ani_status ANIRegister(ani_env *env)
32 {
33 return g_status1;
34 }
35 }
36
37 namespace {
38 class PowerAniConstructorTest : public ::testing::Test {
39 public:
SetUpTestCase()40 static void SetUpTestCase() {}
TearDownTestCase()41 static void TearDownTestCase() {}
SetUp()42 void SetUp() {}
TearDown()43 void TearDown() {}
44 };
45
GetEnv(ani_vm * vm,uint32_t version,ani_env ** result)46 static ani_status GetEnv(ani_vm *vm, uint32_t version, ani_env **result)
47 {
48 return g_status2;
49 }
50 /**
51 * @tc.name: PowerAniConstructorTest_001
52 * @tc.desc: test AniConstructor
53 * @tc.type: FUNC
54 * @tc.require: issue#ICAK9Z
55 */
56 HWTEST_F(PowerAniConstructorTest, PowerAniConstructorTest_001, TestSize.Level1)
57 {
58 POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_001 start");
59 ani_vm vm;
60 __ani_vm_api mock_c_api = {.GetEnv = GetEnv};
61 vm.c_api = &mock_c_api;
62 uint32_t result = 0;
63
64 ani_status status = ANI_Constructor(&vm, &result);
65
66 EXPECT_EQ(status, ANI_OK);
67 EXPECT_EQ(result, ANI_VERSION_1);
68 POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_001 end");
69 }
70
71 /**
72 * @tc.name: PowerAniConstructorTest_002
73 * @tc.desc: test AniConstructor
74 * @tc.type: FUNC
75 * @tc.require: issue#ICAK9Z
76 */
77 HWTEST_F(PowerAniConstructorTest, PowerAniConstructorTest_002, TestSize.Level1)
78 {
79 POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_002 start");
80 ani_vm vm;
81 __ani_vm_api mock_c_api = {.GetEnv = GetEnv};
82 vm.c_api = &mock_c_api;
83 uint32_t result = 0;
84
85 g_status1 = ANI_ERROR;
86 ani_status status = ANI_Constructor(&vm, &result);
87
88 EXPECT_EQ(status, ANI_ERROR);
89 POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_002 end");
90 }
91
92 /**
93 * @tc.name: PowerAniConstructorTest_003
94 * @tc.desc: test AniConstructor
95 * @tc.type: FUNC
96 * @tc.require: issue#ICAK9Z
97 */
98 HWTEST_F(PowerAniConstructorTest, PowerAniConstructorTest_003, TestSize.Level1)
99 {
100 POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_003 start");
101 ani_vm vm;
102 __ani_vm_api mock_c_api = {.GetEnv = GetEnv};
103 vm.c_api = &mock_c_api;
104 uint32_t result = 0;
105
106 g_status1 = ANI_OK;
107 g_status2 = ANI_ERROR;
108 ani_status status = ANI_Constructor(&vm, &result);
109
110 EXPECT_EQ(status, ANI_ERROR);
111 POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_003 end");
112 }
113 }