优化tdd用例

Signed-off-by: cy7717 <chenyu301@huawei.com>
This commit is contained in:
cy7717 2023-06-25 10:23:39 +08:00
parent e215fafb5f
commit 804305bd4d
5 changed files with 15 additions and 58 deletions

View File

@ -83,7 +83,9 @@ public:
void InputMethodSwitchTest::SetUpTestCase(void)
{
IMSA_HILOGI("InputMethodSwitchTest::SetUpTestCase");
TddUtil::GrantNativePermission();
TddUtil::StorageSelfTokenID();
TddUtil::AllocTestTokenID("ohos.inputMethod.test");
TddUtil::SetTestTokenID();
imc_ = InputMethodController::GetInstance();
imc_->SetSettingListener(std::make_shared<InputMethodSettingListenerImpl>());
imc_->UpdateListenEventFlag("imeChange", true);
@ -93,6 +95,8 @@ void InputMethodSwitchTest::TearDownTestCase(void)
{
IMSA_HILOGI("InputMethodSwitchTest::TearDownTestCase");
InputMethodController::GetInstance()->Close();
TddUtil::RestoreSelfTokenID();
TddUtil::DeleteTestTokenID();
}
void InputMethodSwitchTest::SetUp(void)

View File

@ -79,7 +79,9 @@ public:
void NewImeSwitchTest::SetUpTestCase(void)
{
IMSA_HILOGI("NewImeSwitchTest::SetUpTestCase");
TddUtil::GrantNativePermission();
TddUtil::StorageSelfTokenID();
TddUtil::AllocTestTokenID("ohos.inputMethod.test");
TddUtil::SetTestTokenID();
imc_ = InputMethodController::GetInstance();
imc_->SetSettingListener(std::make_shared<InputMethodSettingListenerImpl>());
imc_->UpdateListenEventFlag("imeChange", true);
@ -89,6 +91,8 @@ void NewImeSwitchTest::TearDownTestCase(void)
{
IMSA_HILOGI("NewImeSwitchTest::TearDownTestCase");
InputMethodController::GetInstance()->Close();
TddUtil::RestoreSelfTokenID();
TddUtil::DeleteTestTokenID();
}
void NewImeSwitchTest::SetUp(void)

View File

@ -33,7 +33,6 @@
using namespace testing::ext;
namespace OHOS {
namespace MiscServices {
constexpr const uint16_t EACH_LINE_LENGTH = 500;
class TextListener : public OnTextChangedListener {
public:
TextListener() = default;
@ -90,7 +89,6 @@ class PermissionVerificationExceptionTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
static bool ExecuteCmd(const std::string &cmd, std::string &result);
void SetUp();
void TearDown();
static void AllocAndSetTestTokenID(const std::string &bundleName);
@ -127,7 +125,7 @@ void PermissionVerificationExceptionTest::TearDownTestCase(void)
TddUtil::DeleteTestTokenID();
std::string result;
auto property = imc_->GetCurrentInputMethod();
auto ret = PermissionVerificationExceptionTest::ExecuteCmd("ps -ef| grep " + property->name, result);
auto ret = TddUtil::ExecuteCmd("ps -ef| grep " + property->name, result);
IMSA_HILOGI("ret: %{public}d, result is: %{public}s", ret, result.c_str());
std::smatch regResult;
std::regex pattern("\\s+\\d{3,6}");
@ -139,7 +137,7 @@ void PermissionVerificationExceptionTest::TearDownTestCase(void)
return;
}
IMSA_HILOGI("pid is %{public}s.", pid.c_str());
ret = PermissionVerificationExceptionTest::ExecuteCmd("kill " + pid, result);
ret = TddUtil::ExecuteCmd("kill " + pid, result);
IMSA_HILOGI("ret: %{public}d, result is: %{public}s", ret, result.c_str());
}
@ -153,24 +151,6 @@ void PermissionVerificationExceptionTest::TearDown(void)
IMSA_HILOGI("PermissionVerificationExceptionTest::TearDown");
}
bool PermissionVerificationExceptionTest::ExecuteCmd(const std::string &cmd, std::string &result)
{
char buff[EACH_LINE_LENGTH] = { 0x00 };
std::stringstream output;
FILE *ptr = popen(cmd.c_str(), "r");
if (ptr != nullptr) {
while (fgets(buff, sizeof(buff), ptr) != nullptr) {
output << buff;
}
pclose(ptr);
ptr = nullptr;
} else {
return false;
}
result = output.str();
return true;
}
/**
* @tc.name: ShowAndHideSoftKeyboard
* @tc.desc: PermissionVerificationExceptionTest ShowAndHideSoftKeyboard.

View File

@ -40,7 +40,6 @@ using namespace OHOS::AccountSA;
constexpr int32_t INVALID_USER_ID = -1;
constexpr int32_t MAIN_USER_ID = 100;
constexpr const uint16_t EACH_LINE_LENGTH = 500;
constexpr const uint16_t TOTAL_LENGTH = 4096;
uint64_t TddUtil::selfTokenID_ = 0;
uint64_t TddUtil::testTokenID_ = 0;
int64_t TddUtil::selfUid_ = -1;
@ -135,50 +134,21 @@ void TddUtil::RestoreSelfUid()
setuid(selfUid_);
}
void TddUtil::GrantNativePermission()
{
const char **perms = new const char *[1];
perms[0] = "ohos.permission.CONNECT_IME_ABILITY";
TokenInfoParams infoInstance = {
.dcapsNum = 0,
.permsNum = 1,
.aclsNum = 0,
.dcaps = nullptr,
.perms = perms,
.acls = nullptr,
.processName = "inputmethod_imf",
.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;
}
bool TddUtil::ExecuteCmd(const std::string &cmd, std::string &result)
{
char buff[EACH_LINE_LENGTH] = { 0x00 };
char output[TOTAL_LENGTH] = { 0x00 };
std::stringstream output;
FILE *ptr = popen(cmd.c_str(), "r");
if (ptr != nullptr) {
while (fgets(buff, sizeof(buff), ptr) != nullptr) {
if (strcat_s(output, sizeof(output), buff) != 0) {
pclose(ptr);
ptr = nullptr;
return false;
}
output << buff;
}
pclose(ptr);
ptr = nullptr;
} else {
return false;
}
result = std::string(output);
result = output.str();
return true;
}
} // namespace MiscServices

View File

@ -30,7 +30,6 @@ public:
static void StorageSelfUid();
static void SetTestUid();
static void RestoreSelfUid();
static void GrantNativePermission();
static bool ExecuteCmd(const std::string &cmd, std::string &result);
private: