1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include <climits>
17a3e0fd82Sopenharmony_ci#include <gtest/gtest.h>
18a3e0fd82Sopenharmony_ci
19a3e0fd82Sopenharmony_ci#include "common/graphic_startup.h"
20a3e0fd82Sopenharmony_ci#include "common/task_manager.h"
21a3e0fd82Sopenharmony_ci#include "components/root_view.h"
22a3e0fd82Sopenharmony_ci#include "components/ui_view.h"
23a3e0fd82Sopenharmony_ci#include "components/ui_view_group.h"
24a3e0fd82Sopenharmony_ci#include "window/window.h"
25a3e0fd82Sopenharmony_ci
26a3e0fd82Sopenharmony_ciusing namespace testing::ext;
27a3e0fd82Sopenharmony_cinamespace OHOS {
28a3e0fd82Sopenharmony_cistatic uint16_t g_measureCount = 0;
29a3e0fd82Sopenharmony_ciclass RenderTest : public testing::Test {
30a3e0fd82Sopenharmony_cipublic:
31a3e0fd82Sopenharmony_ci    RenderTest() {}
32a3e0fd82Sopenharmony_ci    virtual ~RenderTest() {}
33a3e0fd82Sopenharmony_ci
34a3e0fd82Sopenharmony_ci    static void SetUpTestCase()
35a3e0fd82Sopenharmony_ci    {
36a3e0fd82Sopenharmony_ci        GraphicStartUp::Init();
37a3e0fd82Sopenharmony_ci    }
38a3e0fd82Sopenharmony_ci    static void TearDownTestCase() {}
39a3e0fd82Sopenharmony_ci
40a3e0fd82Sopenharmony_ci    static void CreateDefaultWindow(RootView* rootView, int x, int y)
41a3e0fd82Sopenharmony_ci    {
42a3e0fd82Sopenharmony_ci        WindowConfig config = {};
43a3e0fd82Sopenharmony_ci        config.rect = rootView->GetRect();
44a3e0fd82Sopenharmony_ci        config.rect.SetPosition(x, y);
45a3e0fd82Sopenharmony_ci        Window* window = Window::CreateWindow(config);
46a3e0fd82Sopenharmony_ci        if (window != nullptr) {
47a3e0fd82Sopenharmony_ci            window->BindRootView(rootView);
48a3e0fd82Sopenharmony_ci        }
49a3e0fd82Sopenharmony_ci    }
50a3e0fd82Sopenharmony_ci
51a3e0fd82Sopenharmony_ci    static void DestroyWindow(RootView* rootView)
52a3e0fd82Sopenharmony_ci    {
53a3e0fd82Sopenharmony_ci        Window::DestroyWindow(rootView->GetBoundWindow());
54a3e0fd82Sopenharmony_ci    }
55a3e0fd82Sopenharmony_ci};
56a3e0fd82Sopenharmony_ci
57a3e0fd82Sopenharmony_ciclass UITestView : public UIView {
58a3e0fd82Sopenharmony_cipublic:
59a3e0fd82Sopenharmony_ci    UITestView() {}
60a3e0fd82Sopenharmony_ci    virtual ~UITestView() {}
61a3e0fd82Sopenharmony_ci
62a3e0fd82Sopenharmony_ci    void ReMeasure() override
63a3e0fd82Sopenharmony_ci    {
64a3e0fd82Sopenharmony_ci        g_measureCount++;
65a3e0fd82Sopenharmony_ci    }
66a3e0fd82Sopenharmony_ci};
67a3e0fd82Sopenharmony_ci
68a3e0fd82Sopenharmony_ciclass UITestViewGroup : public UIViewGroup {
69a3e0fd82Sopenharmony_cipublic:
70a3e0fd82Sopenharmony_ci    UITestViewGroup() {}
71a3e0fd82Sopenharmony_ci    virtual ~UITestViewGroup() {}
72a3e0fd82Sopenharmony_ci
73a3e0fd82Sopenharmony_ci    void ReMeasure() override
74a3e0fd82Sopenharmony_ci    {
75a3e0fd82Sopenharmony_ci        g_measureCount++;
76a3e0fd82Sopenharmony_ci    }
77a3e0fd82Sopenharmony_ci};
78a3e0fd82Sopenharmony_ci
79a3e0fd82Sopenharmony_ci/**
80a3e0fd82Sopenharmony_ci * @tc.name: Graphic_RenderTest_Test_Measuer_001
81a3e0fd82Sopenharmony_ci * @tc.desc: Verity measure call when render
82a3e0fd82Sopenharmony_ci * @tc.type: FUNC
83a3e0fd82Sopenharmony_ci * @tc.require: SR000FH555
84a3e0fd82Sopenharmony_ci */
85a3e0fd82Sopenharmony_ciHWTEST_F(RenderTest, Graphic_RenderTest_Test_Measuer_001, TestSize.Level0)
86a3e0fd82Sopenharmony_ci{
87a3e0fd82Sopenharmony_ci    RootView* rootView = RootView::GetWindowRootView();
88a3e0fd82Sopenharmony_ci    rootView->SetWidth(600);  // 600: width
89a3e0fd82Sopenharmony_ci    rootView->SetHeight(500); // 500: height
90a3e0fd82Sopenharmony_ci    rootView->SetPosition(0, 0);
91a3e0fd82Sopenharmony_ci    UITestView* view1 = new UITestView();
92a3e0fd82Sopenharmony_ci    UITestViewGroup* vg1 = new UITestViewGroup();
93a3e0fd82Sopenharmony_ci    rootView->Add(vg1);
94a3e0fd82Sopenharmony_ci    vg1->Add(view1);
95a3e0fd82Sopenharmony_ci    vg1->Invalidate();
96a3e0fd82Sopenharmony_ci    rootView->Invalidate();
97a3e0fd82Sopenharmony_ci    g_measureCount = 0;
98a3e0fd82Sopenharmony_ci
99a3e0fd82Sopenharmony_ci    RenderTest::CreateDefaultWindow(rootView, 0, 0);
100a3e0fd82Sopenharmony_ci    usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task
101a3e0fd82Sopenharmony_ci    TaskManager::GetInstance()->TaskHandler();
102a3e0fd82Sopenharmony_ci    EXPECT_EQ(g_measureCount, 2); // 2: measure view
103a3e0fd82Sopenharmony_ci    rootView->RemoveAll();
104a3e0fd82Sopenharmony_ci    delete view1;
105a3e0fd82Sopenharmony_ci    delete vg1;
106a3e0fd82Sopenharmony_ci    RenderTest::DestroyWindow(rootView);
107a3e0fd82Sopenharmony_ci    RootView::DestroyWindowRootView(rootView);
108a3e0fd82Sopenharmony_ci}
109a3e0fd82Sopenharmony_ci
110a3e0fd82Sopenharmony_ci/**
111a3e0fd82Sopenharmony_ci * @tc.name: Graphic_RenderTest_Test_Measuer_02
112a3e0fd82Sopenharmony_ci * @tc.desc: Verity measure call when view invisible
113a3e0fd82Sopenharmony_ci * @tc.type: FUNC
114a3e0fd82Sopenharmony_ci * @tc.require: AR000FH556
115a3e0fd82Sopenharmony_ci */
116a3e0fd82Sopenharmony_ciHWTEST_F(RenderTest, Graphic_RenderTest_Test_Measuer_02, TestSize.Level0)
117a3e0fd82Sopenharmony_ci{
118a3e0fd82Sopenharmony_ci    RootView* rootView = RootView::GetWindowRootView();
119a3e0fd82Sopenharmony_ci    rootView->SetWidth(600);  // 600: width
120a3e0fd82Sopenharmony_ci    rootView->SetHeight(500); // 500: height
121a3e0fd82Sopenharmony_ci    rootView->SetPosition(0, 0);
122a3e0fd82Sopenharmony_ci    UITestView* view1 = new UITestView();
123a3e0fd82Sopenharmony_ci    UITestViewGroup* vg1 = new UITestViewGroup();
124a3e0fd82Sopenharmony_ci    rootView->Add(vg1);
125a3e0fd82Sopenharmony_ci    vg1->Add(view1);
126a3e0fd82Sopenharmony_ci    // invisible view not need to measure
127a3e0fd82Sopenharmony_ci    view1->SetVisible(false);
128a3e0fd82Sopenharmony_ci    rootView->Invalidate();
129a3e0fd82Sopenharmony_ci    g_measureCount = 0;
130a3e0fd82Sopenharmony_ci
131a3e0fd82Sopenharmony_ci    RenderTest::CreateDefaultWindow(rootView, 0, 0);
132a3e0fd82Sopenharmony_ci    usleep(DEFAULT_TASK_PERIOD * 1000); // DEFAULT_TASK_PERIOD * 1000: wait next render task
133a3e0fd82Sopenharmony_ci    TaskManager::GetInstance()->TaskHandler();
134a3e0fd82Sopenharmony_ci    EXPECT_EQ(g_measureCount, 1);
135a3e0fd82Sopenharmony_ci
136a3e0fd82Sopenharmony_ci    rootView->RemoveAll();
137a3e0fd82Sopenharmony_ci    delete view1;
138a3e0fd82Sopenharmony_ci    delete vg1;
139a3e0fd82Sopenharmony_ci    RenderTest::DestroyWindow(rootView);
140a3e0fd82Sopenharmony_ci    RootView::DestroyWindowRootView(rootView);
141a3e0fd82Sopenharmony_ci}
142a3e0fd82Sopenharmony_ci} // namespace OHOS
143