1/*
2 * Copyright (c) 2020-2021 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 <climits>
17#include <gtest/gtest.h>
18
19#include "common/graphic_startup.h"
20#include "common/task_manager.h"
21#include "components/root_view.h"
22#include "components/ui_view.h"
23#include "components/ui_view_group.h"
24#include "window/window.h"
25
26using namespace testing::ext;
27namespace OHOS {
28static uint16_t g_measureCount = 0;
29class RenderTest : public testing::Test {
30public:
31    RenderTest() {}
32    virtual ~RenderTest() {}
33
34    static void SetUpTestCase()
35    {
36        GraphicStartUp::Init();
37    }
38    static void TearDownTestCase() {}
39
40    static void CreateDefaultWindow(RootView* rootView, int x, int y)
41    {
42        WindowConfig config = {};
43        config.rect = rootView->GetRect();
44        config.rect.SetPosition(x, y);
45        Window* window = Window::CreateWindow(config);
46        if (window != nullptr) {
47            window->BindRootView(rootView);
48        }
49    }
50
51    static void DestroyWindow(RootView* rootView)
52    {
53        Window::DestroyWindow(rootView->GetBoundWindow());
54    }
55};
56
57class UITestView : public UIView {
58public:
59    UITestView() {}
60    virtual ~UITestView() {}
61
62    void ReMeasure() override
63    {
64        g_measureCount++;
65    }
66};
67
68class UITestViewGroup : public UIViewGroup {
69public:
70    UITestViewGroup() {}
71    virtual ~UITestViewGroup() {}
72
73    void ReMeasure() override
74    {
75        g_measureCount++;
76    }
77};
78
79/**
80 * @tc.name: Graphic_RenderTest_Test_Measuer_001
81 * @tc.desc: Verity measure call when render
82 * @tc.type: FUNC
83 * @tc.require: SR000FH555
84 */
85HWTEST_F(RenderTest, Graphic_RenderTest_Test_Measuer_001, TestSize.Level0)
86{
87    RootView* rootView = RootView::GetWindowRootView();
88    rootView->SetWidth(600);  // 600: width
89    rootView->SetHeight(500); // 500: height
90    rootView->SetPosition(0, 0);
91    UITestView* view1 = new UITestView();
92    UITestViewGroup* vg1 = new UITestViewGroup();
93    rootView->Add(vg1);
94    vg1->Add(view1);
95    vg1->Invalidate();
96    rootView->Invalidate();
97    g_measureCount = 0;
98
99    RenderTest::CreateDefaultWindow(rootView, 0, 0);
100    usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task
101    TaskManager::GetInstance()->TaskHandler();
102    EXPECT_EQ(g_measureCount, 2); // 2: measure view
103    rootView->RemoveAll();
104    delete view1;
105    delete vg1;
106    RenderTest::DestroyWindow(rootView);
107    RootView::DestroyWindowRootView(rootView);
108}
109
110/**
111 * @tc.name: Graphic_RenderTest_Test_Measuer_02
112 * @tc.desc: Verity measure call when view invisible
113 * @tc.type: FUNC
114 * @tc.require: AR000FH556
115 */
116HWTEST_F(RenderTest, Graphic_RenderTest_Test_Measuer_02, TestSize.Level0)
117{
118    RootView* rootView = RootView::GetWindowRootView();
119    rootView->SetWidth(600);  // 600: width
120    rootView->SetHeight(500); // 500: height
121    rootView->SetPosition(0, 0);
122    UITestView* view1 = new UITestView();
123    UITestViewGroup* vg1 = new UITestViewGroup();
124    rootView->Add(vg1);
125    vg1->Add(view1);
126    // invisible view not need to measure
127    view1->SetVisible(false);
128    rootView->Invalidate();
129    g_measureCount = 0;
130
131    RenderTest::CreateDefaultWindow(rootView, 0, 0);
132    usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task
133    TaskManager::GetInstance()->TaskHandler();
134    EXPECT_EQ(g_measureCount, 1);
135
136    rootView->RemoveAll();
137    delete view1;
138    delete vg1;
139    RenderTest::DestroyWindow(rootView);
140    RootView::DestroyWindowRootView(rootView);
141}
142} // namespace OHOS
143