1 /*
2 * Copyright (c) 2022-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
16 #include "battery_light_test.h"
17 #include "battery_config.h"
18 #include "battery_config_test.h"
19 #include "battery_light.h"
20 #include "power_common.h"
21 #include <memory>
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace PowerMgr {
27 namespace {
28 BatteryLight g_light;
29 } // namespace
30
SetUpTestCase()31 void BatteryLightTest::SetUpTestCase()
32 {
33 BatteryConfig::GetInstance().ParseConfig();
34 g_light.InitLight();
35 g_light.TurnOff();
36 GTEST_LOG_(INFO) << "The current device supports Light: " << g_light.isAvailable();
37 }
38
TearDown()39 void BatteryLightTest::TearDown()
40 {
41 g_light.TurnOff();
42 }
43
44 /**
45 * @tc.name: BatteryLight001
46 * @tc.desc: Turn off light
47 * @tc.type: FUNC
48 * @tc.require: issueI5YZR1
49 */
50 HWTEST_F(BatteryLightTest, BatteryLight001, TestSize.Level1)
51 {
52 RETURN_IF(!g_light.isAvailable());
53 g_light.TurnOff();
54 EXPECT_TRUE(g_light.GetLightColor() == 0U);
55 }
56
57 /**
58 * @tc.name: BatteryLight002
59 * @tc.desc: Turn on light
60 * @tc.type: FUNC
61 * @tc.require: issueI5YZR1
62 */
63 HWTEST_F(BatteryLightTest, BatteryLight002, TestSize.Level1)
64 {
65 RETURN_IF(!g_light.isAvailable());
66 uint32_t color = 0U;
67 g_light.TurnOn(color);
68 EXPECT_TRUE(g_light.GetLightColor() == color);
69 }
70
71 /**
72 * @tc.name: BatteryLight003
73 * @tc.desc: Update light color according to the capacity value 1
74 * @tc.type: FUNC
75 * @tc.require: issueI5YZR1
76 */
77 HWTEST_F(BatteryLightTest, BatteryLight003, TestSize.Level1)
78 {
79 RETURN_IF(!g_light.isAvailable());
80 int32_t capacity = 1;
81 g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, capacity);
82 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::RED_LIGHT);
83 EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, capacity));
84 }
85
86 /**
87 * @tc.name: BatteryLight004
88 * @tc.desc: Turn on light set light color
89 * @tc.type: FUNC
90 * @tc.require: issueI5YZR1
91 */
92 HWTEST_F(BatteryLightTest, BatteryLight004, TestSize.Level1)
93 {
94 RETURN_IF(!g_light.isAvailable());
95 uint32_t color = 0x7fffffff;
96 g_light.TurnOn(color);
97 EXPECT_TRUE(g_light.GetLightColor() == color);
98 }
99
100 /**
101 * @tc.name: BatteryLight005
102 * @tc.desc: ChargingStatus is Discharging, Not bright lights
103 * @tc.type: FUNC
104 */
105 HWTEST_F(BatteryLightTest, BatteryLight005, TestSize.Level1)
106 {
107 RETURN_IF(!g_light.isAvailable());
108 GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
109 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
110 // First turn on the light
111 EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 11));
112 GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::YELLOW_LIGHT;
113 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::YELLOW_LIGHT);
114
115 EXPECT_FALSE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_NONE, 0));
116 GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::LIGHT_OFF;
117 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
118 }
119
120 /**
121 * @tc.name: BatteryLight006
122 * @tc.desc: ChargingStatus is Charging, capacity is 9, the red light on
123 * @tc.type: FUNC
124 */
125 HWTEST_F(BatteryLightTest, BatteryLight006, TestSize.Level1)
126 {
127 RETURN_IF(!g_light.isAvailable());
128 GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
129 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
130 EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 9));
131 GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::RED_LIGHT;
132 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::RED_LIGHT);
133 }
134
135 /**
136 * @tc.name: BatteryLight007
137 * @tc.desc: ChargingStatus is Charging, capacity is 89, the yellow light on
138 * @tc.type: FUNC
139 */
140 HWTEST_F(BatteryLightTest, BatteryLight007, TestSize.Level1)
141 {
142 RETURN_IF(!g_light.isAvailable());
143 GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
144 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
145 EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 89));
146 GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::YELLOW_LIGHT;
147 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::YELLOW_LIGHT);
148 }
149
150 /**
151 * @tc.name: BatteryLight008
152 * @tc.desc: ChargingStatus is Charging, capacity is 100, the green light on
153 * @tc.type: FUNC
154 */
155 HWTEST_F(BatteryLightTest, BatteryLight008, TestSize.Level1)
156 {
157 RETURN_IF(!g_light.isAvailable());
158 GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
159 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
160 EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 100));
161 GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
162 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
163 }
164
165 /**
166 * @tc.name: BatteryLight009
167 * @tc.desc: ChargingStatus is Charging, capacity is -1, Light does not change
168 * @tc.type: FUNC
169 */
170 HWTEST_F(BatteryLightTest, BatteryLight009, TestSize.Level1)
171 {
172 RETURN_IF(!g_light.isAvailable());
173 GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
174 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
175 // First turn on the light
176 EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 91));
177 GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
178 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
179
180 // Capacity invalid value, Not bright lights
181 EXPECT_FALSE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, -1));
182 GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
183 EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
184 }
185
186 /**
187 * @tc.name: BatteryLight010
188 * @tc.desc: The call interface was not initialized
189 * @tc.type: FUNC
190 */
191 HWTEST_F(BatteryLightTest, BatteryLight010, TestSize.Level1)
192 {
193 BatteryLight light;
194 uint32_t initColor = light.GetLightColor();
195 GTEST_LOG_(INFO) << "initial:" << initColor;
196 light.TurnOn(123);
197 EXPECT_EQ(light.GetLightColor(), initColor);
198 light.TurnOff();
199 EXPECT_EQ(light.GetLightColor(), initColor);
200 }
201 } // namespace PowerMgr
202 } // namespace OHOS
203