• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Print {
28 class PrintBMSHelperTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34     void CallRemoteObject(const sptr<MockBundleMgr> &obj, sptr<IRemoteObject::DeathRecipient> &dr);
35 };
36 
SetUpTestCase(void)37 void PrintBMSHelperTest::SetUpTestCase(void) {}
38 
TearDownTestCase(void)39 void PrintBMSHelperTest::TearDownTestCase(void) {}
40 
SetUp(void)41 void PrintBMSHelperTest::SetUp(void) {}
42 
TearDown(void)43 void PrintBMSHelperTest::TearDown(void) {}
44 
CallRemoteObject(const sptr<MockBundleMgr> & obj,sptr<IRemoteObject::DeathRecipient> & dr)45 void PrintBMSHelperTest::CallRemoteObject(const sptr<MockBundleMgr> &obj,
46     sptr<IRemoteObject::DeathRecipient> &dr)
47 {
48     EXPECT_CALL(*obj, IsProxyObject()).WillRepeatedly(Return(true));
49     EXPECT_CALL(*obj, RemoveDeathRecipient(_)).WillRepeatedly(Return(true));
50     EXPECT_CALL(*obj, AddDeathRecipient(_)).WillRepeatedly(
51         [&dr](const sptr<IRemoteObject::DeathRecipient> &recipient) {
52             dr = recipient;
53             return true;
54         });
55 }
56 
57 /**
58 * @tc.name: PrintBMSHelperTest_0001
59 * @tc.desc: PrintBMSHelper
60 * @tc.type: FUNC
61 * @tc.require:
62 */
63 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0001, TestSize.Level1)
64 {
65     PrintBMSHelper printBMSHelper;
66     printBMSHelper.~PrintBMSHelper();
67 }
68 
69 /**
70 * @tc.name: PrintBMSHelperTest_0002
71 * @tc.desc: PrintBMSHelper
72 * @tc.type: FUNC
73 * @tc.require:
74 */
75 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0002, TestSize.Level1)
76 {
77     PrintBMSHelper printBMSHelper;
78     auto helper = std::make_shared<MockPrintServiceHelper>();
79     printBMSHelper.SetHelper(helper);
80 }
81 
82 /**
83 * @tc.name: PrintBMSHelperTest_0003
84 * @tc.desc: QueryExtensionInfos
85 * @tc.type: FUNC
86 * @tc.require:
87 */
88 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0003, TestSize.Level1)
89 {
90     std::vector<AppExecFwk::ExtensionAbilityInfo> resultInfo;
91     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(nullptr);
92     EXPECT_FALSE(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryExtensionInfos(resultInfo));
93 }
94 
95 /**
96 * @tc.name: PrintBMSHelperTest_0004
97 * @tc.desc: QueryExtensionInfos
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0004, TestSize.Level1)
102 {
103     std::vector<AppExecFwk::ExtensionAbilityInfo> resultInfo;
104     auto helper = std::make_shared<MockPrintServiceHelper>();
105     EXPECT_NE(helper, nullptr);
106     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(helper);
107     EXPECT_CALL(*helper, GetBundleMgr()).Times(1).WillOnce(Return(nullptr));
108     EXPECT_FALSE(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryExtensionInfos(resultInfo));
109 }
110 
111 /**
112 * @tc.name: PrintBMSHelperTest_0005
113 * @tc.desc: QueryExtensionInfos
114 * @tc.type: FUNC
115 * @tc.require:
116 */
117 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0005, TestSize.Level1)
118 {
119     std::vector<AppExecFwk::ExtensionAbilityInfo> resultInfo;
120     auto helper = std::make_shared<MockPrintServiceHelper>();
121     EXPECT_NE(helper, nullptr);
122     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(helper);
123     EXPECT_CALL(*helper, GetBundleMgr()).Times(1);
124     ON_CALL(*helper, GetBundleMgr).WillByDefault(
__anon391dc48b0202() 125         []() -> sptr<IRemoteObject> {
126             return new (std::nothrow) MockRemoteObject();
127         });
128     EXPECT_FALSE(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryExtensionInfos(resultInfo));
129 }
130 
131 /**
132 * @tc.name: PrintBMSHelperTest_0006
133 * @tc.desc: QueryExtensionInfos
134 * @tc.type: FUNC
135 * @tc.require:
136 */
137 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0006, TestSize.Level1)
138 {
139     std::vector<AppExecFwk::ExtensionAbilityInfo> resultInfo;
140     auto helper = std::make_shared<MockPrintServiceHelper>();
141     EXPECT_NE(helper, nullptr);
142     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(helper);
143     sptr<MockBundleMgr> obj = new (std::nothrow) MockBundleMgr();
144     EXPECT_NE(obj, nullptr);
145     EXPECT_CALL(*helper, GetBundleMgr()).Times(1);
146     ON_CALL(*helper, GetBundleMgr).WillByDefault(
__anon391dc48b0302() 147         [&obj]() -> sptr<IRemoteObject> {
148             return obj;
149         });
150     EXPECT_CALL(*helper, QueryAccounts(_)).Times(1).WillOnce(Return(false));
151     sptr<IRemoteObject::DeathRecipient> dr = nullptr;
152     CallRemoteObject(obj, dr);
153     EXPECT_FALSE(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryExtensionInfos(resultInfo));
154     EXPECT_NE(dr, nullptr);
155     dr->OnRemoteDied(obj);
156 }
157 
158 /**
159 * @tc.name: PrintBMSHelperTest_0007
160 * @tc.desc: QueryExtensionInfos
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0007, TestSize.Level1)
165 {
166     std::vector<AppExecFwk::ExtensionAbilityInfo> resultInfo;
167 
168     auto helper = std::make_shared<MockPrintServiceHelper>();
169     EXPECT_NE(helper, nullptr);
170     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(helper);
171     sptr<MockBundleMgr> obj = new (std::nothrow) MockBundleMgr();
172     EXPECT_NE(obj, nullptr);
173     EXPECT_CALL(*helper, GetBundleMgr()).Times(1);
174     ON_CALL(*helper, GetBundleMgr).WillByDefault(
__anon391dc48b0402() 175         [&obj]() -> sptr<IRemoteObject> {
176             return obj;
177         });
178     ON_CALL(*helper, QueryAccounts).WillByDefault(
__anon391dc48b0502(std::vector<int> &accountList) 179         [](std::vector<int> &accountList) {
180             accountList.emplace_back(1);
181             return true;
182         });
183     sptr<IRemoteObject::DeathRecipient> dr = nullptr;
184     EXPECT_CALL(*helper, QueryExtension(_, _, _)).WillRepeatedly(Return(true));
185     CallRemoteObject(obj, dr);
186     EXPECT_TRUE(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryExtensionInfos(resultInfo));
187     EXPECT_NE(dr, nullptr);
188     dr->OnRemoteDied(obj);
189 }
190 
191 /**
192 * @tc.name: PrintBMSHelperTest_0008
193 * @tc.desc: QueryCallerBundleName fail
194 * @tc.type: FUNC
195 * @tc.require:
196 */
197 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0008, TestSize.Level1)
198 {
199     std::string testName = "";
200     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(nullptr);
201     EXPECT_EQ(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName(), testName);
202 }
203 
204 /**
205 * @tc.name: PrintBMSHelperTest_0009
206 * @tc.desc: QueryCallerBundleName
207 * @tc.type: FUNC
208 * @tc.require:
209 */
210 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0009, TestSize.Level1)
211 {
212     std::string testName = "com.example.ext.extability";
213 
214     auto helper = std::make_shared<MockPrintServiceHelper>();
215     EXPECT_NE(helper, nullptr);
216     DelayedSingleton<PrintBMSHelper>::GetInstance()->SetHelper(helper);
217     sptr<MockBundleMgr> obj = new (std::nothrow) MockBundleMgr();
218     EXPECT_NE(obj, nullptr);
219     EXPECT_CALL(*helper, GetBundleMgr()).Times(1).WillOnce(Return(obj));
220     EXPECT_CALL(*helper, QueryNameForUid(_, _, _)).WillRepeatedly(Return(true));
221     sptr<IRemoteObject::DeathRecipient> dr = nullptr;
222     CallRemoteObject(obj, dr);
223     EXPECT_EQ(DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName(), "");
224     EXPECT_NE(dr, nullptr);
225     dr->OnRemoteDied(obj);
226 }
227 
228 /**
229 * @tc.name: PrintBMSHelperTest_0010
230 * @tc.desc: QueryCallerBundleName fail
231 * @tc.type: FUNC
232 * @tc.require:
233 */
234 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0010, TestSize.Level1)
235 {
236     const wptr<IRemoteObject> testObj = nullptr;
237     DelayedSingleton<PrintBMSHelper>::GetInstance()->ResetProxy(testObj);
238 }
239 
240 /**
241 * @tc.name: PrintBMSHelperTest_0011
242 * @tc.desc: QueryCallerBundleName fail
243 * @tc.type: FUNC
244 * @tc.require:
245 */
246 HWTEST_F(PrintBMSHelperTest, PrintBMSHelperTest_0011, TestSize.Level1)
247 {
248     wptr<IRemoteObject> testObj = new (std::nothrow) MockBundleMgr();
249     DelayedSingleton<PrintBMSHelper>::GetInstance()->ResetProxy(testObj);
250 }
251 }  // namespace Print
252 }  // namespace OHOS
253