1 /*
2 * Copyright (c) 2021-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 <thread>
19 #include "window_test_utils.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 using utils = WindowTestUtils;
27 class WindowMultiAbilityTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 virtual void SetUp() override;
32 virtual void TearDown() override;
33 };
34
SetUpTestCase()35 void WindowMultiAbilityTest::SetUpTestCase()
36 {
37 }
38
TearDownTestCase()39 void WindowMultiAbilityTest::TearDownTestCase()
40 {
41 }
42
SetUp()43 void WindowMultiAbilityTest::SetUp()
44 {
45 }
46
TearDown()47 void WindowMultiAbilityTest::TearDown()
48 {
49 }
50
51 const int SLEEP_MS = 20;
52
ShowHideWindowSceneCallable(int i)53 static void ShowHideWindowSceneCallable(int i)
54 {
55 unsigned int sleepTimeMs = i * SLEEP_MS;
56 usleep(sleepTimeMs);
57 sptr<WindowScene> scene = utils::CreateWindowScene();
58 const int loop = 10;
59 int j = 0;
60 for (; j < loop; j++) {
61 usleep(sleepTimeMs);
62 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
63 usleep(sleepTimeMs);
64 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
65 usleep(sleepTimeMs);
66 }
67 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
68 }
69
CreateDestroyWindowSceneCallable(int i)70 static void CreateDestroyWindowSceneCallable(int i)
71 {
72 unsigned int sleepTimeMs = i * SLEEP_MS;
73 const int loop = 10;
74 int j = 0;
75 for (; j < loop; j++) {
76 usleep(sleepTimeMs);
77 sptr<WindowScene> scene = utils::CreateWindowScene();
78 usleep(sleepTimeMs);
79 ASSERT_EQ(WMError::WM_OK, scene->GoForeground());
80 usleep(sleepTimeMs);
81 ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
82 usleep(sleepTimeMs);
83 ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
84 usleep(sleepTimeMs);
85 scene.clear();
86 usleep(sleepTimeMs);
87 }
88 }
89
90 /**
91 * @tc.name: MultiAbilityWindow01
92 * @tc.desc: Five scene process in one thread
93 * @tc.type: FUNC
94 */
95 HWTEST_F(WindowMultiAbilityTest, MultiAbilityWindow01, Function | MediumTest | Level2)
96 {
97 sptr<WindowScene> scene1 = utils::CreateWindowScene();
98 sptr<WindowScene> scene2 = utils::CreateWindowScene();
99 sptr<WindowScene> scene3 = utils::CreateWindowScene();
100 sptr<WindowScene> scene4 = utils::CreateWindowScene();
101 sptr<WindowScene> scene5 = utils::CreateWindowScene();
102
103 ASSERT_EQ(WMError::WM_OK, scene1->GoForeground());
104 ASSERT_EQ(WMError::WM_OK, scene2->GoForeground());
105 ASSERT_EQ(WMError::WM_OK, scene3->GoForeground());
106 ASSERT_EQ(WMError::WM_OK, scene4->GoForeground());
107 ASSERT_EQ(WMError::WM_OK, scene5->GoForeground());
108 ASSERT_EQ(WMError::WM_OK, scene5->GoBackground());
109 ASSERT_EQ(WMError::WM_OK, scene4->GoBackground());
110 ASSERT_EQ(WMError::WM_OK, scene3->GoBackground());
111 ASSERT_EQ(WMError::WM_OK, scene2->GoBackground());
112 ASSERT_EQ(WMError::WM_OK, scene1->GoBackground());
113 ASSERT_EQ(WMError::WM_OK, scene1->GoDestroy());
114 ASSERT_EQ(WMError::WM_OK, scene2->GoDestroy());
115 ASSERT_EQ(WMError::WM_OK, scene3->GoDestroy());
116 ASSERT_EQ(WMError::WM_OK, scene4->GoDestroy());
117 ASSERT_EQ(WMError::WM_OK, scene5->GoDestroy());
118 }
119
120 /**
121 * @tc.name: MultiAbilityWindow02
122 * @tc.desc: Five scene process show/hide in five threads
123 * @tc.type: FUNC
124 */
125 HWTEST_F(WindowMultiAbilityTest, MultiAbilityWindow02, Function | MediumTest | Level2)
126 {
127 std::thread th1(ShowHideWindowSceneCallable, 1);
128 std::thread th2(ShowHideWindowSceneCallable, 2);
129 std::thread th3(ShowHideWindowSceneCallable, 3);
130 std::thread th4(ShowHideWindowSceneCallable, 4);
131 std::thread th5(ShowHideWindowSceneCallable, 5);
132 th1.join();
133 th2.join();
134 th3.join();
135 th4.join();
136 th5.join();
137 }
138
139 /**
140 * @tc.name: MultiAbilityWindow03
141 * @tc.desc: Five scene process create/destroy in five threads
142 * @tc.type: FUNC
143 */
144 HWTEST_F(WindowMultiAbilityTest, MultiAbilityWindow03, Function | MediumTest | Level2)
145 {
146 std::thread th1(CreateDestroyWindowSceneCallable, 1);
147 std::thread th2(CreateDestroyWindowSceneCallable, 2);
148 std::thread th3(CreateDestroyWindowSceneCallable, 3);
149 std::thread th4(CreateDestroyWindowSceneCallable, 4);
150 std::thread th5(CreateDestroyWindowSceneCallable, 5);
151 th1.join();
152 th2.join();
153 th3.join();
154 th4.join();
155 th5.join();
156 }
157
158 /**
159 * @tc.name: MultiAbilityWindow04
160 * @tc.desc: Five scene process in one thread, create/show/hide/destroy in order
161 * @tc.type: FUNC
162 */
163 HWTEST_F(WindowMultiAbilityTest, MultiAbilityWindow04, Function | MediumTest | Level3)
164 {
165 sptr<WindowScene> scene1 = utils::CreateWindowScene();
166 ASSERT_EQ(WMError::WM_OK, scene1->GoForeground());
167 ASSERT_EQ(WMError::WM_OK, scene1->GoBackground());
168 ASSERT_EQ(WMError::WM_OK, scene1->GoDestroy());
169
170 sptr<WindowScene> scene2 = utils::CreateWindowScene();
171 ASSERT_EQ(WMError::WM_OK, scene2->GoForeground());
172 ASSERT_EQ(WMError::WM_OK, scene2->GoBackground());
173 ASSERT_EQ(WMError::WM_OK, scene2->GoDestroy());
174
175 sptr<WindowScene> scene3 = utils::CreateWindowScene();
176 ASSERT_EQ(WMError::WM_OK, scene3->GoForeground());
177 ASSERT_EQ(WMError::WM_OK, scene3->GoBackground());
178 ASSERT_EQ(WMError::WM_OK, scene3->GoDestroy());
179
180 sptr<WindowScene> scene4 = utils::CreateWindowScene();
181 ASSERT_EQ(WMError::WM_OK, scene4->GoForeground());
182 ASSERT_EQ(WMError::WM_OK, scene4->GoBackground());
183 ASSERT_EQ(WMError::WM_OK, scene4->GoDestroy());
184
185 sptr<WindowScene> scene5 = utils::CreateWindowScene();
186 ASSERT_EQ(WMError::WM_OK, scene5->GoForeground());
187 ASSERT_EQ(WMError::WM_OK, scene5->GoBackground());
188 ASSERT_EQ(WMError::WM_OK, scene5->GoDestroy());
189 }
190
191 /**
192 * @tc.name: MultiAbilityWindow05
193 * @tc.desc: Five scene process in one thread, create/show/hide/destroy out of order
194 * @tc.type: FUNC
195 */
196 HWTEST_F(WindowMultiAbilityTest, MultiAbilityWindow05, Function | MediumTest | Level3)
197 {
198 sptr<WindowScene> scene1 = utils::CreateWindowScene();
199 ASSERT_EQ(WMError::WM_OK, scene1->GoForeground());
200 sptr<WindowScene> scene2 = utils::CreateWindowScene();
201 sptr<WindowScene> scene3 = utils::CreateWindowScene();
202 ASSERT_EQ(WMError::WM_OK, scene3->GoForeground());
203 ASSERT_EQ(WMError::WM_OK, scene1->GoBackground());
204 ASSERT_EQ(WMError::WM_OK, scene1->GoDestroy());
205 sptr<WindowScene> scene4 = utils::CreateWindowScene();
206 ASSERT_EQ(WMError::WM_OK, scene3->GoBackground());
207 ASSERT_EQ(WMError::WM_OK, scene2->GoForeground());
208 ASSERT_EQ(WMError::WM_OK, scene4->GoForeground());
209 ASSERT_EQ(WMError::WM_OK, scene2->GoBackground());
210 sptr<WindowScene> scene5 = utils::CreateWindowScene();
211 ASSERT_EQ(WMError::WM_OK, scene3->GoDestroy());
212 ASSERT_EQ(WMError::WM_OK, scene5->GoForeground());
213 ASSERT_EQ(WMError::WM_OK, scene5->GoBackground());
214 ASSERT_EQ(WMError::WM_OK, scene4->GoBackground());
215 ASSERT_EQ(WMError::WM_OK, scene4->GoDestroy());
216 ASSERT_EQ(WMError::WM_OK, scene5->GoDestroy());
217 ASSERT_EQ(WMError::WM_OK, scene2->GoDestroy());
218 }
219 } // namespace Rosen
220 } // namespace OHOS
221