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_battery_info.c"
20 #include "lnn_battery_info.h"
21 #include "lnn_net_ledger_mock.h"
22 #include "lnn_sync_info_mock.h"
23 #include "softbus_error_code.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
TearDownTestCase()49 void LNNBatteryInfoTest::TearDownTestCase() { }
50
SetUp()51 void LNNBatteryInfoTest::SetUp() { }
52
TearDown()53 void LNNBatteryInfoTest::TearDown() { }
54
55 /*
56 * @tc.name: LNN_SYNC_BATTERY_INFO_TEST_001
57 * @tc.desc: test LnnSyncBatteryInfo
58 * @tc.type: FUNC
59 * @tc.require:
60 */
61 HWTEST_F(LNNBatteryInfoTest, LNN_SYNC_BATTERY_INFO_TEST_001, TestSize.Level1)
62 {
63 NiceMock<LnnNetLedgertInterfaceMock> ledgerMock;
64 EXPECT_CALL(ledgerMock, LnnGetRemoteNodeInfoById)
65 .WillOnce(Return(SOFTBUS_NETWORK_GET_NODE_INFO_ERR))
66 .WillRepeatedly(Return(SOFTBUS_OK));
67 NiceMock<LnnSyncInfoInterfaceMock> SyncInfoMock;
68 EXPECT_CALL(SyncInfoMock, LnnSendSyncInfoMsg).WillRepeatedly(Return(SOFTBUS_OK));
69 int32_t ret = LnnSyncBatteryInfo(UDID1, LEVEL, true);
70 EXPECT_EQ(SOFTBUS_NETWORK_GET_NODE_INFO_ERR, ret);
71 ret = LnnSyncBatteryInfo(UDID1, LEVEL, true);
72 EXPECT_EQ(SOFTBUS_OK, ret);
73 }
74
75 /*
76 * @tc.name: ON_RECEIVE_BATTERY_INFO_TEST_001
77 * @tc.desc: test OnReceiveBatteryInfo
78 * @tc.type: FUNC
79 * @tc.require:
80 */
81 HWTEST_F(LNNBatteryInfoTest, ON_RECEIVE_BATTERY_INFO_TEST_001, TestSize.Level1)
82 {
83 NodeInfo nodeInfo;
84 NiceMock<LnnNetLedgertInterfaceMock> netLedgerMock;
85 EXPECT_CALL(netLedgerMock, LnnGetNodeInfoById).WillRepeatedly(Return(&nodeInfo));
86 const char *networkId = NETWORKID;
87 OnReceiveBatteryInfo(LNN_INFO_TYPE_DEVICE_NAME, nullptr, nullptr, TEST_VALID_UDID_LEN);
88 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, nullptr, nullptr, TEST_VALID_UDID_LEN);
89 OnReceiveBatteryInfo(LNN_INFO_TYPE_DEVICE_NAME, networkId, nullptr, 0);
90 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, nullptr, 0);
91 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, MSG1, 0);
92 EXPECT_NE(nodeInfo.batteryInfo.isCharging, true);
93 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, MSG2, 0);
94 EXPECT_NE(nodeInfo.batteryInfo.isCharging, true);
95 OnReceiveBatteryInfo(LNN_INFO_TYPE_BATTERY_INFO, networkId, MSG3, 0);
96 EXPECT_NE(nodeInfo.batteryInfo.isCharging, true);
97 }
98 } // namespace OHOS
99