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 <memory>
17 
18 #include "gtest/gtest.h"
19 
20 #define private public
21 #include "distributed_device_callback.h"
22 
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace Notification {
26 class DistributedDeviceCallbackTest : public testing::Test {
27 public:
28     void SetUp() override;
29     void TearDown() override;
30 
31 public:
32     virtual void OnConnected(const std::string &deviceId);
33     virtual void OnDisconnected(const std::string &deviceId);
34 };
35 
SetUp()36 void DistributedDeviceCallbackTest::SetUp()
37 {}
38 
TearDown()39 void DistributedDeviceCallbackTest::TearDown()
40 {}
41 
OnConnected(const std::string & deviceId)42 void DistributedDeviceCallbackTest::OnConnected(const std::string &deviceId)
43 {
44     EXPECT_EQ(deviceId, "device1");
45 }
46 
OnDisconnected(const std::string & deviceId)47 void DistributedDeviceCallbackTest::OnDisconnected(const std::string &deviceId)
48 {
49     EXPECT_EQ(deviceId, "device2");
50 }
51 
52 /**
53  * @tc.name      : DistributedDeviceCallback_00100
54  * @tc.number    : DistributedDeviceCallback_00100
55  * @tc.desc      : test OnDeviceOnline function and callback_.OnConnected is nullptr .
56  */
57 HWTEST_F(DistributedDeviceCallbackTest, DistributedDeviceCallback_00100, Function | SmallTest | Level1)
58 {
59     DistributedDeviceCallback::IDeviceChange deviceCallback = {
60         .OnConnected = std::bind(&DistributedDeviceCallbackTest::OnConnected, this, std::placeholders::_1),
61         .OnDisconnected = std::bind(&DistributedDeviceCallbackTest::OnDisconnected, this, std::placeholders::_1),
62     };
63     std::shared_ptr<DistributedDeviceCallback> deviceCallback_ =
64         std::make_shared<DistributedDeviceCallback>(deviceCallback);
65     DistributedHardware::DmDeviceInfo info;
66     strcpy_s(info.deviceId, sizeof(info.deviceId) - 1, "device1");
67     deviceCallback_->OnDeviceOnline(info);
68 }
69 
70 /**
71  * @tc.name      : DistributedDeviceCallback_00200
72  * @tc.number    : DistributedDeviceCallback_00200
73  * @tc.desc      : test OnDeviceOffline function and callback_.OnDisconnected is nullptr .
74  */
75 HWTEST_F(DistributedDeviceCallbackTest, DistributedDeviceCallback_00200, Function | SmallTest | Level1)
76 {
77     DistributedDeviceCallback::IDeviceChange deviceCallback = {
78         .OnConnected = std::bind(&DistributedDeviceCallbackTest::OnConnected, this, std::placeholders::_1),
79         .OnDisconnected = std::bind(&DistributedDeviceCallbackTest::OnDisconnected, this, std::placeholders::_1),
80     };
81     std::shared_ptr<DistributedDeviceCallback> deviceCallback_ =
82         std::make_shared<DistributedDeviceCallback>(deviceCallback);
83     DistributedHardware::DmDeviceInfo info;
84     strcpy_s(info.deviceId, sizeof(info.deviceId) - 1, "device2");
85     deviceCallback_->OnDeviceOffline(info);
86     deviceCallback_->OnDeviceChanged(info);
87     deviceCallback_->OnDeviceReady(info);
88 }
89 }  // namespace Notification
90 }  // namespace OHOS