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