1 /*
2 * Copyright (c) 2023-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 #include <gtest/gtest.h>
16 #include <thread>
17
18 #include "nfc_controller_callback_stub.h"
19 #include "nfc_service_ipc_interface_code.h"
20
21 namespace OHOS {
22 namespace NFC {
23 namespace TEST {
24 using namespace testing::ext;
25 using namespace OHOS::NFC::KITS;
26 class NfcControllerCallBackStubTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp();
31 void TearDown();
32 };
33
34 class INfcControllerCallbackImpl : public INfcControllerCallback {
35 public:
INfcControllerCallbackImpl()36 INfcControllerCallbackImpl() {}
37
~INfcControllerCallbackImpl()38 virtual ~INfcControllerCallbackImpl() {}
39
40 public:
OnNfcStateChanged(int nfcState)41 void OnNfcStateChanged(int nfcState) override
42 {
43 }
44
AsObject()45 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
46 {
47 return nullptr;
48 }
49 };
50
SetUpTestCase()51 void NfcControllerCallBackStubTest::SetUpTestCase()
52 {
53 std::cout << " SetUpTestCase NfcControllerCallBackStubTest." << std::endl;
54 }
55
TearDownTestCase()56 void NfcControllerCallBackStubTest::TearDownTestCase()
57 {
58 std::cout << " TearDownTestCase NfcControllerCallBackStubTest." << std::endl;
59 }
60
SetUp()61 void NfcControllerCallBackStubTest::SetUp()
62 {
63 std::cout << " SetUp NfcControllerCallBackStubTest." << std::endl;
64 }
65
TearDown()66 void NfcControllerCallBackStubTest::TearDown()
67 {
68 std::cout << " TearDown NfcControllerCallBackStubTest." << std::endl;
69 }
70
71 /**
72 * @tc.name: RegisterCallBack001
73 * @tc.desc: Test NfcControllerCallBackStub RegisterCallBack.
74 * @tc.type: FUNC
75 */
76 HWTEST_F(NfcControllerCallBackStubTest, RegisterCallBack001, TestSize.Level1)
77 {
78 const sptr<INfcControllerCallback> callBack = nullptr;
79 NfcControllerCallBackStub& ctrl = NfcControllerCallBackStub::GetInstance();
80 KITS::ErrorCode registerCallBack = ctrl.RegisterCallBack(callBack);
81 ASSERT_TRUE(registerCallBack == KITS::ERR_NFC_PARAMETERS);
82 }
83 /**
84 * @tc.name: RegisterCallBack002
85 * @tc.desc: Test NfcControllerCallBackStub RegisterCallBack.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(NfcControllerCallBackStubTest, RegisterCallBack002, TestSize.Level1)
89 {
90 NfcControllerCallBackStub& ctrl = NfcControllerCallBackStub::GetInstance();
91 const sptr<NfcControllerCallBackStub> g_nfcControllerCallbackStub =
92 sptr<NfcControllerCallBackStub>(new (std::nothrow) NfcControllerCallBackStub());
93 KITS::ErrorCode registerCallBack = ctrl.RegisterCallBack(g_nfcControllerCallbackStub);
94 ASSERT_TRUE(registerCallBack == KITS::ERR_NONE);
95 }
96 /**
97 * @tc.name: OnRemoteRequest001
98 * @tc.desc: Test NfcControllerCallBackStub OnRemoteRequest.
99 * @tc.type: FUNC
100 */
101 HWTEST_F(NfcControllerCallBackStubTest, OnRemoteRequest001, TestSize.Level1)
102 {
103 uint32_t code = static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_NOTIFY);
104 MessageParcel data;
105 MessageParcel reply;
106 MessageOption option;
107 NfcControllerCallBackStub& ctrl = NfcControllerCallBackStub::GetInstance();
108 int onRemoteRequest = ctrl.OnRemoteRequest(code, data, reply, option);
109 ASSERT_TRUE(onRemoteRequest == KITS::ERR_NFC_PARAMETERS);
110 }
111
112 /**
113 * @tc.name: OnRemoteRequest002
114 * @tc.desc: Test NfcControllerCallBackStub OnRemoteRequest.
115 * @tc.type: FUNC
116 */
117 HWTEST_F(NfcControllerCallBackStubTest, OnRemoteRequest002, TestSize.Level1)
118 {
119 uint32_t code = static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_NOTIFY);
120 MessageParcel data;
121 MessageParcel reply;
122 MessageOption option;
123 std::u16string descriptor = u"ohos.nfc.kits.INfcControllerCallback";
124 data.WriteInterfaceToken(descriptor);
125 data.WriteInt32(1);
126 NfcControllerCallBackStub& ctrl = NfcControllerCallBackStub::GetInstance();
127 int onRemoteRequest = ctrl.OnRemoteRequest(code, data, reply, option);
128 ASSERT_TRUE(onRemoteRequest == 1);
129 }
130
131 /**
132 * @tc.name: OnRemoteRequest003
133 * @tc.desc: Test NfcControllerCallBackStub OnRemoteRequest.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(NfcControllerCallBackStubTest, OnRemoteRequest003, TestSize.Level1)
137 {
138 uint32_t code = 0;
139 MessageParcel data;
140 MessageParcel reply;
141 MessageOption option;
142 std::u16string descriptor = u"ohos.nfc.kits.INfcControllerCallback";
143 data.WriteInterfaceToken(descriptor);
144 data.WriteInt32(0);
145 NfcControllerCallBackStub& ctrl = NfcControllerCallBackStub::GetInstance();
146 int onRemoteRequest = ctrl.OnRemoteRequest(code, data, reply, option);
147 ASSERT_TRUE(onRemoteRequest);
148 }
149
150 /**
151 * @tc.name: OnRemoteRequest004
152 * @tc.desc: Test NfcControllerCallBackStub OnRemoteRequest.
153 * @tc.type: FUNC
154 */
155 HWTEST_F(NfcControllerCallBackStubTest, OnRemoteRequest004, TestSize.Level1)
156 {
157 uint32_t code = 104;
158 MessageParcel data;
159 MessageParcel reply;
160 MessageOption option;
161 std::u16string descriptor = u"ohos.nfc.kits.INfcControllerCallback";
162 data.WriteInterfaceToken(descriptor);
163 data.WriteInt32(0);
164 NfcControllerCallBackStub& ctrl = NfcControllerCallBackStub::GetInstance();
165 int onRemoteRequest = ctrl.OnRemoteRequest(code, data, reply, option);
166 ASSERT_TRUE(!onRemoteRequest);
167 }
168
169 /**
170 * @tc.name: OnRemoteRequest005
171 * @tc.desc: Test NfcControllerCallBackStub OnRemoteRequest.
172 * @tc.type: FUNC
173 */
174 HWTEST_F(NfcControllerCallBackStubTest, OnRemoteRequest005, TestSize.Level1)
175 {
176 uint32_t code = 104;
177 MessageParcel data;
178 MessageParcel reply;
179 MessageOption option;
180 std::u16string descriptor = u"ohos.nfc.kits.INfcControllerCallback";
181 data.WriteInterfaceToken(descriptor);
182 data.WriteInt32(0);
183 NfcControllerCallBackStub& ctrl = NfcControllerCallBackStub::GetInstance();
184 const sptr<NfcControllerCallBackStub> g_nfcControllerCallbackStub =
185 sptr<NfcControllerCallBackStub>(new (std::nothrow) NfcControllerCallBackStub());
186 ctrl.RegisterCallBack(g_nfcControllerCallbackStub);
187 int onRemoteRequest = ctrl.OnRemoteRequest(code, data, reply, option);
188 ASSERT_TRUE(!onRemoteRequest);
189 }
190 }
191 }
192 }
193