ui_image_unit_test.cpp 12.6 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
/*
 * 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 "components/ui_image_view.h"
#include <climits>
#include <gtest/gtest.h>
#include "components/root_view.h"
#include "components/ui_view_group.h"
21
#include "draw/draw_utils.h"
M
mamingshuai 已提交
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
#include "test_resource_config.h"

using namespace testing::ext;

namespace OHOS {
class UIImageViewTest : public testing::Test {
public:
    UIImageViewTest() : imageView_(nullptr) {}
    ~UIImageViewTest() {}
    static void SetUpTestCase(void) {}
    static void TearDownTestCase(void) {}
    void SetUp(void);
    void TearDown(void);
    UIImageView* imageView_;
};

void UIImageViewTest::SetUp(void)
{
    if (imageView_ == nullptr) {
        imageView_ = new UIImageView();
    }
}

void UIImageViewTest::TearDown(void)
{
    if (imageView_ != nullptr) {
        delete imageView_;
        imageView_ = nullptr;
    }
}

/**
 * @tc.name: UIImageViewGetViewType_001
 * @tc.desc: Verify GetViewType function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
邓志豪 已提交
59
HWTEST_F(UIImageViewTest, UIImageViewGetViewType_001, TestSize.Level1)
M
mamingshuai 已提交
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
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    EXPECT_EQ(imageView_->GetViewType(), UI_IMAGE_VIEW);
}

/**
 * @tc.name: UIImageViewSetPosition_001
 * @tc.desc: Verify SetPosition function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetPosition_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const int16_t initPosX = 10;
    const int16_t initPosY = 10;
    const int16_t initWidth = 100;
    const int16_t initHeight = 150;

    imageView_->SetPosition(initPosX, initPosY);
    imageView_->SetHeight(initWidth);
    imageView_->SetWidth(initHeight);

    EXPECT_EQ(imageView_->GetX(), initPosX);
    EXPECT_EQ(imageView_->GetY(), initPosY);
    EXPECT_EQ(imageView_->GetHeight(), initWidth);
    EXPECT_EQ(imageView_->GetWidth(), initHeight);

    imageView_->SetPosition(0, 0, 0, 0);
    EXPECT_EQ(imageView_->GetX(), 0);
    EXPECT_EQ(imageView_->GetY(), 0);
    EXPECT_EQ(imageView_->GetWidth(), 0);
    EXPECT_EQ(imageView_->GetHeight(), 0);
}

#ifndef VERSION_LITE
/**
 * @tc.name: UIImageViewSetSrc_001
 * @tc.desc: Verify SetSrc function, correct gif path, equal.
 * @tc.type: FUNC
 * @tc.require: SR000F3PEO
 */
邓志豪 已提交
108
HWTEST_F(UIImageViewTest, UIImageViewSetSrc_001, TestSize.Level1)
M
mamingshuai 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const char* strPath1 = static_cast<const char*>(GIF_IMAGE_PATH1);
    imageView_->SetSrc(strPath1);
    EXPECT_EQ(imageView_->GetPath(), nullptr);
}

/**
 * @tc.name: UIImageViewSetSrc_002
 * @tc.desc: Verify SetSrc function, error gif path, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F3R70
 */
邓志豪 已提交
125
HWTEST_F(UIImageViewTest, UIImageViewSetSrc_002, TestSize.Level1)
M
mamingshuai 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const char* strPathError = static_cast<const char*>(GIF_IMAGE_PATH_ERROR);
    imageView_->SetSrc(strPathError);
    EXPECT_EQ(imageView_->GetPath(), nullptr);
}

/**
 * @tc.name: UIImageViewSetSrc_003
 * @tc.desc: Verify SetSrc function, tif image format path, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F3R70
 */
邓志豪 已提交
142
HWTEST_F(UIImageViewTest, UIImageViewSetSrc_003, TestSize.Level1)
M
mamingshuai 已提交
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
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const char* strPath = static_cast<const char*>(TIF_IMAGE_PATH);
    imageView_->SetSrc(strPath);
    EXPECT_STREQ(imageView_->GetPath(), strPath);
}
#endif

