modify ut

Signed-off-by: zhaolinglan <zhaolinglan1@huawei.com>
This commit is contained in:
zhaolinglan 2024-04-02 15:37:13 +08:00
parent b2fb047545
commit bb7ad913d5
9 changed files with 118 additions and 341 deletions

View File

@ -42,7 +42,6 @@ ohos_static_library("inputmethod_tdd_util") {
debug = false
}
sources = [
"src/access_util.cpp",
"src/key_event_util.cpp",
"src/tdd_util.cpp",
]
@ -57,7 +56,6 @@ ohos_static_library("inputmethod_tdd_util") {
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"data_share:datashare_common",
"data_share:datashare_consumer",

View File

@ -1,46 +0,0 @@
/*
* Copyright (c) 2024 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.
*/
#ifndef INPUTMETHOD_IMF_ACCESS_UTIL_H
#define INPUTMETHOD_IMF_ACCESS_UTIL_H
#include <string>
#include "bundle_mgr_interface.h"
namespace OHOS {
namespace MiscServices {
class AccessUtil {
public:
static uint64_t GetSelfToken();
static void SetSelfToken(uint64_t tokenId);
static void StorageSelfToken();
static void RestoreSelfToken();
static uint64_t GetTokenID(const std::string &bundleName);
static void DeleteTokenID(uint64_t tokenId);
static uint64_t AllocTestTokenID(
bool isSystemApp, const std::string &bundleName, const std::vector<std::string> &premission = {});
static int32_t GetUid(const std::string &bundleName);
static void SetSelfUid(int32_t uid);
static void GrantNativePermissions(const std::vector<std::string> &permissions);
static void GrantNativePermission(const std::string &permission);
private:
static sptr<AppExecFwk::IBundleMgr> GetBundleMgr();
static int32_t GetUserIdByBundleName(const std::string &bundleName, int32_t currentUserId);
static uint64_t selfTokenID_;
};
} // namespace MiscServices
} // namespace OHOS
#endif // INPUTMETHOD_IMF_ACCESS_UTIL_H

View File

@ -16,8 +16,8 @@
#ifndef INPUTMETHOD_IMF_TEST_UNITTEST_COMMON_SCOPE_UTILS_H
#define INPUTMETHOD_IMF_TEST_UNITTEST_COMMON_SCOPE_UTILS_H
#include "access_util.h"
#include "global.h"
#include "tdd_util.h"
namespace OHOS {
namespace MiscServices {
constexpr int32_t ROOT_UID = 0;
@ -27,19 +27,19 @@ public:
{
IMSA_HILOGI("enter");
if (tokenId > 0) {
originalTokenId_ = AccessUtil::GetSelfToken();
AccessUtil::SetSelfToken(tokenId);
originalTokenId_ = TddUtil::GetCurrentTokenID();
TddUtil::SetTestTokenID(tokenId);
}
if (uid > 0) {
AccessUtil::SetSelfUid(uid);
TddUtil::SetSelfUid(uid);
}
}
~AccessScope()
{
if (originalTokenId_ > 0) {
AccessUtil::SetSelfToken(originalTokenId_);
TddUtil::SetTestTokenID(originalTokenId_);
}
AccessUtil::SetSelfUid(ROOT_UID);
TddUtil::SetSelfUid(ROOT_UID);
IMSA_HILOGI("exit");
}
@ -52,12 +52,12 @@ public:
explicit TokenScope(uint64_t tokenId)
{
IMSA_HILOGI("enter");
originalTokenId_ = AccessUtil::GetSelfToken();
AccessUtil::SetSelfToken(tokenId);
originalTokenId_ = TddUtil::GetCurrentTokenID();
TddUtil::SetTestTokenID(tokenId);
}
virtual ~TokenScope()
~TokenScope()
{
AccessUtil::SetSelfToken(originalTokenId_);
TddUtil::SetTestTokenID(originalTokenId_);
IMSA_HILOGI("exit");
}
@ -70,11 +70,11 @@ public:
explicit UidScope(int32_t uid)
{
IMSA_HILOGI("enter, uid: %{public}d", uid);
AccessUtil::SetSelfUid(uid);
TddUtil::SetSelfUid(uid);
}
virtual ~UidScope()
~UidScope()
{
AccessUtil::SetSelfUid(ROOT_UID);
TddUtil::SetSelfUid(ROOT_UID);
IMSA_HILOGI("exit");
}
};

View File

@ -47,6 +47,9 @@ public:
static void DeleteTestTokenID(uint64_t tokenId);
static void SetTestTokenID(uint64_t tokenId);
static void RestoreSelfTokenID();
static uint64_t GetCurrentTokenID();
static int32_t GetUid(const std::string &bundleName);
static void SetSelfUid(int32_t uid);
static bool ExecuteCmd(const std::string &cmd, std::string &result);
static pid_t GetImsaPid();
static bool KillImsaProcess();

View File

@ -1,223 +0,0 @@
/*
* Copyright (c) 2024 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 "access_util.h"
#include <unistd.h>
#include <csignal>
#include <cstdint>
#include <sstream>
#include <string>
#include <vector>
#include "accesstoken_kit.h"
#include "global.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "nativetoken_kit.h"
#include "os_account_manager.h"
#include "system_ability.h"
#include "system_ability_definition.h"
#include "tdd_util.h"
#include "token_setproc.h"
namespace OHOS {
namespace MiscServices {
using namespace Security::AccessToken;
using namespace AppExecFwk;
const std::string PERMISSION_GET_BUNDLE_INFO = "ohos.permission.GET_BUNDLE_INFO";
uint64_t AccessUtil::selfTokenID_ = 0;
uint64_t AccessUtil::GetSelfToken()
{
return GetSelfTokenID();
}
void AccessUtil::SetSelfToken(uint64_t tokenId)
{
auto ret = SetSelfTokenID(tokenId);
IMSA_HILOGI("SetSelfTokenID ret: %{public}d", ret);
}
void AccessUtil::StorageSelfToken()
{
selfTokenID_ = GetSelfTokenID();
}
void AccessUtil::RestoreSelfToken()
{
auto ret = SetSelfTokenID(selfTokenID_);
IMSA_HILOGI("SetSelfTokenID ret = %{public}d", ret);
}
uint64_t AccessUtil::GetTokenID(const std::string &bundleName)
{
IMSA_HILOGI("bundleName: %{public}s", bundleName.c_str());
HapInfoParams infoParams = { .userID = GetUserIdByBundleName(bundleName, TddUtil::GetCurrentUserId()),
.bundleName = bundleName,
.instIndex = 0,
.appIDDesc = "ohos.inputmethod_test.demo" };
return AccessTokenKit::GetHapTokenID(infoParams.userID, infoParams.bundleName, infoParams.instIndex);
}
void AccessUtil::DeleteTokenID(uint64_t tokenId)
{
AccessTokenKit::DeleteToken(tokenId);
}
uint64_t AccessUtil::AllocTestTokenID(
bool isSystemApp, const std::string &bundleName, const std::vector<std::string> &premission)
{
IMSA_HILOGI("bundleName: %{public}s", bundleName.c_str());
HapInfoParams infoParams = { .userID = TddUtil::GetCurrentUserId(),
.bundleName = bundleName,
.instIndex = 0,
.appIDDesc = bundleName,
.isSystemApp = isSystemApp };
std::vector<PermissionStateFull> permStateList;
for (const auto &prem : premission) {
PermissionStateFull permissionState = { .permissionName = prem,
.isGeneral = true,
.resDeviceID = { "local" },
.grantStatus = { PermissionState::PERMISSION_GRANTED },
.grantFlags = { 1 } };
permStateList.push_back(permissionState);
}
HapPolicyParams policyParams = {
.apl = APL_NORMAL, .domain = bundleName, .permList = {}, .permStateList = permStateList
};
if (premission.empty()) {
policyParams = { .apl = APL_NORMAL, .domain = bundleName, .permList = {}, .permStateList = {} };
}
auto tokenInfo = AccessTokenKit::AllocHapToken(infoParams, policyParams);
return tokenInfo.tokenIDEx;
}
int32_t AccessUtil::GetUid(const std::string &bundleName)
{
auto currentToken = GetSelfTokenID();
GrantNativePermission(PERMISSION_GET_BUNDLE_INFO);
auto bundleMgr = GetBundleMgr();
if (bundleMgr == nullptr) {
IMSA_HILOGE("bundleMgr nullptr");
return -1;
}
BundleInfo bundleInfo;
bool result =
bundleMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, TddUtil::GetCurrentUserId());
if (!result) {
IMSA_HILOGE("failed to get bundle info");
return -1;
}
IMSA_HILOGI("bundleName: %{public}s, uid: %{public}d", bundleName.c_str(), bundleInfo.uid);
SetSelfToken(currentToken);
return bundleInfo.uid;
}
void AccessUtil::SetSelfUid(int32_t uid)
{
setuid(uid);
IMSA_HILOGI("set uid to: %{public}d", uid);
}
void AccessUtil::GrantNativePermissions(const std::vector<std::string> &permissions)
{
if (permissions.empty()) {
return;
}
uint32_t size = permissions.size();
const char **perms = new const char *[size];
for (uint32_t i = 0; i < size; i++) {
perms[i] = permissions[i].c_str();
}
TokenInfoParams infoInstance = {
.dcapsNum = 0,
.permsNum = 1,
.aclsNum = 0,
.dcaps = nullptr,
.perms = perms,
.acls = nullptr,
.processName = "imf_test",
.aplStr = "system_core",
};
uint64_t tokenId = GetAccessTokenId(&infoInstance);
int res = SetSelfTokenID(tokenId);
if (res == 0) {
IMSA_HILOGI("SetSelfTokenID success");
} else {
IMSA_HILOGE("SetSelfTokenID fail");
}
AccessTokenKit::ReloadNativeTokenInfo();
delete[] perms;
}
void AccessUtil::GrantNativePermission(const std::string &permission)
{
const char **perms = new const char *[1];
perms[0] = permission.c_str();
TokenInfoParams infoInstance = {
.dcapsNum = 0,
.permsNum = 1,
.aclsNum = 0,
.dcaps = nullptr,
.perms = perms,
.acls = nullptr,
.processName = "imf_test",
.aplStr = "system_core",
};
uint64_t tokenId = GetAccessTokenId(&infoInstance);
int res = SetSelfTokenID(tokenId);
if (res == 0) {
IMSA_HILOGI("SetSelfTokenID success");
} else {
IMSA_HILOGE("SetSelfTokenID fail");
}
AccessTokenKit::ReloadNativeTokenInfo();
delete[] perms;
}
sptr<AppExecFwk::IBundleMgr> AccessUtil::GetBundleMgr()
{
sptr<ISystemAbilityManager> systemAbilityManager =
SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (systemAbilityManager == nullptr) {
IMSA_HILOGE("systemAbilityManager is nullptr");
return nullptr;
}
sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
if (remoteObject == nullptr) {
IMSA_HILOGE("remoteObject is nullptr");
return nullptr;
}
return iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
}
int32_t AccessUtil::GetUserIdByBundleName(const std::string &bundleName, const int currentUserId)
{
auto bundleMgr = GetBundleMgr();
if (bundleMgr == nullptr) {
IMSA_HILOGE("Get bundleMgr failed");
return -1;
}
auto uid = bundleMgr->GetUidByBundleName(bundleName, currentUserId);
if (uid == -1) {
IMSA_HILOGE("failed to get information and the parameters may be wrong");
return -1;
}
// 200000 means userId = uid / 200000.
return uid / 200000;
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -26,7 +26,6 @@
#include <string>
#include <vector>
#include "access_util.h"
#include "accesstoken_kit.h"
#include "datashare_helper.h"
#include "global.h"
@ -154,6 +153,33 @@ void TddUtil::RestoreSelfTokenID()
IMSA_HILOGI("SetSelfTokenID ret = %{public}d", ret);
}
uint64_t TddUtil::GetCurrentTokenID()
{
return GetSelfTokenID();
}
int32_t TddUtil::GetUid(const std::string &bundleName)
{
auto bundleMgr = GetBundleMgr();
if (bundleMgr == nullptr) {
IMSA_HILOGE("bundleMgr nullptr");
return -1;
}
auto uid = bundleMgr->GetUidByBundleName(bundleName, GetCurrentUserId());
if (uid == -1) {
IMSA_HILOGE("failed to get information and the parameters may be wrong.");
return -1;
}
IMSA_HILOGI("bundleName: %{public}s, uid: %{public}d", bundleName.c_str(), uid);
return uid;
}
void TddUtil::SetSelfUid(int32_t uid)
{
setuid(uid);
IMSA_HILOGI("set uid to: %{public}d", uid);
}
bool TddUtil::ExecuteCmd(const std::string &cmd, std::string &result)
{
char buff[EACH_LINE_LENGTH] = { 0x00 };
@ -324,7 +350,9 @@ bool TddUtil::GetUnfocused()
void TddUtil::WindowManager::CreateWindow()
{
windowTokenId_ = AccessUtil::AllocTestTokenID(true, "undefined", {});
if (windowTokenId_ == 0) {
windowTokenId_ = AllocTestTokenID(true, "TestWindow", {});
}
TokenScope scope(windowTokenId_);
std::string windowName = "inputmethod_test_window";
sptr<WindowOption> winOption = new OHOS::Rosen::WindowOption();

View File

@ -25,7 +25,6 @@
#include <thread>
#include <vector>
#include "access_util.h"
#include "global.h"
#include "i_input_data_channel.h"
#include "input_attribute.h"
@ -111,12 +110,12 @@ public:
// Set the tokenID to the tokenID of the current ime
TddUtil::StorageSelfTokenID();
std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest";
currentImeTokenId_ = AccessUtil::GetTokenID(bundleName);
currentImeUid_ = AccessUtil::GetUid(bundleName);
auto currentIme = property != nullptr ? property->name : "default.inputmethod.unittest";
currentImeTokenId_ = TddUtil::GetTestTokenID(currentIme);
currentImeUid_ = TddUtil::GetUid(currentIme);
auto ret = InputMethodController::GetInstance()->GetDefaultInputMethod(property);
bundleName = ret == ErrorCode::NO_ERROR ? property->name : "default.inputmethod.unittest";
defaultImeTokenId_ = AccessUtil::GetTokenID(bundleName);
auto defaultIme = ret == ErrorCode::NO_ERROR ? property->name : "default.inputmethod.unittest";
defaultImeTokenId_ = TddUtil::GetTestTokenID(defaultIme);
{
TokenScope scope(currentImeTokenId_);
inputMethodAbility_ = InputMethodAbility::GetInstance();
@ -133,6 +132,7 @@ public:
imc_->Close();
TextListener::ResetParam();
TddUtil::DestroyWindow();
TddUtil::RestoreSelfTokenID();
}
void SetUp()
{
@ -167,6 +167,7 @@ public:
}
void CheckPanelInfoInHide(const std::shared_ptr<InputMethodPanel> &panel, const PanelStatusInfo &info)
{
AccessScope scope(currentImeTokenId_, currentImeUid_);
auto ret = inputMethodAbility_->HidePanel(panel);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
if (info.panelInfo.panelFlag != FLG_CANDIDATE_COLUMN) {
@ -1078,6 +1079,7 @@ HWTEST_F(InputMethodAbilityTest, testSendPrivateCommand_001, TestSize.Level0)
IMSA_HILOGI("InputMethodAbility testSendPrivateCommand_001 Test START");
TextListener::ResetParam();
imc_->Close();
TddUtil::RestoreSelfTokenID();
std::unordered_map<std::string, PrivateDataValue> privateCommand;
auto ret = inputMethodAbility_->SendPrivateCommand(privateCommand);
EXPECT_EQ(ret, ErrorCode::ERROR_NOT_DEFAULT_IME);

View File

@ -49,6 +49,7 @@
#include "key_event_util.h"
#include "keyboard_listener.h"
#include "message_parcel.h"
#include "scope_utils.h"
#include "system_ability.h"
#include "system_ability_definition.h"
#include "tdd_util.h"
@ -117,7 +118,6 @@ public:
static void CheckProxyObject();
static void DispatchKeyEventCallback(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed);
static bool WaitKeyEventCallback();
static void SetDefaultIme();
static void CheckTextConfig(const TextConfig &config);
static sptr<InputMethodController> inputMethodController_;
static sptr<InputMethodAbility> inputMethodAbility_;
@ -148,6 +148,8 @@ public:
static constexpr uint32_t DELAY_TIME = 1;
static constexpr uint32_t KEY_EVENT_DELAY_TIME = 100;
static constexpr int32_t TASK_DELAY_TIME = 10;
static uint64_t defaultImeTokenId_;
static uint64_t permissionTokenId_;
class KeyboardListenerImpl : public KeyboardListener {
public:
@ -251,6 +253,8 @@ std::condition_variable InputMethodControllerTest::keyEventCv_;
std::mutex InputMethodControllerTest::keyEventLock_;
bool InputMethodControllerTest::consumeResult_{ false };
std::shared_ptr<AppExecFwk::EventHandler> InputMethodControllerTest::textConfigHandler_{ nullptr };
uint64_t InputMethodControllerTest::defaultImeTokenId_ = 0;
uint64_t InputMethodControllerTest::permissionTokenId_ = 0;
void InputMethodControllerTest::SetUpTestCase(void)
{
@ -259,9 +263,11 @@ void InputMethodControllerTest::SetUpTestCase(void)
// Set the tokenID to the tokenID of the current ime
std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest";
TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName));
inputMethodAbility_ = InputMethodAbility::GetInstance();
inputMethodAbility_->SetCoreAndAgent();
{
TokenScope tokenScope(TddUtil::GetTestTokenID(bundleName));
inputMethodAbility_ = InputMethodAbility::GetInstance();
inputMethodAbility_->SetCoreAndAgent();
}
imeListener_ = std::make_shared<InputMethodEngineListenerImpl>();
controllerListener_ = std::make_shared<SelectListenerMock>();
textListener_ = new TextListener();
@ -273,20 +279,24 @@ void InputMethodControllerTest::SetUpTestCase(void)
keyEvent_->SetFunctionKey(MMI::KeyEvent::NUM_LOCK_FUNCTION_KEY, 0);
keyEvent_->SetFunctionKey(MMI::KeyEvent::CAPS_LOCK_FUNCTION_KEY, 1);
keyEvent_->SetFunctionKey(MMI::KeyEvent::SCROLL_LOCK_FUNCTION_KEY, 1);
TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, "undefine", { "ohos.permission.CONNECT_IME_ABILITY" }));
TddUtil::InitWindow(true);
SetInputDeathRecipient();
TextListener::ResetParam();
auto ret = InputMethodController::GetInstance()->GetDefaultInputMethod(property);
auto defaultIme = ret == ErrorCode::NO_ERROR ? property->name : "default.inputmethod.unittest";
defaultImeTokenId_ = TddUtil::GetTestTokenID(defaultIme);
permissionTokenId_ = TddUtil::AllocTestTokenID(false, "undefine", { "ohos.permission.CONNECT_IME_ABILITY" });
}
void InputMethodControllerTest::TearDownTestCase(void)
{
IMSA_HILOGI("InputMethodControllerTest::TearDownTestCase");
TddUtil::RestoreSelfTokenID();
TextListener::ResetParam();
TddUtil::DestroyWindow();
inputMethodController_->SetControllerListener(nullptr);
TddUtil::RestoreSelfTokenID();
}
void InputMethodControllerTest::SetUp(void)
@ -466,15 +476,6 @@ bool InputMethodControllerTest::WaitKeyEventCallback()
return consumeResult_;
}
void InputMethodControllerTest::SetDefaultIme()
{
TddUtil::RestoreSelfTokenID();
std::shared_ptr<Property> defaultImeProperty = std::make_shared<Property>();
auto ret = InputMethodController::GetInstance()->GetDefaultInputMethod(defaultImeProperty);
auto bundleName = ret == ErrorCode::NO_ERROR ? defaultImeProperty->name : "undefined";
TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, bundleName, { "ohos.permission.CONNECT_IME_ABILITY" }));
}
/**
* @tc.name: testIMCAttach001
* @tc.desc: IMC Attach.
@ -784,6 +785,7 @@ HWTEST_F(InputMethodControllerTest, testShowTextInput, TestSize.Level0)
HWTEST_F(InputMethodControllerTest, testShowSoftKeyboard, TestSize.Level0)
{
IMSA_HILOGI("IMC ShowSoftKeyboard Test START");
TokenScope scope(InputMethodControllerTest::permissionTokenId_);
imeListener_->keyboardState_ = false;
TextListener::ResetParam();
int32_t ret = inputMethodController_->ShowSoftKeyboard();
@ -863,6 +865,7 @@ HWTEST_F(InputMethodControllerTest, testOnEditorAttributeChanged, TestSize.Level
HWTEST_F(InputMethodControllerTest, testHideSoftKeyboard, TestSize.Level0)
{
IMSA_HILOGI("IMC HideSoftKeyboard Test START");
TokenScope scope(InputMethodControllerTest::permissionTokenId_);
imeListener_->keyboardState_ = true;
TextListener::ResetParam();
int32_t ret = inputMethodController_->HideSoftKeyboard();
@ -1131,7 +1134,7 @@ HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_001, TestSize.Level0)
HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_002, TestSize.Level0)
{
IMSA_HILOGI("IMC testSendPrivateCommand_002 Test START");
InputMethodControllerTest::SetDefaultIme();
TokenScope scope(InputMethodControllerTest::defaultImeTokenId_);
InputMethodEngineListenerImpl::ResetParam();
inputMethodController_->Close();
std::unordered_map<std::string, PrivateDataValue> privateCommand;
@ -1155,7 +1158,7 @@ HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_002, TestSize.Level0)
HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_003, TestSize.Level0)
{
IMSA_HILOGI("IMC testSendPrivateCommand_003 Test START");
InputMethodControllerTest::SetDefaultIme();
TokenScope scope(InputMethodControllerTest::defaultImeTokenId_);
InputMethodEngineListenerImpl::ResetParam();
auto ret = inputMethodController_->Attach(textListener_, false);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
@ -1177,7 +1180,7 @@ HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_003, TestSize.Level0)
HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_004, TestSize.Level0)
{
IMSA_HILOGI("IMC testSendPrivateCommand_004 Test START");
InputMethodControllerTest::SetDefaultIme();
TokenScope scope(InputMethodControllerTest::defaultImeTokenId_);
InputMethodEngineListenerImpl::ResetParam();
auto ret = inputMethodController_->Attach(textListener_, false);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
@ -1204,7 +1207,7 @@ HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_004, TestSize.Level0)
HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_005, TestSize.Level0)
{
IMSA_HILOGI("IMC testSendPrivateCommand_005 Test START");
InputMethodControllerTest::SetDefaultIme();
TokenScope scope(InputMethodControllerTest::defaultImeTokenId_);
InputMethodEngineListenerImpl::ResetParam();
auto ret = inputMethodController_->Attach(textListener_, false);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
@ -1231,7 +1234,7 @@ HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_005, TestSize.Level0)
HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_006, TestSize.Level0)
{
IMSA_HILOGI("IMC testSendPrivateCommand_006 Test START");
InputMethodControllerTest::SetDefaultIme();
TokenScope scope(InputMethodControllerTest::defaultImeTokenId_);
InputMethodEngineListenerImpl::ResetParam();
std::unordered_map<std::string, PrivateDataValue> privateCommand;
PrivateDataValue privateDataValue1 = std::string("stringValue");
@ -1259,7 +1262,7 @@ HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_006, TestSize.Level0)
HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_007, TestSize.Level0)
{
IMSA_HILOGI("IMC testSendPrivateCommand_007 Test START");
InputMethodControllerTest::SetDefaultIme();
TokenScope scope(InputMethodControllerTest::defaultImeTokenId_);
TextListener::ResetParam();
auto ret = inputMethodController_->Attach(textListener_, false);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
@ -1284,7 +1287,7 @@ HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_008, TestSize.Level0)
IMSA_HILOGI("IMC testSendPrivateCommand_008 Test START");
auto ret = inputMethodController_->Attach(textListener_, false);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodControllerTest::SetDefaultIme();
TokenScope scope(InputMethodControllerTest::defaultImeTokenId_);
TextListener::ResetParam();
std::unordered_map<std::string, PrivateDataValue> privateCommand1{ { "v",
string(PRIVATE_COMMAND_SIZE_MAX - 2, 'a') } };

View File

@ -25,7 +25,6 @@
#include <cstdint>
#include <string>
#include "access_util.h"
#include "display_manager.h"
#include "global.h"
#include "input_method_ability.h"
@ -50,12 +49,14 @@ public:
void SetUp();
void TearDown();
static std::shared_ptr<InputMethodPanel> CreatePanel();
static void DestroyPanel(const std::shared_ptr<InputMethodPanel> &panel);
static void Attach();
static void InitPanel();
static void ClearPanel();
static bool TriggerShowCallback(std::shared_ptr<InputMethodPanel> &inputMethodPanel);
static bool TriggerHideCallback(std::shared_ptr<InputMethodPanel> &inputMethodPanel);
static void ImaCreatePanel(const PanelInfo &info, std::shared_ptr<InputMethodPanel> &panel);
static void ImaDestroyPanel(const std::shared_ptr<InputMethodPanel> &panel);
static void ImcPanelListeningTestRestore();
static void ImcPanelShowNumCheck(uint32_t num);
static void ImcPanelHideNumCheck(uint32_t num);
@ -103,7 +104,6 @@ public:
static constexpr int32_t INTERVAL = 10;
static std::shared_ptr<AppExecFwk::EventHandler> panelHandler_;
static uint64_t sysTokenId_;
static uint64_t testTokenId_;
static int32_t currentImeUid_;
static uint64_t currentImeTokenId_;
static sptr<OnTextChangedListener> textListener_;
@ -148,7 +148,6 @@ sptr<InputMethodAbility> InputMethodPanelTest::ima_{ nullptr };
uint32_t InputMethodPanelTest::windowWidth_ = 0;
uint32_t InputMethodPanelTest::windowHeight_ = 0;
uint64_t InputMethodPanelTest::sysTokenId_ = 0;
uint64_t InputMethodPanelTest::testTokenId_ = 0;
uint64_t InputMethodPanelTest::currentImeTokenId_ = 0;
int32_t InputMethodPanelTest::currentImeUid_ = 0;
sptr<OnTextChangedListener> InputMethodPanelTest::textListener_{ nullptr };
@ -167,10 +166,9 @@ void InputMethodPanelTest::SetUpTestCase(void)
// set token as current input method
std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest";
testTokenId_ = TddUtil::AllocTestTokenID(true, bundleName, {});
currentImeTokenId_ = AccessUtil::GetTokenID(bundleName);
currentImeUid_ = AccessUtil::GetUid(bundleName);
sysTokenId_ = AccessUtil::AllocTestTokenID(true, "undefined", {});
currentImeTokenId_ = TddUtil::GetTestTokenID(bundleName);
currentImeUid_ = TddUtil::GetUid(bundleName);
sysTokenId_ = TddUtil::AllocTestTokenID(true, "undefined", {});
{
TokenScope tokenScope(currentImeTokenId_);
ima_ = InputMethodAbility::GetInstance();
@ -185,6 +183,7 @@ void InputMethodPanelTest::TearDownTestCase(void)
IMSA_HILOGI("InputMethodPanelTest::TearDownTestCase");
ClearPanel();
TddUtil::RestoreSelfTokenID();
TddUtil::KillImsaProcess();
}
void InputMethodPanelTest::SetUp(void)
@ -200,6 +199,7 @@ void InputMethodPanelTest::TearDown(void)
std::shared_ptr<InputMethodPanel> InputMethodPanelTest::CreatePanel()
{
AccessScope scope(currentImeTokenId_, currentImeUid_);
auto inputMethodPanel = std::make_shared<InputMethodPanel>();
PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
auto ret = inputMethodPanel->CreatePanel(nullptr, panelInfo);
@ -207,6 +207,14 @@ std::shared_ptr<InputMethodPanel> InputMethodPanelTest::CreatePanel()
return inputMethodPanel;
}
void InputMethodPanelTest::DestroyPanel(const std::shared_ptr<InputMethodPanel> &panel)
{
ASSERT_NE(panel, nullptr);
AccessScope scope(currentImeTokenId_, currentImeUid_);
auto ret = panel->DestroyPanel();
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
}
void InputMethodPanelTest::ImaCreatePanel(const PanelInfo &info, std::shared_ptr<InputMethodPanel> &panel)
{
AccessScope scope(currentImeTokenId_, currentImeUid_);
@ -214,6 +222,13 @@ void InputMethodPanelTest::ImaCreatePanel(const PanelInfo &info, std::shared_ptr
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
}
void InputMethodPanelTest::ImaDestroyPanel(const std::shared_ptr<InputMethodPanel> &panel)
{
AccessScope scope(currentImeTokenId_, currentImeUid_);
auto ret = ima_->DestroyPanel(panel);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
}
void InputMethodPanelTest::Attach()
{
TddUtil::InitWindow(true);
@ -342,6 +357,8 @@ void InputMethodPanelTest::ImcPanelListeningTestRestore()
void InputMethodPanelTest::TestShowPanel(const std::shared_ptr<InputMethodPanel> &panel)
{
ASSERT_NE(panel, nullptr);
// set tokenId as current ime
TokenScope scope(currentImeTokenId_);
auto ret = panel->ShowPanel();
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
}
@ -586,8 +603,7 @@ HWTEST_F(InputMethodPanelTest, testIsPanelShown_001, TestSize.Level0)
InputMethodPanelTest::TestHidePanel(inputMethodPanel);
InputMethodPanelTest::TestIsPanelShown(panelInfo, false);
ret = ima_->DestroyPanel(inputMethodPanel);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel);
InputMethodPanelTest::imc_->Close();
TddUtil::DestroyWindow();
}
@ -615,8 +631,7 @@ HWTEST_F(InputMethodPanelTest, testIsPanelShown_002, TestSize.Level0)
panelInfo.panelFlag = PanelFlag::FLG_FLOATING;
InputMethodPanelTest::TestIsPanelShown(panelInfo, true);
ret = ima_->DestroyPanel(inputMethodPanel);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel);
InputMethodPanelTest::imc_->Close();
TddUtil::DestroyWindow();
}
@ -642,8 +657,7 @@ HWTEST_F(InputMethodPanelTest, testIsPanelShown_003, TestSize.Level0)
InputMethodPanelTest::TestHidePanel(inputMethodPanel);
InputMethodPanelTest::TestIsPanelShown(panelInfo, false);
ret = ima_->DestroyPanel(inputMethodPanel);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel);
InputMethodPanelTest::imc_->Close();
TddUtil::DestroyWindow();
}
@ -670,8 +684,7 @@ HWTEST_F(InputMethodPanelTest, testSetPanelStatusListener, TestSize.Level0)
EXPECT_TRUE(InputMethodPanelTest::TriggerShowCallback(inputMethodPanel));
EXPECT_TRUE(InputMethodPanelTest::TriggerHideCallback(inputMethodPanel));
ret = inputMethodPanel->DestroyPanel();
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel);
}
/**
@ -767,8 +780,10 @@ HWTEST_F(InputMethodPanelTest, testChangePanelFlag, TestSize.Level0)
HWTEST_F(InputMethodPanelTest, testClearPanelListener, TestSize.Level0)
{
IMSA_HILOGI("InputMethodPanelTest::testClearPanelListener start.");
AccessScope scope(InputMethodPanelTest::currentImeTokenId_, InputMethodPanelTest::currentImeUid_);
auto inputMethodPanel = InputMethodPanelTest::CreatePanel();
auto statusListener = std::make_shared<InputMethodPanelTest::PanelStatusListenerImpl>();
inputMethodPanel->SetPanelStatusListener(statusListener, "show");
inputMethodPanel->SetPanelStatusListener(statusListener, "hide");
inputMethodPanel->ClearPanelListener("show");
EXPECT_FALSE(InputMethodPanelTest::TriggerShowCallback(inputMethodPanel));
@ -778,8 +793,7 @@ HWTEST_F(InputMethodPanelTest, testClearPanelListener, TestSize.Level0)
EXPECT_FALSE(InputMethodPanelTest::TriggerShowCallback(inputMethodPanel));
EXPECT_FALSE(InputMethodPanelTest::TriggerHideCallback(inputMethodPanel));
auto ret = inputMethodPanel->DestroyPanel();
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodPanelTest::DestroyPanel(inputMethodPanel);
}
/**
@ -791,7 +805,6 @@ HWTEST_F(InputMethodPanelTest, testRegisterListener, TestSize.Level0)
{
// on('show')->on('hide')->show->hide->off('show')->show->hide->on('show')->show
IMSA_HILOGI("InputMethodPanelTest::testRegisterListener start.");
AccessScope scope(InputMethodPanelTest::currentImeTokenId_, InputMethodPanelTest::currentImeUid_);
auto inputMethodPanel = InputMethodPanelTest::CreatePanel();
auto statusListener = std::make_shared<InputMethodPanelTest::PanelStatusListenerImpl>();
@ -807,8 +820,7 @@ HWTEST_F(InputMethodPanelTest, testRegisterListener, TestSize.Level0)
inputMethodPanel->SetPanelStatusListener(statusListener, "show");
EXPECT_TRUE(InputMethodPanelTest::TriggerShowCallback(inputMethodPanel));
auto ret = inputMethodPanel->DestroyPanel();
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodPanelTest::DestroyPanel(inputMethodPanel);
}
/*
@ -819,10 +831,10 @@ HWTEST_F(InputMethodPanelTest, testRegisterListener, TestSize.Level0)
HWTEST_F(InputMethodPanelTest, testImcPanelListening_001, TestSize.Level0)
{
IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_001 start.");
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
{
// set system app
TokenScope tokenScope(InputMethodPanelTest::sysTokenId_);
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_SHOW_MASK, listener);
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_HIDE_MASK, listener);
}
@ -852,10 +864,10 @@ HWTEST_F(InputMethodPanelTest, testImcPanelListening_001, TestSize.Level0)
HWTEST_F(InputMethodPanelTest, testImcPanelListening_002, TestSize.Level0)
{
IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_002 start.");
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
{
// set system app
TokenScope tokenScope(InputMethodPanelTest::sysTokenId_);
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_SHOW_MASK, listener);
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_HIDE_MASK, listener);
}
@ -887,10 +899,10 @@ HWTEST_F(InputMethodPanelTest, testImcPanelListening_002, TestSize.Level0)
HWTEST_F(InputMethodPanelTest, testImcPanelListening_003, TestSize.Level0)
{
IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_003 start.");
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
{
// set system app
TokenScope tokenScope(InputMethodPanelTest::sysTokenId_);
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_SHOW_MASK, listener);
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_HIDE_MASK, listener);
}
@ -919,19 +931,19 @@ HWTEST_F(InputMethodPanelTest, testImcPanelListening_003, TestSize.Level0)
HWTEST_F(InputMethodPanelTest, testImcPanelListening_004, TestSize.Level0)
{
IMSA_HILOGI("InputMethodPanelTest::testImcPanelListening_004 start.");
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
{
// set system app
TokenScope tokenScope(InputMethodPanelTest::sysTokenId_);
auto listener = std::make_shared<InputMethodSettingListenerImpl>();
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_SHOW_MASK, listener);
ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_IME_HIDE_MASK, listener);
}
InputMethodPanelTest::ImcPanelListeningTestRestore();
InputMethodPanelTest::TestShowPanel(InputMethodPanelTest::inputMethodPanel_);
InputMethodPanelTest::TestShowPanel(InputMethodPanelTest::inputMethodStatusBar_);
InputMethodPanelTest::ImcPanelShowNumCheck(0);
InputMethodPanelTest::ImcPanelListeningTestRestore();
InputMethodPanelTest::TestHidePanel(InputMethodPanelTest::inputMethodPanel_);
InputMethodPanelTest::TestHidePanel(InputMethodPanelTest::inputMethodStatusBar_);
InputMethodPanelTest::ImcPanelHideNumCheck(0);
{
// set system app