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_led_test.h"
17 #include "battery_config.h"
18 #include "battery_config_test.h"
19 #include "battery_led.h"
20
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Battery {
26 namespace V1_1 {
27 namespace {
28 constexpr const char* VENDOR_BATTERY_CONFIG_PATH = "/vendor/etc/battery/battery_config.json";
29 BatteryLed g_led;
30 }
SetUpTestCase(void)31 void BatteryLedTest::SetUpTestCase(void)
32 {
33 BatteryConfig::GetInstance().ParseConfig(VENDOR_BATTERY_CONFIG_PATH);
34 g_led.InitLight();
35
36 GTEST_LOG_(INFO) << "available battery light " << g_led.IsAvailable();
37 }
38
TearDown(void)39 void BatteryLedTest::TearDown(void)
40 {
41 g_led.TurnOff();
42 }
43
44 /**
45 * @tc.name: BatteryLight001
46 * @tc.desc: ChargingStatus is Discharging, Not bright lights
47 * @tc.type: FUNC
48 */
49 HWTEST_F (BatteryLedTest, BatteryLight001, TestSize.Level1)
50 {
51 if (!g_led.IsAvailable()) {
52 return;
53 }
54 GTEST_LOG_(INFO) << "initial:" << g_led.GetLightColor();
55 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
56 // First turn on the light
57 ASSERT_TRUE(g_led.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 11));
58 GTEST_LOG_(INFO) << "actual:" << g_led.GetLightColor() << "=expect:" << BatteryConfigTest::YELLOW_LIGHT;
59 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::YELLOW_LIGHT);
60
61 ASSERT_FALSE(g_led.UpdateColor(BatteryChargeState::CHARGE_STATE_NONE, 0));
62 GTEST_LOG_(INFO) << "actual:" << g_led.GetLightColor() << "=expect:" << BatteryConfigTest::LIGHT_OFF;
63 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
64 }
65
66 /**
67 * @tc.name: BatteryLight002
68 * @tc.desc: ChargingStatus is Charging, capacity is 9, the red light on
69 * @tc.type: FUNC
70 */
71 HWTEST_F (BatteryLedTest, BatteryLight002, TestSize.Level1)
72 {
73 if (!g_led.IsAvailable()) {
74 return;
75 }
76 GTEST_LOG_(INFO) << "initial:" << g_led.GetLightColor();
77 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
78 ASSERT_TRUE(g_led.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 9));
79 GTEST_LOG_(INFO) << "actual:" << g_led.GetLightColor() << "=expect:" << BatteryConfigTest::RED_LIGHT;
80 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::RED_LIGHT);
81 }
82
83 /**
84 * @tc.name: BatteryLight003
85 * @tc.desc: ChargingStatus is Charging, capacity is 89, the yellow light on
86 * @tc.type: FUNC
87 */
88 HWTEST_F (BatteryLedTest, BatteryLight003, TestSize.Level1)
89 {
90 if (!g_led.IsAvailable()) {
91 return;
92 }
93 GTEST_LOG_(INFO) << "initial:" << g_led.GetLightColor();
94 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
95 ASSERT_TRUE(g_led.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 89));
96 GTEST_LOG_(INFO) << "actual:" << g_led.GetLightColor() << "=expect:" << BatteryConfigTest::YELLOW_LIGHT;
97 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::YELLOW_LIGHT);
98 }
99
100 /**
101 * @tc.name: BatteryLight004
102 * @tc.desc: ChargingStatus is Charging, capacity is 100, the green light on
103 * @tc.type: FUNC
104 */
105 HWTEST_F (BatteryLedTest, BatteryLight004, TestSize.Level1)
106 {
107 if (!g_led.IsAvailable()) {
108 return;
109 }
110 GTEST_LOG_(INFO) << "initial:" << g_led.GetLightColor();
111 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
112 ASSERT_TRUE(g_led.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 100));
113 GTEST_LOG_(INFO) << "actual:" << g_led.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
114 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
115 }
116
117 /**
118 * @tc.name: BatteryLight005
119 * @tc.desc: ChargingStatus is Charging, capacity is -1, Light does not change
120 * @tc.type: FUNC
121 */
122 HWTEST_F (BatteryLedTest, BatteryLight005, TestSize.Level1)
123 {
124 if (!g_led.IsAvailable()) {
125 return;
126 }
127 GTEST_LOG_(INFO) << "initial:" << g_led.GetLightColor();
128 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::LIGHT_OFF);
129 // First turn on the light
130 ASSERT_TRUE(g_led.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, 91));
131 GTEST_LOG_(INFO) << "actual:" << g_led.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
132 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
133
134 // Capacity invalid value, Not bright lights
135 ASSERT_FALSE(g_led.UpdateColor(BatteryChargeState::CHARGE_STATE_ENABLE, -1));
136 GTEST_LOG_(INFO) << "actual:" << g_led.GetLightColor() << "=expect:" << BatteryConfigTest::GREEN_LIGHT;
137 ASSERT_EQ(g_led.GetLightColor(), BatteryConfigTest::GREEN_LIGHT);
138 }
139 } // namespace V1_1
140 } // namespace Battery
141 } // namespace HDI
142 } // namespace OHOS
143