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 "ui_test_edit_text.h"
17
18 #include "common/input_method_manager.h"
19 #include "common/screen.h"
20 #include "components/ui_edit_text.h"
21 #include "components/ui_label.h"
22 #include "font/ui_font.h"
23 #include "securec.h"
24 #include "test_edit_text/custom_input_method.h"
25
26 namespace OHOS {
27 namespace {
28 constexpr int16_t EDIT_VIEW_OFFSET = 20;
29 }
30
SetUp()31 void UITestEditText::SetUp()
32 {
33 CustomInputMethod* inputMethod = new CustomInputMethod();
34 InputMethodManager::GetInstance().SetInputMethodListener(inputMethod);
35
36 if (container_ == nullptr) {
37 container_ = new UIScrollView();
38 container_->SetThrowDrag(true);
39 container_->SetHorizontalScrollState(false);
40 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
41 }
42
43 if (changeHint_ == nullptr) {
44 UILabel* title = new UILabel();
45 container_->Add(title);
46 title->SetText("Value:");
47 title->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
48 // 0: position x; 40: position y 60: width 48 height
49 title->SetPosition(0, 40, 60, 48);
50
51 changeHint_ = new UILabel();
52 container_->Add(changeHint_);
53 container_->SetViewId("ChanggeHint");
54 // 60: position x; 40: position y 288: width 48 height
55 changeHint_->SetPosition(60, 40, 288, 48);
56 changeHint_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
57 }
58 }
59
TearDown()60 void UITestEditText::TearDown()
61 {
62 DeleteChildren(container_);
63 container_ = nullptr;
64 changeHint_ = nullptr;
65 }
66
GetTestView()67 const UIView* UITestEditText::GetTestView()
68 {
69 UIKitUIEditTextTestDisplay001();
70 return container_;
71 }
72
UIKitUIEditTextTestDisplay001()73 void UITestEditText::UIKitUIEditTextTestDisplay001()
74 {
75 if (container_ == nullptr) {
76 return;
77 }
78
79 UILabel* label = new UILabel();
80 container_->Add(label);
81 // 288: width; 48: height
82 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
83 label->SetText("UIEditText 效果:");
84 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
85 UIEditText* editText1 = SetupEditText("", "Name:", "editText1");
86 editText1->SetPosition(20, 80); // 20:position x; 80: position x;
87 editText1->SetPlaceholderColor(Color::Blue());
88 editText1->SetCursorColor(Color::Red());
89 container_->Add(editText1);
90
91 UIEditText* editText2 = SetupEditText("zhangsan", "Name 2:", "editText2");
92 container_->Add(editText2);
93 editText2->LayoutBottomToSibling("editText1", EDIT_VIEW_OFFSET);
94 editText2->AlignLeftToSibling("editText1");
95
96 UIEditText* editText3 = SetupEditText("", "Password:", "editText3");
97 container_->Add(editText3);
98 editText3->SetMaxLength(8); // 8: max length
99 editText3->SetInputType(InputType::PASSWORD_TYPE);
100 editText3->LayoutBottomToSibling("editText2", EDIT_VIEW_OFFSET);
101 editText3->AlignLeftToSibling("editText2");
102
103 UIEditText* editText4 = SetupEditText("", "This is a long placeholder", "editText4");
104 container_->Add(editText4);
105 editText4->LayoutBottomToSibling("editText3", EDIT_VIEW_OFFSET);
106 editText4->AlignLeftToSibling("editText3");
107
108 UIEditText* editText5 = SetupEditText("This is a long default value", "This is a long placeholder", "editText5");
109 container_->Add(editText5);
110 editText5->LayoutBottomToSibling("editText4", EDIT_VIEW_OFFSET);
111 editText5->AlignLeftToSibling("editText4");
112 editText5->SetTextColor(Color::Red());
113 }
114
OnClick(UIView & view,const ClickEvent & event)115 bool UITestEditText::OnClick(UIView& view, const ClickEvent& event)
116 {
117 return true;
118 }
119
SetupEditText(const char * value,const char * placeholder,const char * viewId)120 UIEditText* UITestEditText::SetupEditText(const char* value, const char* placeholder, const char* viewId)
121 {
122 UIEditText* editText = new UIEditText();
123 editText->SetText(value);
124 editText->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
125 editText->Resize(280, 50); // 280: width 50: height
126 editText->SetPlaceholder(placeholder);
127 editText->SetViewId(viewId);
128 editText->SetOnChangeListener(this);
129 return editText;
130 }
131
OnChange(UIView & view,const char * value)132 void UITestEditText::OnChange(UIView& view, const char* value)
133 {
134 printf("onchange viewId:%s, value:%s\n", view.GetViewId(), value);
135
136 if (changeHint_ != nullptr) {
137 changeHint_->SetText(value);
138 }
139 }
140 } // namespace OHOS
141