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 #define private public
18 #include "ffrt.h"
19 #include "net_policy_core.h"
20 #include "net_policy_event_handler.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace testing::ext;
26 constexpr const char *NET_POLICY_WORK_TEST_THREAD = "NET_POLICY_WORK_TEST_THREAD";
27 constexpr const char *NET_POLICY_STUB_QUEUE = "NET_POLICY_STUB_QUEUE";
28 std::shared_ptr<AppExecFwk::EventRunner> g_runner = nullptr;
29 std::shared_ptr<NetPolicyCore> g_netPolicyCore = nullptr;
30 ffrt::queue ffrtQueue_ = NET_POLICY_STUB_QUEUE;
31 } // namespace
32
33 class UtNetPolicyEventHandlerTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 static inline std::shared_ptr<NetPolicyEventHandler> instance_ = nullptr;
40 };
41
SetUpTestCase()42 void UtNetPolicyEventHandlerTest::SetUpTestCase()
43 {
44 g_runner = AppExecFwk::EventRunner::Create(NET_POLICY_WORK_TEST_THREAD);
45 g_netPolicyCore = DelayedSingleton<NetPolicyCore>::GetInstance();
46 instance_ = std::make_shared<NetPolicyEventHandler>(g_netPolicyCore, ffrtQueue_);
47 }
48
TearDownTestCase()49 void UtNetPolicyEventHandlerTest::TearDownTestCase() {}
50
SetUp()51 void UtNetPolicyEventHandlerTest::SetUp() {}
52
TearDown()53 void UtNetPolicyEventHandlerTest::TearDown() {}
54
55 HWTEST_F(UtNetPolicyEventHandlerTest, ProcessEventTest001, TestSize.Level1)
56 {
57 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get();
58 auto eventId = static_cast<int32_t>(event->GetInnerEventId());
59 auto eventData = event->GetSharedObject<PolicyEvent>();
60 event.reset();
61 instance_->ProcessEvent(eventId, eventData);
62 ASSERT_NE(instance_->core_, nullptr);
63 }
64
65 HWTEST_F(UtNetPolicyEventHandlerTest, ProcessEventTest002, TestSize.Level1)
66 {
67 std::shared_ptr<NetPolicyCore> netPolicyCore = nullptr;
68 auto instance = std::make_shared<NetPolicyEventHandler>(g_netPolicyCore, ffrtQueue_);
69 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get();
70 auto eventId = static_cast<int32_t>(event->GetInnerEventId());
71 auto eventData = event->GetSharedObject<PolicyEvent>();
72 instance->ProcessEvent(eventId, eventData);
73 ASSERT_NE(instance_->core_, nullptr);
74 }
75
76 HWTEST_F(UtNetPolicyEventHandlerTest, ProcessEventTest003, TestSize.Level1)
77 {
78 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get();
79 auto eventId = static_cast<int32_t>(event->GetInnerEventId());
80 auto eventData = event->GetSharedObject<PolicyEvent>();
81 instance_->ProcessEvent(eventId, eventData);
82 ASSERT_NE(instance_->core_, nullptr);
83 }
84
85 HWTEST_F(UtNetPolicyEventHandlerTest, ProcessEventTest004, TestSize.Level1)
86 {
87 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get();
88 auto eventId = static_cast<int32_t>(event->GetInnerEventId());
89 auto eventData = event->GetSharedObject<PolicyEvent>();
90 instance_->core_ = nullptr;
91 instance_->ProcessEvent(eventId, eventData);
92 ASSERT_EQ(instance_->core_, nullptr);
93 }
94 } // namespace NetManagerStandard
95 } // namespace OHOS