• 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 "core/common/ime/text_selection.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/text_field/textfield_theme.h"
21 #include "core/components/theme/theme_manager_impl.h"
22 #include "frameworks/bridge/common/dom/dom_document.h"
23 #include "frameworks/bridge/common/dom/dom_textarea.h"
24 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS::Ace::Framework {
30 namespace {
31 
32 constexpr uint32_t TEXTAREA_MAXLENGTH_VALUE_DEFAULT = std::numeric_limits<uint32_t>::max();
33 const FontWeight TEXTAREA_FONT_WEIGHT_VALUE_DEFAULT = FontWeight::W400;
34 const std::string TEXTAREA_FONT_FAMILY_VALUE_DEFAULT = "sans-serif";
35 
36 const std::string TEXTAREA_DATA_VALUE = "test dom textarea";
37 const std::string TEXTAREA_PLACEHOLDER_VALUE = "please input";
38 constexpr uint32_t TEXTAREA_MAXLENGTH_VALUE = 100;
39 const std::string TEXTAREA_COLOR_STR_VALUE = "#12345678";
40 const std::string TEXTAREA_PLACEHOLDER_COLOR_STR_VALUE = "#12345678";
41 constexpr double TEXTAREA_FONT_SIZE_VALUE = 30.0;
42 const FontWeight TEXTAREA_FONT_WEIGHT_VALUE = FontWeight::W600;
43 const std::string TEXTAREA_FONT_FAMILY_VALUE = "serif";
44 const std::string TEXTAREA_HEADER_ICON = "test.png";
45 const TextSelection TEXTAREA_SELECTION = TextSelection(2, 5);
46 const TextSelection TEXTAREA_SELECTION_DEFAULT = TextSelection(-1, -1);
47 
48 } // namespace
49 
50 class DomTextareaTest : public testing::Test {
51 public:
52     static void SetUpTestCase();
53     static void TearDownTestCase();
54     void SetUp();
55     void TearDown();
56 };
57 
SetUpTestCase()58 void DomTextareaTest::SetUpTestCase() {}
TearDownTestCase()59 void DomTextareaTest::TearDownTestCase() {}
SetUp()60 void DomTextareaTest::SetUp() {}
TearDown()61 void DomTextareaTest::TearDown() {}
62 
63 /**
64  * @tc.name: DomTextareaCreatorTest001
65  * @tc.desc: Test textarea component are created as default.
66  * @tc.type: FUNC
67  */
68 HWTEST_F(DomTextareaTest, DomTextareaCreatorTest001, TestSize.Level1)
69 {
70     const std::string textareaTestJsonStr = "{"
71                                     "  \"tag\": \"textarea\"                                "
72                                     "}";
73     /**
74      * @tc.steps: step1. Construct string without style and attribute, then create textarea node with it
75      * @tc.expected: step1. textarea component are created successfully.
76      */
77     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(textareaTestJsonStr);
78     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
79     ASSERT_TRUE(boxChild);
80     RefPtr<TextFieldComponent> textareaComponent = AceType::DynamicCast<TextFieldComponent>(boxChild->GetChild());
81     ASSERT_TRUE(textareaComponent);
82 
83     /**
84      * @tc.steps: step2. Check styles and attributes of created textarea node.
85      * @tc.expected: step2. The styles and attributes are as expected.
86      */
87     auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
88     RefPtr<TextFieldTheme> theme = themeManager->GetTheme<TextFieldTheme>();
89     ASSERT_TRUE(theme);
90 
91     EXPECT_EQ(textareaComponent->GetPlaceholderColor(), theme->GetPlaceholderColor());
92     EXPECT_EQ(textareaComponent->GetCursorColor(), theme->GetCursorColor());
93     EXPECT_EQ(textareaComponent->GetMaxLength(), TEXTAREA_MAXLENGTH_VALUE_DEFAULT);
94     EXPECT_EQ(textareaComponent->GetSelection(), TEXTAREA_SELECTION_DEFAULT);
95     EXPECT_TRUE(textareaComponent->IsSoftKeyboardEnabled());
96 
97     auto style = textareaComponent->GetTextStyle();
98     EXPECT_EQ(style.GetTextColor(), theme->GetTextColor());
99     EXPECT_EQ(style.GetFontSize(), theme->GetFontSize());
100     EXPECT_EQ(style.GetFontWeight(), TEXTAREA_FONT_WEIGHT_VALUE_DEFAULT);
101     EXPECT_EQ(style.GetFontFamilies()[0], TEXTAREA_FONT_FAMILY_VALUE_DEFAULT);
102 }
103 
104 /**
105  * @tc.name: DomTextareaCreatorTest002
106  * @tc.desc: Test textarea component are created with all style and attribute.
107  * @tc.type: FUNC
108  */
109 HWTEST_F(DomTextareaTest, DomTextareaCreatorTest002, TestSize.Level1)
110 {
111     const std::string textareaTestJsonStr = "{"
112                                     "  \"tag\": \"textarea\",                               "
113                                     "  \"attr\": [{                                         "
114                                     "                \"value\": \"test dom textarea\"       "
115                                     "              },                                       "
116                                     "              {                                        "
117                                     "                \"placeholder\": \"please input\"      "
118                                     "              },                                       "
119                                     "              {                                        "
120                                     "                \"maxlength\": \"100\"                 "
121                                     "              },                                       "
122                                     "              {                                        "
123                                     "                \"extend\": \"true\"                   "
124                                     "              },                                       "
125                                     "              {                                        "
126                                     "                \"softkeyboardenabled\": \"false\"     "
127                                     "              },                                       "
128                                     "              {                                        "
129                                     "                \"selectedstart\": \"2\"               "
130                                     "              },                                       "
131                                     "              {                                        "
132                                     "                \"selectedend\": \"5\"                 "
133                                     "              },                                       "
134                                     "              {                                        "
135                                     "                \"headerIcon\": \"test.png\"           "
136                                     "              }],                                      "
137                                     "  \"style\":  [{                                       "
138                                     "                \"color\" : \"#12345678\"              "
139                                     "              },                                       "
140                                     "              {                                        "
141                                     "                \"placeholderColor\": \"#12345678\"    "
142                                     "              },                                       "
143                                     "              {                                        "
144                                     "                \"caretColor\": \"#12345678\"          "
145                                     "              },                                       "
146                                     "              {                                        "
147                                     "                \"fontSize\": \"30.0\"                 "
148                                     "              },                                       "
149                                     "              {                                        "
150                                     "                \"fontWeight\": \"600\"                "
151                                     "              },                                       "
152                                     "              {                                        "
153                                     "                \"fontFamily\": \"serif\"              "
154                                     "              }]                                       "
155                                     "}";
156     /**
157      * @tc.steps: step1. Construct string with all style and attribute, then create textarea node with it
158      * @tc.expected: step1. textarea component are created successfully.
159      */
160     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(textareaTestJsonStr);
161     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
162     ASSERT_TRUE(boxChild);
163     RefPtr<TextFieldComponent> textareaComponent = AceType::DynamicCast<TextFieldComponent>(boxChild->GetChild());
164     ASSERT_TRUE(textareaComponent);
165 
166     /**
167      * @tc.steps: step2. Check styles and attributes of created textarea node.
168      * @tc.expected: step2. The styles and attributes are as expected.
169      */
170     EXPECT_EQ(textareaComponent->GetPlaceholder(), TEXTAREA_PLACEHOLDER_VALUE);
171     EXPECT_EQ(textareaComponent->GetPlaceholderColor(), Color::FromString(TEXTAREA_COLOR_STR_VALUE));
172     EXPECT_EQ(textareaComponent->GetCursorColor(), Color::FromString(TEXTAREA_COLOR_STR_VALUE));
173     EXPECT_EQ(textareaComponent->GetMaxLength(), TEXTAREA_MAXLENGTH_VALUE);
174     EXPECT_TRUE(textareaComponent->IsExtend());
175     EXPECT_TRUE(!textareaComponent->IsSoftKeyboardEnabled());
176     EXPECT_EQ(textareaComponent->GetIconImage(), TEXTAREA_HEADER_ICON);
177     EXPECT_EQ(textareaComponent->GetSelection(), TEXTAREA_SELECTION);
178 
179     auto style = textareaComponent->GetTextStyle();
180     EXPECT_EQ(style.GetTextColor(), Color::FromString(TEXTAREA_PLACEHOLDER_COLOR_STR_VALUE));
181     EXPECT_EQ(style.GetFontSize().Value(), TEXTAREA_FONT_SIZE_VALUE);
182     EXPECT_EQ(style.GetFontWeight(), TEXTAREA_FONT_WEIGHT_VALUE);
183     EXPECT_EQ(style.GetFontFamilies()[0], TEXTAREA_FONT_FAMILY_VALUE);
184 }
185 
186 } // namespace OHOS::Ace::Framework
187