/**
 * @tc.name: UIImageViewSetSrc_004
 * @tc.desc: Verify SetSrc function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetSrc_004, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    char* srcPath = nullptr;
    imageView_->SetSrc(srcPath);
    EXPECT_EQ(imageView_->GetPath(), srcPath);
    const char* strPath2 = static_cast<const char*>(BLUE_RGB888_IMAGE_PATH);
    imageView_->SetSrc(strPath2);
    EXPECT_STREQ(imageView_->GetPath(), strPath2);
    ImageInfo* srcPath2 = nullptr;
    imageView_->SetSrc(srcPath2);
    EXPECT_EQ(imageView_->GetImageInfo(), srcPath2);
}

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
/**
 * @tc.name: UIImageViewSetSrc001
 * @tc.desc: Verify SetSrc function, equal.
 * @tc.type: FUNC
 * @tc.require: SR000FQNFC
 */
HWTEST_F(UIImageViewTest, UIImageViewSetSrc001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const ImageInfo* imageInfo = nullptr;
    imageView_->SetSrc(imageInfo);
    EXPECT_EQ(imageView_->GetImageInfo(), nullptr);
}

/**
 * @tc.name: UIImageViewSetSrc002
 * @tc.desc: Verify SetSrc function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000FQNFD
 */
HWTEST_F(UIImageViewTest, UIImageViewSetSrc002, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const ImageInfo imageInfo {0};
    imageView_->SetSrc(&imageInfo);
    EXPECT_EQ(imageView_->GetImageInfo()->dataSize, 0);
}

/**
 * @tc.name: UIImageViewSetSrc003
 * @tc.desc: Verify SetSrc function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000FQNFD
 */
HWTEST_F(UIImageViewTest, UIImageViewSetSrc003, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
223
    ImageHeader header {A8, 0, 0, 0, 0, 0};
224 225 226 227 228 229
    const ImageInfo imageInfo {header, 0, nullptr, nullptr};
    imageView_->SetSrc(&imageInfo);
    EXPECT_EQ(imageView_->GetImageInfo()->dataSize, 0);
}

/**
230
 * @tc.name: UIImageViewSetSrc004
231 232 233 234
 * @tc.desc: Verify SetSrc function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000FQNFD
 */
235
HWTEST_F(UIImageViewTest, UIImageViewSetSrc004, TestSize.Level0)
236 237 238 239 240
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
241
    ImageHeader header {A8, 0, 0, 0, 0, 0};
242 243 244 245 246 247 248
    const ImageInfo imageInfo {header, 0, nullptr, nullptr};
    imageView_->SetSrc(&imageInfo);
    ColorMode colorMode = static_cast<ColorMode>(imageView_->GetImageInfo()->header.colorMode);
    uint8_t pxSize = DrawUtils::GetPxSizeByColorMode(colorMode);
    EXPECT_EQ(pxSize, 8); // 8 bits
}

