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 #include <gtest/gtest.h>
17 #include "window_manager_service.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace Rosen {
24 using ConfigItem = WindowManagerConfig::ConfigItem;
25 const std::string XML_STR = R"(<?xml version='1.0' encoding="utf-8"?>
26 <!--
27  * Copyright (c) 2022 Huawei Device Co., Ltd.
28  * Licensed under the Apache License, Version 2.0 (the "License");
29  * you may not use this file except in compliance with the License.
30  * You may obtain a copy of the License at
31  *
32  *     http://www.apache.org/licenses/LICENSE-2.0
33  *
34  * Unless required by applicable law or agreed to in writing, software
35  * distributed under the License is distributed on an "AS IS" BASIS,
36  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37  * See the License for the specific language governing permissions and
38  * limitations under the License.
39  -->
40 <Configs>
41     <!--decor enable is true means app main window show decoration-->
42     <decor enable="false"></decor>
43     <!--max number of main window that could be shown on display-->
44     <maxAppWindowNumber>100</maxAppWindowNumber>
45     <!--minimizeByOther enable is true means fullscreen window will be minmized by other fullscreen window-->
46     <minimizeByOther enable="true"></minimizeByOther>
47     <!--window mdoe change hot zones config, fullscreen primary secondary-->
48     <modeChangeHotZones>50 50 50</modeChangeHotZones>
49     <!--stretchable enable is true means all window be stretchable-->
50     <stretchable enable="false"></stretchable>
51     <!--exit split screen mode ratio config-->
52     <exitSplitRatios>0.1 0.9</exitSplitRatios>
53     <!--split screen ratios config-->
54     <splitRatios>0.5 0.33 0.67</splitRatios>
55     <!--default window mode config-->
56     <defaultWindowMode>1</defaultWindowMode>
57     <!--window animation config-->
58     <windowAnimation>
59         <timing>
60             <!--duration of animation when add/remove window, unit is ms-->
61             <duration>350</duration>
62             <!--timing curve of animation, config it as below:
63             name=ease, easeIn, easeOut, easeInOut, default, linear,
64             spring, interactiveSpring, cubic(float float float float)-->
65             <curve name="easeOut"></curve>
66         </timing>
67         <!--scaleX and scaleY of animation start state-->
68         <scale>0.7 0.7</scale>
69         <!--rotation of animation start state, 4 numbers is axis and angle-->
70         <rotation>0 0 1 0</rotation>
71         <!--translateX and translateY of animation start state-->
72         <translate>0 0</translate>
73         <!--opacity of animation start state-->
74         <opacity>0</opacity>
75     </windowAnimation>
76     <!--keyboard animation config-->
77     <keyboardAnimation>
78         <timing>
79             <!--duration of animation when add keyboard, unit is ms-->
80             <durationIn>500</durationIn>
81             <!--duration of animation when remove keyboard, unit is ms-->
82             <durationOut>300</durationOut>
83             <!--friction curve-->
84             <curve name="cubic">0.2 0.0 0.2 1.0</curve>
85         </timing>
86     </keyboardAnimation>
87     <!--enable/disable remote animation-->
88     <remoteAnimation enable="true"></remoteAnimation>
89     <!--window effect config-->
90     <windowEffect>
91         <appWindows>
92             <cornerRadius>
93                 <!--off: no corner, defaultCornerRadiusXS: 4vp, defaultCornerRadiusS: 8vp-->
94                 <!--defaultCornerRadiusM: 12vp, defaultCornerRadiusL: 16vp, defaultCornerRadiusXL: 24vp-->
95                 <fullScreen>off</fullScreen>
96                 <split>off</split>
97                 <float>off</float>
98             </cornerRadius>
99             <shadow>
100                 <focused>
101                     <elevation>0</elevation>
102                     <color>#000000</color>
103                     <offsetX>0</offsetX>
104                     <offsetY>0</offsetY>
105                     <alpha>0</alpha>
106                 </focused>
107                 <unfocused>
108                     <elevation>0</elevation>
109                     <color>#000000</color>
110                     <offsetX>0</offsetX>
111                     <offsetY>0</offsetY>
112                     <alpha>0</alpha>
113                 </unfocused>
114             </shadow>
115         </appWindows>
116     </windowEffect>
117 </Configs>
118 )";
119 
120 class WindowManagerConfigTest : public testing::Test {
121 public:
122     static void SetUpTestCase();
123     static void TearDownTestCase();
124     virtual void SetUp() override;
125     virtual void TearDown() override;
126     ConfigItem ReadConfig(const std::string& xmlStr);
127 };
128 
SetUpTestCase()129 void WindowManagerConfigTest::SetUpTestCase()
130 {
131 }
132 
TearDownTestCase()133 void WindowManagerConfigTest::TearDownTestCase()
134 {
135 }
136 
SetUp()137 void WindowManagerConfigTest::SetUp()
138 {
139 }
140 
TearDown()141 void WindowManagerConfigTest::TearDown()
142 {
143 }
144 
ReadConfig(const std::string & xmlStr)145 ConfigItem WindowManagerConfigTest::ReadConfig(const std::string& xmlStr)
146 {
147     ConfigItem config;
148     xmlDocPtr docPtr = xmlParseMemory(xmlStr.c_str(), xmlStr.length() + 1);
149     if (docPtr == nullptr) {
150         return config;
151     }
152 
153     xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
154     if (rootPtr == nullptr || rootPtr->name == nullptr ||
155         xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar*>("Configs"))) {
156         xmlFreeDoc(docPtr);
157         return config;
158     }
159 
160     std::map<std::string, ConfigItem> configMap;
161     config.SetValue(configMap);
162     WindowManagerConfig::ReadConfig(rootPtr, *config.mapValue_);
163     xmlFreeDoc(docPtr);
164     return config;
165 }
166 namespace {
167 /**
168  * @tc.name: AnimationConfig
169  * @tc.desc: animation config test
170  * @tc.type: FUNC
171  * @tc.require issueI5N26H
172  */
173 HWTEST_F(WindowManagerConfigTest, AnimationConfig, Function | SmallTest | Level2)
174 {
175     WindowManagerConfig::config_ = ReadConfig(XML_STR);
176     WindowManagerService::GetInstance().ConfigureWindowManagerService();
177     ConfigItem item = WindowManagerConfig::config_["windowAnimation"];
178     ASSERT_EQ(true, item.IsMap());
179     item = WindowManagerConfig::config_["windowAnimation"]["timing"]["duration"];
180     ASSERT_EQ(true, item.IsInts());
181     auto value = *item.intsValue_;
182     ASSERT_EQ(true, value.size() == 1);
183     ASSERT_EQ(350, value[0]);
184     item = WindowManagerConfig::config_["windowAnimation"]["timing"]["curve"].GetProp("name");
185     ASSERT_EQ(true, item.IsString());
186     ASSERT_EQ("easeOut", item.stringValue_);
187     item = WindowManagerConfig::config_["windowAnimation"]["scale"];
188     ASSERT_EQ(true, item.IsFloats());
189     ASSERT_EQ(true, item.floatsValue_->size() == 2);
190     item = WindowManagerConfig::config_["windowAnimation"]["rotation"];
191     ASSERT_EQ(true, item.IsFloats());
192     ASSERT_EQ(true, item.floatsValue_->size() == 4);
193     item = WindowManagerConfig::config_["windowAnimation"]["translate"];
194     ASSERT_EQ(true, item.IsFloats());
195     ASSERT_EQ(true, item.floatsValue_->size() == 2);
196     item = WindowManagerConfig::config_["windowAnimation"]["opacity"];
197     ASSERT_EQ(true, item.IsFloats());
198     ASSERT_EQ(true, item.floatsValue_->size() == 1);
199 }
200 
201 /**
202  * @tc.name: maxAppWindowNumber
203  * @tc.desc: maxAppWindowNumber test
204  * @tc.type: FUNC
205  */
206 HWTEST_F(WindowManagerConfigTest, MaxAppWindowNumber, Function | SmallTest | Level2)
207 {
208     std::string xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
209         "<Configs>"
210         "<maxAppWindowNumber>0</maxAppWindowNumber>"
211         "</Configs>";
212     WindowManagerConfig::config_ = ReadConfig(xmlStr);
213     WindowManagerService::GetInstance().ConfigureWindowManagerService();
214     ASSERT_EQ(true, WindowManagerService::GetInstance().windowRoot_->maxAppWindowNumber_ == 100);
215 
216     xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
217         "<Configs>"
218         "<maxAppWindowNumber>-2</maxAppWindowNumber>"
219         "</Configs>";
220     WindowManagerConfig::config_ = ReadConfig(xmlStr);
221     WindowManagerService::GetInstance().ConfigureWindowManagerService();
222     ASSERT_EQ(true, WindowManagerService::GetInstance().windowRoot_->maxAppWindowNumber_ == 100);
223 
224     xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
225         "<Configs>"
226         "<maxAppWindowNumber>4</maxAppWindowNumber>"
227         "</Configs>";
228     WindowManagerConfig::config_ = ReadConfig(xmlStr);
229     WindowManagerService::GetInstance().ConfigureWindowManagerService();
230     ASSERT_EQ(true, WindowManagerService::GetInstance().windowRoot_->maxAppWindowNumber_ == 4);
231 
232     xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
233         "<Configs>"
234         "<maxAppWindowNumber>1000</maxAppWindowNumber>"
235         "</Configs>";
236     WindowManagerConfig::config_ = ReadConfig(xmlStr);
237     WindowManagerService::GetInstance().ConfigureWindowManagerService();
238     ASSERT_EQ(true, WindowManagerService::GetInstance().windowRoot_->maxAppWindowNumber_ == 1000);
239 }
240 
241 /**
242  * @tc.name: DecorConfig01
243  * @tc.desc: set decor true and false without mode support.
244  * @tc.type: FUNC
245  * @tc.require: issueI68QCO
246  */
247 HWTEST_F(WindowManagerConfigTest, DecorConfig01, Function | SmallTest | Level2)
248 {
249     auto& sysConfig = WindowManagerService::GetInstance().systemConfig_;
250     std::string xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
251         "<Configs>"
252         "<decor enable=\"true\"/>"
253         "</Configs>";
254     WindowManagerConfig::config_ = ReadConfig(xmlStr);
255     WindowManagerService::GetInstance().ConfigureWindowManagerService();
256     ASSERT_EQ(true, WindowManagerService::GetInstance().systemConfig_.isSystemDecorEnable_);
257     ASSERT_TRUE(WindowModeSupport::WINDOW_MODE_SUPPORT_ALL == sysConfig.decorWindowModeSupportType_);
258 
259     xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
260         "<Configs>"
261         "<decor enable=\"false\"/>"
262         "</Configs>";
263     WindowManagerConfig::config_ = ReadConfig(xmlStr);
264     WindowManagerService::GetInstance().ConfigureWindowManagerService();
265     ASSERT_EQ(false, WindowManagerService::GetInstance().systemConfig_.isSystemDecorEnable_);
266     ASSERT_TRUE(WindowModeSupport::WINDOW_MODE_SUPPORT_ALL == sysConfig.decorWindowModeSupportType_);
267 }
268 
269 /**
270  * @tc.name: DecorConfig02
271  * @tc.desc: set decor true and mode support fullscreen.
272  * @tc.type: FUNC
273  * @tc.require: issueI68QCO
274  */
275 HWTEST_F(WindowManagerConfigTest, DecorConfig02, Function | SmallTest | Level2)
276 {
277     auto& sysConfig = WindowManagerService::GetInstance().systemConfig_;
278     std::string xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
279         "<Configs>"
280         "<decor enable=\"true\">"
281         "<supportedMode>fullscreen</supportedMode>"
282         "</decor>"
283         "</Configs>";
284     WindowManagerConfig::config_ = ReadConfig(xmlStr);
285     WindowManagerService::GetInstance().ConfigureWindowManagerService();
286     ASSERT_EQ(true, sysConfig.isSystemDecorEnable_);
287     ASSERT_TRUE(WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN == sysConfig.decorWindowModeSupportType_);
288 }
289 
290 /**
291  * @tc.name: DecorConfig03
292  * @tc.desc: set decor true and mode support floating.
293  * @tc.type: FUNC
294  * @tc.require: issueI68QCO
295  */
296 HWTEST_F(WindowManagerConfigTest, DecorConfig03, Function | SmallTest | Level2)
297 {
298     auto& sysConfig = WindowManagerService::GetInstance().systemConfig_;
299     std::string xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
300         "<Configs>"
301         "<decor enable=\"true\">"
302         "<supportedMode>floating</supportedMode>"
303         "</decor>"
304         "</Configs>";
305     WindowManagerConfig::config_ = ReadConfig(xmlStr);
306     WindowManagerService::GetInstance().ConfigureWindowManagerService();
307     ASSERT_EQ(true, sysConfig.isSystemDecorEnable_);
308     ASSERT_TRUE(WindowModeSupport::WINDOW_MODE_SUPPORT_FLOATING == sysConfig.decorWindowModeSupportType_);
309 }
310 
311 /**
312  * @tc.name: DecorConfig04
313  * @tc.desc: set decor true and mode support fullscreen|floating.
314  * @tc.type: FUNC
315  * @tc.require: issueI68QCO
316  */
317 HWTEST_F(WindowManagerConfigTest, DecorConfig04, Function | SmallTest | Level2)
318 {
319     auto& sysConfig = WindowManagerService::GetInstance().systemConfig_;
320     std::string xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
321         "<Configs>"
322         "<decor enable=\"true\">"
323         "<supportedMode>fullscreen floating</supportedMode>"
324         "</decor>"
325         "</Configs>";
326     WindowManagerConfig::config_ = ReadConfig(xmlStr);
327     WindowManagerService::GetInstance().ConfigureWindowManagerService();
328     ASSERT_EQ(true, sysConfig.isSystemDecorEnable_);
329     ASSERT_TRUE((WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN |
330         WindowModeSupport::WINDOW_MODE_SUPPORT_FLOATING) == sysConfig.decorWindowModeSupportType_);
331 }
332 
333 /**
334  * @tc.name: DecorConfig05
335  * @tc.desc: set decor true and mode support split.
336  * @tc.type: FUNC
337  * @tc.require: issueI68QCO
338  */
339 HWTEST_F(WindowManagerConfigTest, DecorConfig05, Function | SmallTest | Level2)
340 {
341     auto& sysConfig = WindowManagerService::GetInstance().systemConfig_;
342     std::string xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
343         "<Configs>"
344         "<decor enable=\"true\">"
345         "<supportedMode>split</supportedMode>"
346         "</decor>"
347         "</Configs>";
348     WindowManagerConfig::config_ = ReadConfig(xmlStr);
349     WindowManagerService::GetInstance().ConfigureWindowManagerService();
350     ASSERT_EQ(true, sysConfig.isSystemDecorEnable_);
351     ASSERT_TRUE((WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_PRIMARY |
352         WindowModeSupport::WINDOW_MODE_SUPPORT_SPLIT_SECONDARY) == sysConfig.decorWindowModeSupportType_);
353 }
354 
355 /**
356  * @tc.name: DecorConfig06
357  * @tc.desc: set invalid mode support.
358  * @tc.type: FUNC
359  * @tc.require: issueI68QCO
360  */
361 HWTEST_F(WindowManagerConfigTest, DecorConfig06, Function | SmallTest | Level2)
362 {
363     auto& sysConfig = WindowManagerService::GetInstance().systemConfig_;
364     std::string xmlStr = "<?xml version='1.0' encoding=\"utf-8\"?>"
365         "<Configs>"
366         "<decor enable=\"true\">"
367         "<supportedMode>xxxxx fullscreen xxxxx</supportedMode>"
368         "</decor>"
369         "</Configs>";
370     WindowManagerConfig::config_ = ReadConfig(xmlStr);
371     WindowManagerService::GetInstance().ConfigureWindowManagerService();
372     ASSERT_EQ(true, sysConfig.isSystemDecorEnable_);
373     ASSERT_TRUE(WindowModeSupport::WINDOW_MODE_SUPPORT_ALL == sysConfig.decorWindowModeSupportType_);
374 }
375 } // namespace
376 } // namespace Rosen
377 } // namespace OHOS