• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #define private public
16 #define protected public
17 #include "full_ime_info_manager.h"
18 #include "settings_data_utils.h"
19 #include "user_session_manager.h"
20 #include "task_manager.h"
21 #undef private
22 
23 #include <unistd.h>
24 
25 #include <csignal>
26 #include <cstdint>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 #include "accesstoken_kit.h"
32 #include "datashare_helper.h"
33 #include "global.h"
34 #include "if_system_ability_manager.h"
35 #include "input_method_controller.h"
36 #include "iservice_registry.h"
37 #include "nativetoken_kit.h"
38 #include "os_account_manager.h"
39 #include "scope_utils.h"
40 #include "system_ability.h"
41 #include "system_ability_definition.h"
42 #include "tdd_util.h"
43 #include "token_setproc.h"
44 
45 namespace OHOS {
46 namespace MiscServices {
47 using namespace OHOS::Security::AccessToken;
48 using namespace OHOS::AccountSA;
49 using namespace Rosen;
50 constexpr int32_t INVALID_USER_ID = -1;
51 constexpr int32_t MAIN_USER_ID = 100;
52 constexpr const uint16_t EACH_LINE_LENGTH = 500;
53 constexpr int32_t PERMISSION_NUM = 6;
54 constexpr int32_t FIRST_PARAM_INDEX = 0;
55 constexpr int32_t SECOND_PARAM_INDEX = 1;
56 constexpr int32_t THIRD_PARAM_INDEX = 2;
57 constexpr int32_t FOURTH_PARAM_INDEX = 3;
58 constexpr int32_t FIFTH_PARAM_INDEX = 4;
59 constexpr int32_t SIXTH_PARAM_INDEX = 5;
60 static constexpr int32_t MAX_TIMEOUT_WAIT_FOCUS = 2000;
61 uint64_t TddUtil::selfTokenID_ = 0;
62 int32_t TddUtil::userID_ = INVALID_USER_ID;
63 std::string TddUtil::currentBundleNameMock_;
64 sptr<Window> TddUtil::WindowManager::window_ = nullptr;
65 int32_t TddUtil::WindowManager::currentWindowId_ = 0;
66 uint64_t TddUtil::WindowManager::windowTokenId_ = 0;
67 std::shared_ptr<BlockData<bool>> FocusChangedListenerTestImpl::isFocused_ =
68     std::make_shared<BlockData<bool>>(MAX_TIMEOUT_WAIT_FOCUS, false);
69 std::shared_ptr<BlockData<bool>> FocusChangedListenerTestImpl::unFocused_ =
70     std::make_shared<BlockData<bool>>(MAX_TIMEOUT_WAIT_FOCUS, false);
71 static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime";
OnFocused(const sptr<Rosen::FocusChangeInfo> & focusChangeInfo)72 void FocusChangedListenerTestImpl::OnFocused(const sptr<Rosen::FocusChangeInfo> &focusChangeInfo)
73 {
74     IMSA_HILOGI("get onFocus information from window manager.");
75     if (focusChangeInfo->windowId_ == TddUtil::WindowManager::currentWindowId_) {
76         getFocus_ = true;
77         isFocused_->SetValue(getFocus_);
78         unFocused_->Clear(false);
79     }
80 }
81 
OnUnfocused(const sptr<Rosen::FocusChangeInfo> & focusChangeInfo)82 void FocusChangedListenerTestImpl::OnUnfocused(const sptr<Rosen::FocusChangeInfo> &focusChangeInfo)
83 {
84     IMSA_HILOGI("get unfocus information from window manager.");
85     if (focusChangeInfo->windowId_ == TddUtil::WindowManager::currentWindowId_) {
86         getFocus_ = false;
87         isFocused_->Clear(false);
88         bool unFocus = !getFocus_;
89         unFocused_->SetValue(unFocus);
90     }
91 }
92 
GetCurrentUserId()93 int32_t TddUtil::GetCurrentUserId()
94 {
95     if (userID_ != INVALID_USER_ID) {
96         return userID_;
97     }
98     int32_t userId = -1;
99     auto ret = OsAccountManager::GetForegroundOsAccountLocalId(userId);
100     if (ret != ErrorCode::NO_ERROR) {
101         IMSA_HILOGE("GetForegroundOsAccountLocalId failed");
102         return MAIN_USER_ID;
103     }
104     return userId;
105 }
StorageSelfTokenID()106 void TddUtil::StorageSelfTokenID()
107 {
108     selfTokenID_ = GetSelfTokenID();
109 }
110 
AllocTestTokenID(bool isSystemApp,const std::string & bundleName,const std::vector<std::string> & premission)111 uint64_t TddUtil::AllocTestTokenID(
112     bool isSystemApp, const std::string &bundleName, const std::vector<std::string> &premission)
113 {
114     IMSA_HILOGI("bundleName: %{public}s", bundleName.c_str());
115     HapInfoParams infoParams = { .userID = GetCurrentUserId(),
116         .bundleName = bundleName,
117         .instIndex = 0,
118         .appIDDesc = bundleName,
119         .isSystemApp = isSystemApp };
120     std::vector<PermissionStateFull> permStateList;
121     for (const auto &prem : premission) {
122         PermissionStateFull permissionState = { .permissionName = prem,
123             .isGeneral = true,
124             .resDeviceID = { "local" },
125             .grantStatus = { PermissionState::PERMISSION_GRANTED },
126             .grantFlags = { 1 } };
127         permStateList.push_back(permissionState);
128     }
129     HapPolicyParams policyParams = {
130         .apl = APL_NORMAL, .domain = bundleName, .permList = {}, .permStateList = permStateList
131     };
132     if (premission.empty()) {
133         policyParams = { .apl = APL_NORMAL, .domain = bundleName, .permList = {}, .permStateList = {} };
134     }
135     auto tokenInfo = AccessTokenKit::AllocHapToken(infoParams, policyParams);
136     return tokenInfo.tokenIDEx;
137 }
138 
GetTestTokenID(const std::string & bundleName)139 uint64_t TddUtil::GetTestTokenID(const std::string &bundleName)
140 {
141     HapInfoParams infoParams = { .userID = GetCurrentUserId(), .bundleName = bundleName,
142         .instIndex = 0, .appIDDesc = "ohos.inputmethod_test.demo"
143     };
144     return AccessTokenKit::GetHapTokenID(infoParams.userID, infoParams.bundleName, infoParams.instIndex);
145 }
146 
DeleteTestTokenID(uint64_t tokenId)147 void TddUtil::DeleteTestTokenID(uint64_t tokenId)
148 {
149     AccessTokenKit::DeleteToken(tokenId);
150 }
151 
SetTestTokenID(uint64_t tokenId)152 void TddUtil::SetTestTokenID(uint64_t tokenId)
153 {
154     auto ret = SetSelfTokenID(tokenId);
155     IMSA_HILOGI("SetSelfTokenID ret: %{public}d", ret);
156 }
157 
RestoreSelfTokenID()158 void TddUtil::RestoreSelfTokenID()
159 {
160     auto ret = SetSelfTokenID(selfTokenID_);
161     IMSA_HILOGI("SetSelfTokenID ret = %{public}d", ret);
162 }
163 
GetCurrentTokenID()164 uint64_t TddUtil::GetCurrentTokenID()
165 {
166     return GetSelfTokenID();
167 }
168 
GetUid(const std::string & bundleName)169 int32_t TddUtil::GetUid(const std::string &bundleName)
170 {
171     auto bundleMgr = GetBundleMgr();
172     if (bundleMgr == nullptr) {
173         IMSA_HILOGE("bundleMgr nullptr");
174         return -1;
175     }
176     auto uid = bundleMgr->GetUidByBundleName(bundleName, GetCurrentUserId());
177     if (uid == -1) {
178         IMSA_HILOGE("failed to get information and the parameters may be wrong.");
179         return -1;
180     }
181     IMSA_HILOGI("bundleName: %{public}s, uid: %{public}d", bundleName.c_str(), uid);
182     return uid;
183 }
184 
SetSelfUid(int32_t uid)185 void TddUtil::SetSelfUid(int32_t uid)
186 {
187     setuid(uid);
188     IMSA_HILOGI("set uid to: %{public}d", uid);
189 }
190 
ExecuteCmd(const std::string & cmd,std::string & result)191 bool TddUtil::ExecuteCmd(const std::string &cmd, std::string &result)
192 {
193     char buff[EACH_LINE_LENGTH] = { 0x00 };
194     std::stringstream output;
195     FILE *ptr = popen(cmd.c_str(), "r");
196     if (ptr != nullptr) {
197         IMSA_HILOGI("Execute cmd: %{public}s", cmd.c_str());
198         while (fgets(buff, sizeof(buff), ptr) != nullptr) {
199             output << buff;
200         }
201         pclose(ptr);
202         ptr = nullptr;
203     } else {
204         return false;
205     }
206     result = output.str();
207     return true;
208 }
209 
GetImsaPid()210 pid_t TddUtil::GetImsaPid()
211 {
212     auto currentToken = GetSelfTokenID();
213     GrantNativePermission();
214     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
215     SystemProcessInfo info;
216     int32_t ret = saMgr->GetSystemProcessInfo(INPUT_METHOD_SYSTEM_ABILITY_ID, info);
217     if (ret != ErrorCode::NO_ERROR) {
218         IMSA_HILOGE("failed to get sa info, ret: %{public}d", ret);
219         return -1;
220     }
221     SetSelfTokenID(currentToken);
222     return info.pid;
223 }
224 
KillImsaProcess()225 bool TddUtil::KillImsaProcess()
226 {
227     pid_t pid = GetImsaPid();
228     if (pid == -1) {
229         IMSA_HILOGE("failed to get pid");
230         return false;
231     }
232     auto ret = kill(pid, SIGTERM);
233     if (ret != 0) {
234         IMSA_HILOGE("Kill failed, ret: %{public}d", ret);
235         return false;
236     }
237     IMSA_HILOGI("Kill [%{public}d] success", pid);
238     return true;
239 }
240 
GetBundleMgr()241 sptr<OHOS::AppExecFwk::IBundleMgr> TddUtil::GetBundleMgr()
242 {
243     sptr<ISystemAbilityManager> systemAbilityManager =
244         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
245     if (systemAbilityManager == nullptr) {
246         IMSA_HILOGE("systemAbilityManager is nullptr");
247         return nullptr;
248     }
249     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
250     if (remoteObject == nullptr) {
251         IMSA_HILOGE("remoteObject is nullptr");
252         return nullptr;
253     }
254     return iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
255 }
256 
GetUserIdByBundleName(const std::string & bundleName,const int currentUserId)257 int TddUtil::GetUserIdByBundleName(const std::string &bundleName, const int currentUserId)
258 {
259     auto bundleMgr = TddUtil::GetBundleMgr();
260     if (bundleMgr == nullptr) {
261         IMSA_HILOGE("Get bundleMgr failed.");
262         return -1;
263     }
264     auto uid = bundleMgr->GetUidByBundleName(bundleName, currentUserId);
265     if (uid == -1) {
266         IMSA_HILOGE("failed to get information and the parameters may be wrong.");
267         return -1;
268     }
269     // 200000 means userId = uid / 200000.
270     return uid / 200000;
271 }
272 
GrantNativePermission()273 void TddUtil::GrantNativePermission()
274 {
275     const char **perms = new const char *[PERMISSION_NUM];
276     perms[FIRST_PARAM_INDEX] = "ohos.permission.MANAGE_SECURE_SETTINGS";
277     perms[SECOND_PARAM_INDEX] = "ohos.permission.CONNECT_IME_ABILITY";
278     perms[THIRD_PARAM_INDEX] = "ohos.permission.MANAGE_SETTINGS";
279     perms[FOURTH_PARAM_INDEX] = "ohos.permission.INJECT_INPUT_EVENT";
280     perms[FIFTH_PARAM_INDEX] = "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED";
281     perms[SIXTH_PARAM_INDEX] = "ohos.permission.GET_RUNNING_INFO";
282     TokenInfoParams infoInstance = {
283         .dcapsNum = 0,
284         .permsNum = PERMISSION_NUM,
285         .aclsNum = 0,
286         .dcaps = nullptr,
287         .perms = perms,
288         .acls = nullptr,
289         .processName = "imf_test",
290         .aplStr = "system_core",
291     };
292     uint64_t tokenId = GetAccessTokenId(&infoInstance);
293     int res = SetSelfTokenID(tokenId);
294     if (res == 0) {
295         IMSA_HILOGI("SetSelfTokenID success!");
296     } else {
297         IMSA_HILOGE("SetSelfTokenID fail!");
298     }
299     AccessTokenKit::ReloadNativeTokenInfo();
300     delete[] perms;
301 }
302 
PushEnableImeValue(const std::string & key,const std::string & value)303 void TddUtil::PushEnableImeValue(const std::string &key, const std::string &value)
304 {
305     IMSA_HILOGI("key: %{public}s, value: %{public}s", key.c_str(), value.c_str());
306     auto helper = SettingsDataUtils::GetInstance().CreateDataShareHelper(SETTING_URI_PROXY);
307     if (helper == nullptr) {
308         IMSA_HILOGE("helper is nullptr.");
309         return;
310     }
311     DataShare::DataShareValueObject keyObj(key);
312     DataShare::DataShareValueObject valueObj(value);
313     DataShare::DataShareValuesBucket bucket;
314     bucket.Put(SETTING_COLUMN_KEYWORD, keyObj);
315     bucket.Put(SETTING_COLUMN_VALUE, valueObj);
316     DataShare::DataSharePredicates predicates;
317     predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
318     Uri uri(SettingsDataUtils::GetInstance().GenerateTargetUri(SETTING_URI_PROXY, key));
319     if (helper->Update(uri, predicates, bucket) <= 0) {
320         int index = helper->Insert(uri, bucket);
321         IMSA_HILOGI("no data exists, insert ret index: %{public}d", index);
322     } else {
323         IMSA_HILOGI("data exits");
324     }
325     bool ret = SettingsDataUtils::GetInstance().ReleaseDataShareHelper(helper);
326     IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret);
327 }
328 
DeleteGlobalTable(const std::string & key)329 void TddUtil::DeleteGlobalTable(const std::string &key)
330 {
331     IMSA_HILOGI("key: %{public}s", key.c_str());
332     std::string uriProxy = SETTING_URI_PROXY;
333     DeleteTable(key, uriProxy);
334 }
335 
DeleteUserTable(int32_t userId,const std::string & key)336 void TddUtil::DeleteUserTable(int32_t userId, const std::string &key)
337 {
338     IMSA_HILOGI("userId:%{public}d, key:%{public}s", userId, key.c_str());
339     std::string uriProxy = SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true";
340     DeleteTable(key, uriProxy);
341 }
342 
DeleteTable(const std::string & key,const std::string & uriProxy)343 void TddUtil::DeleteTable(const std::string &key, const std::string &uriProxy)
344 {
345     auto helper = SettingsDataUtils::GetInstance().CreateDataShareHelper(uriProxy);
346     if (helper == nullptr) {
347         IMSA_HILOGE("helper is nullptr.");
348         return;
349     }
350     DataShare::DataSharePredicates predicates;
351     predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
352     Uri uri(SettingsDataUtils::GetInstance().GenerateTargetUri(uriProxy, key));
353     auto result = helper->DeleteEx(uri, predicates);
354     IMSA_HILOGI("Delete ret: [%{public}d, %{public}d]", result.first, result.second);
355     bool ret = SettingsDataUtils::GetInstance().ReleaseDataShareHelper(helper);
356     IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret);
357 }
358 
SetGlobalTable(const std::string & key,const std::string & content)359 void TddUtil::SetGlobalTable(const std::string &key, const std::string &content)
360 {
361     std::string uriProxy = SETTING_URI_PROXY;
362     SetTable(key, uriProxy, content);
363 }
SetUserTable(int32_t userId,const std::string & key,const std::string & content)364 void TddUtil::SetUserTable(int32_t userId, const std::string &key, const std::string &content)
365 {
366     std::string uriProxy = SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true";
367     SetTable(key, uriProxy, content);
368 }
369 
SetTable(const std::string & key,const std::string & uriProxy,const std::string & content)370 void TddUtil::SetTable(const std::string &key, const std::string &uriProxy, const std::string &content)
371 {
372     SettingsDataUtils::GetInstance().SetStringValue(uriProxy, key, content);
373 }
374 
GetTable(const std::string & key,const std::string & uriProxy)375 std::string TddUtil::GetTable(const std::string &key, const std::string &uriProxy)
376 {
377     std::string content;
378     auto ret = SettingsDataUtils::GetInstance().GetStringValue(uriProxy, key, content);
379     IMSA_HILOGI("%{public}s/%{public}s ret:%{public}d.", key.c_str(), uriProxy.c_str(), ret);
380     return content;
381 }
382 
GetGlobalTable(const std::string & key)383 std::string TddUtil::GetGlobalTable(const std::string &key)
384 {
385     std::string uriProxy = SETTING_URI_PROXY;
386     return GetTable(key, uriProxy);
387 }
388 
GetUserTable(int32_t userId,const std::string & key)389 std::string TddUtil::GetUserTable(int32_t userId, const std::string &key)
390 {
391     std::string uriProxy = SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true";
392     return GetTable(key, uriProxy);
393 }
394 
GetEnableData(std::string & value)395 int32_t TddUtil::GetEnableData(std::string &value)
396 {
397     auto ret = SettingsDataUtils::GetInstance().GetStringValue(SETTING_URI_PROXY, ENABLE_IME, value);
398     if (ret == ErrorCode::NO_ERROR) {
399         IMSA_HILOGI("success, value: %{public}s", value.c_str());
400     }
401     IMSA_HILOGI("GetStringValue ret: %{public}d", ret);
402     return ret;
403 }
404 
InitWindow(bool isShow)405 void TddUtil::InitWindow(bool isShow)
406 {
407     WindowManager::RegisterFocusChangeListener();
408     WindowManager::CreateWindow();
409     if (!isShow) {
410         return;
411     }
412     WindowManager::ShowWindow();
413     bool isFocused = FocusChangedListenerTestImpl::isFocused_->GetValue();
414     IMSA_HILOGI("getFocus end, isFocused = %{public}d", isFocused);
415 }
416 
DestroyWindow()417 void TddUtil::DestroyWindow()
418 {
419     WindowManager::HideWindow();
420     WindowManager::DestroyWindow();
421 }
422 
GetFocused()423 bool TddUtil::GetFocused()
424 {
425     WindowManager::ShowWindow();
426     bool isFocused = FocusChangedListenerTestImpl::isFocused_->GetValue();
427     IMSA_HILOGI("getFocus end, isFocused = %{public}d", isFocused);
428     return isFocused;
429 }
430 
GetUnfocused()431 bool TddUtil::GetUnfocused()
432 {
433     WindowManager::HideWindow();
434     bool unFocused = FocusChangedListenerTestImpl::unFocused_->GetValue();
435     IMSA_HILOGI("unFocused end, unFocused = %{public}d", unFocused);
436     return unFocused;
437 }
438 
InitCurrentImePermissionInfo()439 void TddUtil::InitCurrentImePermissionInfo()
440 {
441     auto userId = GetCurrentUserId();
442     auto session = UserSessionManager::GetInstance().GetUserSession(userId);
443     if (session == nullptr) {
444         UserSessionManager::GetInstance().AddUserSession(userId);
445     }
446     session = UserSessionManager::GetInstance().GetUserSession(userId);
447     if (session == nullptr) {
448         IMSA_HILOGE("session is nullptr.");
449         return;
450     }
451     std::shared_ptr<Property> property = nullptr;
452     InputMethodController::GetInstance()->GetDefaultInputMethod(property);
453     if (property == nullptr) {
454         IMSA_HILOGI("default ime is nullptr.");
455         return;
456     }
457     currentBundleNameMock_ = property->name;
458     session->InitImeData({ property->name, property->id });
459     ImeCfgManager::GetInstance().imeConfigs_ = {
460         { userId, property->name + "/" + property->id, "", false }
461     };
462 }
463 
EnabledAllIme()464 void TddUtil::EnabledAllIme()
465 {
466     auto imc = InputMethodController::GetInstance();
467     if (imc == nullptr) {
468         return;
469     }
470     std::vector<Property> props;
471     imc->ListInputMethod(false, props);
472     for (const auto &prop : props) {
473         imc->EnableIme(prop.name, prop.id, EnabledStatus::BASIC_MODE);
474     }
475 }
476 
DisabledAllIme()477 void TddUtil::DisabledAllIme()
478 {
479     auto imc = InputMethodController::GetInstance();
480     if (imc == nullptr) {
481         return;
482     }
483     std::vector<Property> props;
484     imc->ListInputMethod(true, props);
485     for (const auto &prop : props) {
486         imc->EnableIme(prop.name, prop.id, EnabledStatus::DISABLED);
487     }
488 }
489 
StartApp(const std::string & bundleName)490 void TddUtil::StartApp(const std::string &bundleName)
491 {
492     std::string cmd = std::string("aa start ability -a EntryAbility -b") + " " + bundleName;
493     std::string result;
494     auto ret = ExecuteCmd(cmd, result);
495     IMSA_HILOGI("ExecuteCmd ret = %{public}d", ret);
496     sleep(WAIT_APP_START_COMPLETE);
497 }
498 
StopApp(const std::string & bundleName)499 void TddUtil::StopApp(const std::string &bundleName)
500 {
501     static std::string cmd = std::string("aa force-stop") + " " + bundleName;
502     std::string result;
503     auto ret = ExecuteCmd(cmd, result);
504     IMSA_HILOGI("ExecuteCmd ret = %{public}d", ret);
505 }
506 
ClickApp(const std::string & cmd)507 void TddUtil::ClickApp(const std::string &cmd)
508 {
509     std::string result;
510     auto ret = TddUtil::ExecuteCmd(cmd, result);
511     IMSA_HILOGI("ExecuteCmd ret = %{public}d", ret);
512 }
513 
WaitTaskEmpty()514 bool TddUtil::WaitTaskEmpty()
515 {
516     return BlockRetry(WAIT_TASK_EMPTY_INTERVAL, WAIT_TASK_EMPTY_TIMES, []() {
517         return TaskManager::GetInstance().curTask_ == nullptr && TaskManager::GetInstance().amsTasks_.empty()
518                && TaskManager::GetInstance().imaTasks_.empty() && TaskManager::GetInstance().imsaTasks_.empty()
519                && TaskManager::GetInstance().innerTasks_.empty();
520     });
521 }
522 
CreateWindow()523 void TddUtil::WindowManager::CreateWindow()
524 {
525     if (windowTokenId_ == 0) {
526         windowTokenId_ = AllocTestTokenID(true, "TestWindow", { "ohos.permission.SYSTEM_FLOAT_WINDOW" });
527     }
528     TokenScope scope(windowTokenId_);
529     std::string windowName = "inputmethod_test_window";
530     sptr<WindowOption> winOption = new OHOS::Rosen::WindowOption();
531     winOption->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
532     winOption->SetFocusable(true);
533     std::shared_ptr<AbilityRuntime::Context> context = nullptr;
534     WMError wmError = WMError::WM_OK;
535     window_ = Window::Create(windowName, winOption, context, wmError);
536     if (window_ == nullptr) {
537         IMSA_HILOGE("failed to create window, ret: %{public}d", wmError);
538         return;
539     }
540     IMSA_HILOGI("Create window ret:%{public}d", wmError);
541     currentWindowId_ = window_->GetWindowId();
542 }
543 
ShowWindow()544 void TddUtil::WindowManager::ShowWindow()
545 {
546     if (window_ == nullptr) {
547         IMSA_HILOGE("window is not exist.");
548         return;
549     }
550     TokenScope scope(windowTokenId_);
551     auto ret = window_->Show();
552     IMSA_HILOGI("Show window end, ret = %{public}d", ret);
553 }
554 
HideWindow()555 void TddUtil::WindowManager::HideWindow()
556 {
557     if (window_ == nullptr) {
558         IMSA_HILOGE("window is not exist.");
559         return;
560     }
561     TokenScope scope(windowTokenId_);
562     auto ret = window_->Hide();
563     IMSA_HILOGI("Hide window end, ret = %{public}d", ret);
564 }
565 
DestroyWindow()566 void TddUtil::WindowManager::DestroyWindow()
567 {
568     if (window_ == nullptr) {
569         IMSA_HILOGE("window_ nullptr");
570         return;
571     }
572     TokenScope scope(windowTokenId_);
573     auto wmError = window_->Destroy();
574     IMSA_HILOGI("Destroy window ret: %{public}d", wmError);
575 }
576 
RegisterFocusChangeListener()577 void TddUtil::WindowManager::RegisterFocusChangeListener()
578 {
579     auto listener = new (std::nothrow) FocusChangedListenerTestImpl();
580     WMError ret = Rosen::WindowManager::GetInstance().RegisterFocusChangedListener(listener);
581     IMSA_HILOGI("register focus changed listener ret: %{public}d", ret);
582 }
583 } // namespace MiscServices
584 } // namespace OHOS
585