1 /*
2 * Copyright (c) 2022-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 "UTTest_dm_service_load.h"
17
18 #include <unistd.h>
19
20 #include "dm_constants.h"
21 #include "dm_service_load.h"
22 #include "dm_system_ability_manager_mock.h"
23 #include "if_system_ability_manager.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26
27 using ::testing::_;
28 using ::testing::An;
29 using ::testing::Return;
30
31 namespace OHOS {
32 namespace DistributedHardware {
SetUp()33 void DmServiceLoadTest::SetUp()
34 {
35 }
36
TearDown()37 void DmServiceLoadTest::TearDown()
38 {
39 }
40
SetUpTestCase()41 void DmServiceLoadTest::SetUpTestCase()
42 {
43 }
44
TearDownTestCase()45 void DmServiceLoadTest::TearDownTestCase()
46 {
47 }
48
49 namespace {
50 HWTEST_F(DmServiceLoadTest, LoadDMService_001, testing::ext::TestSize.Level0)
51 {
52 int32_t ret = DmServiceLoad::GetInstance().LoadDMService();
53 ASSERT_EQ(ret, DM_OK);
54 ret = DmServiceLoad::GetInstance().LoadDMService();
55 ASSERT_EQ(ret, DM_OK);
56 }
57
58 HWTEST_F(DmServiceLoadTest, SetLoadFinish_001, testing::ext::TestSize.Level0)
59 {
60 DmServiceLoad::GetInstance().SetLoadFinish();
61 int32_t systemAbilityId = 1000;
62 sptr<IRemoteObject> remoteObject = nullptr;
63 DMLoadCallbackTest dmLoadCallback;
64 dmLoadCallback.OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
65 dmLoadCallback.OnLoadSystemAbilityFail(systemAbilityId);
66 ASSERT_EQ(DmServiceLoad::GetInstance().isDMServiceLoading_, false);
67 }
68
69 HWTEST_F(DmServiceLoadTest, LoadDMService_002, testing::ext::TestSize.Level2)
70 {
71 auto iSystemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
72 auto mockAbilityMgr = sptr<SystemAbilityManagerMock>(new SystemAbilityManagerMock());
73 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockAbilityMgr;
74 EXPECT_CALL(*mockAbilityMgr, LoadSystemAbility(_, An<const sptr<ISystemAbilityLoadCallback>&>()))
75 .Times(1)
76 .WillOnce(Return(ERR_DM_FAILED));
77
78 int32_t ret = DmServiceLoad::GetInstance().LoadDMService();
79 EXPECT_EQ(ret, ERR_DM_FAILED);
80 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr;
81 }
82 }
83 } // namespace DistributedHardware
84 } // namespace OHOS
85