* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "UTTest_dm_service_load.h"
#include <unistd.h>
#include "dm_constants.h"
#include "dm_service_load.h"
#include "dm_system_ability_manager_mock.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
using ::testing::_;
using ::testing::An;
using ::testing::Return;
namespace OHOS {
namespace DistributedHardware {
void DmServiceLoadTest::SetUp()
{
}
void DmServiceLoadTest::TearDown()
{
}
void DmServiceLoadTest::SetUpTestCase()
{
}
void DmServiceLoadTest::TearDownTestCase()
{
}
namespace {
HWTEST_F(DmServiceLoadTest, LoadDMService_001, testing::ext::TestSize.Level0)
{
int32_t ret = DmServiceLoad::GetInstance().LoadDMService();
ASSERT_EQ(ret, DM_OK);
ret = DmServiceLoad::GetInstance().LoadDMService();
ASSERT_EQ(ret, DM_OK);
}
HWTEST_F(DmServiceLoadTest, SetLoadFinish_001, testing::ext::TestSize.Level0)
{
DmServiceLoad::GetInstance().SetLoadFinish();
int32_t systemAbilityId = 1000;
sptr<IRemoteObject> remoteObject = nullptr;
DMLoadCallbackTest dmLoadCallback;
dmLoadCallback.OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
dmLoadCallback.OnLoadSystemAbilityFail(systemAbilityId);
ASSERT_EQ(DmServiceLoad::GetInstance().isDMServiceLoading_, false);
}
HWTEST_F(DmServiceLoadTest, LoadDMService_002, testing::ext::TestSize.Level2)
{
auto iSystemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
auto mockAbilityMgr = sptr<SystemAbilityManagerMock>(new SystemAbilityManagerMock());
SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = mockAbilityMgr;
EXPECT_CALL(*mockAbilityMgr, LoadSystemAbility(_, An<const sptr<ISystemAbilityLoadCallback>&>()))
.Times(1)
.WillOnce(Return(ERR_DM_FAILED));
int32_t ret = DmServiceLoad::GetInstance().LoadDMService();
EXPECT_EQ(ret, ERR_DM_FAILED);
SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = iSystemAbilityMgr;
}
* @tc.name: LoadDMService_003
* @tc.desc: isDMServiceLoading_ already true -> LoadDMService early-returns DM_OK without reloading
* @tc.type: FUNC
*/
HWTEST_F(DmServiceLoadTest, LoadDMService_003, testing::ext::TestSize.Level2)
{
DmServiceLoad::GetInstance().isDMServiceLoading_ = true;
int32_t ret = DmServiceLoad::GetInstance().LoadDMService();
EXPECT_EQ(ret, DM_OK);
DmServiceLoad::GetInstance().isDMServiceLoading_ = false;
}
* @tc.name: OnLoadSystemAbilitySuccess_001
* @tc.desc: 1. construct a real DMLoadCallback instance
* 2. call OnLoadSystemAbilitySuccess with a nullptr remoteObject
* 3. verify the null-remoteObject early-return branch is hit and loading flag reset
* @tc.type: FUNC
*/
HWTEST_F(DmServiceLoadTest, OnLoadSystemAbilitySuccess_001, testing::ext::TestSize.Level2)
{
DMLoadCallback dmLoadCallback;
int32_t systemAbilityId = 1000;
sptr<IRemoteObject> remoteObject = nullptr;
dmLoadCallback.OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
ASSERT_EQ(DmServiceLoad::GetInstance().isDMServiceLoading_, false);
}
* @tc.name: OnLoadSystemAbilityFail_001
* @tc.desc: 1. construct a real DMLoadCallback instance
* 2. call OnLoadSystemAbilityFail
* 3. verify loading flag is reset to false
* @tc.type: FUNC
*/
HWTEST_F(DmServiceLoadTest, OnLoadSystemAbilityFail_001, testing::ext::TestSize.Level2)
{
DMLoadCallback dmLoadCallback;
int32_t systemAbilityId = 1001;
dmLoadCallback.OnLoadSystemAbilityFail(systemAbilityId);
ASSERT_EQ(DmServiceLoad::GetInstance().isDMServiceLoading_, false);
}
* @tc.name: SetLoadFinish_002
* @tc.desc: 1. set isDMServiceLoading_ to true by triggering a load
* 2. call SetLoadFinish explicitly
* 3. verify isDMServiceLoading_ flips back to false
* @tc.type: FUNC
*/
HWTEST_F(DmServiceLoadTest, SetLoadFinish_002, testing::ext::TestSize.Level2)
{
DmServiceLoad::GetInstance().SetLoadFinish();
ASSERT_EQ(DmServiceLoad::GetInstance().isDMServiceLoading_, false);
}
}
}
}