1 /*
2 * Copyright (C) 2021 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 "invoker_factory.h"
19 #undef private
20 #include "binder_invoker.h"
21 #include "iremote_invoker.h"
22
23 namespace OHOS {
24 using namespace testing::ext;
25 using namespace OHOS;
26
27 class InvokerFactoryTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 };
34
SetUp()35 void InvokerFactoryTest::SetUp() {}
36
TearDown()37 void InvokerFactoryTest::TearDown() {}
38
SetUpTestCase()39 void InvokerFactoryTest::SetUpTestCase() {}
40
TearDownTestCase()41 void InvokerFactoryTest::TearDownTestCase() {}
42
43 /**
44 * @tc.name: Register001
45 * @tc.desc: Register
46 * @tc.type: FUNC
47 */
48 HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1)
49 {
50 InvokerFactory &invokerFactory = InvokerFactory::Get();
51 invokerFactory.isAvailable_ = false;
52 int protocol = 1;
53
54 IRemoteInvoker* invoker = nullptr;
__anonf503cb330102() 55 auto invokerObject = [&invoker]() -> IRemoteInvoker* {
56 invoker = new (std::nothrow) BinderInvoker();
57 if (invoker == nullptr) {
58 return nullptr;
59 }
60 return invoker;
61 };
62
63 bool ret = invokerFactory.Register(protocol, invokerObject);
64 EXPECT_EQ(ret, false);
65 if (invoker != nullptr) {
66 delete invoker;
67 invoker = nullptr;
68 }
69 invokerFactory.isAvailable_ = true;
70
71 ret = invokerFactory.Register(protocol, invokerObject);
72 EXPECT_EQ(ret, false);
73 IRemoteInvoker* iRemoteInvoker = invokerFactory.newInstance(protocol);
74 EXPECT_NE(iRemoteInvoker, nullptr);
75 if (invoker != nullptr) {
76 delete invoker;
77 invoker = nullptr;
78 }
79 }
80
81 /**
82 * @tc.name: Unregister001
83 * @tc.desc: Unregister
84 * @tc.type: FUNC
85 */
86 HWTEST_F(InvokerFactoryTest, Unregister001, TestSize.Level1)
87 {
88 InvokerFactory &invokerFactory = InvokerFactory::Get();
89 invokerFactory.isAvailable_ = false;
90 int protocol = 1;
91 invokerFactory.Unregister(protocol);
92 EXPECT_EQ(invokerFactory.isAvailable_, false);
93 }
94 } // namespace OHOS
95