• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_impl.h"
20 #include "window_test_utils.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 
39 private:
40     static constexpr uint32_t TEST_SLEEP_S = 1;
41 };
42 
SetUpTestCase()43 void WindowRaiseToAppTopTest::SetUpTestCase() {}
44 
TearDownTestCase()45 void WindowRaiseToAppTopTest::TearDownTestCase() {}
46 
SetUp()47 void WindowRaiseToAppTopTest::SetUp()
48 {
49     fullInfo_ = {
50         .name = "",
51         .rect = Utils::customAppRect_,
52         .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
53         .parentId = INVALID_WINDOW_ID,
54     };
55     activeWindows_.clear();
56 }
57 
TearDown()58 void WindowRaiseToAppTopTest::TearDown()
59 {
60     while (!activeWindows_.empty()) {
61         ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
62         activeWindows_.pop_back();
63     }
64 }
65 
66 namespace {
67 /**
68  * @tc.name: WindowRaiseToAppTopTest1
69  * @tc.desc: to raise to app top, normal
70  * @tc.type: FUNC
71  */
72 HWTEST_F(WindowRaiseToAppTopTest, NormalRaise1, TestSize.Level1)
73 {
74     fullInfo_.name = "mainWindow.1";
75     sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
76     if (mainWindow == nullptr) {
77         return;
78     }
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(WMError::WM_ERROR_INVALID_PARENT, result1);
104     auto result2 = subWindow1->RaiseToAppTop();
105     ASSERT_EQ(WMError::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, TestSize.Level1)
114 {
115     fullInfo_.name = "mainWindow.1";
116     sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
117     if (mainWindow == nullptr) {
118         return;
119     }
120     ASSERT_NE(nullptr, mainWindow);
121     activeWindows_.push_back(mainWindow);
122     ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
123     sleep(TEST_SLEEP_S);
124 
125     fullInfo_.name = "subWindow.1";
126     fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
127     fullInfo_.parentId = mainWindow->GetWindowId();
128     sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
129     ASSERT_NE(nullptr, subWindow1);
130     activeWindows_.push_back(subWindow1);
131     ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
132     sleep(TEST_SLEEP_S);
133 
134     fullInfo_.name = "subWindow.2";
135     fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
136     fullInfo_.parentId = mainWindow->GetWindowId();
137     sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
138     ASSERT_NE(nullptr, subWindow2);
139     activeWindows_.push_back(subWindow2);
140     ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
141     sleep(TEST_SLEEP_S);
142 
143     fullInfo_.name = "dialog.2";
144     fullInfo_.type = WindowType::WINDOW_TYPE_DIALOG;
145     fullInfo_.parentId = INVALID_WINDOW_ID;
146     sptr<Window> dialog = Utils::CreateTestWindow(fullInfo_);
147     ASSERT_NE(nullptr, dialog);
148     activeWindows_.push_back(dialog);
149     ASSERT_EQ(WMError::WM_OK, dialog->Show());
150     sleep(TEST_SLEEP_S);
151 
152     auto result = subWindow1->RaiseToAppTop();
153     ASSERT_EQ(WMError::WM_OK, result);
154 }
155 
156 /**
157  * @tc.name: WindowRaiseToAppTopTest3
158  * @tc.desc: to raise to app top, in hide
159  * @tc.type: FUNC
160  */
161 HWTEST_F(WindowRaiseToAppTopTest, RaiseWhenHide, TestSize.Level1)
162 {
163     fullInfo_.name = "mainWindow.1";
164     sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
165     if (mainWindow == nullptr) {
166         return;
167     }
168     ASSERT_NE(nullptr, mainWindow);
169     activeWindows_.push_back(mainWindow);
170     ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
171     sleep(TEST_SLEEP_S);
172 
173     fullInfo_.name = "subWindow.1";
174     fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
175     fullInfo_.parentId = mainWindow->GetWindowId();
176     sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
177     if (subWindow1 == nullptr) {
178         return;
179     }
180     ASSERT_NE(nullptr, subWindow1);
181     activeWindows_.push_back(subWindow1);
182     ASSERT_EQ(WMError::WM_OK, subWindow1->Show());
183     sleep(TEST_SLEEP_S);
184 
185     fullInfo_.name = "subWindow.2";
186     fullInfo_.type = WindowType::WINDOW_TYPE_APP_SUB_WINDOW;
187     fullInfo_.parentId = mainWindow->GetWindowId();
188     sptr<Window> subWindow2 = Utils::CreateTestWindow(fullInfo_);
189     ASSERT_NE(nullptr, subWindow2);
190     activeWindows_.push_back(subWindow2);
191     ASSERT_EQ(WMError::WM_OK, subWindow2->Show());
192     sleep(TEST_SLEEP_S);
193 
194     ASSERT_EQ(WMError::WM_OK, mainWindow->Hide());
195     sleep(TEST_SLEEP_S);
196 
197     auto result = subWindow1->RaiseToAppTop();
198     ASSERT_EQ(WMError::WM_OK, result);
199 
200     ASSERT_EQ(WMError::WM_OK, subWindow1->Hide());
201     sleep(TEST_SLEEP_S);
202 
203     result = subWindow1->RaiseToAppTop();
204     ASSERT_EQ(WMError::WM_DO_NOTHING, result);
205 }
206 
207 /**
208  * @tc.name: WindowRaiseToAppTopTest3
209  * @tc.desc: to raise to app top, not app subwindow
210  * @tc.type: FUNC
211  */
212 HWTEST_F(WindowRaiseToAppTopTest, NotAppSubWindow, TestSize.Level1)
213 {
214     CommonTestUtils::GuaranteeFloatWindowPermission("window_raisetoapptop_test");
215     fullInfo_.name = "mainWindow.1";
216     fullInfo_.type = WindowType::WINDOW_TYPE_FLOAT;
217     sptr<Window> mainWindow = Utils::CreateTestWindow(fullInfo_);
218     if (mainWindow == nullptr) {
219         return;
220     }
221     ASSERT_NE(nullptr, mainWindow);
222     activeWindows_.push_back(mainWindow);
223     ASSERT_EQ(WMError::WM_OK, mainWindow->Show());
224     sleep(TEST_SLEEP_S);
225 
226     fullInfo_.name = "subWindow.1";
227     fullInfo_.type = WindowType::WINDOW_TYPE_SYSTEM_SUB_WINDOW;
228     fullInfo_.parentId = mainWindow->GetWindowId();
229     sptr<Window> subWindow1 = Utils::CreateTestWindow(fullInfo_);
230     if (subWindow1 == nullptr) {
231         return;
232     }
233     ASSERT_NE(nullptr, subWindow1);
234     activeWindows_.push_back(subWindow1);
235     ASSERT_EQ(WMError::WM_OK, subWindow1->Show(0, true));
236     sleep(TEST_SLEEP_S);
237 
238     auto result = subWindow1->RaiseToAppTop();
239     ASSERT_EQ(WMError::WM_ERROR_INVALID_CALLING, result);
240 }
241 } // namespace
242 } // namespace Rosen
243 } // namespace OHOS
244