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 // gtest
17 #include <gtest/gtest.h>
18 #include "ability_context_impl.h"
19 #include "ipc_skeleton.h"
20 #include "window.h"
21 #include "window_manager.h"
22 #include "window_option.h"
23 #include "window_scene.h"
24 #include "window_test_utils.h"
25 #include "wm_common.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32
33 class WindowSystemToastWindowTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp() override;
38 void TearDown() override;
39
40 static inline float virtualPixelRatio_ = 1.0;
41 static inline Rect displayRect_ {0, 0, 0, 0};
42 static inline std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_ = nullptr;
43 };
44
SetUpTestCase()45 void WindowSystemToastWindowTest::SetUpTestCase()
46 {
47 auto display = DisplayManager::GetInstance().GetDisplayById(0);
48 ASSERT_TRUE((display != nullptr));
49 displayRect_.width_ = display->GetWidth();
50 displayRect_.height_ = display->GetHeight();
51 WindowTestUtils::InitByDisplayRect(displayRect_);
52 virtualPixelRatio_ = WindowTestUtils::GetVirtualPixelRatio(0);
53 }
54
TearDownTestCase()55 void WindowSystemToastWindowTest::TearDownTestCase()
56 {
57 }
58
SetUp()59 void WindowSystemToastWindowTest::SetUp()
60 {
61 }
62
TearDown()63 void WindowSystemToastWindowTest::TearDown()
64 {
65 }
66
CreateWindowScene()67 static sptr<WindowScene> CreateWindowScene()
68 {
69 sptr<IWindowLifeCycle> listener = nullptr;
70 WindowSystemToastWindowTest::abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
71
72 sptr<WindowScene> scene = new WindowScene();
73 scene->Init(0, WindowSystemToastWindowTest::abilityContext_, listener);
74 return scene;
75 }
76
CreateSystemToastWindow(WindowType type,Rect rect,std::string name="")77 static sptr<Window> CreateSystemToastWindow(WindowType type, Rect rect, std::string name = "")
78 {
79 sptr<WindowOption> option = new WindowOption();
80 option->SetWindowType(type);
81 option->SetWindowRect(rect);
82
83 static int cnt = 0;
84 std::string winName = (name == "") ? "systemToastWindowTest" + std::to_string(cnt++) : name;
85
86 return Window::Create(winName, option, WindowSystemToastWindowTest::abilityContext_);
87 }
88
GetRectWithVpr(int32_t x,int32_t y,uint32_t w,uint32_t h)89 static inline Rect GetRectWithVpr(int32_t x, int32_t y, uint32_t w, uint32_t h)
90 {
91 auto vpr = WindowSystemToastWindowTest::virtualPixelRatio_;
92 return {x, y, static_cast<uint32_t>(w * vpr), static_cast<uint32_t>(h * vpr)};
93 }
94
95 /**
96 * @tc.name: SystemToastWindow01
97 * @tc.desc: SystemToastWindow life cycle
98 * @tc.type: FUNC
99 */
100 HWTEST_F(WindowSystemToastWindowTest, SystemToastWindow01, Function | MediumTest | Level2)
101 {
102 sptr<WindowScene> scene = CreateWindowScene();
103 ASSERT_NE(nullptr, scene);
104
105 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
106 sptr<Window> fltWin = CreateSystemToastWindow(WindowType::WINDOW_TYPE_SYSTEM_TOAST, fltWindRect);
107 ASSERT_NE(nullptr, fltWin);
108 if (scene->GoForeground() == WMError::WM_OK) {
109 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
110 }
111
112 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
113
114 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
115 if (scene->GoForeground() == WMError::WM_OK) {
116 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
117 } else {
118 ASSERT_NE(WMError::WM_OK, scene->GoForeground());
119 }
120
121 if (scene->GetMainWindow() == nullptr) {
122 return;
123 }
124 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
125 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
126 }
127
128 /**
129 * @tc.name: SystemToastWindow02
130 * @tc.desc: SystemToastWindow life cycle, main window hide first
131 * @tc.type: FUNC
132 */
133 HWTEST_F(WindowSystemToastWindowTest, SystemToastWindow02, Function | MediumTest | Level3)
134 {
135 sptr<WindowScene> scene = CreateWindowScene();
136 ASSERT_NE(nullptr, scene);
137
138 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
139 sptr<Window> fltWin = CreateSystemToastWindow(WindowType::WINDOW_TYPE_SYSTEM_TOAST, fltWindRect);
140 if (fltWin == nullptr) {
141 return;
142 }
143 ASSERT_NE(nullptr, fltWin);
144 if (scene->GoForeground() == WMError::WM_OK) {
145 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
146 } else {
147 ASSERT_NE(WMError::WM_OK, scene->GoForeground());
148 }
149
150 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
151
152 if (scene->GoForeground() == WMError::WM_OK) {
153 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
154 } else {
155 ASSERT_NE(WMError::WM_OK, scene->GoForeground());
156 }
157
158 if (scene->GetMainWindow() == nullptr) {
159 return;
160 }
161 ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
162 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
163
164 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
165 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
166 }
167
168 /**
169 * @tc.name: SystemToastWindow03
170 * @tc.desc: SystemToastWindow life cycle, app floating window hide first
171 * @tc.type: FUNC
172 */
173 HWTEST_F(WindowSystemToastWindowTest, SystemToastWindow03, Function | MediumTest | Level3)
174 {
175 sptr<WindowScene> scene = CreateWindowScene();
176 ASSERT_NE(nullptr, scene);
177
178 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
179 sptr<Window> fltWin = CreateSystemToastWindow(WindowType::WINDOW_TYPE_SYSTEM_TOAST, fltWindRect);
180 if (fltWin == nullptr) {
181 return;
182 }
183 ASSERT_NE(nullptr, fltWin);
184 if (scene->GoForeground() == WMError::WM_OK) {
185 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
186 } else {
187 ASSERT_NE(WMError::WM_OK, scene->GoForeground());
188 }
189
190 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
191
192 ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
193 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
194 if (scene->GetMainWindow() == nullptr) {
195 return;
196 }
197 ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
198 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
199
200 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
201 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
202 }
203
204 /**
205 * @tc.name: SystemToastWindow04
206 * @tc.desc: SystemToastWindow life cycle, main window destroy first
207 * @tc.type: FUNC
208 */
209 HWTEST_F(WindowSystemToastWindowTest, SystemToastWindow04, Function | MediumTest | Level3)
210 {
211 sptr<WindowScene> scene = CreateWindowScene();
212 ASSERT_NE(nullptr, scene);
213
214 Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
215 sptr<Window> fltWin = CreateSystemToastWindow(WindowType::WINDOW_TYPE_SYSTEM_TOAST, fltWindRect);
216 if (fltWin == nullptr) {
217 return;
218 }
219 ASSERT_NE(nullptr, fltWin);
220
221 if (scene->GoForeground() == WMError::WM_OK) {
222 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
223 ASSERT_EQ(WMError::WM_OK, fltWin->Show());
224 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
225 } else {
226 ASSERT_NE(WMError::WM_OK, scene->GoForeground());
227 }
228
229 if (scene->GetMainWindow() == nullptr) {
230 return;
231 }
232 ASSERT_EQ(nullptr, scene->GetMainWindow());
233 ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
234 ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
235 }
236 }
237 }