1/*
2 * Copyright (c) 2023 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// gtest
17#include <gtest/gtest.h>
18#include "common_test_utils.h"
19#include "window_test_utils.h"
20#include "window_impl.h"
21#include "wm_common.h"
22
23using namespace testing;
24using namespace testing::ext;
25
26namespace OHOS {
27namespace Rosen {
28using Utils = WindowTestUtils;
29class WindowRaiseToAppTopTest : public testing::Test {
30public:
31    static void SetUpTestCase();
32    static void TearDownTestCase();
33    void SetUp() override;
34    void TearDown() override;
35
36    std::vector<sptr<Window>> activeWindows_;
37    Utils::TestWindowInfo fullInfo_;
38private:
39    static constexpr uint32_t TEST_SLEEP_S = 1;
40};
41
42void WindowRaiseToAppTopTest::SetUpTestCase()
43{
44}
45
46void WindowRaiseToAppTopTest::TearDownTestCase()
47{
48}
49
50void WindowRaiseToAppTopTest::SetUp()
51{
52    fullInfo_ = {
53            .name = "",
54            .rect = Utils::customAppRect_,
55            .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
56            .parentId = INVALID_WINDOW_ID,
57    };
58    activeWindows_.clear();
59}
60
61void WindowRaiseToAppTopTest::TearDown()
62{
63    while (!activeWindows_.empty()) {
64        ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
65        activeWindows_.pop_back();
66    }
67}
68
69namespace {
70/**
71* @tc.name: WindowRaiseToAppTopTest1
72* @tc.desc: to raise to app top, normal
73* @tc.type: FUNC
74*/
75HWTEST_F(WindowRaiseToAppTopTest, NormalRaise1, Function | MediumTest | Level3)
76{
77    fullInfo_.name  = "mainWindow.1";
78    sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
79    if (mainWindow == nullptr) {
80        return;
81    }
82    ASSERT_NE(nullptr, mainWindow);
83    activeWindows_.push_back(mainWindow);
84    ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
85    sleep(TEST_SLEEP_S);
86
87    fullInfo_.name  = "subWindow.1";
88    fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
89    fullInfo_.parentId  = mainWindow->GetWindowId();
90    sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
91    ASSERT_NE(nullptr, subWindow1);
92    activeWindows_.push_back(subWindow1);
93    ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
94    sleep(TEST_SLEEP_S);
95
96    fullInfo_.name  = "subWindow.2";
97    fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
98    fullInfo_.parentId  = mainWindow->GetWindowId();
99    sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
100    ASSERT_NE(nullptr, subWindow2);
101    activeWindows_.push_back(subWindow2);
102    ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
103    sleep(TEST_SLEEP_S);
104
105    auto result1 = mainWindow->RaiseToAppTop();
106    ASSERT_EQ(WMError::WM_ERROR_INVALID_PARENT, result1);
107    auto result2 = subWindow1->RaiseToAppTop();
108    ASSERT_EQ(WMError::WM_OK, result2);
109}
110
111/**
112* @tc.name: WindowRaiseToAppTopTest2
113* @tc.desc: to raise to app top, with dialog
114* @tc.type: FUNC
115*/
116HWTEST_F(WindowRaiseToAppTopTest, RaiseWithDialog1, Function | MediumTest | Level3)
117{
118    fullInfo_.name  = "mainWindow.1";
119    sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
120    if (mainWindow == nullptr) {
121        return;
122    }
123    ASSERT_NE(nullptr, mainWindow);
124    activeWindows_.push_back(mainWindow);
125    ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
126    sleep(TEST_SLEEP_S);
127
128    fullInfo_.name  = "subWindow.1";
129    fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
130    fullInfo_.parentId  = mainWindow->GetWindowId();
131    sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
132    ASSERT_NE(nullptr, subWindow1);
133    activeWindows_.push_back(subWindow1);
134    ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
135    sleep(TEST_SLEEP_S);
136
137    fullInfo_.name  = "subWindow.2";
138    fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
139    fullInfo_.parentId  = mainWindow->GetWindowId();
140    sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
141    ASSERT_NE(nullptr, subWindow2);
142    activeWindows_.push_back(subWindow2);
143    ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
144    sleep(TEST_SLEEP_S);
145
146    fullInfo_.name  = "dialog.2";
147    fullInfo_.type = WindowType::WINDOW_TYPE_DIALOG;
148    fullInfo_.parentId  = INVALID_WINDOW_ID;
149    sptr<Window> dialog = Utils::CreateTestWindow(fullInfo_);
150    ASSERT_NE(nullptr, dialog);
151    activeWindows_.push_back(dialog);
152    ASSERT_EQ(WMError::WM_OK, dialog->Show());
153    sleep(TEST_SLEEP_S);
154
155    auto result = subWindow1->RaiseToAppTop();
156    ASSERT_EQ(WMError::WM_OK, result);
157}
158
159/**
160* @tc.name: WindowRaiseToAppTopTest3
161* @tc.desc: to raise to app top, in hide
162* @tc.type: FUNC
163*/
164HWTEST_F(WindowRaiseToAppTopTest, RaiseWhenHide, Function | MediumTest | Level3)
165{
166    fullInfo_.name  = "mainWindow.1";
167    sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
168    if (mainWindow == nullptr) {
169        return;
170    }
171    ASSERT_NE(nullptr, mainWindow);
172    activeWindows_.push_back(mainWindow);
173    ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
174    sleep(TEST_SLEEP_S);
175
176    fullInfo_.name  = "subWindow.1";
177    fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
178    fullInfo_.parentId  = mainWindow->GetWindowId();
179    sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
180    if (subWindow1 == nullptr) {
181        return;
182    }
183    ASSERT_NE(nullptr, subWindow1);
184    activeWindows_.push_back(subWindow1);
185    ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
186    sleep(TEST_SLEEP_S);
187
188    fullInfo_.name  = "subWindow.2";
189    fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
190    fullInfo_.parentId  = mainWindow->GetWindowId();
191    sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
192    ASSERT_NE(nullptr, subWindow2);
193    activeWindows_.push_back(subWindow2);
194    ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
195    sleep(TEST_SLEEP_S);
196
197    ASSERT_EQ(WMError::WM_OK, mainWindow->Hide());
198    sleep(TEST_SLEEP_S);
199
200    auto result = subWindow1->RaiseToAppTop();
201    ASSERT_EQ(WMError::WM_OK, result);
202
203    ASSERT_EQ(WMError::WM_OK, subWindow1->Hide());
204    sleep(TEST_SLEEP_S);
205
206    result = subWindow1->RaiseToAppTop();
207    ASSERT_EQ(WMError::WM_DO_NOTHING, result);
208}
209
210/**
211* @tc.name: WindowRaiseToAppTopTest3
212* @tc.desc: to raise to app top, not app subwindow
213* @tc.type: FUNC
214*/
215HWTEST_F(WindowRaiseToAppTopTest, NotAppSubWindow, Function | MediumTest | Level3)
216{
217    CommonTestUtils::GuaranteeFloatWindowPermission("window_raisetoapptop_test");
218    fullInfo_.name  = "mainWindow.1";
219    fullInfo_.type = WindowType::WINDOW_TYPE_FLOAT;
220    sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
221    if (mainWindow == nullptr) {
222        return;
223    }
224    ASSERT_NE(nullptr, mainWindow);
225    activeWindows_.push_back(mainWindow);
226    ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
227    sleep(TEST_SLEEP_S);
228
229    fullInfo_.name  = "subWindow.1";
230    fullInfo_.type = WindowType::WINDOW_TYPE_SYSTEM_SUB_WINDOW;
231    fullInfo_.parentId  = mainWindow->GetWindowId();
232    sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
233    if (subWindow1 == nullptr) {
234        return;
235    }
236    ASSERT_NE(nullptr, subWindow1);
237    activeWindows_.push_back(subWindow1);
238    ASSERT_EQ(WMError::WM_OK, subWindow1->Show(0, true));
239    sleep(TEST_SLEEP_S);
240
241    auto result = subWindow1->RaiseToAppTop();
242    ASSERT_EQ(WMError::WM_ERROR_INVALID_CALLING, result);
243}
244}
245} // namespace Rosen
246} // namespace OHOS
247