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
16 #include <gtest/gtest.h>
17 #include <securec.h>
18
19 #include "lnn_net_ledger_mock.h"
20 #include "softbus_errcode.h"
21 #include "lnn_battery_info.c"
22 #include "lnn_battery_info.h"
23 #include "lnn_sync_info_mock.h"
24
25 #define TEST_VALID_PEER_NETWORKID "12345678"
26 #define TEST_VALID_UDID_LEN 32
27
28 constexpr int32_t LEVEL = 10;
29 constexpr char UDID1[] = "123456789AB";
30 constexpr uint8_t MSG1[] = "{\"BatteryLeavel\":123,\"IsCharging\":true}";
31 constexpr uint8_t MSG2[] = "{\"IsCharging\":true}";
32 constexpr uint8_t MSG3[] = "{\"BatteryLeavel\":123}";
33 constexpr char NETWORKID[] = "networkIdTest";
34
35 namespace OHOS {
36 using namespace testing;
37 using namespace testing::ext;
38
39 class LNNBatteryInfoTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 };
46
SetUpTestCase()47 void LNNBatteryInfoTest::SetUpTestCase()
48 {
49 }
50
TearDownTestCase()51 void LNNBatteryInfoTest::TearDownTestCase()
52 {
53 }
54
SetUp()55 void LNNBatteryInfoTest::SetUp()
56 {
57 }
58
TearDown()59 void LNNBatteryInfoTest::TearDown()
60 {
61 }
62
63 /*
64 * @tc.name: LNN_ON_RECEIVE_BATTERY_INFO_TEST_001
65 * @tc.desc: test OnReceiveBatteryInfo
66 * @tc.type: FUNC
67 * @tc.require:
68 */
69 HWTEST_F(LNNBatteryInfoTest, LNN_ON_RECEIVE_BATTERY_INFO_TEST_001, TestSize.Level1)
70 {
71 OnReceiveBatteryInfo(LNN_INFO_TYPE_DEVICE_NAME, nullptr, nullptr, TEST_VALID_UDID_LEN);
72 }
73
74 /*
75 * @tc.name: LNN_ON_RECEIVE_BATTERY_INFO_TEST_002
76 * @tc.desc: test OnReceiveBatteryInfo
77 * @tc.type: FUNC
78 * @tc.require:
79 */
80 HWTEST_F(LNNBatteryInfoTest, LNN_ON_RECEIVE_BATTERY_INFO_TEST_002, TestSize.Level1)
81 {
82 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, nullptr, nullptr, TEST_VALID_UDID_LEN);
83 }
84
85 /*
86 * @tc.name: LNN_SYNC_BATTERY_INFO_TEST_001
87 * @tc.desc: test LnnSyncBatteryInfo
88 * @tc.type: FUNC
89 * @tc.require:
90 */
91 HWTEST_F(LNNBatteryInfoTest, LNN_SYNC_BATTERY_INFO_TEST_001, TestSize.Level1)
92 {
93 NiceMock<LnnNetLedgertInterfaceMock> ledgerMock;
94 EXPECT_CALL(ledgerMock, LnnGetRemoteNodeInfoById)
95 .WillOnce(Return(SOFTBUS_ERR))
96 .WillRepeatedly(Return(SOFTBUS_OK));
97 NiceMock<LnnSyncInfoInterfaceMock> SyncInfoMock;
98 EXPECT_CALL(SyncInfoMock, LnnSendSyncInfoMsg).WillRepeatedly(Return(SOFTBUS_OK));
99 int32_t ret = LnnSyncBatteryInfo(UDID1, LEVEL, true);
100 EXPECT_EQ(SOFTBUS_ERR, ret);
101 ret = LnnSyncBatteryInfo(UDID1, LEVEL, true);
102 EXPECT_EQ(SOFTBUS_OK, ret);
103 }
104
105 /*
106 * @tc.name: ON_RECEIVE_BATTERY_INFO_TEST_001
107 * @tc.desc: test OnReceiveBatteryInfo
108 * @tc.type: FUNC
109 * @tc.require:
110 */
111 HWTEST_F(LNNBatteryInfoTest, ON_RECEIVE_BATTERY_INFO_TEST_001, TestSize.Level1)
112 {
113 NiceMock<LnnNetLedgertInterfaceMock> netLedgerMock;
114 const char *networkId = NETWORKID;
115 OnReceiveBatteryInfo(LNN_INFO_TYPE_DEVICE_NAME, networkId, nullptr, 0);
116 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, nullptr, 0);
117 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, MSG1, 0);
118 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, MSG2, 0);
119 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, MSG3, 0);
120 }
121 } // namespace OHOS
122