1 /*
2 * Copyright (c) 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 #include "gtest/gtest.h"
17
18 #include "base/json/json_util.h"
19 #include "base/utils/utils.h"
20 #include "frameworks/bridge/common/dom/dom_document.h"
21 #include "frameworks/bridge/common/dom/dom_stack.h"
22 #include "frameworks/bridge/common/dom/dom_text.h"
23 #include "frameworks/bridge/test/unittest/cardfrontend/card_test_factory.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS::Ace::Framework {
29
30 class DomCardStackTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36 };
37
SetUpTestCase()38 void DomCardStackTest::SetUpTestCase() {}
TearDownTestCase()39 void DomCardStackTest::TearDownTestCase() {}
SetUp()40 void DomCardStackTest::SetUp() {}
TearDown()41 void DomCardStackTest::TearDown() {}
42
43 /**
44 * @tc.name: DomCardStackTest001
45 * @tc.desc: Verify that DomStack can be created successfully.
46 * @tc.type: FUNC
47 * @tc.require: AR000F2BBG
48 * @tc.author: lixingchi
49 */
50 HWTEST_F(DomCardStackTest, DomCardStackTest001, TestSize.Level1)
51 {
52 const std::string index = "{"
53 " \"template\": { "
54 " \"type\": \"stack\" "
55 " }, "
56 " \"styles\": {}, "
57 " \"actions\": {}, "
58 " \"data\": {} "
59 "} ";
60 /**
61 * @tc.steps: step1. parse card dsl by CardTestFactory.
62 */
63 auto document = AceType::MakeRefPtr<DOMDocument>(0);
64 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
65 CardTestFactory::GetInstance().ParseCardDsl(page, index);
66 auto domStack = document->GetDOMNodeById(DOM_ROOT_NODE_ID_BASE);
67 ASSERT_TRUE(domStack != nullptr);
68 ASSERT_TRUE(domStack->GetTag() == "stack");
69 ASSERT_TRUE(CardTestFactory::GetInstance().GetNodeNumber() == 1);
70 }
71
72 /**
73 * @tc.name: DomCardStackTest002
74 * @tc.desc: Verify that DomStack data binding successfully.
75 * @tc.type: FUNC
76 * @tc.require: AR000F2BBG
77 * @tc.author: lixingchi
78 */
79 HWTEST_F(DomCardStackTest, DomCardStackTest002, TestSize.Level1)
80 {
81 const std::string index = "{"
82 " \"template\": { "
83 " \"type\": \"stack\", "
84 " \"shown\": \"{{show}}\" "
85 " }, "
86 " \"styles\": {}, "
87 " \"actions\": {}, "
88 " \"data\": { "
89 " \"show\": false "
90 " } "
91 "} ";
92 /**
93 * @tc.steps: step1. parse card dsl by CardTestFactory.
94 */
95 auto document = AceType::MakeRefPtr<DOMDocument>(0);
96 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
97 CardTestFactory::GetInstance().ParseCardDsl(page, index);
98 auto domStack = document->GetDOMNodeById(DOM_ROOT_NODE_ID_BASE);
99 ASSERT_TRUE(domStack != nullptr);
100 ASSERT_TRUE(domStack->GetTag() == "stack");
101 ASSERT_TRUE(domStack->IsShow() == false);
102 ASSERT_TRUE(CardTestFactory::GetInstance().GetNodeNumber() == 1);
103 }
104
105 /**
106 * @tc.name: DomCardStackTest003
107 * @tc.desc: Verify that DomStack append child successfully.
108 * @tc.type: FUNC
109 * @tc.require: AR000F2BBG
110 * @tc.author: lixingchi
111 */
112 HWTEST_F(DomCardStackTest, DomCardStackTest003, TestSize.Level1)
113 {
114 const std::string index = "{"
115 " \"template\": { "
116 " \"type\": \"stack\", "
117 " \"shown\": \"{{show}}\", "
118 " \"children\": [ "
119 " { "
120 " \"type\": \"text\" "
121 " }, "
122 " { "
123 " \"type\": \"text\" "
124 " }, "
125 " { "
126 " \"type\": \"text\" "
127 " } "
128 " ] "
129 " }, "
130 " \"styles\": {}, "
131 " \"actions\": {}, "
132 " \"data\": { "
133 " \"show\": true "
134 " } "
135 "}";
136 /**
137 * @tc.steps: step1. parse card dsl by CardTestFactory.
138 */
139 auto document = AceType::MakeRefPtr<DOMDocument>(0);
140 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
141 CardTestFactory::GetInstance().ParseCardDsl(page, index);
142 auto domStack = document->GetDOMNodeById(DOM_ROOT_NODE_ID_BASE);
143 ASSERT_TRUE(domStack != nullptr);
144 ASSERT_TRUE(domStack->GetTag() == "stack");
145 ASSERT_TRUE(domStack->IsShow() == true);
146 ASSERT_TRUE(domStack->GetChildList().size() == 3);
147 ASSERT_TRUE(CardTestFactory::GetInstance().GetNodeNumber() == 4);
148 }
149
150 /**
151 * @tc.name: DomCardStackTest004
152 * @tc.desc: Verify that DomStack styles set successfully.
153 * @tc.type: FUNC
154 * @tc.require: AR000F2BBG
155 * @tc.author: lixingchi
156 */
157 HWTEST_F(DomCardStackTest, DomCardStackTest004, TestSize.Level1)
158 {
159 const std::string index = "{ "
160 " \"template\": { "
161 " \"type\": \"stack\", "
162 " \"classList\": [ "
163 " \"container\" "
164 " ] "
165 " }, "
166 " \"styles\": { "
167 " \".container\": { "
168 " \"height\": \"300px\", "
169 " \"width\": \"300px\", "
170 " \"marginTop\": \"30px\", "
171 " \"marginBottom\": \"30px\", "
172 " \"marginLeft\": \"30px\", "
173 " \"marginRight\": \"30px\" "
174 " } "
175 " }, "
176 " \"actions\": {}, "
177 " \"data\": {} "
178 "}";
179
180 /**
181 * @tc.steps: step1. parse card dsl by CardTestFactory.
182 */
183 auto document = AceType::MakeRefPtr<DOMDocument>(0);
184 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
185 CardTestFactory::GetInstance().ParseCardDsl(page, index);
186 auto domStack = document->GetDOMNodeById(DOM_ROOT_NODE_ID_BASE);
187 ASSERT_TRUE(domStack != nullptr);
188 ASSERT_TRUE(domStack->GetTag() == "stack");
189 ASSERT_TRUE(static_cast<int32_t>(domStack->GetHeight().Value()) == 300);
190 ASSERT_TRUE(static_cast<int32_t>(domStack->GetWidth().Value()) == 300);
191 ASSERT_TRUE(CardTestFactory::GetInstance().GetNodeNumber() == 1);
192
193 auto declaration = domStack->GetDeclaration();
194 ASSERT_TRUE(declaration != nullptr);
195 auto& marginStyle = static_cast<CommonMarginStyle&>(declaration->GetStyle(StyleTag::COMMON_MARGIN_STYLE));
196 ASSERT_TRUE(marginStyle.IsValid());
197 ASSERT_TRUE(static_cast<int32_t>(marginStyle.margin.Top().Value()) == 30);
198 ASSERT_TRUE(static_cast<int32_t>(marginStyle.margin.Bottom().Value()) == 30);
199 ASSERT_TRUE(static_cast<int32_t>(marginStyle.margin.Left().Value()) == 30);
200 ASSERT_TRUE(static_cast<int32_t>(marginStyle.margin.Right().Value()) == 30);
201 }
202
203 /**
204 * @tc.name: DomCardStackTest005
205 * @tc.desc: Verify that DomStack event binding successfully.
206 * @tc.type: FUNC
207 * @tc.require: AR000F2BBG
208 * @tc.author: lixingchi
209 */
210 HWTEST_F(DomCardStackTest, DomCardStackTest005, TestSize.Level1)
211 {
212 const std::string index = "{ "
213 " \"template\": { "
214 " \"type\": \"stack\", "
215 " \"events\": { "
216 " \"click\": \"test\" "
217 " } "
218 " }, "
219 " \"styles\": {}, "
220 " \"actions\": {}, "
221 " \"data\": {} "
222 "}";
223 /**
224 * @tc.steps: step1. parse card dsl by CardTestFactory.
225 */
226 auto document = AceType::MakeRefPtr<DOMDocument>(0);
227 auto page = AceType::MakeRefPtr<JsAcePage>(0, document, "");
228 CardTestFactory::GetInstance().ParseCardDsl(page, index);
229 auto domStack = document->GetDOMNodeById(DOM_ROOT_NODE_ID_BASE);
230 ASSERT_TRUE(domStack != nullptr);
231 auto gestureEventComponent = domStack->GetGestureListenerComponent();
232 ASSERT_TRUE(gestureEventComponent != nullptr);
233 ASSERT_TRUE(domStack->GetTag() == "stack");
234 EXPECT_TRUE(gestureEventComponent->GetOnClickId().GetData().eventType == DOM_CLICK);
235 EXPECT_TRUE(gestureEventComponent->GetOnClickId().GetData().eventId == DOM_DEFAULT_ROOT_NODE_ID);
236 ASSERT_TRUE(CardTestFactory::GetInstance().GetNodeNumber() == 1);
237 }
238
239 } // namespace OHOS::Ace::Framework
240