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 "window_test_utils.h"
19 
20 #include "window_impl.h"
21 #include "wm_common.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 using Utils = WindowTestUtils;
29 class WindowRaiseToAppTopTest : public testing::Test {
30 public:
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_;
38 private:
39     static constexpr uint32_t TEST_SLEEP_S = 1;
40 };
41 
SetUpTestCase()42 void WindowRaiseToAppTopTest::SetUpTestCase()
43 {
44 }
45 
TearDownTestCase()46 void WindowRaiseToAppTopTest::TearDownTestCase()
47 {
48 }
49 
SetUp()50 void 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 
TearDown()61 void WindowRaiseToAppTopTest::TearDown()
62 {
63     while (!activeWindows_.empty()) {
64         ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
65         activeWindows_.pop_back();
66     }
67 }
68 
69 namespace {
70 /**
71 * @tc.name: WindowRaiseToAppTopTest1
72 * @tc.desc: to raise to app top, normal
73 * @tc.type: FUNC
74 */
75 HWTEST_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 */
116 HWTEST_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 */
164 HWTEST_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 */
215 HWTEST_F(WindowRaiseToAppTopTest, NotAppSubWindow, Function | MediumTest | Level3)
216 {
217     fullInfo_.name  = "mainWindow.1";
218     fullInfo_.type = WindowType::WINDOW_TYPE_FLOAT;
219     sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
220     if (mainWindow == nullptr) {
221         return;
222     }
223     ASSERT_NE(nullptr, mainWindow);
224     activeWindows_.push_back(mainWindow);
225     ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
226     sleep(TEST_SLEEP_S);
227 
228     fullInfo_.name  = "subWindow.1";
229     fullInfo_.type = WindowType::WINDOW_TYPE_SYSTEM_SUB_WINDOW;
230     fullInfo_.parentId  = mainWindow->GetWindowId();
231     sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
232     if (subWindow1 == nullptr) {
233         return;
234     }
235     ASSERT_NE(nullptr, subWindow1);
236     activeWindows_.push_back(subWindow1);
237     ASSERT_EQ(WMError::WM_OK, subWindow1->Show(0, true));
238     sleep(TEST_SLEEP_S);
239 
240     auto result = subWindow1->RaiseToAppTop();
241     ASSERT_EQ(WMError::WM_ERROR_INVALID_CALLING, result);
242 }
243 }
244 } // namespace Rosen
245 } // namespace OHOS
246