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 <gtest/gtest.h>
17
18 #include "core_service_client.h"
19 #include "cellular_data_client.h"
20
21 #define private public
22 #include "net_connect_callback_impl.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::NWeb;
28 using namespace OHOS::Telephony;
29
30 namespace OHOS {
31 namespace Telephony {
32 namespace {
33 int32_t g_slotId = 0;
34 }
35
GetDefaultCellularDataSlotId()36 int32_t CellularDataClient::GetDefaultCellularDataSlotId()
37 {
38 return g_slotId;
39 }
40 }
41 namespace NWeb {
42 using namespace OHOS::NetManagerStandard;
43 class NetConnectCallbackImplTest : public testing::Test {
44 public:
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase(void)51 void NetConnectCallbackImplTest::SetUpTestCase(void)
52 {}
53
TearDownTestCase(void)54 void NetConnectCallbackImplTest::TearDownTestCase(void)
55 {}
56
SetUp(void)57 void NetConnectCallbackImplTest::SetUp(void)
58 {}
59
TearDown(void)60 void NetConnectCallbackImplTest::TearDown(void)
61 {}
62
63 class NetConnCallbackTest : public NetConnCallback {
64 public:
65 NetConnCallbackTest() = default;
66 virtual ~NetConnCallbackTest() = default;
NetAvailable()67 int32_t NetAvailable() override
68 {
69 return 0;
70 }
NetCapabilitiesChange(const NetConnectType & netConnectType,const NetConnectSubtype & netConnectSubtype)71 int32_t NetCapabilitiesChange(const NetConnectType &netConnectType,
72 const NetConnectSubtype &netConnectSubtype) override
73 {
74 return 0;
75 }
NetConnectionPropertiesChange()76 int32_t NetConnectionPropertiesChange() override
77 {
78 return 0;
79 }
NetUnavailable()80 int32_t NetUnavailable() override
81 {
82 return 0;
83 }
84 };
85
86 /**
87 * @tc.name: NetConnectCallbackImplTest_001.
88 * @tc.desc: test lock type.
89 * @tc.type: FUNC
90 * @tc.require:
91 */
92 HWTEST_F(NetConnectCallbackImplTest, NetConnectCallbackImplTest_001, TestSize.Level1)
93 {
94 std::shared_ptr<NetConnCallbackTest> cb = std::make_shared<NetConnCallbackTest>();
95 std::shared_ptr<NetConnectCallbackImpl> netConnectCallbackImpl = std::make_shared<NetConnectCallbackImpl>(cb);
96 EXPECT_NE(netConnectCallbackImpl, nullptr);
97 sptr<NetHandle> netHandle(new NetHandle);
98 sptr<NetHandle> netHandleNull(nullptr);
99 sptr<NetLinkInfo> info(new NetLinkInfo);
100 sptr<NetLinkInfo> infoNull(nullptr);
101 EXPECT_EQ(netConnectCallbackImpl->NetAvailable(netHandle), 0);
102 EXPECT_EQ(netConnectCallbackImpl->NetAvailable(netHandleNull), 0);
103 EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandle, info), 0);
104 EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandleNull, infoNull), 0);
105 EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandle, infoNull), 0);
106 EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandleNull, info), 0);
107 EXPECT_EQ(netConnectCallbackImpl->NetLost(netHandle), 0);
108 EXPECT_EQ(netConnectCallbackImpl->NetUnavailable(), 0);
109
110 sptr<NetAllCapabilities> netAllCap(new NetAllCapabilities);
111 netAllCap->bearerTypes_.insert(NetBearType::BEARER_WIFI);
112 netAllCap->bearerTypes_.insert(NetBearType::BEARER_BLUETOOTH);
113 netAllCap->bearerTypes_.insert(NetBearType::BEARER_ETHERNET);
114 netAllCap->bearerTypes_.insert(NetBearType::BEARER_CELLULAR);
115 EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, netAllCap), 0);
116 g_slotId = -1;
117 EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, netAllCap), 0);
118 EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, nullptr), 0);
119 EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandleNull, netAllCap), 0);
120 EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandleNull, nullptr), 0);
121 }
122
123 /**
124 * @tc.name: NetConnectCallbackImplTest_002.
125 * @tc.desc: test lock type.
126 * @tc.type: FUNC
127 * @tc.require:
128 */
129 HWTEST_F(NetConnectCallbackImplTest, NetConnectCallbackImplTest_002, TestSize.Level1)
130 {
131 std::shared_ptr<NetConnectCallbackImpl> netConnectCallbackImpl = std::make_shared<NetConnectCallbackImpl>(nullptr);
132 sptr<NetHandle> netHandle(new NetHandle);
133 sptr<NetLinkInfo> info(new NetLinkInfo);
134 EXPECT_EQ(netConnectCallbackImpl->NetAvailable(netHandle), 0);
135 EXPECT_EQ(netConnectCallbackImpl->NetConnectionPropertiesChange(netHandle, info), 0);
136 EXPECT_EQ(netConnectCallbackImpl->NetLost(netHandle), 0);
137 EXPECT_EQ(netConnectCallbackImpl->NetUnavailable(), 0);
138 EXPECT_EQ(netConnectCallbackImpl->NetBlockStatusChange(netHandle, false), 0);
139 sptr<NetAllCapabilities> netAllCap(new NetAllCapabilities);
140 netAllCap->bearerTypes_.insert(NetBearType::BEARER_WIFI);
141 EXPECT_EQ(netConnectCallbackImpl->NetCapabilitiesChange(netHandle, netAllCap), 0);
142 }
143 }
144 }
145