• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/utils/system_properties.h"
19 #include "core/common/ime/text_selection.h"
20 #include "core/components/common/layout/constants.h"
21 #include "frameworks/bridge/common/dom/dom_document.h"
22 #define private public
23 #include "frameworks/bridge/common/dom/dom_search.h"
24 #undef private
25 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS::Ace::Framework {
31 namespace {
32 
33 const std::string TEST_ICON_SRC = "Common/email.png";
34 const std::string TEST_HINT = "this is hint";
35 const std::string TEST_VALUE = "this is value";
36 const std::string EMPTY_STRING = "";
37 const std::string TEST_TEXT_COLOR = "#FF0000";
38 const std::string TEST_PLACEHOLDER_COLOR = "#0000FF";
39 const std::string TEST_BACKGROUND_COLOR = "#00ff00";
40 const std::string TEST_TEXT_FONT_FAMILY = "sans-serif";
41 constexpr FontWeight TEST_TEXT_FONT_WEIGHT = FontWeight::W400;
42 constexpr double TEST_TEXT_FONT_SIZE = 30.0;
43 const std::string DEFAULT_COLOR = "#000000";
44 const std::string DEFAULT_BACKGROUND_COLOR = "#0C000000";
45 const std::string DEFAULT_TEXT_COLOR = "#E5000000";
46 const std::string DEFAULT_PLACEHOLDER_COLOR = "#99000000";
47 constexpr FontWeight DEFAULT_TEXT_FONT_WEIGHT = FontWeight::W400;
48 constexpr FontWeight DEFAULT_FONT_WEIGHT_VALUE = FontWeight::NORMAL;
49 constexpr double DEFAULT_TEXT_FONT_SIZE = 16.0;
50 constexpr double INVALID_TEXT_FONT_SIZE = 0.0;
51 constexpr double TEST_PADDING_LEFT_VALUE = 10.0;
52 constexpr double TEST_PADDING_TOP_VALUE = 20.0;
53 constexpr double TEST_PADDING_RIGHT_VALUE = 70.0;
54 constexpr double TEST_PADDING_BOTTOM_VALUE = 40.0;
55 constexpr double DEFAULT_PADDING_LEFT_VALUE = 4.0;
56 constexpr double DEFAULT_PADDING_TOP_VALUE = 0.0;
57 constexpr double DEFAULT_PADDING_RIGHT_VALUE = 40.0;
58 constexpr double DEFAULT_PADDING_BOTTOM_VALUE = 0.0;
59 constexpr double ERROR_PADDING_LEFT_VALUE = 0.0;
60 const TextSelection TEST_SELECTION = TextSelection(2, 5);
61 const TextSelection DEFAULT_SELECTION = TextSelection(-1, -1);
62 
63 const std::string SEARCH_JSON = "{                                                          "
64                                 "  \"tag\": \"search\",                                     "
65                                 "  \"attr\": [{                                             "
66                                 "                \"icon\" : \"Common/email.png\"            "
67                                 "              },                                           "
68                                 "              {                                            "
69                                 "                \"hint\" : \"this is hint\"                "
70                                 "              },                                           "
71                                 "              {                                            "
72                                 "                \"selectedstart\": \"2\"                   "
73                                 "              },                                           "
74                                 "              {                                            "
75                                 "                \"selectedend\": \"5\"                     "
76                                 "              },                                           "
77                                 "              {                                            "
78                                 "                \"softkeyboardenabled\": \"false\"         "
79                                 "              },                                           "
80                                 "              {                                            "
81                                 "                \"value\" : \"this is value\"              "
82                                 "              }],                                          "
83                                 "  \"style\": [{                                            "
84                                 "           \"fontSize\":\"30.0\"                           "
85                                 "          },                                               "
86                                 "          {                                                "
87                                 "           \"fontWeight\":\"400\"                          "
88                                 "           },                                              "
89                                 "          {                                                "
90                                 "           \"color\":\"#ff0000\"                           "
91                                 "           },                                              "
92                                 "          {                                                "
93                                 "           \"placeholderColor\":\"#0000ff\"                "
94                                 "           },                                              "
95                                 "          {                                                "
96                                 "           \"caretColor\":\"#0000ff\"                      "
97                                 "           },                                              "
98                                 "           {                                               "
99                                 "           \"fontFamily\":\"sans-serif\"                   "
100                                 "           },                                              "
101                                 "           {                                               "
102                                 "           \"backgroundColor\":\"#00ff00\"                 "
103                                 "           },                                              "
104                                 "           {                                               "
105                                 "           \"paddingLeft\":\"10px\"                        "
106                                 "           },                                              "
107                                 "          {                                                "
108                                 "           \"paddingTop\":\"20px\"                         "
109                                 "           },                                              "
110                                 "          {                                                "
111                                 "           \"paddingRight\":\"30px\"                       "
112                                 "           },                                              "
113                                 "          {                                                "
114                                 "           \"paddingBottom\":\"40px\"                      "
115                                 "            }]                                             "
116                                 "}                                                          ";
117 const std::string INVALID_SEARCH_JSON = "{                                                          "
118                                         "  \"tag\": \"search\",                                     "
119                                         "  \"style\": [{                                            "
120                                         "           \"fontSize\":\"abcd\"                           "
121                                         "          },                                               "
122                                         "          {                                                "
123                                         "           \"fontWeight\":\"abcd\"                         "
124                                         "           },                                              "
125                                         "          {                                                "
126                                         "           \"color\":\"abcd\"                              "
127                                         "           },                                              "
128                                         "          {                                                "
129                                         "           \"placeholderColor\":\"abcd\"                   "
130                                         "           },                                              "
131                                         "          {                                                "
132                                         "           \"caretColor\":\"abcd\"                         "
133                                         "           },                                              "
134                                         "           {                                               "
135                                         "           \"paddingLeft\":\"abcd\"                        "
136                                         "           },                                              "
137                                         "          {                                                "
138                                         "           \"paddingTop\":\"abcd\"                         "
139                                         "           },                                              "
140                                         "          {                                                "
141                                         "           \"paddingRight\":\"abcd\"                       "
142                                         "           },                                              "
143                                         "          {                                                "
144                                         "           \"paddingBottom\":\"abcd\"                      "
145                                         "           }]                                              "
146                                         "}                                                          ";
147 
148 const std::string PADDING_SEARCH_JSON = "{                                                          "
149                                         "  \"tag\": \"search\",                                     "
150                                         "  \"style\": [{                                            "
151                                         "           \"paddingStart\":\"10px\"                       "
152                                         "           },                                              "
153                                         "          {                                                "
154                                         "           \"paddingEnd\":\"30px\"                         "
155                                         "            }]                                             "
156                                         "}                                                          ";
157 } // namespace
158 
159 class DomSearchTest : public testing::Test {
160 public:
161     static void SetUpTestCase();
162     static void TearDownTestCase();
163     void SetUp();
164     void TearDown();
165 };
166 
SetUpTestCase()167 void DomSearchTest::SetUpTestCase() {}
TearDownTestCase()168 void DomSearchTest::TearDownTestCase() {}
SetUp()169 void DomSearchTest::SetUp() {}
TearDown()170 void DomSearchTest::TearDown() {}
171 
IsTv()172 bool IsTv()
173 {
174     return SystemProperties::GetDeviceType() == DeviceType::TV;
175 }
176 
177 /**
178  * @tc.name: CreateSearchFromDsl001
179  * @tc.desc: Test search node and search component created successfully with all attributes set.
180  * @tc.type: FUNC
181  */
182 HWTEST_F(DomSearchTest, CreateSearchFromDsl001, TestSize.Level1)
183 {
184     /**
185      * @tc.steps: step1. Construct json string with attributes set, then create search node with it .
186      * @tc.expected: step1. Search node and child search component are created with correct attributes and styles.
187      */
188     auto domSearch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(SEARCH_JSON);
189     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSearch);
190     RefPtr<SearchComponent> searchComponent = AceType::DynamicCast<SearchComponent>(boxChild->GetChild());
191     EXPECT_TRUE(searchComponent);
192     RefPtr<TextFieldComponent> textFieldComponent =
193         AceType::DynamicCast<TextFieldComponent>(searchComponent->GetChild());
194     EXPECT_TRUE(textFieldComponent);
195 
196     /**
197      * @tc.steps: step2. Check styles and attributes of created search node.
198      * @tc.expected: step2. Search node's attrs and styles are as expected.
199      */
200     const auto& textStyle = AceType::DynamicCast<DOMSearch>(domSearch)->textStyle_;
201     EXPECT_EQ(textStyle.GetFontSize().Value(), TEST_TEXT_FONT_SIZE);
202     EXPECT_TRUE(textStyle.GetFontWeight() == TEST_TEXT_FONT_WEIGHT);
203     EXPECT_EQ(textStyle.GetFontFamilies()[0], TEST_TEXT_FONT_FAMILY);
204     EXPECT_EQ(textFieldComponent->GetFocusTextColor(), Color::FromString(TEST_TEXT_COLOR));
205     EXPECT_EQ(textFieldComponent->GetPlaceholderColor(), Color::FromString(TEST_PLACEHOLDER_COLOR));
206     EXPECT_EQ(textFieldComponent->GetCursorColor(), Color::FromString(TEST_PLACEHOLDER_COLOR));
207     EXPECT_EQ(textFieldComponent->GetBgColor(), Color::FromString(TEST_BACKGROUND_COLOR));
208     EXPECT_EQ(textFieldComponent->GetIconImage(), (IsTv() ? "" : TEST_ICON_SRC));
209     EXPECT_EQ(textFieldComponent->GetPlaceholder(), TEST_HINT);
210     EXPECT_EQ(textFieldComponent->GetValue(), TEST_VALUE);
211     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Left().Value(), TEST_PADDING_LEFT_VALUE);
212     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Top().Value(), TEST_PADDING_TOP_VALUE);
213     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Right().Value(), TEST_PADDING_RIGHT_VALUE);
214     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Bottom().Value(), TEST_PADDING_BOTTOM_VALUE);
215     EXPECT_EQ(textFieldComponent->GetSelection(), TEST_SELECTION);
216     EXPECT_TRUE(!textFieldComponent->IsSoftKeyboardEnabled());
217 }
218 
219 /**
220  * @tc.name: CreateSearchFromDsl002
221  * @tc.desc: Test search node and search component created correctly with no styles and attributes set.
222  * @tc.type: FUNC
223  */
224 HWTEST_F(DomSearchTest, CreateSearchFromDsl002, TestSize.Level1)
225 {
226     /**
227      * @tc.steps: step1. Construct json string with no styles and attributes set, then create search node with it .
228      * @tc.expected: step1. Search node and child search component are created with default attributes and styles.
229      */
230     std::string searchDsl = "{  \"tag\": \"search\" }";
231     auto domSearch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(searchDsl);
232     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSearch);
233     RefPtr<SearchComponent> searchComponent = AceType::DynamicCast<SearchComponent>(boxChild->GetChild());
234     EXPECT_TRUE(searchComponent);
235     RefPtr<TextFieldComponent> textFieldComponent =
236         AceType::DynamicCast<TextFieldComponent>(searchComponent->GetChild());
237     EXPECT_TRUE(textFieldComponent);
238 
239     /**
240      * @tc.steps: step2. Check styles and attributes of created search node.
241      * @tc.expected: step2. Search node's attrs and styles are as default.
242      */
243     const auto& textStyle = AceType::DynamicCast<DOMSearch>(domSearch)->textStyle_;
244     EXPECT_EQ(textStyle.GetFontSize().Value(), DEFAULT_TEXT_FONT_SIZE);
245     EXPECT_TRUE(textStyle.GetFontWeight() == DEFAULT_TEXT_FONT_WEIGHT);
246     EXPECT_EQ(textStyle.GetFontFamilies()[0], TEST_TEXT_FONT_FAMILY);
247     EXPECT_EQ(textFieldComponent->GetIconImage(), EMPTY_STRING);
248     EXPECT_EQ(textFieldComponent->GetPlaceholder(), EMPTY_STRING);
249     EXPECT_EQ(textFieldComponent->GetTextEditController()->GetText(), EMPTY_STRING);
250     EXPECT_EQ(textFieldComponent->GetFocusTextColor(), Color::FromString(DEFAULT_TEXT_COLOR));
251     EXPECT_EQ(textFieldComponent->GetPlaceholderColor(), Color::FromString(DEFAULT_PLACEHOLDER_COLOR));
252     EXPECT_EQ(textFieldComponent->GetBgColor(), Color::FromString(DEFAULT_BACKGROUND_COLOR));
253     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Left().Value(), DEFAULT_PADDING_LEFT_VALUE);
254     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Top().Value(), DEFAULT_PADDING_TOP_VALUE);
255     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Right().Value(), DEFAULT_PADDING_RIGHT_VALUE);
256     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Bottom().Value(), DEFAULT_PADDING_BOTTOM_VALUE);
257     EXPECT_EQ(textFieldComponent->GetSelection(), DEFAULT_SELECTION);
258 }
259 
260 /**
261  * @tc.name: CreateSearchFromDsl003
262  * @tc.desc: Test search node and search component created correctly with invalid text styles and attributes set.
263  * @tc.type: FUNC
264  */
265 HWTEST_F(DomSearchTest, CreateSearchFromDsl003, TestSize.Level1)
266 {
267     /**
268      * @tc.steps: step1. Construct string with invalid text styles and attributes set, then create search node with it.
269      * @tc.expected: step1. Search node and child search component are created with default text attributes and styles.
270      */
271     auto domSearch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(INVALID_SEARCH_JSON);
272     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSearch);
273     RefPtr<SearchComponent> searchComponent = AceType::DynamicCast<SearchComponent>(boxChild->GetChild());
274     EXPECT_TRUE(searchComponent);
275     RefPtr<TextFieldComponent> textFieldComponent =
276         AceType::DynamicCast<TextFieldComponent>(searchComponent->GetChild());
277     EXPECT_TRUE(textFieldComponent);
278 
279     /**
280      * @tc.steps: step2. Check styles and attributes of created search node.
281      * @tc.expected: step2. Search node's attrs and styles are as default.
282      */
283     const auto& textStyle = AceType::DynamicCast<DOMSearch>(domSearch)->textStyle_;
284     EXPECT_EQ(textStyle.GetFontSize().Value(), INVALID_TEXT_FONT_SIZE);
285     EXPECT_TRUE(textStyle.GetFontWeight() == DEFAULT_FONT_WEIGHT_VALUE);
286     EXPECT_EQ(textStyle.GetFontFamilies()[0], TEST_TEXT_FONT_FAMILY);
287     EXPECT_EQ(textFieldComponent->GetFocusTextColor(), Color::FromString(DEFAULT_COLOR));
288     EXPECT_EQ(textFieldComponent->GetPlaceholderColor(), Color::FromString(DEFAULT_COLOR));
289     EXPECT_EQ(textFieldComponent->GetBgColor(), Color::FromString(DEFAULT_BACKGROUND_COLOR));
290     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Left().Value(), ERROR_PADDING_LEFT_VALUE);
291     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Top().Value(), DEFAULT_PADDING_TOP_VALUE);
292     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Right().Value(), DEFAULT_PADDING_RIGHT_VALUE);
293     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Bottom().Value(), DEFAULT_PADDING_BOTTOM_VALUE);
294     EXPECT_EQ(textFieldComponent->GetIconImage(), EMPTY_STRING);
295     EXPECT_EQ(textFieldComponent->GetPlaceholder(), EMPTY_STRING);
296     EXPECT_EQ(textFieldComponent->GetTextEditController()->GetText(), EMPTY_STRING);
297     EXPECT_EQ(textFieldComponent->GetSelection(), DEFAULT_SELECTION);
298 }
299 
300 /**
301  * @tc.name: CreateSearchFromDsl004
302  * @tc.desc: Test search node and search component created correctly with paddingStart and paddingEnd styles.
303  * @tc.type: FUNC
304  */
305 HWTEST_F(DomSearchTest, CreateSearchFromDsl004, TestSize.Level1)
306 {
307     /**
308      * @tc.steps: step1. Construct string with paddingStart and paddingEnd set, then create search node with it.
309      * @tc.expected: step1. Search node and child search component are created with correct paddingStart and paddingEnd.
310      */
311     auto domSearch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(PADDING_SEARCH_JSON);
312     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSearch);
313     RefPtr<SearchComponent> searchComponent = AceType::DynamicCast<SearchComponent>(boxChild->GetChild());
314     EXPECT_TRUE(searchComponent);
315     RefPtr<TextFieldComponent> textFieldComponent =
316         AceType::DynamicCast<TextFieldComponent>(searchComponent->GetChild());
317     EXPECT_TRUE(textFieldComponent);
318 
319     /**
320      * @tc.steps: step2. Check padding values of created search node.
321      * @tc.expected: step2. Search node's padding values are as expected.
322      */
323     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Left().Value(), TEST_PADDING_LEFT_VALUE);
324     EXPECT_EQ(textFieldComponent->GetDecoration()->GetPadding().Right().Value(), TEST_PADDING_RIGHT_VALUE);
325 }
326 
327 } // namespace OHOS::Ace::Framework
328