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