1 /*
2  * Copyright (c) 2020-2021 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 /**
17  * @addtogroup UI_DFX
18  * @{
19  *
20  * @brief Provides test and analysis capabilities, such as stimulating input events and viewing information about a
21  *        Document Object Model (DOM) tree.
22  *
23  * @since 1.0
24  * @version 1.0
25  */
26 
27 /**
28  * @file ui_dump_dom_tree.h
29  *
30  * @brief Declares a function for exporting information about a Document Object Model (DOM) tree or DOM node.
31  *
32  * @since 1.0
33  * @version 1.0
34  */
35 #ifndef GRAPHIC_LITE_UI_DUMP_DOM_TREE_H
36 #define GRAPHIC_LITE_UI_DUMP_DOM_TREE_H
37 
38 #include "graphic_config.h"
39 #include "gfx_utils/heap_base.h"
40 #if ENABLE_DEBUG
41 #include "cJSON.h"
42 #include "components/ui_view.h"
43 #endif // ENABLE_DEBUG
44 namespace OHOS {
45 #if ENABLE_DEBUG
46 /**
47  * @brief Enumerates export modes.
48  */
49 enum DumpMode {
50     /** DOM tree */
51     DUMP_TREE,
52     /** DOM node */
53     DUMP_NODE
54 };
55 #endif // ENABLE_DEBUG
56 
57 /**
58  * @brief Provides functions for exporting information about a specified DOM node or information about the DOM tree
59  * starting from a specified DOM node.
60  *
61  * @since 1.0
62  * @version 1.0
63  */
64 class UIDumpDomTree : public HeapBase {
65 public:
66     /**
67      * @brief Obtains an instance in singleton pattern.
68      *
69      * @return Returns the function instance for exporting DOM information.
70      * @since 1.0
71      * @version 1.0
72      */
73     static UIDumpDomTree* GetInstance();
74 
75     /**
76      * @brief Exports information about a DOM tree starting from a specified DOM node and saves the information to
77      * a specified path.
78      *
79      * @param id Indicates the pointer to the DOM node ID.
80      * @param path Indicates the pointer to the specified path.
81      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
82      * @since 1.0
83      * @version 1.0
84      */
85     bool DumpDomTree(const char* id, const char* path);
86 
87     /**
88      * @brief Exports information about a DOM tree starting from a specified DOM node and saves the information to
89      * the default path.
90      * The default path is {@link DEFAULT_DUMP_DOM_TREE_PATH}.
91      *
92      * @param id Indicates the pointer to the DOM node ID.
93      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
94      * @since 1.0
95      * @version 1.0
96      */
DumpDomTree(const char * id)97     bool DumpDomTree(const char* id)
98     {
99         return DumpDomTree(id, nullptr);
100     }
101 
102     /**
103      * @brief Exports information about a specified DOM node.
104      *
105      * @param id Indicates the pointer to the DOM node ID.
106      * @return Returns a character string about the DOM node information. The character string memory is released by
107      * invoking <b>cJSON_free()</b>.
108      * @since 1.0
109      * @version 1.0
110      */
111     char* DumpDomNode(const char* id);
112 
113 private:
114 #if ENABLE_DEBUG
115     bool searchFlag_;     // Search flag, set when find the view with right id.
116     bool allocErrorFlag_; // Alloc error flag, set when cJSON fails to allocate memory.
117     char* pJson_;         // Pointer for returning cJSON string.
118     cJSON* rootJson_;     // Root pointer of cJSON structure.
119     const char* path_;
120 
121     void DumpJsonById(UIView* view, const char* id, DumpMode mode);
122 
123     void AddNameField(UIViewType type, cJSON* usr) const;
124     void AddCommonField(UIView* view, cJSON* usr) const;
125     void AddImageViewSpecialField(const UIView* view, cJSON* usr) const;
126     void AddLabelField(const UIView* view, cJSON* usr) const;
127     void AddLabelButtonField(const UIView* view, cJSON* usr) const;
128     void AddCheckboxField(const UIView* view, cJSON* usr) const;
129     void AddToggleButtonField(const UIView* view, cJSON* usr) const;
130     void AddProgressField(const UIView* view, cJSON* usr) const;
131     void AddScrollViewField(const UIView* view, cJSON* usr) const;
132     void AddListField(const UIView* view, cJSON* usr) const;
133     void AddClockField(const UIView* view, cJSON* usr) const;
134     void AddPickerField(const UIView* view, cJSON* usr) const;
135     void AddSwipeViewField(const UIView* view, cJSON* usr) const;
136     void AddTimePickerField(const UIView* view, cJSON* usr) const;
137     void AddSpecialField(const UIView* view, cJSON* usr) const;
138     void OutputDomNode(UIView* view);
139     void OutputDomTree(UIView* view, cJSON* usr);
140     bool WriteDumpFile() const;
141 
UIDumpDomTree()142     UIDumpDomTree()
143         : searchFlag_(false),
144           allocErrorFlag_(false),
145           pJson_(nullptr),
146           rootJson_(nullptr),
147           path_(DEFAULT_DUMP_DOM_TREE_PATH)
148     {
149     }
150 #else
151     UIDumpDomTree() {}
152 #endif // ENABLE_DEBUG
~UIDumpDomTree()153     virtual ~UIDumpDomTree() {}
154 
155     UIDumpDomTree(const UIDumpDomTree&) = delete;
156     UIDumpDomTree& operator=(const UIDumpDomTree&) = delete;
157     UIDumpDomTree(UIDumpDomTree&&) = delete;
158     UIDumpDomTree& operator=(UIDumpDomTree&&) = delete;
159 };
160 } // namespace OHOS
161 #endif // GRAPHIC_LITE_UI_DUMP_DOM_TREE_H
162