• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_adapter_impl.h"
23 #include "net_connect_utils.h"
24 #include "core_service_client.h"
25 #include "cellular_data_client.h"
26 #include "net_conn_client.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS;
31 using namespace OHOS::NWeb;
32 using namespace OHOS::Telephony;
33 
34 namespace OHOS {
35 namespace {
36     int32_t g_regNetConnCallback = 0;
37     int32_t g_unregNetConnCallback = 0;
38     int32_t g_getDefaultNet = 0;
39     int32_t g_getNetCap = 0;
40     int32_t g_slotId = 0;
41 }
42 
43 namespace NetManagerStandard {
RegisterNetConnCallback(const sptr<INetConnCallback> & callback)44 int32_t NetConnClient::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
45 {
46     return g_regNetConnCallback;
47 }
UnregisterNetConnCallback(const sptr<INetConnCallback> & callback)48 int32_t NetConnClient::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
49 {
50     return g_unregNetConnCallback;
51 }
GetDefaultNet(NetHandle & nethamdle)52 int32_t NetConnClient::GetDefaultNet(NetHandle &nethamdle)
53 {
54     return g_getDefaultNet;
55 }
GetNetCapabilities(const NetHandle & nethamdle,NetAllCapabilities & netAllCap)56 int32_t NetConnClient::GetNetCapabilities(const NetHandle &nethamdle, NetAllCapabilities &netAllCap)
57 {
58     netAllCap.bearerTypes_.insert(NetBearType::BEARER_WIFI);
59     netAllCap.bearerTypes_.insert(NetBearType::BEARER_CELLULAR);
60     return g_getNetCap;
61 }
62 }
63 
64 namespace Telephony {
GetDefaultCellularDataSlotId()65 int32_t CellularDataClient::GetDefaultCellularDataSlotId()
66 {
67     return g_slotId;
68 }
69 }
70 
71 namespace NWeb {
72 using namespace OHOS::NetManagerStandard;
73 class NetConnectAdapterImplTest : public testing::Test {
74 public:
75     static void SetUpTestCase(void);
76     static void TearDownTestCase(void);
77     void SetUp();
78     void TearDown();
79 };
80 
SetUpTestCase(void)81 void NetConnectAdapterImplTest::SetUpTestCase(void)
82 {}
83 
TearDownTestCase(void)84 void NetConnectAdapterImplTest::TearDownTestCase(void)
85 {}
86 
SetUp(void)87 void NetConnectAdapterImplTest::SetUp(void)
88 {}
89 
TearDown(void)90 void NetConnectAdapterImplTest::TearDown(void)
91 {}
92 
93 class NetConnCallbackTest : public NetConnCallback {
94 public:
95     NetConnCallbackTest() = default;
96     virtual ~NetConnCallbackTest() = default;
NetAvailable()97     int32_t NetAvailable() override
98     {
99         return 0;
100     }
NetCapabilitiesChange(const NetConnectType & netConnectType,const NetConnectSubtype & netConnectSubtype)101     int32_t NetCapabilitiesChange(const NetConnectType &netConnectType,
102         const NetConnectSubtype &netConnectSubtype) override
103     {
104         return 0;
105     }
NetConnectionPropertiesChange()106     int32_t NetConnectionPropertiesChange() override
107     {
108         return 0;
109     }
NetUnavailable()110     int32_t NetUnavailable() override
111     {
112         return 0;
113     }
114 };
115 
116 /**
117  * @tc.name: NetConnectAdapterImplTest_001.
118  * @tc.desc: test lock type.
119  * @tc.type: FUNC
120  * @tc.require:
121  */
122 HWTEST_F(NetConnectAdapterImplTest, NetConnectAdapterImplTest_001, TestSize.Level1)
123 {
124     std::shared_ptr<NetConnCallbackTest> cb = std::make_shared<NetConnCallbackTest>();
125     std::shared_ptr<NetConnectAdapterImpl> netConnectAdapterImpl = std::make_shared<NetConnectAdapterImpl>();
126     EXPECT_NE(netConnectAdapterImpl, nullptr);
127     g_regNetConnCallback = static_cast<int32_t>(NETMANAGER_SUCCESS);
128     EXPECT_EQ(netConnectAdapterImpl->RegisterNetConnCallback(cb), 0);
129     g_regNetConnCallback = -1;
130     EXPECT_EQ(netConnectAdapterImpl->RegisterNetConnCallback(cb), -1);
131     EXPECT_EQ(netConnectAdapterImpl->RegisterNetConnCallback(nullptr), -1);
132     g_unregNetConnCallback = static_cast<int32_t>(NETMANAGER_SUCCESS);
133     EXPECT_EQ(netConnectAdapterImpl->UnregisterNetConnCallback(cb), 0);
134     EXPECT_EQ(netConnectAdapterImpl->UnregisterNetConnCallback(cb), -1);
135 
136     g_regNetConnCallback = static_cast<int32_t>(NETMANAGER_SUCCESS);
137     EXPECT_EQ(netConnectAdapterImpl->RegisterNetConnCallback(cb), 0);
138     g_unregNetConnCallback = -1;
139     EXPECT_EQ(netConnectAdapterImpl->UnregisterNetConnCallback(cb), -1);
140     EXPECT_EQ(netConnectAdapterImpl->UnregisterNetConnCallback(nullptr), -1);
141 }
142 
143 /**
144  * @tc.name: NetConnectAdapterImplTest_002.
145  * @tc.desc: test lock type.
146  * @tc.type: FUNC
147  * @tc.require:
148  */
149 HWTEST_F(NetConnectAdapterImplTest, NetConnectAdapterImplTest_002, TestSize.Level1)
150 {
151     std::shared_ptr<NetConnectAdapterImpl> netConnectAdapterImpl = std::make_shared<NetConnectAdapterImpl>();
152     NetConnectType type = NetConnectType::CONNECTION_UNKNOWN;
153     NetConnectSubtype subtype = NetConnectSubtype::SUBTYPE_UNKNOWN;
154     g_getDefaultNet = static_cast<int32_t>(NETMANAGER_SUCCESS);
155     g_getNetCap = static_cast<int32_t>(NETMANAGER_SUCCESS);
156     EXPECT_EQ(netConnectAdapterImpl->GetDefaultNetConnect(type, subtype), 0);
157     g_slotId = -1;
158     EXPECT_EQ(netConnectAdapterImpl->GetDefaultNetConnect(type, subtype), 0);
159     g_getDefaultNet = -1;
160     EXPECT_EQ(netConnectAdapterImpl->GetDefaultNetConnect(type, subtype), -1);
161     g_getDefaultNet = static_cast<int32_t>(NETMANAGER_SUCCESS);
162     g_getNetCap = -1;
163     EXPECT_EQ(netConnectAdapterImpl->GetDefaultNetConnect(type, subtype), -1);
164 }
165 }
166 }