• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
16 #include "accessibility_ui_test_ability_impl.h"
17 
18 #include <mutex>
19 #include "hilog_wrapper.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace Accessibility {
26 static std::mutex g_Mutex;
27 static std::shared_ptr<AccessibilityUITestAbilityImpl> g_Instance = nullptr;
28 
GetInstance()29 std::shared_ptr<AccessibilityUITestAbility> AccessibilityUITestAbility::GetInstance()
30 {
31     std::lock_guard<std::mutex> lock(g_Mutex);
32     if (!g_Instance) {
33         g_Instance = std::make_shared<AccessibilityUITestAbilityImpl>();
34     }
35     return g_Instance;
36 }
37 
AccessibilityUITestAbilityImpl()38 AccessibilityUITestAbilityImpl::AccessibilityUITestAbilityImpl()
39 {
40     HILOG_DEBUG("start.");
41 
42     extensionContext_ = std::make_shared<AccessibilityExtensionContext>();
43 
44     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45     if (!samgr) {
46         HILOG_ERROR("Failed to get ISystemAbilityManager");
47         return;
48     }
49     HILOG_DEBUG("ISystemAbilityManager obtained");
50 
51     sptr<IRemoteObject> object = samgr->GetSystemAbility(ACCESSIBILITY_MANAGER_SERVICE_ID);
52     if (!object) {
53         HILOG_ERROR("Get IAccessibleAbilityManagerServiceClient object from samgr failed");
54         return;
55     }
56     HILOG_DEBUG("Get remote object ok");
57 
58     serviceProxy_ = iface_cast<AccessibleAbilityManagerServiceClientProxy>(object);
59     if (!serviceProxy_) {
60         HILOG_ERROR("Get aams proxy failed");
61         return;
62     }
63 
64     stub_ = new AccessibleAbilityClientStubImpl();
65     stub_->SetUITestEnabled();
66 }
67 
RegisterListener(const std::shared_ptr<IAccessibleUITestAbilityListener> & listener)68 bool AccessibilityUITestAbilityImpl::RegisterListener(const std::shared_ptr<IAccessibleUITestAbilityListener> &listener)
69 {
70     HILOG_DEBUG("start.");
71     std::lock_guard<std::mutex> lock(g_Mutex);
72 
73     if (!listener) {
74         HILOG_ERROR("listener is nullptr.");
75         return false;
76     }
77 
78     if (!stub_) {
79         HILOG_ERROR("AccessibleAbility::stub_ is nullptr");
80         return false;
81     }
82     return stub_->RegisterUITestAbilityListener(listener);
83 }
84 
Connect()85 bool AccessibilityUITestAbilityImpl::Connect()
86 {
87     HILOG_DEBUG("start.");
88     std::lock_guard<std::mutex> lock(g_Mutex);
89 
90     if (!serviceProxy_) {
91         HILOG_ERROR("Failed to get aams service");
92         return false;
93     }
94 
95     if (!stub_) {
96         HILOG_ERROR("stub_ is nullptr");
97         return false;
98     }
99     return serviceProxy_->RegisterUITestAbilityConnectionClient(stub_);
100 }
101 
Disconnect()102 bool AccessibilityUITestAbilityImpl::Disconnect()
103 {
104     HILOG_DEBUG("start.");
105     std::lock_guard<std::mutex> lock(g_Mutex);
106 
107     if (!serviceProxy_) {
108         HILOG_ERROR("Failed to get aams service");
109         return false;
110     }
111     return serviceProxy_->DeregisterUITestAbilityConnectionClient();
112 }
113 
GetFocusElementInfo(uint32_t focusType,std::optional<AccessibilityElementInfo> & elementInfo)114 bool AccessibilityUITestAbilityImpl::GetFocusElementInfo(uint32_t focusType,
115     std::optional<AccessibilityElementInfo> &elementInfo)
116 {
117     return extensionContext_->GetFocusElementInfo(focusType, elementInfo);
118 }
119 
GetRootElementInfo(std::optional<AccessibilityElementInfo> & elementInfo)120 bool AccessibilityUITestAbilityImpl::GetRootElementInfo(std::optional<AccessibilityElementInfo> &elementInfo)
121 {
122     return extensionContext_->GetRootElementInfo(elementInfo);
123 }
124 
GetWindows()125 std::vector<AccessibilityWindowInfo> AccessibilityUITestAbilityImpl::GetWindows()
126 {
127     return extensionContext_->GetWindows();
128 }
129 
ExecuteCommonAction(UiTestGlobalAction action)130 bool AccessibilityUITestAbilityImpl::ExecuteCommonAction(UiTestGlobalAction action)
131 {
132     GlobalAction extensionAction;
133     switch (action) {
134         case UI_GLOBAL_ACTION_BACK:
135             extensionAction = GLOBAL_ACTION_BACK;
136             break;
137         case UI_GLOBAL_ACTION_HOME:
138             extensionAction = GLOBAL_ACTION_HOME;
139             break;
140         case UI_GLOBAL_ACTION_RECENT:
141             extensionAction = GLOBAL_ACTION_RECENT;
142             break;
143         case UI_GLOBAL_ACTION_NOTIFICATION:
144             extensionAction = GLOBAL_ACTION_NOTIFICATION;
145             break;
146         case UI_GLOBAL_ACTION_POP_UP_POWER_DIALOG:
147             extensionAction = GLOBAL_ACTION_POP_UP_POWER_DIALOG;
148             break;
149         case UI_GLOBAL_ACTION_DIVIDE_SCREEN:
150             extensionAction = GLOBAL_ACTION_DIVIDE_SCREEN;
151             break;
152         case UI_GLOBAL_ACTION_LOCK_SCREEN:
153             extensionAction = GLOBAL_ACTION_LOCK_SCREEN;
154             break;
155         case UI_GLOBAL_ACTION_CAPTURE_SCREEN:
156             extensionAction = GLOBAL_ACTION_CAPTURE_SCREEN;
157             break;
158         default:
159             extensionAction = GLOBAL_ACTION_INVALID;
160             break;
161     }
162     return extensionContext_->ExecuteCommonAction(static_cast<uint32_t>(extensionAction));
163 }
164 
DispatchOnSimulationGestureResult(uint32_t sequence,bool result)165 void AccessibilityUITestAbilityImpl::DispatchOnSimulationGestureResult(uint32_t sequence, bool result)
166 {
167     return extensionContext_->DispatchOnSimulationGestureResult(sequence, result);
168 }
169 
SetChannelId(uint32_t channelId)170 void AccessibilityUITestAbilityImpl::SetChannelId(uint32_t channelId)
171 {
172     return extensionContext_->SetChannelId(channelId);
173 }
174 } // namespace Accessibility
175 } // namespace OHOS