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 "window_utils_test.h"
17
18#include "define_multimodal.h"
19#include "mmi_log.h"
20#include "wm_common.h"
21
22#undef MMI_LOG_TAG
23#define MMI_LOG_TAG "WindowUtilsTest"
24
25namespace OHOS {
26namespace MMI {
27namespace {
28constexpr int32_t IMAGE_WIDTH = 720;
29constexpr int32_t IMAGE_HEIGHT = 1280;
30constexpr uint32_t defaultWindowId = -1;
31std::string windowName = "WindowUtilsTest";
32} // namespace
33
34WindowUtilsTest::~WindowUtilsTest()
35{
36    ClearTestWindow();
37}
38
39std::shared_ptr<WindowUtilsTest> WindowUtilsTest::GetInstance()
40{
41    if (windowUtils_ == nullptr) {
42        windowUtils_ = std::make_shared<WindowUtilsTest>();
43    }
44    return windowUtils_;
45}
46
47void WindowUtilsTest::ClearTestWindow()
48{
49    CALL_DEBUG_ENTER;
50    CHKPV(testWindow_);
51    testWindow_->Destroy();
52}
53
54bool WindowUtilsTest::DrawTestWindow()
55{
56    CALL_DEBUG_ENTER;
57    testWindow_ = Rosen::Window::Find(windowName);
58    if (testWindow_ == nullptr) {
59        CreateSmoothWindow();
60    }
61
62    CHKPF(testWindow_);
63    return testWindow_->Show() == Rosen::WMError::WM_OK;
64}
65
66sptr<Rosen::Window>& WindowUtilsTest::GetWindow()
67{
68    return testWindow_;
69}
70
71uint32_t WindowUtilsTest::GetWindowId()
72{
73    CHKPR(testWindow_, defaultWindowId);
74    return testWindow_->GetWindowId();
75}
76
77void WindowUtilsTest::CreateSmoothWindow()
78{
79    TestWindowInfo info = {
80        .name = windowName,
81        .rect = {
82            .posX_ = 0,
83            .posY_ = 0,
84            .width_ = IMAGE_WIDTH,
85            .height_ = IMAGE_HEIGHT,
86        },
87        .type = Rosen::WindowType::WINDOW_TYPE_SCREENSHOT,
88        .mode = Rosen::WindowMode::WINDOW_MODE_FULLSCREEN,
89        .needAvoid = false,
90        .parentLimit = false,
91        .parentId = Rosen::INVALID_WINDOW_ID,
92    };
93    testWindow_ = CreateWindow(info);
94}
95
96sptr<Rosen::Window> WindowUtilsTest::CreateWindow(const TestWindowInfo& info)
97{
98    sptr<Rosen::WindowOption> option = new (std::nothrow) Rosen::WindowOption();
99    CHKPP(option);
100    option->SetWindowRect(info.rect);
101    option->SetWindowType(info.type);
102    option->SetWindowMode(info.mode);
103    option->SetFocusable(info.focusable_);
104    option->SetTurnScreenOn(true);
105    option->SetDisplayId(0);
106    option->SetRequestedOrientation(info.orientation_);
107    option->SetMainHandlerAvailable(false);
108    if (info.parentId != Rosen::INVALID_WINDOW_ID) {
109        option->SetParentId(info.parentId);
110    }
111    if (info.needAvoid) {
112        option->AddWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_NEED_AVOID);
113    } else {
114        option->RemoveWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_NEED_AVOID);
115    }
116    if (info.parentLimit) {
117        option->AddWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
118    } else {
119        option->RemoveWindowFlag(Rosen::WindowFlag::WINDOW_FLAG_PARENT_LIMIT);
120    }
121    return Rosen::Window::Create(info.name, option);
122}
123} // namespace MMI
124} // namespace OHOS