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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "dfs_error.h"
20 #include "net_conn_callback_observer.h"
21 
22 namespace OHOS::FileManagement::CloudSync::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 
27 class NetConnCallbackObserverTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33     std::shared_ptr<NetConnCallbackObserver> oberverPtr_;
34 };
SetUpTestCase(void)35 void NetConnCallbackObserverTest::SetUpTestCase(void)
36 {
37     GTEST_LOG_(INFO) << "SetUpTestCase";
38 }
39 
TearDownTestCase(void)40 void NetConnCallbackObserverTest::TearDownTestCase(void)
41 {
42     GTEST_LOG_(INFO) << "TearDownTestCase";
43 }
44 
SetUp(void)45 void NetConnCallbackObserverTest::SetUp(void)
46 {
47     if (oberverPtr_ == nullptr) {
48         auto dataSyncManager = std::make_shared<DataSyncManager>();
49         oberverPtr_ = make_shared<NetConnCallbackObserver>(dataSyncManager);
50         ASSERT_TRUE(oberverPtr_ != nullptr) << "CallbackObserver failed";
51     }
52     GTEST_LOG_(INFO) << "SetUp";
53 }
54 
TearDown(void)55 void NetConnCallbackObserverTest::TearDown(void)
56 {
57     oberverPtr_ = nullptr;
58     GTEST_LOG_(INFO) << "TearDown";
59 }
60 
61 /**
62  * @tc.name: NetAvailableTest
63  * @tc.desc: Verify the NetAvailable function
64  * @tc.type: FUNC
65  * @tc.require: I6JPKG
66  */
67 HWTEST_F(NetConnCallbackObserverTest, NetAvailable, TestSize.Level1)
68 {
69     GTEST_LOG_(INFO) << "NetAvailable Start";
70     sptr<NetManagerStandard::NetHandle> netHandle = sptr(new NetManagerStandard::NetHandle());
71     int res = oberverPtr_->NetAvailable(netHandle);
72     EXPECT_EQ(res, E_OK);
73     GTEST_LOG_(INFO) << "NetAvailable End";
74 }
75 
76 /**
77  * @tc.name: NetCapabilitiesChangeTest
78  * @tc.desc: Verify the NetCapabilitiesChange function
79  * @tc.type: FUNC
80  * @tc.require: I6JPKG
81  */
82 HWTEST_F(NetConnCallbackObserverTest, NetCapabilitiesChange, TestSize.Level1)
83 {
84     GTEST_LOG_(INFO) << "NetCapabilitiesChange Start";
85     sptr<NetManagerStandard::NetHandle> netHandle = sptr(new NetManagerStandard::NetHandle());
86     sptr<NetManagerStandard::NetAllCapabilities> netAllCap = sptr(new NetManagerStandard::NetAllCapabilities());
87     int res = oberverPtr_->NetCapabilitiesChange(netHandle, netAllCap);
88     EXPECT_EQ(res, E_OK);
89     GTEST_LOG_(INFO) << "NetCapabilitiesChange End";
90 }
91 
92 /**
93  * @tc.name: NetConnectionPropertiesChangeTest
94  * @tc.desc: Verify the NetConnectionPropertiesChange function
95  * @tc.type: FUNC
96  * @tc.require: I6JPKG
97  */
98 HWTEST_F(NetConnCallbackObserverTest, NetConnectionPropertiesChange, TestSize.Level1)
99 {
100     GTEST_LOG_(INFO) << "NetConnectionPropertiesChange Start";
101     sptr<NetManagerStandard::NetHandle> netHandle = sptr(new NetManagerStandard::NetHandle());
102     sptr<NetManagerStandard::NetLinkInfo> info = sptr(new NetManagerStandard::NetLinkInfo());
103     int res = oberverPtr_->NetConnectionPropertiesChange(netHandle, info);
104     EXPECT_EQ(res, E_OK);
105     GTEST_LOG_(INFO) << "NetConnectionPropertiesChange End";
106 }
107 
108 /**
109  * @tc.name: NetLostTest
110  * @tc.desc: Verify the NetLost function
111  * @tc.type: FUNC
112  * @tc.require: I6JPKG
113  */
114 HWTEST_F(NetConnCallbackObserverTest, NetLost, TestSize.Level1)
115 {
116     GTEST_LOG_(INFO) << "NetLost Start";
117     sptr<NetManagerStandard::NetHandle> netHandle = sptr(new NetManagerStandard::NetHandle());
118     int res = oberverPtr_->NetLost(netHandle);
119     EXPECT_EQ(res, E_OK);
120     GTEST_LOG_(INFO) << "NetLost End";
121 }
122 
123 /**
124  * @tc.name: NetUnavailableTest
125  * @tc.desc: Verify the NetUnavailable function
126  * @tc.type: FUNC
127  * @tc.require: I6JPKG
128  */
129 HWTEST_F(NetConnCallbackObserverTest, NetUnavailable, TestSize.Level1)
130 {
131     GTEST_LOG_(INFO) << "NetUnavailable Start";
132     int res = oberverPtr_->NetUnavailable();
133     EXPECT_EQ(res, E_OK);
134     GTEST_LOG_(INFO) << "NetUnavailable End";
135 }
136 
137 /**
138  * @tc.name: NetBlockStatusChangeTest
139  * @tc.desc: Verify the NetBlockStatusChange function
140  * @tc.type: FUNC
141  * @tc.require: I6JPKG
142  */
143 HWTEST_F(NetConnCallbackObserverTest, NetBlockStatusChange, TestSize.Level1)
144 {
145     GTEST_LOG_(INFO) << "NetBlockStatusChange Start";
146     sptr<NetManagerStandard::NetHandle> netHandle = sptr(new NetManagerStandard::NetHandle());
147     bool blocked = true;
148     int res = oberverPtr_->NetBlockStatusChange(netHandle, blocked);
149     EXPECT_EQ(res, E_OK);
150     GTEST_LOG_(INFO) << "NetBlockStatusChange End";
151 }
152 }