• 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/i18n/localization.h"
19 #include "core/components/common/layout/constants.h"
20 #include "frameworks/bridge/common/dom/dom_document.h"
21 #include "frameworks/bridge/common/dom/dom_switch.h"
22 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Ace::Framework {
28 namespace {
29 
30 const std::string TEST_TEXT_ON = "Onn";
31 const std::string TEST_TEXT_OFF = "Offf";
32 const std::string TEST_TEXT_COLOR_ON = "#00FF00";
33 const std::string TEST_TEXT_COLOR_OFF = "#0000FF";
34 const std::string TEST_TEXT_FONT_FAMILY = "serif";
35 constexpr FontWeight TEST_TEXT_FONT_WEIGHT = FontWeight::W400;
36 constexpr FontStyle TEST_TEXT_FONT_STYLE = FontStyle::NORMAL;
37 constexpr TextDecoration TEST_TEXT_DECORATION = TextDecoration::NONE;
38 const double TEST_TEXT_PADDING = 12;
39 constexpr double TEST_TEXT_FONT_SIZE = 30.0;
40 constexpr Dimension TEST_TEXT_LETTER_SPACING = 10.0_px;
41 const std::string DEFAULT_TEXT_ON = "On";
42 const std::string DEFAULT_TEXT_OFF = "Off";
43 const std::string DEFAULT_TEXT_COLOR_ON = "#000000";
44 const std::string DEFAULT_TEXT_COLOR_OFF = "#000000";
45 constexpr FontWeight DEFAULT_TEXT_FONT_WEIGHT = FontWeight::NORMAL;
46 constexpr FontStyle DEFAULT_TEXT_FONT_STYLE = FontStyle::NORMAL;
47 constexpr TextDecoration DEFAULT_TEXT_DECORATION = TextDecoration::NONE;
48 const double DEFAULT_TEXT_PADDING = 0.0;
49 constexpr double DEFAULT_TEXT_FONT_SIZE = 14.0;
50 constexpr double INVALID_TEXT_FONT_SIZE = 0.0;
51 constexpr Dimension DEFAULT_TEXT_LETTER_SPACING = 0.0_px;
52 const std::string TEXT_SWITCH_JSON = "{                                                          "
53                                      "  \"tag\": \"switch\",                                     "
54                                      "  \"attr\": [{                                             "
55                                      "                \"texton\" : \"Onn\"                       "
56                                      "              },                                           "
57                                      "              {                                            "
58                                      "                \"textoff\" : \"Offf\"                     "
59                                      "              },                                           "
60                                      "              {                                            "
61                                      "                \"showtext\" : \"true\"                    "
62                                      "              }],                                          "
63                                      "  \"style\": [{                                            "
64                                      "           \"fontSize\":\"30.0\"                           "
65                                      "          },                                               "
66                                      "          {                                                "
67                                      "           \"textPadding\":\"12.0\"                        "
68                                      "          },                                               "
69                                      "          {                                                "
70                                      "           \"fontWeight\":\"400\"                          "
71                                      "           },                                              "
72                                      "          {                                                "
73                                      "           \"textonColor\":\"#00ff00\"                     "
74                                      "           },                                              "
75                                      "          {                                                "
76                                      "           \"textoffColor\":\"#0000ff\"                    "
77                                      "           },                                              "
78                                      "          {                                                "
79                                      "           \"fontStyle\":\"normal\"                        "
80                                      "           },                                              "
81                                      "          {                                                "
82                                      "           \"textDecoration\":\"none\"                     "
83                                      "           },                                              "
84                                      "          {                                                "
85                                      "           \"letterSpacing\":\"10.0\"                      "
86                                      "           },                                              "
87                                      "           {                                               "
88                                      "           \"fontFamily\":\"serif\"                        "
89                                      "            }]                                             "
90                                      "}                                                          ";
91 const std::string INVALID_TEXT_ATTR_SWITCH_JSON = "{                                                          "
92                                                   "  \"tag\": \"switch\",                                     "
93                                                   "  \"attr\": [{                                             "
94                                                   "                \"showtext\" : \"1111\"                    "
95                                                   "              }],                                          "
96                                                   "  \"style\": [{                                            "
97                                                   "           \"fontSize\":\"abcd\"                           "
98                                                   "          },                                               "
99                                                   "          {                                                "
100                                                   "           \"textPadding\":\"abcd\"                        "
101                                                   "          },                                               "
102                                                   "          {                                                "
103                                                   "           \"fontWeight\":\"abc\"                          "
104                                                   "           },                                              "
105                                                   "          {                                                "
106                                                   "           \"textonColor\":\"wxyz\"                        "
107                                                   "           },                                              "
108                                                   "          {                                                "
109                                                   "           \"textoffColor\":\"#wxyz\"                      "
110                                                   "           },                                              "
111                                                   "          {                                                "
112                                                   "           \"fontStyle\":\"abcd\"                          "
113                                                   "           },                                              "
114                                                   "          {                                                "
115                                                   "           \"textDecoration\":\"abcd\"                     "
116                                                   "           },                                              "
117                                                   "           {                                               "
118                                                   "           \"letterSpacing\":\"abcd\"                      "
119                                                   "            }]                                             "
120                                                   "}                                                          ";
121 } // namespace
122 
123 class DomSwitchTest : public testing::Test {
124 public:
125     static void SetUpTestCase();
126     static void TearDownTestCase();
127     void SetUp();
128     void TearDown();
129 };
130 
SetUpTestCase()131 void DomSwitchTest::SetUpTestCase() {}
TearDownTestCase()132 void DomSwitchTest::TearDownTestCase() {}
SetUp()133 void DomSwitchTest::SetUp() {}
TearDown()134 void DomSwitchTest::TearDown() {}
135 
136 /**
137  * @tc.name: DomSwitchCreatorTest001
138  * @tc.desc: Test switch node and child switch component are created as expected.
139  * @tc.type: FUNC
140  */
141 HWTEST_F(DomSwitchTest, DomSwitchCreatorTest001, TestSize.Level1)
142 {
143     /**
144      * @tc.steps: step1. Construct string with right fields, then create switch node with it.
145      * @tc.expected: step1. Switch node and switch component are created successfully.
146      */
147     std::string switchDsl = "{                                                          "
148                             "  \"tag\": \"switch\",                                     "
149                             "  \"attr\": [{                                             "
150                             "                \"checked\" : \"true\"                     "
151                             "              },                                           "
152                             "              {                                            "
153                             "                \"disabled\" : \"true\"                    "
154                             "              }],                                          "
155                             "  \"event\": [ \"change\" ]                                "
156                             "}                                                          ";
157     Localization::GetInstance()->SetLocale("en", "US", "", "", "en-US");
158     auto domSwitch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(switchDsl);
159     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSwitch);
160     RefPtr<SwitchComponent> switchComponent = AceType::DynamicCast<SwitchComponent>(boxChild->GetChild());
161     EXPECT_TRUE(switchComponent);
162 
163     /**
164      * @tc.steps: step2. Check styles and attributes of created switch node.
165      * @tc.expected: step2. The styles and attributes are as expected.
166      */
167     EXPECT_TRUE(switchComponent->GetValue());
168     EXPECT_TRUE(switchComponent->IsDisabled());
169     EXPECT_TRUE(switchComponent->GetChangeEvent() == std::to_string(domSwitch->GetNodeId()));
170 }
171 
172 /**
173  * @tc.name: DomSwitchCreatorTest002
174  * @tc.desc: Test switch node and switch component created successfully with empty input of style and attribute.
175  * @tc.type: FUNC
176  */
177 HWTEST_F(DomSwitchTest, DomSwitchCreatorTest002, TestSize.Level1)
178 {
179     /**
180      * @tc.steps: step1. Construct string without style and attribute, then create switch node with it .
181      * @tc.expected: step1. Switch node and child switch component are created successfully.
182      */
183     std::string switchDsl = "{  \"tag\": \"switch\" }";
184     Localization::GetInstance()->SetLocale("en", "US", "", "", "en-US");
185     auto domSwitch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(switchDsl);
186     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSwitch);
187     RefPtr<SwitchComponent> switchComponent = AceType::DynamicCast<SwitchComponent>(boxChild->GetChild());
188     EXPECT_TRUE(switchComponent);
189 
190     /**
191      * @tc.steps: step2. Check styles and attributes of created switch node.
192      * @tc.expected: step2. Switch node's attrs and styles are initialized with default value.
193      */
194     EXPECT_FALSE(switchComponent->GetValue());
195     EXPECT_FALSE(switchComponent->IsDisabled());
196 }
197 
198 /**
199  * @tc.name: CreateTextSwitchFromDsl001
200  * @tc.desc: Test switch node and switch component created successfully with text styles and attributes set.
201  * @tc.type: FUNC
202  */
203 HWTEST_F(DomSwitchTest, CreateTextSwitchFromDsl001, TestSize.Level1)
204 {
205     /**
206      * @tc.steps: step1. Construct string text styles and attributes set, then create switch node with it .
207      * @tc.expected: step1. Switch node and child switch component are created with correct text attributes and styles.
208      */
209     Localization::GetInstance()->SetLocale("en", "US", "", "", "en-US");
210     auto domSwitch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(TEXT_SWITCH_JSON);
211     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSwitch);
212     RefPtr<SwitchComponent> switchComponent = AceType::DynamicCast<SwitchComponent>(boxChild->GetChild());
213     EXPECT_TRUE(switchComponent);
214 
215     /**
216      * @tc.steps: step2. Check styles and attributes of created switch node.
217      * @tc.expected: step2. Switch node's attrs and styles are as expected.
218      */
219     EXPECT_TRUE(switchComponent->GetShowText());
220     EXPECT_EQ(switchComponent->GetTextOn(), TEST_TEXT_ON);
221     EXPECT_EQ(switchComponent->GetTextOff(), TEST_TEXT_OFF);
222     EXPECT_EQ(switchComponent->GetTextPadding().Value(), TEST_TEXT_PADDING);
223     EXPECT_TRUE(switchComponent->GetTextStyle().GetFontWeight() == TEST_TEXT_FONT_WEIGHT);
224     EXPECT_TRUE(switchComponent->GetTextStyle().GetFontStyle() == TEST_TEXT_FONT_STYLE);
225     EXPECT_TRUE(switchComponent->GetTextStyle().GetTextDecoration() == TEST_TEXT_DECORATION);
226     EXPECT_EQ(switchComponent->GetTextStyle().GetFontSize().Value(), TEST_TEXT_FONT_SIZE);
227     EXPECT_EQ(switchComponent->GetTextColorOn(), Color::FromString(TEST_TEXT_COLOR_ON));
228     EXPECT_EQ(switchComponent->GetTextColorOff(), Color::FromString(TEST_TEXT_COLOR_OFF));
229     EXPECT_TRUE(
230         NearEqual(switchComponent->GetTextStyle().GetLetterSpacing().Value(), TEST_TEXT_LETTER_SPACING.Value()));
231     EXPECT_EQ(switchComponent->GetTextStyle().GetFontFamilies()[0], TEST_TEXT_FONT_FAMILY);
232 }
233 
234 /**
235  * @tc.name: CreateTextSwitchFromDsl002
236  * @tc.desc: Test switch node and switch component created correctly with no text styles and attributes set.
237  * @tc.type: FUNC
238  */
239 HWTEST_F(DomSwitchTest, CreateTextSwitchFromDsl002, TestSize.Level1)
240 {
241     /**
242      * @tc.steps: step1. Construct string with no text styles and attributes set, then create switch node with it .
243      * @tc.expected: step1. Switch node and child switch component are created with default text attributes and styles.
244      */
245     std::string defaultTextSwitch = "{  \"tag\": \"switch\" }";
246     Localization::GetInstance()->SetLocale("en", "US", "", "", "en-US");
247     auto domSwitch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(defaultTextSwitch);
248     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSwitch);
249     RefPtr<SwitchComponent> switchComponent = AceType::DynamicCast<SwitchComponent>(boxChild->GetChild());
250     EXPECT_TRUE(switchComponent);
251 
252     /**
253      * @tc.steps: step2. Check styles and attributes of created switch node.
254      * @tc.expected: step2. Switch node's attrs and styles are as default.
255      */
256     EXPECT_TRUE(!switchComponent->GetShowText());
257     EXPECT_EQ(switchComponent->GetTextOn(), DEFAULT_TEXT_ON);
258     EXPECT_EQ(switchComponent->GetTextOff(), DEFAULT_TEXT_OFF);
259     EXPECT_EQ(switchComponent->GetTextPadding().Value(), DEFAULT_TEXT_PADDING);
260     EXPECT_TRUE(switchComponent->GetTextStyle().GetFontWeight() == DEFAULT_TEXT_FONT_WEIGHT);
261     EXPECT_TRUE(switchComponent->GetTextStyle().GetFontStyle() == DEFAULT_TEXT_FONT_STYLE);
262     EXPECT_TRUE(switchComponent->GetTextStyle().GetTextDecoration() == DEFAULT_TEXT_DECORATION);
263     EXPECT_EQ(switchComponent->GetTextStyle().GetFontSize().Value(), DEFAULT_TEXT_FONT_SIZE);
264     EXPECT_EQ(switchComponent->GetTextColorOn(), Color::FromString(DEFAULT_TEXT_COLOR_ON));
265     EXPECT_EQ(switchComponent->GetTextColorOff(), Color::FromString(DEFAULT_TEXT_COLOR_OFF));
266     EXPECT_TRUE(
267         NearEqual(switchComponent->GetTextStyle().GetLetterSpacing().Value(), DEFAULT_TEXT_LETTER_SPACING.Value()));
268     EXPECT_TRUE(switchComponent->GetTextStyle().GetFontFamilies().empty());
269 }
270 
271 /**
272  * @tc.name: CreateTextSwitchFromDsl003
273  * @tc.desc: Test switch node and switch component created correctly with invalid text styles and attributes set.
274  * @tc.type: FUNC
275  */
276 HWTEST_F(DomSwitchTest, CreateTextSwitchFromDsl003, TestSize.Level1)
277 {
278     /**
279      * @tc.steps: step1. Construct string with invalid text styles and attributes set, then create switch node with it .
280      * @tc.expected: step1. Switch node and child switch component are created with default text attributes and styles.
281      */
282     Localization::GetInstance()->SetLocale("en", "US", "", "", "en-US");
283     auto domSwitch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(INVALID_TEXT_ATTR_SWITCH_JSON);
284     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSwitch);
285     RefPtr<SwitchComponent> switchComponent = AceType::DynamicCast<SwitchComponent>(boxChild->GetChild());
286     EXPECT_TRUE(switchComponent);
287 
288     /**
289      * @tc.steps: step2. Check styles and attributes of created switch node.
290      * @tc.expected: step2. Switch node's attrs and styles are as default.
291      */
292     EXPECT_TRUE(!switchComponent->GetShowText());
293     EXPECT_EQ(switchComponent->GetTextOn(), DEFAULT_TEXT_ON);
294     EXPECT_EQ(switchComponent->GetTextOff(), DEFAULT_TEXT_OFF);
295     EXPECT_EQ(switchComponent->GetTextPadding().Value(), DEFAULT_TEXT_PADDING);
296     EXPECT_TRUE(switchComponent->GetTextStyle().GetFontWeight() == DEFAULT_TEXT_FONT_WEIGHT);
297     EXPECT_TRUE(switchComponent->GetTextStyle().GetFontStyle() == DEFAULT_TEXT_FONT_STYLE);
298     EXPECT_TRUE(switchComponent->GetTextStyle().GetTextDecoration() == DEFAULT_TEXT_DECORATION);
299     EXPECT_EQ(switchComponent->GetTextStyle().GetFontSize().Value(), INVALID_TEXT_FONT_SIZE);
300     EXPECT_EQ(switchComponent->GetTextColorOn(), Color::FromString(DEFAULT_TEXT_COLOR_ON));
301     EXPECT_EQ(switchComponent->GetTextColorOff(), Color::FromString(DEFAULT_TEXT_COLOR_OFF));
302     EXPECT_TRUE(
303         NearEqual(switchComponent->GetTextStyle().GetLetterSpacing().Value(), DEFAULT_TEXT_LETTER_SPACING.Value()));
304 }
305 
306 /**
307  * @tc.name: CreateTextSwitchFromDsl004
308  * @tc.desc: Test switch set [showtext] without [texton]/[textoff] can display default texts..
309  * @tc.type: FUNC
310  */
311 HWTEST_F(DomSwitchTest, CreateTextSwitchFromDsl004, TestSize.Level1)
312 {
313     /**
314      * @tc.steps: step1. Construct string with right fields, then create switch node with it.
315      * @tc.expected: step1. Switch node and switch component are created successfully.
316      */
317     std::string switchDsl = "{                                                          "
318                             "  \"tag\": \"switch\",                                     "
319                             "  \"attr\": [{                                             "
320                             "                \"showtext\" : \"true\"                    "
321                             "              }]                                           "
322                             "}                                                          ";
323     Localization::GetInstance()->SetLocale("en", "US", "", "", "en-US");
324     auto domSwitch = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(switchDsl);
325     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domSwitch);
326     RefPtr<SwitchComponent> switchComponent = AceType::DynamicCast<SwitchComponent>(boxChild->GetChild());
327     EXPECT_TRUE(switchComponent);
328 
329     /**
330      * @tc.steps: step2. Check styles and attributes of created switch node.
331      * @tc.expected: step2. Value of [texton]/[textoff] is as expected.
332      */
333     EXPECT_TRUE(switchComponent->GetShowText());
334     EXPECT_EQ(switchComponent->GetTextOn(), DEFAULT_TEXT_ON);
335     EXPECT_EQ(switchComponent->GetTextOff(), DEFAULT_TEXT_OFF);
336 }
337 
338 } // namespace OHOS::Ace::Framework
339