ui_test_label.cpp 13.5 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ui_test_label.h"
#include "common/screen.h"
#include "components/ui_checkbox.h"
#include "components/ui_label.h"
#include "font/ui_font.h"

namespace OHOS {
namespace {
const int16_t GAP = 5;
const int16_t TITLE_HEIGHT = 20;
const uint16_t LABEL_WIDTH = 350;
const uint16_t LABEL_HEIGHT = 50;
} // namespace

void UITestLabel::SetUp()
{
    if (container_ == nullptr) {
        container_ = new UIScrollView();
        container_->SetThrowDrag(true);
        container_->SetHorizontalScrollState(false);
        container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
        positionX_ = 0;
        positionY_ = 0;
    }
}

void UITestLabel::TearDown()
{
    DeleteChildren(container_);
    container_ = nullptr;
    uiLabel = nullptr;
    labelFontSizeBtn1_ = nullptr;
    labelFontSizeBtn2_ = nullptr;
    labelFontSizeBtn3_ = nullptr;
    labelHorAlignBtn1_ = nullptr;
    labelHorAlignBtn2_ = nullptr;
    labelHorAlignBtn3_ = nullptr;
    labelColorBtn1_ = nullptr;
    labelColorBtn2_ = nullptr;
    labelColorBtn3_ = nullptr;
    labelBeyondBtn1_ = nullptr;
    labelBeyondBtn2_ = nullptr;
    labelBeyondBtn3_ = nullptr;
    labelLineBtn1_ = nullptr;
    labelLineBtn2_ = nullptr;
    labelVerAlignlBtn1_ = nullptr;
    labelVerAlignlBtn2_ = nullptr;
    labelVerAlignlBtn3_ = nullptr;
    labelDirectionBtn1_ = nullptr;
    labelDirectionBtn2_ = nullptr;
    labelSizeBtn1_ = nullptr;
    labelSizeBtn2_ = nullptr;
}

UIView* UITestLabel::GetTestView()
{
    UIKit_UILabel_Test_Display_001();
    UIKit_UILabel_Test_Display_002();
    UIKit_UILabel_Test_Display_003();
    return container_;
}

void UITestLabel::UIKit_UILabel_Test_Display_001()
{
    if (container_ != nullptr) {
        UIViewGroup* uiViewGroup = new UIViewGroup();
        // 320: width; 390: height
        uiViewGroup->SetPosition(positionX_, positionY_, 320, 390);
        container_->Add(uiViewGroup);
        UILabel* label = new UILabel();
        uiViewGroup->Add(label);
        // 288: width; 48: height
        label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
        label->SetText("UILabel效果");
        label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
        UIViewGroup* uiViewGroupFrame = new UIViewGroup();
        uiViewGroup->Add(uiViewGroupFrame);
        // 288: width; 336: height
        uiViewGroupFrame->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
        uiViewGroupFrame->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
        uiViewGroupFrame->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
        uiViewGroupFrame->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
        uiViewGroupFrame->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
        uiViewGroupFrame->SetStyle(STYLE_BACKGROUND_OPA, 0);
        uiLabel = new UILabel();
        uiLabel->SetText("鸿蒙轻量级图形子系统");
        uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: font size
        uiLabel->SetPosition(0, 141, 280, 336);             // 141: y-coordinate; 280: width; 336: height
        uiViewGroupFrame->Add(uiLabel);
        positionX_ += 336; // 336: x-coordinate

        UIViewGroup* uiViewGroup2 = new UIViewGroup();
        uiViewGroup2->SetPosition(positionX_, 0, 300, 384); // 300: width; 384: height
        container_->Add(uiViewGroup2);
        CreateLabels(uiViewGroup2);
    }
}

void UITestLabel::CreateLabels(UIViewGroup* uiViewGroup)
{
    if (uiViewGroup == nullptr) {
        return;
    }
    UILabel* label1 = new UILabel();
    uiViewGroup->Add(label1);
    // 288: width; 48: height
    label1->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
    label1->SetText("字号");
    label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
    labelFontSizeBtn1_ = SetUpButton("18 ", 24, 48, uiViewGroup); // 24: x-coordinate; 48: y-coordinate
    labelFontSizeBtn2_ = SetUpButton("24", 116, 48, uiViewGroup); // 116: x-coordinate; 48: y-coordinate
    labelFontSizeBtn3_ = SetUpButton("30", 208, 48, uiViewGroup); // 208: x-coordinate; 48: y-coordinate

    UILabel* label2 = new UILabel();
    uiViewGroup->Add(label2);
    label2->SetPosition(24, 107, 288, 48); // 24: x-coordinate, 107: y-coordinate, 288: width; 48: height
    label2->SetText("水平对齐方式");
    label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
    labelHorAlignBtn1_ = SetUpButton("左对齐 ", 24, 144, uiViewGroup);  // 24: x-coordinate; 144: y-coordinate
    labelHorAlignBtn2_ = SetUpButton("中对齐 ", 116, 144, uiViewGroup); // 116: x-coordinate; 144: y-coordinate
    labelHorAlignBtn3_ = SetUpButton("右对齐 ", 208, 144, uiViewGroup); // 208: x-coordinate; 144: y-coordinate

    UILabel* label3 = new UILabel();
    uiViewGroup->Add(label3);
    label3->SetPosition(24, 203, 288, 48); // 24: x-coordinate, 203: y-coordinate, 288: width; 48: height
    label3->SetText("颜色");
    label3->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
    labelColorBtn1_ = SetUpButton("白色 ", 24, 240, uiViewGroup);    // 24: x-coordinate; 240: y-coordinate
    labelColorBtn2_ = SetUpButton("变色 ", 116, 240, uiViewGroup);   // 116: x-coordinate; 240: y-coordinate
    labelColorBtn3_ = SetUpButton("半透明 ", 208, 240, uiViewGroup); // 208: x-coordinate; 240: y-coordinate

    UILabel* label4 = new UILabel();
    uiViewGroup->Add(label4);
    label4->SetPosition(24, 299, 288, 48); // 24: x-coordinate, 299: y-coordinate, 288: width; 48: height
    label4->SetText("超出处理");
    label4->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
    labelBeyondBtn1_ = SetUpButton("截断 ", 24, 336, uiViewGroup);  // 24: x-coordinate; 336: y-coordinate
    labelBeyondBtn2_ = SetUpButton("省略 ", 116, 336, uiViewGroup); // 116: x-coordinate; 336: y-coordinate
    labelBeyondBtn3_ = SetUpButton("滚动 ", 208, 336, uiViewGroup); // 208: x-coordinate; 336: y-coordinate
    positionX_ += 312;                                              // 312: increase y-coordinate
}

void UITestLabel::UIKit_UILabel_Test_Display_002()
{
    if (container_ != nullptr) {
        UIViewGroup* uiViewGroup = new UIViewGroup();
        uiViewGroup->SetPosition(positionX_, positionY_, 320, 390); // 320: width; 390: height
        container_->Add(uiViewGroup);
        UILabel* label = new UILabel();
        uiViewGroup->Add(label);
        // 288: width; 48: height
        label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
        label->SetText("行数");
        label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
        labelLineBtn1_ = SetUpButton("单行 ", 24, 48, uiViewGroup);  // 24: x-coordinate; 48: y-coordinate
        labelLineBtn2_ = SetUpButton("多行 ", 116, 48, uiViewGroup); // 116: x-coordinate; 48: y-coordinate
        UILabel* label2 = new UILabel();
        uiViewGroup->Add(label2);
        label2->SetPosition(24, 107, 288, 48); // 24: x-coordinate, 107: y-coordinate, 288: width; 48: height
        label2->SetText("竖直对齐方式");
        label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
        labelVerAlignlBtn1_ = SetUpButton("上对齐 ", 24, 144, uiViewGroup);  // 24: x-coordinate; 144: y-coordinate
        labelVerAlignlBtn2_ = SetUpButton("中对齐 ", 116, 144, uiViewGroup); // 116: x-coordinate; 144: y-coordinate
        labelVerAlignlBtn3_ = SetUpButton("下对齐 ", 208, 144, uiViewGroup); // 208: x-coordinate; 144: y-coordinate
        UILabel* label3 = new UILabel();
        uiViewGroup->Add(label3);
        label3->SetPosition(24, 203, 288, 48); // 24: x-coordinate, 203: y-coordinate, 288: width; 48: height
        label3->SetText("方向");
        label3->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
        labelDirectionBtn1_ = SetUpButton("左往右 ", 24, 240, uiViewGroup);  // 24: x-coordinate; 240: y-coordinate
        labelDirectionBtn2_ = SetUpButton("右往左 ", 116, 240, uiViewGroup); // 116: x-coordinate; 240: y-coordinate
        positionY_ += 384;                                                   // 384: increase x-coordinate
        positionX_ -= 312;                                                   // 312: increase y-coordinate
    }
}

void UITestLabel::UIKit_UILabel_Test_Display_003()
{
    if (container_ != nullptr) {
        UIViewGroup* uiViewGroup = new UIViewGroup();
        uiViewGroup->SetPosition(positionX_, positionY_, 450, 110); // 450: width; 110: height
        container_->Add(uiViewGroup);
        UILabel* label = new UILabel();
        uiViewGroup->Add(label);
        label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 420, 48); // 420: width; 48: height
        label->SetText("Label大小");
        label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
        // 24: x-coordinate; 48: y-coordinate; 120: width
        labelSizeBtn1_ = SetUpButton("动态宽度 ", 24, 48, uiViewGroup, 120);
        // 156: x-coordinate; 48: y-coordinate; 120: width
        labelSizeBtn2_ = SetUpButton("动态高度 ", 156, 48, uiViewGroup, 120);
    }
}

