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, TestSize.Level1)
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, TestSize.Level1)
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 }
93 
94 /**
95 * @tc.name: PrintBMSHelperTest_0008
96 * @tc.desc: QueryCallerBundleName fail
97 * @tc.type: FUNC
98 * @tc.require:
99 */
100 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0008, TestSize.Level1)
101 {
102     std::string testName = "";
103     std::shared_ptr<PrintServiceHelper> temp = nullptr;
104     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(temp);
105     EXPECT_EQ(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName(), testName);
106 }
107 
108 /**
109 * @tc.name: PrintBMSHelperTest_0010
110 * @tc.desc: QueryCallerBundleName fail
111 * @tc.type: FUNC
112 * @tc.require:
113 */
114 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0010, TestSize.Level1)
115 {
116     PrintBMSHelper printBMSHelper;
117     const wptr<IRemoteObject> testObj = nullptr;
118     DelayedSingleton<PrintBMSHelper>::GetInstance()->ResetProxy(testObj);
119     wptr<IRemoteObject> testObj2 = new (std::nothrow) MockBundleMgr();
120     printBMSHelper.ResetProxy(testObj2);
121     EXPECT_EQ(printBMSHelper.sptrBundleMgr_, nullptr);
122     EXPECT_EQ(printBMSHelper.printBMSDeath_, nullptr);
123 }
124 }  // namespace Print
125 }  // namespace OHOS
126