• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 constexpr const char* SYSTEM_BATTERY_CONFIG_PATH = "/system/etc/battery/battery_config.json";
29 
30 BatteryLight g_light;
31 } // namespace
32 
SetUpTestCase()33 void BatteryLightTest::SetUpTestCase()
34 {
35     BatteryConfig::GetInstance().ParseConfig(SYSTEM_BATTERY_CONFIG_PATH);
36     g_light.InitLight();
37     g_light.TurnOff();
38 }
39 
TearDown()40 void BatteryLightTest::TearDown()
41 {
42     g_light.TurnOff();
43 }
44 
45 /**
46  * @tc.name: BatteryLight001
47  * @tc.desc: Turn off light
48  * @tc.type: FUNC
49  * @tc.require: issueI5YZR1
50  */
51 HWTEST_F(BatteryLightTest, BatteryLight001, TestSize.Level1)
52 {
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     uint32_t color = 0U;
66     g_light.TurnOn(color);
67     EXPECT_TRUE(g_light.GetLightColor() == color);
68 }
69 
70 /**
71  * @tc.name: BatteryLight003
72  * @tc.desc: Update light color according to the capacity value 1
73  * @tc.type: FUNC
74  * @tc.require: issueI5YZR1
75  */
76 HWTEST_F(BatteryLightTest, BatteryLight003, TestSize.Level1)
77 {
78     int32_t capacity = 1;
79     g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, capacity);
80     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::RED_LIGHT);
81 }
82 
83 /**
84  * @tc.name: BatteryLight004
85  * @tc.desc: Turn on light set light color
86  * @tc.type: FUNC
87  * @tc.require: issueI5YZR1
88  */
89 HWTEST_F(BatteryLightTest, BatteryLight004, TestSize.Level1)
90 {
91     uint32_t color = 0x7fffffff;
92     g_light.TurnOn(color);
93     EXPECT_TRUE(g_light.GetLightColor() == color);
94 }
95 
96 /**
97  * @tc.name: BatteryLight005
98  * @tc.desc: ChargingStatus is Discharging, Not bright lights
99  * @tc.type: FUNC
100  */
101 HWTEST_F(BatteryLightTest, BatteryLight005, TestSize.Level1)
102 {
103     RETURN_IF(!g_light.isAvailable());
104     GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
105     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
106     // First turn on the light
107     EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 11));
108     GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::YELLOW_LIGHT;
109     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::YELLOW_LIGHT);
110 
111     EXPECT_FALSE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_NONE, 0));
112     GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::LIGHT_OFF;
113     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
114 }
115 
116 /**
117  * @tc.name: BatteryLight006
118  * @tc.desc: ChargingStatus is Charging, capacity is 9, the red light on
119  * @tc.type: FUNC
120  */
121 HWTEST_F(BatteryLightTest, BatteryLight006, TestSize.Level1)
122 {
123     RETURN_IF(!g_light.isAvailable());
124     GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
125     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
126     EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 9));
127     GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::RED_LIGHT;
128     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::RED_LIGHT);
129 }
130 
131 /**
132  * @tc.name: BatteryLight007
133  * @tc.desc: ChargingStatus is Charging, capacity is 89, the yellow light on
134  * @tc.type: FUNC
135  */
136 HWTEST_F(BatteryLightTest, BatteryLight007, TestSize.Level1)
137 {
138     RETURN_IF(!g_light.isAvailable());
139     GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
140     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
141     EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 89));
142     GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::YELLOW_LIGHT;
143     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::YELLOW_LIGHT);
144 }
145 
146 /**
147  * @tc.name: BatteryLight008
148  * @tc.desc: ChargingStatus is Charging, capacity is 100, the green light on
149  * @tc.type: FUNC
150  */
151 HWTEST_F(BatteryLightTest, BatteryLight008, TestSize.Level1)
152 {
153     RETURN_IF(!g_light.isAvailable());
154     GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
155     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
156     EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 100));
157     GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
158     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
159 }
160 
161 /**
162  * @tc.name: BatteryLight009
163  * @tc.desc: ChargingStatus is Charging, capacity is -1, Light does not change
164  * @tc.type: FUNC
165  */
166 HWTEST_F(BatteryLightTest, BatteryLight009, TestSize.Level1)
167 {
168     RETURN_IF(!g_light.isAvailable());
169     GTEST_LOG_(INFO) << "initial:" << g_light.GetLightColor();
170     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
171     // First turn on the light
172     EXPECT_TRUE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 91));
173     GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
174     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
175 
176     // Capacity invalid value, Not bright lights
177     EXPECT_FALSE(g_light.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, -1));
178     GTEST_LOG_(INFO) << "actual:" << g_light.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
179     EXPECT_EQ(g_light.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
180 }
181 } // namespace PowerMgr
182 } // namespace OHOS
183