bool UITestLabel::OnClick(UIView& view, const ClickEvent& event)
{
    uiLabel->Resize(288, 100);                          // 288: width; 100: height
    uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: font size
    if (&view == labelFontSizeBtn1_) {
        uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 18); // 18: font size
    } else if (&view == labelFontSizeBtn2_) {
        uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: font size
    } else if (&view == labelFontSizeBtn3_) {
        uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: font size
    } else if (&view == labelHorAlignBtn1_) {
        uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT);
    } else if (&view == labelHorAlignBtn2_) {
        uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
    } else if (&view == labelHorAlignBtn3_) {
        uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT);
    } else if (&view == labelColorBtn1_) {
        uiLabel->SetStyle(STYLE_TEXT_COLOR, Color::White().full);
        uiLabel->SetStyle(STYLE_TEXT_OPA, OPA_OPAQUE);
    } else if (&view == labelColorBtn2_) {
        uiLabel->SetStyle(STYLE_TEXT_COLOR, Color::Red().full);
        uiLabel->SetStyle(STYLE_TEXT_OPA, OPA_OPAQUE);
    } else if (&view == labelColorBtn3_) {
        uiLabel->SetStyle(STYLE_TEXT_OPA, 127); // 127: opaque
    } else if (&view == labelBeyondBtn1_) {
        uiLabel->Resize(288, 35); // 288: width; 35: height
        uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
        uiLabel->SetText("鸿蒙轻量级图形子系统,鸿蒙轻量级图形子系统 ");
        uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
    } else if (&view == labelBeyondBtn2_) {
        uiLabel->Resize(288, 35); // 288: width; 35: height
        uiLabel->SetText("鸿蒙轻量级图形子系统,鸿蒙轻量级图形子系统 ");
        uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
    } else if (&view == labelBeyondBtn3_) {
        uiLabel->SetText("鸿蒙轻量级图形子系统,鸿蒙轻量级图形子系统 ");
        uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
    } else {
        ExpandClick(view, event);
    }
    return true;
}

void UITestLabel::ExpandClick(UIView& view, const ClickEvent& event) const
{
    if (&view == labelLineBtn1_) {
        uiLabel->Resize(288, 50);                           // 288: width; 50: height
        uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30:font size
        uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
    } else if (&view == labelLineBtn2_) {
        uiLabel->Resize(288, 100);                          // 288: width; 100: height
        uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30:font size
        uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
    } else if (&view == labelVerAlignlBtn1_) {
        uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_TOP);
    } else if (&view == labelVerAlignlBtn2_) {
        uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
    } else if (&view == labelVerAlignlBtn3_) {
        uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_BOTTOM);
    } else if (&view == labelDirectionBtn1_) {
        uiLabel->SetDirect(TEXT_DIRECT_LTR);
    } else if (&view == labelDirectionBtn2_) {
        uiLabel->SetDirect(TEXT_DIRECT_RTL);
    } else if (&view == labelSizeBtn1_) {
        uiLabel->SetText("鸿蒙轻量级图形子系统,鸿蒙轻量级图形子系统 ");
        uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
    } else if (&view == labelSizeBtn2_) {
        uiLabel->SetText("鸿蒙轻量级图形子系统,鸿蒙轻量级图形子系统 ");
        uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
    }
}
} // namespace OHOS