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