1 /* 2 * Copyright (c) 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 "components/ui_edit_text.h" 17 18 #include <climits> 19 #include <gtest/gtest.h> 20 21 #include "securec.h" 22 23 using namespace testing::ext; 24 namespace OHOS { 25 namespace { 26 constexpr int16_t INIT_WIDTH = 100; 27 constexpr int16_t INIT_HEIGHT = 150; 28 } // namespace 29 30 class EditTextOnChangeListener : public UIEditText::OnChangeListener { 31 public: OnChange(UIView & view,const char * value)32 void OnChange(UIView& view, const char* value) override 33 { 34 uint32_t textLen = static_cast<uint32_t>(strlen(value)); 35 if (value_ != nullptr) { 36 UIFree(value_); 37 value_ = nullptr; 38 } 39 value_ = static_cast<char*>(UIMalloc(textLen + 1)); 40 if (value_ == nullptr) { 41 return; 42 } 43 if (strncpy_s(value_, textLen + 1, value, textLen) != EOK) { 44 UIFree(value_); 45 value_ = nullptr; 46 return; 47 } 48 } 49 GetValue()50 const char* GetValue() 51 { 52 return value_; 53 } 54 ~EditTextOnChangeListener()55 virtual ~EditTextOnChangeListener() 56 { 57 if (value_ != nullptr) { 58 delete value_; 59 value_ = nullptr; 60 } 61 } 62 63 private: 64 char* value_ = nullptr; 65 }; 66 67 class UIEditTextTest : public testing::Test { 68 public: SetUpTestCase(void)69 static void SetUpTestCase(void) {} TearDownTestCase(void)70 static void TearDownTestCase(void) {} 71 }; 72 73 /** 74 * @tc.name: UIEditTextGetViewType_001 75 * @tc.desc: Verify GetViewType and GetHeight function. 76 * @tc.type: FUNC 77 * @tc.require: issueI5AD4A 78 */ 79 HWTEST_F(UIEditTextTest, UIEditTextGetViewType_001, TestSize.Level1) 80 { 81 UIEditText* editText = new UIEditText(); 82 EXPECT_EQ(editText->GetViewType(), UI_EDIT_TEXT); 83 delete editText; 84 } 85 86 /** 87 * @tc.name: UIEditTextResize_001 88 * @tc.desc: Verify Resize function. 89 * @tc.type: FUNC 90 * @tc.require: issueI5AD4A 91 */ 92 HWTEST_F(UIEditTextTest, UIEditTextResize_001, TestSize.Level1) 93 { 94 UIEditText* editText = new UIEditText(); 95 editText->Resize(INIT_WIDTH, INIT_HEIGHT); 96 EXPECT_EQ(editText->GetWidth(), INIT_WIDTH); 97 EXPECT_EQ(editText->GetHeight(), INIT_HEIGHT); 98 delete editText; 99 } 100 101 /** 102 * @tc.name: UIEditTextSetText_001 103 * @tc.desc: Verify SetText function. 104 * @tc.type: FUNC 105 * @tc.require: issueI5AD4A 106 */ 107 HWTEST_F(UIEditTextTest, UIEditTextSetText_001, TestSize.Level1) 108 { 109 UIEditText* editText = new UIEditText(); 110 const char* text = "abc"; 111 editText->Resize(INIT_WIDTH, INIT_HEIGHT); 112 editText->SetText(text); 113 114 const char* textTmp = editText->GetText(); 115 ASSERT_TRUE(textTmp); 116 EXPECT_EQ(strcmp(textTmp, text), 0); 117 delete editText; 118 } 119 120 /** 121 * @tc.name: UIEditTextSetPlaceholder_001 122 * @tc.desc: Verify SetPlaceholder function. 123 * @tc.type: FUNC 124 * @tc.require: issueI5AD4A 125 */ 126 HWTEST_F(UIEditTextTest, UIEditTextSetPlaceholder_001, TestSize.Level1) 127 { 128 UIEditText* editText = new UIEditText(); 129 const char* placeholder = "Name:"; 130 editText->Resize(INIT_WIDTH, INIT_HEIGHT); 131 editText->SetPlaceholder(placeholder); 132 133 const char* placeholderTmp = editText->GetPlaceholder(); 134 ASSERT_TRUE(placeholderTmp); 135 EXPECT_EQ(strcmp(placeholderTmp, placeholder), 0); 136 delete editText; 137 } 138 139 /** 140 * @tc.name: UIEditTextSetMaxLength_001 141 * @tc.desc: Verify SetMaxLength function. 142 * @tc.type: FUNC 143 * @tc.require: issueI5AD4A 144 */ 145 HWTEST_F(UIEditTextTest, UIEditTextSetMaxLength_001, TestSize.Level1) 146 { 147 UIEditText* editText = new UIEditText(); 148 uint16_t length1 = 20; 149 editText->SetMaxLength(length1); 150 EXPECT_EQ(editText->GetMaxLength(), length1); 151 152 uint16_t length2 = 0; 153 editText->SetMaxLength(length2); 154 EXPECT_EQ(editText->GetMaxLength(), length2); 155 delete editText; 156 } 157 158 /** 159 * @tc.name: UIEditTextSetInputType_001 160 * @tc.desc: Verify SetInputType function. 161 * @tc.type: FUNC 162 * @tc.require: issueI5AD4A 163 */ 164 HWTEST_F(UIEditTextTest, UIEditTextSetInputType_001, TestSize.Level1) 165 { 166 UIEditText* editText = new UIEditText(); 167 // check the default type 168 EXPECT_EQ(editText->GetInputType(), InputType::TEXT_TYPE); 169 170 InputType type = InputType::TEXT_TYPE; 171 editText->SetInputType(type); 172 EXPECT_EQ(editText->GetInputType(), type); 173 174 type = InputType::PASSWORD_TYPE; 175 editText->SetInputType(type); 176 EXPECT_EQ(editText->GetInputType(), type); 177 delete editText; 178 } 179 180 /** 181 * @tc.name: UIEditTextSetTextColor_001 182 * @tc.desc: Verify SetTextColor function. 183 * @tc.type: FUNC 184 * @tc.require: issueI5AD4A 185 */ 186 HWTEST_F(UIEditTextTest, UIEditTextSetTextColor_001, TestSize.Level1) 187 { 188 UIEditText* editText = new UIEditText(); 189 ColorType color = Color::White(); 190 191 editText->SetTextColor(color); 192 EXPECT_EQ(editText->GetTextColor().full, color.full); 193 delete editText; 194 } 195 196 /** 197 * @tc.name: UIEditTextSetPlaceholderColor_001 198 * @tc.desc: Verify SetPlaceholderColor function. 199 * @tc.type: FUNC 200 * @tc.require: issueI5AD4A 201 */ 202 HWTEST_F(UIEditTextTest, UIEditTextSetPlaceholderColor_001, TestSize.Level1) 203 { 204 UIEditText* editText = new UIEditText(); 205 ColorType color = Color::White(); 206 207 editText->SetPlaceholderColor(color); 208 EXPECT_EQ(editText->GetPlaceholderColor().full, color.full); 209 delete editText; 210 } 211 212 /** 213 * @tc.name: UIEditTextSetCursorColor_001 214 * @tc.desc: Verify SetCursorColor function. 215 * @tc.type: FUNC 216 * @tc.require: issueI5AD4A 217 */ 218 HWTEST_F(UIEditTextTest, UIEditTextSetCursorColor_001, TestSize.Level1) 219 { 220 UIEditText* editText = new UIEditText(); 221 ColorType color = Color::White(); 222 223 editText->SetCursorColor(color); 224 EXPECT_EQ(editText->GetCursorColor().full, color.full); 225 delete editText; 226 } 227 228 /** 229 * @tc.name: UIEditTextSetFont_001 230 * @tc.desc: Verify SetFont function. 231 * @tc.type: FUNC 232 * @tc.require: issueI5AD4A 233 */ 234 HWTEST_F(UIEditTextTest, UIEditTextSetFont_001, TestSize.Level1) 235 { 236 UIEditText* editText = new UIEditText(); 237 uint16_t fontId = editText->GetFontId(); 238 239 const uint8_t fontSize = 20; // 20: font size for test 240 editText->SetFont("error_font_name", fontSize); 241 242 EXPECT_EQ(editText->GetFontId(), fontId); 243 delete editText; 244 } 245 246 /** 247 * @tc.name: UIEditTextGetTextWidth_001 248 * @tc.desc: Verify GetTextWidth function. 249 * @tc.type: FUNC 250 * @tc.require: issueI5AD4A 251 */ 252 HWTEST_F(UIEditTextTest, UIEditTextGetTextWidth_001, TestSize.Level1) 253 { 254 UIEditText* editText = new UIEditText(); 255 const char* text = "abc"; 256 editText->SetText(text); 257 uint16_t width = editText->GetTextWidth(); 258 EXPECT_EQ(width, 0); // no font file case 259 delete editText; 260 } 261 262 /** 263 * @tc.name: UIEditTextGetTextHeight_001 264 * @tc.desc: Verify GetTextHeight function. 265 * @tc.type: FUNC 266 * @tc.require: issueI5AD4A 267 */ 268 HWTEST_F(UIEditTextTest, UIEditTextGetTextHeight_001, TestSize.Level1) 269 { 270 UIEditText* editText = new UIEditText(); 271 const char* text = "abc"; 272 editText->SetText(text); 273 uint16_t height = editText->GetTextHeight(); 274 EXPECT_EQ(height, 0); // no font file case 275 delete editText; 276 } 277 278 /** 279 * @tc.name: UIEditTextSetOnChangeListener_001 280 * @tc.desc: Verify SetOnChangeListener function. 281 * @tc.type: FUNC 282 * @tc.require: issueI5AD4A 283 */ 284 HWTEST_F(UIEditTextTest, UIEditTextSetOnChangeListener_001, TestSize.Level1) 285 { 286 UIEditText* editText = new UIEditText(); 287 EditTextOnChangeListener* listener = new EditTextOnChangeListener(); 288 editText->SetOnChangeListener(listener); 289 EXPECT_EQ(editText->GetOnChangeListener(), listener); 290 delete listener; 291 delete editText; 292 } 293 294 /** 295 * @tc.name: UIEditTextSetOnChangeListener_002 296 * @tc.desc: Verify OnChangeListener OnChange function. 297 * @tc.type: FUNC 298 * @tc.require: issueI5AD4A 299 */ 300 HWTEST_F(UIEditTextTest, UIEditTextSetOnChangeListener_002, TestSize.Level1) 301 { 302 UIEditText* editText = new UIEditText(); 303 EditTextOnChangeListener* listener = new EditTextOnChangeListener(); 304 editText->SetOnChangeListener(listener); 305 const char* text = "abc"; 306 editText->SetText(text); 307 const char* textTmp = editText->GetText(); 308 const char* valueTmp = listener->GetValue(); 309 if ((textTmp == nullptr) || (valueTmp == nullptr)) { 310 EXPECT_EQ(1, 0); 311 delete listener; 312 delete editText; 313 return; 314 } 315 bool ret = strcmp(textTmp, valueTmp); 316 EXPECT_EQ(ret, 0); 317 delete listener; 318 delete editText; 319 } 320 } // namespace OHOS 321