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 
145     if (scene->GoForeground() == WMError::WM_OK) {
146         ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
147     } else {
148         ASSERT_NE(WMError::WM_OK, scene->GoForeground());
149     }
150 
151     if (scene->GetMainWindow() == nullptr) {
152         return;
153     }
154     ASSERT_EQ(WMError::WM_OK, fltWin->Show());
155     ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
156     ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
157 
158     ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
159     ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
160 }
161 
162 /**
163  * @tc.name: SystemToastWindow03
164  * @tc.desc: SystemToastWindow life cycle, app floating window hide first
165  * @tc.type: FUNC
166  */
167 HWTEST_F(WindowSystemToastWindowTest, SystemToastWindow03, Function | MediumTest | Level3)
168 {
169     sptr<WindowScene> scene = CreateWindowScene();
170     ASSERT_NE(nullptr, scene);
171 
172     Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
173     sptr<Window> fltWin = CreateSystemToastWindow(WindowType::WINDOW_TYPE_SYSTEM_TOAST, fltWindRect);
174     if (fltWin == nullptr) {
175         return;
176     }
177     ASSERT_NE(nullptr, fltWin);
178     if (scene->GoForeground() == WMError::WM_OK) {
179         ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
180     } else {
181         ASSERT_NE(WMError::WM_OK, scene->GoForeground());
182     }
183 
184     ASSERT_EQ(WMError::WM_OK, fltWin->Show());
185 
186     ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
187     ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
188     if (scene->GetMainWindow() == nullptr) {
189         return;
190     }
191     ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
192     ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
193 
194     ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
195     ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
196 }
197 
198 /**
199  * @tc.name: SystemToastWindow04
200  * @tc.desc: SystemToastWindow life cycle, main window destroy first
201  * @tc.type: FUNC
202  */
203 HWTEST_F(WindowSystemToastWindowTest, SystemToastWindow04, Function | MediumTest | Level3)
204 {
205     sptr<WindowScene> scene = CreateWindowScene();
206     ASSERT_NE(nullptr, scene);
207 
208     Rect fltWindRect = GetRectWithVpr(0, 0, 400, 600);
209     sptr<Window> fltWin = CreateSystemToastWindow(WindowType::WINDOW_TYPE_SYSTEM_TOAST, fltWindRect);
210     if (fltWin == nullptr) {
211         return;
212     }
213     ASSERT_NE(nullptr, fltWin);
214 
215     if (scene->GoForeground() != WMError::WM_OK) {
216         ASSERT_NE(WMError::WM_OK, scene->GoForeground());
217     } else {
218         ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
219         ASSERT_EQ(WMError::WM_OK, fltWin->Show());
220         ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
221     }
222 
223     if (scene->GetMainWindow() == nullptr) {
224         return;
225     }
226     ASSERT_EQ(nullptr, scene->GetMainWindow());
227     ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
228     ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
229 }
230 }
231 }