M
mamingshuai 已提交
249 250 251 252 253 254
/**
 * @tc.name: UIImageViewSetAutoEnable_001
 * @tc.desc: Verify SetAutoEnable function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
邓志豪 已提交
255
HWTEST_F(UIImageViewTest, UIImageViewSetAutoEnable_001, TestSize.Level1)
M
mamingshuai 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    imageView_->SetAutoEnable(true);
    EXPECT_EQ(imageView_->GetAutoEnable(), true);
}

/**
 * @tc.name: UIImageViewSetParent_001
 * @tc.desc: Verify SetParent function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
邓志豪 已提交
271
HWTEST_F(UIImageViewTest, UIImageViewSetParent_001, TestSize.Level1)
M
mamingshuai 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    UIView uiView;
    imageView_->SetParent(nullptr);
    EXPECT_EQ(imageView_->GetParent(), nullptr);
    imageView_->SetParent(&uiView);
    EXPECT_NE(imageView_->GetParent(), nullptr);
}

/**
 * @tc.name: UIImageViewSetNextSibling_001
 * @tc.desc: Verify SetNextSibling function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
邓志豪 已提交
290
HWTEST_F(UIImageViewTest, UIImageViewSetNextSibling_001, TestSize.Level1)
M
mamingshuai 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    UIView uiView;
    imageView_->SetNextSibling(nullptr);
    EXPECT_EQ(imageView_->GetNextSibling(), nullptr);
    imageView_->SetNextSibling(&uiView);
    EXPECT_NE(imageView_->GetNextSibling(), nullptr);
}

/**
 * @tc.name: UIImageViewSetVisible_001
 * @tc.desc: Verify SetVisible function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
邓志豪 已提交
309
HWTEST_F(UIImageViewTest, UIImageViewSetVisible_001, TestSize.Level1)
M
mamingshuai 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    imageView_->SetVisible(true);
    EXPECT_EQ(imageView_->IsVisible(), true);
    imageView_->SetVisible(false);
    EXPECT_EQ(imageView_->IsVisible(), false);
}

/**
 * @tc.name: UIImageViewSetTouchable_001
 * @tc.desc: Verify SetTouchable function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
邓志豪 已提交
327
HWTEST_F(UIImageViewTest, UIImageViewSetTouchable_001, TestSize.Level1)
M
mamingshuai 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    imageView_->SetTouchable(true);
    EXPECT_EQ(imageView_->IsTouchable(), true);
    imageView_->SetTouchable(false);
    EXPECT_EQ(imageView_->IsTouchable(), false);
}

/**
 * @tc.name: UIImageViewSetDraggable_001
 * @tc.desc: Verify SetDraggable function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetDraggable_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    imageView_->SetDraggable(true);
    EXPECT_EQ(imageView_->IsDraggable(), true);
    imageView_->SetDraggable(false);
    EXPECT_EQ(imageView_->IsDraggable(), false);
}

/**
 * @tc.name: UIImageViewSetDragParentInstead_001
 * @tc.desc: Verify SetDragParentInstead function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetDragParentInstead_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    imageView_->SetDragParentInstead(true);
    EXPECT_EQ(imageView_->IsDragParentInstead(), true);
    imageView_->SetDragParentInstead(false);
    EXPECT_EQ(imageView_->IsDragParentInstead(), false);
}

/**
 * @tc.name: UIImageViewResize_001
 * @tc.desc: Verify Resize function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewResize_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const int16_t currPosX = 100;
    const int16_t currPosY = 300;

    imageView_->Resize(currPosX, currPosY);
    EXPECT_EQ(imageView_->GetWidth(), currPosX);
    EXPECT_EQ(imageView_->GetHeight(), currPosY);
}

/**
 * @tc.name: UIImageViewSetListener_001
 * @tc.desc: Verify SetListener function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetListener_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    imageView_->SetOnDragListener(nullptr);
    imageView_->SetOnClickListener(nullptr);
    imageView_->SetOnLongPressListener(nullptr);

    EXPECT_EQ(imageView_->GetOnDragListener(), nullptr);
    EXPECT_EQ(imageView_->GetOnClickListener(), nullptr);
    EXPECT_EQ(imageView_->GetOnLongPressListener(), nullptr);
}

/**
 * @tc.name: UIImageViewSetViewId_001
 * @tc.desc: Verify SetViewId function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetViewId_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    imageView_->SetViewId(nullptr);
    EXPECT_EQ(imageView_->GetViewId(), nullptr);
}

/**
 * @tc.name: UIImageViewSetViewIndex_001
 * @tc.desc: Verify SetViewIndex function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetViewIndex_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    const int16_t index = 101;
    imageView_->SetViewIndex(index);
    EXPECT_EQ(imageView_->GetViewIndex(), index);
}

/**
 * @tc.name: UIImageViewSetBlurLevel_001
 * @tc.desc: Verify SetBlurLevel function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetBlurLevel_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    BlurLevel level = LEVEL0;
    imageView_->SetBlurLevel(level);
    EXPECT_EQ(imageView_->GetBlurLevel(), level);
}

/**
 * @tc.name: UIImageViewSetTransformAlgorithm_001
 * @tc.desc: Verify SetTransformAlgorithm function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000DSMQ1
 */
HWTEST_F(UIImageViewTest, UIImageViewSetTransformAlgorithm_001, TestSize.Level0)
{
    if (imageView_ == nullptr) {
        EXPECT_EQ(1, 0);
        return;
    }
    TransformAlgorithm algorithm = NEAREST_NEIGHBOR;
    imageView_->SetTransformAlgorithm(algorithm);
    EXPECT_EQ(imageView_->GetTransformAlgorithm(), algorithm);
}
} // namespace OHOS