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 "print_bms_helper.h"
18 #include "print_constant.h"
19 #include "mock_bundle_mgr.h"
20 #include "mock_print_service_helper.h"
21 #include "mock_remote_object.h"
22 #include "print_log.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Print {
29 class PrintBMSHelperTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 void CallRemoteObject(const sptr<MockBundleMgr> &obj, sptr<IRemoteObject::DeathRecipient> &dr);
36 };
37
SetUpTestCase(void)38 void PrintBMSHelperTest::SetUpTestCase(void) {}
39
TearDownTestCase(void)40 void PrintBMSHelperTest::TearDownTestCase(void) {}
41
SetUp(void)42 void PrintBMSHelperTest::SetUp(void)
43 {
44 static int32_t testNo = 0;
45 PRINT_HILOGI("PrintBMSHelperTest_%{public}d", ++testNo);
46 }
47
TearDown(void)48 void PrintBMSHelperTest::TearDown(void) {}
49
CallRemoteObject(const sptr<MockBundleMgr> & obj,sptr<IRemoteObject::DeathRecipient> & dr)50 void PrintBMSHelperTest::CallRemoteObject(const sptr<MockBundleMgr> &obj,
51 sptr<IRemoteObject::DeathRecipient> &dr)
52 {
53 EXPECT_CALL(*obj, IsProxyObject()).WillRepeatedly(Return(true));
54 EXPECT_CALL(*obj, RemoveDeathRecipient(_)).WillRepeatedly(Return(true));
55 EXPECT_CALL(*obj, AddDeathRecipient(_)).WillRepeatedly(
56 [&dr](const sptr<IRemoteObject::DeathRecipient> &recipient) {
57 dr = recipient;
58 return true;
59 });
60 }
61
62 /**
63 * @tc.name: PrintBMSHelperTest_0001
64 * @tc.desc: PrintBMSHelper
65 * @tc.type: FUNC
66 * @tc.require:
67 */
68 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0001, TestSize.Level1)
69 {
70 PrintBMSHelper printBMSHelper;
71 printBMSHelper.~PrintBMSHelper();
72 }
73
74 /**
75 * @tc.name: PrintBMSHelperTest_0002
76 * @tc.desc: PrintBMSHelper
77 * @tc.type: FUNC
78 * @tc.require:
79 */
80 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0002, TestSize.Level1)
81 {
82 PrintBMSHelper printBMSHelper;
83 auto helper = std::make_shared<MockPrintServiceHelper>();
84 std::shared_ptr<PrintServiceHelper> temp = std::shared_ptr<PrintServiceHelper>(helper);
85 printBMSHelper.SetHelper(temp);
86 }
87
88 /**
89 * @tc.name: PrintBMSHelperTest_0003
90 * @tc.desc: QueryExtensionInfos
91 * @tc.type: FUNC
92 * @tc.require:
93 */
94 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0003, TestSize.Level1)
95 {
96 std::vector<AppExecFwk::ExtensionAbilityInfo> resultInfo;
97 std::shared_ptr<PrintServiceHelper> temp = nullptr;
98 DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(temp);
99 EXPECT_FALSE(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryExtensionInfos(resultInfo));
100 }
101
102 /**
103 * @tc.name: PrintBMSHelperTest_0008
104 * @tc.desc: QueryCallerBundleName fail
105 * @tc.type: FUNC
106 * @tc.require:
107 */
108 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0008, TestSize.Level1)
109 {
110 std::string testName = "";
111 std::shared_ptr<PrintServiceHelper> temp = nullptr;
112 DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(temp);
113 EXPECT_EQ(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName(), testName);
114 }
115
116 /**
117 * @tc.name: PrintBMSHelperTest_0010
118 * @tc.desc: QueryCallerBundleName fail
119 * @tc.type: FUNC
120 * @tc.require:
121 */
122 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0010, TestSize.Level1)
123 {
124 const wptr<IRemoteObject> testObj = nullptr;
125 DelayedSingleton<PrintBMSHelper>::GetInstance()->ResetProxy(testObj);
126 }
127
128 /**
129 * @tc.name: PrintBMSHelperTest_0011
130 * @tc.desc: QueryCallerBundleName fail
131 * @tc.type: FUNC
132 * @tc.require:
133 */
134 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0011, TestSize.Level1)
135 {
136 wptr<IRemoteObject> testObj = new (std::nothrow) MockBundleMgr();
137 DelayedSingleton<PrintBMSHelper>::GetInstance()->ResetProxy(testObj);
138 }
139 } // namespace Print
140 } // namespace OHOS
141