accessibility/services/test/mock/mock_aams_common_event_registry.cpp
qianchuang eb697e386b 无障碍新增DT用例
Signed-off-by: qianchuang <qianchuang1@huawei.com>
2024-09-27 15:07:02 +08:00

121 lines
4.6 KiB
C++

/*
* Copyright (C) 2022 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 <gtest/gtest.h>
#include "accessibility_common_helper.h"
#include "accessibility_mt_helper.h"
#include "accessible_ability_channel.h"
#include "accessible_ability_connection.h"
#include "accessible_ability_manager_service.h"
#include "common_event_manager.h"
#include "iservice_registry.h"
using namespace testing;
using namespace testing::ext;
using namespace OHOS::EventFwk;
namespace OHOS {
namespace Accessibility {
class MockAccessibilityCommonEventRegistryTest : public ::testing::Test {
public:
MockAccessibilityCommonEventRegistryTest()
{}
~MockAccessibilityCommonEventRegistryTest()
{}
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() override;
void TearDown() override;
void AddAccessibleAbilityConnection();
sptr<AccessibilityAccountData> accountData_ = nullptr;
sptr<AccessibleAbilityChannel> aastub_ = nullptr;
sptr<AppExecFwk::ElementName> elementName_ = nullptr;
sptr<AccessibleAbilityConnection> AAConnection_ = nullptr;
};
void MockAccessibilityCommonEventRegistryTest::SetUpTestCase()
{
GTEST_LOG_(INFO) << "MockAccessibilityCommonEventRegistryTest SetUpTestCase";
// Start AAMS
Singleton<AccessibleAbilityManagerService>::GetInstance().OnStart();
AccessibilityCommonHelper::GetInstance().WaitForServicePublish();
Singleton<AccessibleAbilityManagerService>::GetInstance().SwitchedUser(AccessibilityHelper::accountId_);
GTEST_LOG_(INFO) << "AccessibleAbilityManagerService is published";
}
void MockAccessibilityCommonEventRegistryTest::TearDownTestCase()
{
GTEST_LOG_(INFO) << "MockAccessibilityCommonEventRegistryTest TearDownTestCase";
Singleton<AccessibleAbilityManagerService>::GetInstance().OnStop();
}
void MockAccessibilityCommonEventRegistryTest::SetUp()
{
GTEST_LOG_(INFO) << "SetUp";
}
void MockAccessibilityCommonEventRegistryTest::TearDown()
{
GTEST_LOG_(INFO) << "TearDown";
accountData_ = nullptr;
aastub_ = nullptr;
elementName_ = nullptr;
AAConnection_ = nullptr;
}
void MockAccessibilityCommonEventRegistryTest::AddAccessibleAbilityConnection()
{
GTEST_LOG_(INFO) << "MockAccessibilityCommonEventRegistryTest AddAccessibleAbilityConnection";
AAFwk::Want want;
AppExecFwk::ElementName name;
name.SetAbilityName("com.example.aalisttest.MainAbility");
name.SetBundleName("com.example.aalisttest");
want.SetElement(name);
AccessibilityAbilityInitParams initParams;
std::shared_ptr<AccessibilityAbilityInfo> abilityInfo = std::make_shared<AccessibilityAbilityInfo>(initParams);
accountData_ = Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
AAConnection_ = new AccessibleAbilityConnection(accountData_->GetAccountId(), 0, *abilityInfo);
elementName_ = new AppExecFwk::ElementName("name", "bundleName", "id");
aastub_ = new AccessibleAbilityChannel(accountData_->GetAccountId(), abilityInfo->GetId());
AAConnection_->OnAbilityConnectDoneSync(*elementName_, aastub_);
accountData_->AddInstalledAbility(*abilityInfo);
sleep(1);
}
/**
* @tc.number: AccessibilityCommonEventRegistry_ModuleTest_PackageChanged_001
* @tc.name: PackageChanged
* @tc.desc: There is a connected ability. The package has changed. Empty all install ability.
*/
HWTEST_F(MockAccessibilityCommonEventRegistryTest, AccessibilityCommonEvent_ModuleTest_PackageChanged_001,
TestSize.Level1)
{
GTEST_LOG_(INFO) << "AccessibilityCommonEvent_ModuleTest_PackageChanged_001 start";
AddAccessibleAbilityConnection();
EXPECT_EQ(1, int(accountData_->GetInstalledAbilities().size()));
// PackageChanged
std::string bundleName = "bundleName";
Singleton<AccessibleAbilityManagerService>::GetInstance().PackageChanged(bundleName);
EXPECT_EQ(1, int(accountData_->GetInstalledAbilities().size()));
AAConnection_->OnAbilityDisconnectDoneSync(*elementName_);
GTEST_LOG_(INFO) << "AccessibilityCommonEvent_ModuleTest_PackageChanged_001 end";
}
} // namespace Accessibility
} // namespace OHOS