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 ASSERT_NE(nullptr, mainWindow);
80 activeWindows_.push_back(mainWindow);
81 ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
82 sleep(TEST_SLEEP_S);
83
84 fullInfo_.name = "subWindow.1";
85 fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
86 fullInfo_.parentId = mainWindow->GetWindowId();
87 sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
88 ASSERT_NE(nullptr, subWindow1);
89 activeWindows_.push_back(subWindow1);
90 ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
91 sleep(TEST_SLEEP_S);
92
93 fullInfo_.name = "subWindow.2";
94 fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
95 fullInfo_.parentId = mainWindow->GetWindowId();
96 sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
97 ASSERT_NE(nullptr, subWindow2);
98 activeWindows_.push_back(subWindow2);
99 ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
100 sleep(TEST_SLEEP_S);
101
102 auto result1 = mainWindow->RaiseToAppTop();
103 ASSERT_EQ(WmErrorCode::WM_ERROR_INVALID_PARENT, result1);
104 auto result2 = subWindow1->RaiseToAppTop();
105 ASSERT_EQ(WmErrorCode::WM_OK, result2);
106 }
107
108 /**
109 * @tc.name: WindowRaiseToAppTopTest2
110 * @tc.desc: to raise to app top, with dialog
111 * @tc.type: FUNC
112 */
113 HWTEST_F(WindowRaiseToAppTopTest, RaiseWithDialog1, Function | MediumTest | Level3)
114 {
115 fullInfo_.name = "mainWindow.1";
116 sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
117 ASSERT_NE(nullptr, mainWindow);
118 activeWindows_.push_back(mainWindow);
119 ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
120 sleep(TEST_SLEEP_S);
121
122 fullInfo_.name = "subWindow.1";
123 fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
124 fullInfo_.parentId = mainWindow->GetWindowId();
125 sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
126 ASSERT_NE(nullptr, subWindow1);
127 activeWindows_.push_back(subWindow1);
128 ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
129 sleep(TEST_SLEEP_S);
130
131 fullInfo_.name = "subWindow.2";
132 fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
133 fullInfo_.parentId = mainWindow->GetWindowId();
134 sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
135 ASSERT_NE(nullptr, subWindow2);
136 activeWindows_.push_back(subWindow2);
137 ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
138 sleep(TEST_SLEEP_S);
139
140 fullInfo_.name = "dialog.2";
141 fullInfo_.type = WindowType::WINDOW_TYPE_DIALOG;
142 fullInfo_.parentId = INVALID_WINDOW_ID;
143 sptr<Window> dialog = Utils::CreateTestWindow(fullInfo_);
144 ASSERT_NE(nullptr, dialog);
145 activeWindows_.push_back(dialog);
146 ASSERT_EQ(WMError::WM_OK, dialog->Show());
147 sleep(TEST_SLEEP_S);
148
149 auto result = subWindow1->RaiseToAppTop();
150 ASSERT_EQ(WmErrorCode::WM_OK, result);
151 }
152
153 /**
154 * @tc.name: WindowRaiseToAppTopTest3
155 * @tc.desc: to raise to app top, in hide
156 * @tc.type: FUNC
157 */
158 HWTEST_F(WindowRaiseToAppTopTest, RaiseWhenHide, Function | MediumTest | Level3)
159 {
160 fullInfo_.name = "mainWindow.1";
161 sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
162 ASSERT_NE(nullptr, mainWindow);
163 activeWindows_.push_back(mainWindow);
164 ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
165 sleep(TEST_SLEEP_S);
166
167 fullInfo_.name = "subWindow.1";
168 fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
169 fullInfo_.parentId = mainWindow->GetWindowId();
170 sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
171 ASSERT_NE(nullptr, subWindow1);
172 activeWindows_.push_back(subWindow1);
173 ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
174 sleep(TEST_SLEEP_S);
175
176 fullInfo_.name = "subWindow.2";
177 fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
178 fullInfo_.parentId = mainWindow->GetWindowId();
179 sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
180 ASSERT_NE(nullptr, subWindow2);
181 activeWindows_.push_back(subWindow2);
182 ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
183 sleep(TEST_SLEEP_S);
184
185 ASSERT_EQ(WMError::WM_OK, mainWindow->Hide());
186 sleep(TEST_SLEEP_S);
187
188 auto result = subWindow1->RaiseToAppTop();
189 ASSERT_EQ(WmErrorCode::WM_OK, result);
190
191 ASSERT_EQ(WMError::WM_OK, subWindow1->Hide());
192 sleep(TEST_SLEEP_S);
193
194 result = subWindow1->RaiseToAppTop();
195 ASSERT_EQ(WmErrorCode::WM_ERROR_STATE_ABNORMALLY, result);
196 }
197
198 /**
199 * @tc.name: WindowRaiseToAppTopTest3
200 * @tc.desc: to raise to app top, not app subwindow
201 * @tc.type: FUNC
202 */
203 HWTEST_F(WindowRaiseToAppTopTest, NotAppSubWindow, Function | MediumTest | Level3)
204 {
205 fullInfo_.name = "mainWindow.1";
206 fullInfo_.type = WindowType::WINDOW_TYPE_FLOAT;
207 sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
208 ASSERT_NE(nullptr, mainWindow);
209 activeWindows_.push_back(mainWindow);
210 ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
211 sleep(TEST_SLEEP_S);
212
213 fullInfo_.name = "subWindow.1";
214 fullInfo_.type = WindowType::WINDOW_TYPE_SYSTEM_SUB_WINDOW;
215 fullInfo_.parentId = mainWindow->GetWindowId();
216 sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
217 ASSERT_NE(nullptr, subWindow1);
218 activeWindows_.push_back(subWindow1);
219 ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
220 sleep(TEST_SLEEP_S);
221
222 auto result = subWindow1->RaiseToAppTop();
223 ASSERT_EQ(WmErrorCode::WM_ERROR_INVALID_CALLING, result);
224 }
225 }
226 } // namespace Rosen
227 } // namespace OHOS
228