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 <gtest/gtest.h>
17 #include <iostream>
18 #include <memory>
19 #define private public
20 #include "net_detection_callback_stub.h"
21 #undef private
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25 using namespace testing::ext;
26 class MockNetDetectionCallbackStubTest : public NetDetectionCallbackStub {
27 public:
28 MockNetDetectionCallbackStubTest() = default;
~MockNetDetectionCallbackStubTest()29 ~MockNetDetectionCallbackStubTest() override {}
OnNetDetectionResultChanged(NetDetectionResultCode detectionResult,const std::string & urlRedirect)30 int32_t OnNetDetectionResultChanged(NetDetectionResultCode detectionResult, const std::string &urlRedirect) override
31 {
32 std::cout << std::endl;
33 std::cout << "OnNetDetectionResultChanged" << std::endl;
34 return 0;
35 }
36 };
37
38 class NetDetectionCallbackStubTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44
45 static inline std::shared_ptr<NetDetectionCallbackStub> instance_ =
46 std::make_shared<MockNetDetectionCallbackStubTest>();
47 };
48
SetUpTestCase()49 void NetDetectionCallbackStubTest::SetUpTestCase() {}
50
TearDownTestCase()51 void NetDetectionCallbackStubTest::TearDownTestCase() {}
52
SetUp()53 void NetDetectionCallbackStubTest::SetUp() {}
54
TearDown()55 void NetDetectionCallbackStubTest::TearDown() {}
56
57 /**
58 * @tc.name: OnRemoteRequest001
59 * @tc.desc: Test NetDetectionCallbackStub OnRemoteRequest.
60 * @tc.type: FUNC
61 */
62 HWTEST_F(NetDetectionCallbackStubTest, OnRemoteRequest001, TestSize.Level1)
63 {
64 MessageParcel data;
65 MessageParcel reply;
66 MessageOption option;
67 int32_t ret = instance_->OnRemoteRequest(100, data, reply, option);
68 EXPECT_EQ(ret, NETMANAGER_ERR_DESCRIPTOR_MISMATCH);
69
70 data.WriteInterfaceToken(NetDetectionCallbackStub::GetDescriptor());
71 ret = instance_->OnRemoteRequest(100, data, reply, option);
72 EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR);
73 }
74
75 /**
76 * @tc.name: OnNetDetectionResult001
77 * @tc.desc: Test NetDetectionCallbackStub OnNetDetectionResult.
78 * @tc.type: FUNC
79 */
80 HWTEST_F(NetDetectionCallbackStubTest, OnNetDetectionResult001, TestSize.Level1)
81 {
82 MessageParcel data;
83 MessageParcel reply;
84 MessageOption option;
85 data.WriteInterfaceToken(NetDetectionCallbackStub::GetDescriptor());
86 data.WriteString("test");
87 data.WriteInt32(NET_DETECTION_SUCCESS);
88 int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(DetectionCallback::NET_DETECTION_RESULT),
89 data, reply, option);
90 EXPECT_EQ(ret, NETSYS_SUCCESS);
91 }
92 } // namespace NetManagerStandard
93 } // namespace OHOS