1 /*
2 * Copyright (c) 2024 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 <functional>
17 #include <gtest/gtest.h>
18
19 #include "conditions/network_listener.h"
20 #include "work_scheduler_service.h"
21 #include "work_queue_manager.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "net_supplier_info.h"
25
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace WorkScheduler {
30 const int32_t DEFAULT_VALUE = -1;
31 const int32_t BEARER_CELLULAR = 0;
32 const int32_t BEARER_WIFI = 1;
33 const int32_t BEARER_BLUETOOTH = 2;
34 const int32_t BEARER_ETHERNET = 3;
35 const int32_t BEARER_WIFI_AWARE = 5;
36
37 class NetworkListenerTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
TearDownTestCase()40 static void TearDownTestCase() {};
SetUp()41 void SetUp() {};
TearDown()42 void TearDown() {};
43 static std::shared_ptr<WorkQueueManager> workQueueManager_;
44 static std::shared_ptr<NetworkListener> networkListener_;
45 };
46
47 std::shared_ptr<WorkQueueManager> NetworkListenerTest::workQueueManager_ = nullptr;
48 std::shared_ptr<NetworkListener> NetworkListenerTest::networkListener_ = nullptr;
49
SetUpTestCase()50 void NetworkListenerTest::SetUpTestCase()
51 {
52 std::shared_ptr<WorkSchedulerService> workSchedulerService_ = std::make_shared<WorkSchedulerService>();
53 workQueueManager_ = std::make_shared<WorkQueueManager>(workSchedulerService_);
54 networkListener_ = std::make_shared<NetworkListener>(workQueueManager_);
55 }
56
57 /**
58 * @tc.name: OnConditionChanged_001
59 * @tc.desc: Test networkListener OnConditionChanged.
60 * @tc.type: FUNC
61 * @tc.require: IAJSVG
62 */
63 HWTEST_F(NetworkListenerTest, OnConditionChanged_001, TestSize.Level1)
64 {
65 networkListener_->Start();
66 EventFwk::CommonEventData data;
67 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
68 bool ret = networkListener_->Stop();
69 EXPECT_TRUE(ret);
70 }
71
72 /**
73 * @tc.name: OnConditionChanged_002
74 * @tc.desc: Test networkListener OnConditionChanged.
75 * @tc.type: FUNC
76 * @tc.require: IAJSVG
77 */
78 HWTEST_F(NetworkListenerTest, OnConditionChanged_002, TestSize.Level1)
79 {
80 networkListener_->Start();
81 EventFwk::CommonEventData data;
82 EventFwk::Want want;
83 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
84 want.SetParam("NetType", BEARER_CELLULAR);
85 data.SetWant(want);
86 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
87 EventFwk::CommonEventManager::PublishCommonEvent(data);
88 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
89 bool ret = networkListener_->Stop();
90 EXPECT_TRUE(ret);
91 }
92
93 /**
94 * @tc.name: OnConditionChanged_003
95 * @tc.desc: Test networkListener OnConditionChanged.
96 * @tc.type: FUNC
97 * @tc.require: IAJSVG
98 */
99 HWTEST_F(NetworkListenerTest, OnConditionChanged_003, TestSize.Level1)
100 {
101 networkListener_->Start();
102 EventFwk::CommonEventData data;
103 EventFwk::Want want;
104 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
105 want.SetParam("NetType", BEARER_WIFI);
106 data.SetWant(want);
107 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
108 EventFwk::CommonEventManager::PublishCommonEvent(data);
109 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
110 bool ret = networkListener_->Stop();
111 EXPECT_TRUE(ret);
112 }
113
114 /**
115 * @tc.name: OnConditionChanged_004
116 * @tc.desc: Test networkListener OnConditionChanged.
117 * @tc.type: FUNC
118 * @tc.require: IAJSVG
119 */
120 HWTEST_F(NetworkListenerTest, OnConditionChanged_004, TestSize.Level1)
121 {
122 networkListener_->Start();
123 EventFwk::CommonEventData data;
124 EventFwk::Want want;
125 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
126 want.SetParam("NetType", BEARER_BLUETOOTH);
127 data.SetWant(want);
128 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
129 EventFwk::CommonEventManager::PublishCommonEvent(data);
130 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
131 bool ret = networkListener_->Stop();
132 EXPECT_TRUE(ret);
133 }
134
135 /**
136 * @tc.name: OnConditionChanged_005
137 * @tc.desc: Test networkListener OnConditionChanged.
138 * @tc.type: FUNC
139 * @tc.require: IAJSVG
140 */
141 HWTEST_F(NetworkListenerTest, OnConditionChanged_005, TestSize.Level1)
142 {
143 networkListener_->Start();
144 EventFwk::CommonEventData data;
145 EventFwk::Want want;
146 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
147 want.SetParam("NetType", BEARER_ETHERNET);
148 data.SetWant(want);
149 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
150 EventFwk::CommonEventManager::PublishCommonEvent(data);
151 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
152 bool ret = networkListener_->Stop();
153 EXPECT_TRUE(ret);
154 }
155
156 /**
157 * @tc.name: OnConditionChanged_006
158 * @tc.desc: Test networkListener OnConditionChanged.
159 * @tc.type: FUNC
160 * @tc.require: IAJSVG
161 */
162 HWTEST_F(NetworkListenerTest, OnConditionChanged_006, TestSize.Level1)
163 {
164 networkListener_->Start();
165 EventFwk::CommonEventData data;
166 EventFwk::Want want;
167 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
168 want.SetParam("NetType", BEARER_WIFI_AWARE);
169 data.SetWant(want);
170 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
171 EventFwk::CommonEventManager::PublishCommonEvent(data);
172 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
173 bool ret = networkListener_->Stop();
174 EXPECT_TRUE(ret);
175 }
176
177 /**
178 * @tc.name: OnConditionChanged_007
179 * @tc.desc: Test networkListener OnConditionChanged.
180 * @tc.type: FUNC
181 * @tc.require: IAJSVG
182 */
183 HWTEST_F(NetworkListenerTest, OnConditionChanged_007, TestSize.Level1)
184 {
185 networkListener_->Start();
186 EventFwk::CommonEventData data;
187 EventFwk::Want want;
188 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
189 want.SetParam("NetType", DEFAULT_VALUE);
190 data.SetWant(want);
191 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
192 EventFwk::CommonEventManager::PublishCommonEvent(data);
193 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
194 bool ret = networkListener_->Stop();
195 EXPECT_TRUE(ret);
196 }
197
198 /**
199 * @tc.name: OnConditionChanged_008
200 * @tc.desc: Test networkListener OnConditionChanged.
201 * @tc.type: FUNC
202 * @tc.require: IAJSVG
203 */
204 HWTEST_F(NetworkListenerTest, OnConditionChanged_008, TestSize.Level1)
205 {
206 networkListener_->Start();
207 EventFwk::CommonEventData data;
208 EventFwk::Want want;
209 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
210 data.SetWant(want);
211 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
212 EventFwk::CommonEventManager::PublishCommonEvent(data);
213 networkListener_->commonEventSubscriber->OnReceiveEvent(data);
214 bool ret = networkListener_->Stop();
215 EXPECT_TRUE(ret);
216 }
217 }
218 }