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#ifndef TEST_WUKONG_TREE_MANAGER_H
17#define TEST_WUKONG_TREE_MANAGER_H
18
19#include "ability_tree.h"
20#include "accessibility_element_info.h"
21#include "component_tree.h"
22#include "page_tree.h"
23#include "wukong_util.h"
24
25namespace OHOS {
26namespace WuKong {
27namespace {
28const uint32_t INVALIDED_INPUT_INDEX = 0xFFFFFFFF;
29const uint32_t FOCUS_NUM_DEFAULT = 20;
30}
31/**
32 * @brief Generate component tree, get AccessibilityElementInfo for current active components.
33 */
34class TreeManager : public DelayedSingleton<TreeManager> {
35    /**
36     * for test flow to get and set test element info.
37     */
38public:
39    /**
40     * @brief init and clear container.
41     */
42    void InitContainer()
43    {
44        elementInfoList_.clear();
45        abilityTreeList_.clear();
46        pageTreeList_.clear();
47        componentTreeList_.clear();
48    }
49
50    /**
51     * @brief Reestablish accessible connections.
52     */
53    void ReconnectAccessibility();
54
55    /**
56     * @brief update wukong tree by AccessibilityUITestAbility.
57     * @return An AccessibilityElementInfo
58     */
59    ErrCode UpdateComponentInfo();
60
61    /**
62     * @brief get AccessibilityElementInfo for the Preorder Traversal Algorithms.
63     * @return An AccessibilityElementInfo
64     */
65    const std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo>& GetElementInfoByOrder()
66    {
67        return inputElementInfo_;
68    }
69
70    /**
71     * @brief get AccessibilityElementInfo list of active component.
72     * @return input AccessibilityElementInfo list
73     */
74    const std::vector<std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo>>& GetActiveElementInfos()
75    {
76        return inputElementInfoList_;
77    }
78
79    /**
80     * @brief get AccessibilityElementInfo list of active component.
81     * @return input AccessibilityElementInfo list
82     */
83    const std::vector<std::shared_ptr<ComponentTree>>& GetActiveComponentInfos()
84    {
85        return inputComponentList_;
86    }
87
88    /**
89     * @brief set input event component, and input type.
90     * @param index index of active element info list.
91     * @param actionType component input type.
92     */
93    void SetInputcomponentIndex(int actionType, uint32_t index = INVALIDED_INPUT_INDEX);
94
95    /**
96     * for scene update tree.
97     */
98public:
99    /**
100     * @brief find the index of component, which one to input
101     * @return the index
102     */
103    std::uint32_t FindInputComponentIndex(bool shouldScreenCap);
104
105    /**
106     * @brief take Screen Capture
107     */
108    void ScreenCapture(bool shouldScreenCap);
109
110    /**
111     * @brief decide whether the component need be focused on
112     * @param type the component type to charge
113     */
114    bool NeedFocus(const std::string & type)
115    {
116        if (focusTypeList_.size() > 0) {
117            uint32_t focusTypeIndex = WuKongUtil::GetInstance()->FindElement(focusTypeList_, type);
118            if (focusTypeIndex == INVALIDVALUE) {
119                return false;
120            }
121        }
122        return true;
123    }
124
125    /**
126     * @brief set the component's every focused input number
127     * @param totalNum the focus input number
128     */
129    void SetFocusNum(const std::string & focusNum)
130    {
131        focusNum_ = std::stoi(focusNum);
132    }
133
134    /**
135     * @brief set the component's every focused input number
136     */
137    uint32_t GetFocusNum()
138    {
139        return focusNum_;
140    }
141
142    /**
143     * @brief set the component types to focus on
144     * @param totalNum the focus input number
145     */
146    void SetFocusTypeList(const std::string &optarg)
147    {
148        SplitStr(optarg, ",", focusTypeList_);
149    }
150
151    /**
152     * @brief check cur page has Dialog
153     * @return return hasDialog_
154     */
155    bool HasDialog()
156    {
157        return hasDialog_;
158    }
159
160    /**
161     * @brief set ComponentTree list of active component.
162     * @param inputComponentList ComponentTree list.
163     */
164    void SetActiveComponent(const std::vector<std::shared_ptr<ComponentTree>>& inputComponentList);
165
166    /**
167     * @brief set a ComponentTree of active component.
168     * @param inputComponent a ComponentTree.
169     */
170    void SetActiveComponent(const std::shared_ptr<ComponentTree>& inputComponent);
171
172    /**
173     * @brief get current component tree.
174     * @return A ComponentTree
175     */
176    const std::shared_ptr<ComponentTree>& GetCurrentComponents()
177    {
178        return currentComponentNode_;
179    }
180    /**
181     * @brief get new component tree
182     * @return A ComponentTree
183     */
184    const std::shared_ptr<ComponentTree>& GetNewComponents()
185    {
186        return newComponentNode_;
187    }
188
189    /**
190     * @brief get current page node.
191     * @return A ComponentTree
192     */
193    const std::shared_ptr<PageTree>& GetCurrentPage()
194    {
195        return currentPageNode_;
196    }
197
198    /**
199     * @brief get new page node
200     * @return A ComponentTree
201     */
202    const std::shared_ptr<PageTree>& GetNewPage()
203    {
204        return newPageNode_;
205    }
206
207    /**
208     * @brief get current ability node.
209     * @return A AblilityTree
210     */
211    const std::shared_ptr<AbilityTree>& GetCurrentAbility()
212    {
213        return currentAbilityNode_;
214    }
215
216    /**
217     * @brief get all app bundle tree.
218     * @return A AblilityTree list
219     */
220    const std::vector<std::shared_ptr<AbilityTree>>& GetBundleList()
221    {
222        return abilityTreeList_;
223    }
224
225    /**
226     * @brief add current page as a new page
227     * @return add new page result
228     */
229    bool AddPage();
230    /**
231     * @brief remove new page.
232     * @return remove new page result
233     */
234    bool SamePage();
235
236    /**
237     * @brief back and goto existed page
238     * @param layer 0 update current page, < 0 update parent page, > 0 update child page
239     * @param index child index
240     * @return update page result
241     */
242    bool UpdatePage(int layer, uint32_t index = INVALIDED_INPUT_INDEX);
243
244    const std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo> GetNewElementInfoList(uint32_t index)
245    {
246        if (index < newElementInfoList_.size()) {
247            return newElementInfoList_[index];
248        } else {
249            return {};
250        }
251    }
252
253    const std::map<std::string, std::uint32_t> GetComponentIndexMap()
254    {
255        return page2componentIndex_;
256    }
257
258    const std::map<std::string, std::uint32_t> GetComponentInputCountMap()
259    {
260        return page2inputCount_;
261    }
262
263    void SetOldPagePath(const std::string& pagePath)
264    {
265        pagePath_ = pagePath;
266    }
267
268    std::string GetPagePath()
269    {
270        return pagePath_;
271    }
272
273    void SetComponentType(const std::string& componmentType)
274    {
275        componmentType_ = componmentType;
276    }
277
278    bool RecursComponent(const std::shared_ptr<ComponentTree>& componentTree);
279
280    void RecursSetDialog(const std::shared_ptr<ComponentTree>& componentTree);
281    DECLARE_DELAYED_SINGLETON(TreeManager);
282
283private:
284    bool RecursGetChildElementInfo(const std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo>& parent,
285                                   const std::shared_ptr<ComponentTree>& componentParent);
286    bool FindAbility(const std::shared_ptr<AbilityTree>& abilityNode);
287    ErrCode MakeAndCheckNewAbility();
288    bool UpdateCurrentPage(bool isAdd = false);
289    bool RemovePage();
290
291    std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo> inputElementInfo_;
292    std::shared_ptr<ComponentTree> inputComponent_;
293    std::vector<std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo>> inputElementInfoList_;
294    std::vector<std::shared_ptr<ComponentTree>> inputComponentList_;
295
296    std::shared_ptr<ComponentTree> currentComponentNode_ = std::make_shared<ComponentTree>();
297    std::shared_ptr<ComponentTree> newComponentNode_ = std::make_shared<ComponentTree>();
298
299    std::shared_ptr<AbilityTree> currentAbilityNode_ = std::make_shared<AbilityTree>();
300    std::shared_ptr<AbilityTree> newAbilityNode_ = std::make_shared<AbilityTree>();
301
302    std::shared_ptr<PageTree> currentPageNode_ = std::make_shared<PageTree>();
303    std::shared_ptr<PageTree> newPageNode_ = std::make_shared<PageTree>();
304
305    std::vector<std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo>> newElementInfoList_;
306
307    std::vector<std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo>> elementInfoList_;
308
309    std::vector<std::shared_ptr<AbilityTree>> abilityTreeList_;
310    std::map<std::uint32_t, std::shared_ptr<PageTree>> pageTreeList_;
311    std::map<std::uint32_t, std::shared_ptr<ComponentTree>> componentTreeList_;
312
313    bool isUpdateComponentFinished_ = false;
314    bool isNewAbility_ = false;
315
316    std::string pagePath_;
317    std::map<std::uint64_t, std::uint64_t> componentCountMap_;
318    bool hasDialog_ = false;
319    std::shared_ptr<ComponentTree> dialogComponent_ = nullptr;
320
321    std::map<std::string, std::uint32_t> page2componentIndex_;
322    std::map<std::string, std::uint32_t> page2inputCount_;
323    std::string componmentType_ = "";
324    std::uint32_t focusNum_ = FOCUS_NUM_DEFAULT;
325    std::vector<std::string> focusTypeList_;
326};
327}  // namespace WuKong
328}  // namespace OHOS
329#endif
330