• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <limits>
17 
18 #include "gtest/gtest.h"
19 
20 #include "frameworks/bridge/common/manifest/manifest_window.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS::Ace::Framework {
26 namespace {
27 
28 #ifndef WEARABLE_PRODUCT
29 constexpr int32_t DESIGN_WIDTH_DEFAULT = 720;
30 #else
31 constexpr int32_t DESIGN_WIDTH_DEFAULT = 454;
32 #endif
33 constexpr int32_t DESIGN_WIDTH_CUSTOM = 800;
34 constexpr int32_t DESIGN_WIDTH_MAX = std::numeric_limits<int32_t>::max();
35 
36 const std::string MANIFEST_DESIGN_WIDTH_CUSTOM = ""
37                                                  "{                                                          "
38                                                  "  \"window\": {                                            "
39                                                  "                \"designWidth\" : 800,                     "
40                                                  "                \"autoDesignWidth\" : false                "
41                                                  "              }                                            "
42                                                  "}";
43 
44 const std::string MANIFEST_DESIGN_WIDTH_NEGATIVE = ""
45                                                    "{                                                          "
46                                                    "  \"window\": {                                            "
47                                                    "                \"designWidth\" : -1,                      "
48                                                    "                \"autoDesignWidth\" : false                "
49                                                    "              }                                            "
50                                                    "}";
51 
52 const std::string MANIFEST_DESIGN_WIDTH_ZERO = ""
53                                                "{                                                          "
54                                                "  \"window\": {                                            "
55                                                "                \"designWidth\" : 0,                       "
56                                                "                \"autoDesignWidth\" : false                "
57                                                "              }                                            "
58                                                "}";
59 
60 const std::string MANIFEST_DESIGN_WIDTH_OVER_LIMIT = ""
61                                                      "{                                                          "
62                                                      "  \"window\": {                                            "
63                                                      "                \"designWidth\" : 2147483648,              "
64                                                      "                \"autoDesignWidth\" : false                "
65                                                      "              }                                            "
66                                                      "}";
67 
68 const std::string MANIFEST_DESIGN_WIDTH_FORMAT_ERR = ""
69                                                      "{                                                          "
70                                                      "  \"window\": {                                            "
71                                                      "                \"designWidth\" : \"800\",                 "
72                                                      "                \"autoDesignWidth\" : false                "
73                                                      "              }                                            "
74                                                      "}";
75 
76 const std::string MANIFEST_DESIGN_WIDTH_NONE = ""
77                                                "{                                                          "
78                                                "  \"window\": {                                            "
79                                                "                \"autoDesignWidth\" : false                "
80                                                "              }                                            "
81                                                "}";
82 
83 const std::string MANIFEST_AUTO_DESIGN_WIDTH_TRUE = ""
84                                                     "{                                                          "
85                                                     "  \"window\": {                                            "
86                                                     "                \"autoDesignWidth\" : true                 "
87                                                     "              }                                            "
88                                                     "}";
89 
90 const std::string MANIFEST_AUTO_DESIGN_WIDTH_FALSE = ""
91                                                      "{                                                          "
92                                                      "  \"window\": {                                            "
93                                                      "                \"autoDesignWidth\" : false                "
94                                                      "              }                                            "
95                                                      "}";
96 
97 const std::string MANIFEST_AUTO_DESIGN_WIDTH_FORMAT_ERR = ""
98                                                           "{                                                          "
99                                                           "  \"window\": {                                            "
100                                                           "                \"autoDesignWidth\" : \"true\"             "
101                                                           "              }                                            "
102                                                           "}";
103 
104 const std::string MANIFEST_AUTO_DESIGN_WIDTH_NONE = ""
105                                                     "{                                                          "
106                                                     "  \"window\": {                                            "
107                                                     "              }                                            "
108                                                     "}";
109 
AssertParseMenifest(const std::string & json,int32_t expectDesignWidth,bool expectAutoDesignWidth)110 void AssertParseMenifest(const std::string& json, int32_t expectDesignWidth, bool expectAutoDesignWidth)
111 {
112     auto manifestWindow = AceType::MakeRefPtr<ManifestWindow>();
113     auto rootJson = JsonUtil::ParseJsonString(json);
114     manifestWindow->WindowParse(rootJson);
115     auto& config = manifestWindow->GetWindowConfig();
116     ASSERT_EQ(config.designWidth, expectDesignWidth);
117     ASSERT_EQ(config.autoDesignWidth, expectAutoDesignWidth);
118 }
119 
120 } // namespace
121 
122 class ManifestWindowTest : public testing::Test {
123 public:
124     static void SetUpTestCase();
125     static void TearDownTestCase();
126     void SetUp();
127     void TearDown();
128 };
129 
SetUpTestCase()130 void ManifestWindowTest::SetUpTestCase() {}
TearDownTestCase()131 void ManifestWindowTest::TearDownTestCase() {}
SetUp()132 void ManifestWindowTest::SetUp() {}
TearDown()133 void ManifestWindowTest::TearDown() {}
134 
135 /**
136  * @tc.name: ManifestParseDesignWidth001
137  * @tc.desc: Design width in manifest can be parsed correctly.
138  * @tc.type: FUNC
139  */
140 HWTEST_F(ManifestWindowTest, ManifestParseDesignWidth001, TestSize.Level0)
141 {
142     /**
143      * @tc.steps: step1. Construct ManifestWindow and parse json with normal value.
144      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
145      */
146     AssertParseMenifest(MANIFEST_DESIGN_WIDTH_CUSTOM, DESIGN_WIDTH_CUSTOM, false);
147 }
148 
149 /**
150  * @tc.name: ManifestParseDesignWidth002
151  * @tc.desc: Design width of negative value in manifest can be parsed correctly.
152  * @tc.type: FUNC
153  */
154 HWTEST_F(ManifestWindowTest, ManifestParseDesignWidth002, TestSize.Level0)
155 {
156     /**
157      * @tc.steps: step1. Construct ManifestWindow and parse json with negative value.
158      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
159      */
160     AssertParseMenifest(MANIFEST_DESIGN_WIDTH_NEGATIVE, DESIGN_WIDTH_DEFAULT, false);
161 }
162 
163 /**
164  * @tc.name: ManifestParseDesignWidth003
165  * @tc.desc: Design width of zero value in manifest can be parsed correctly.
166  * @tc.type: FUNC
167  */
168 HWTEST_F(ManifestWindowTest, ManifestParseDesignWidth003, TestSize.Level0)
169 {
170     /**
171      * @tc.steps: step1. Construct ManifestWindow and parse json with zero value.
172      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
173      */
174     AssertParseMenifest(MANIFEST_DESIGN_WIDTH_ZERO, DESIGN_WIDTH_DEFAULT, false);
175 }
176 
177 /**
178  * @tc.name: ManifestParseDesignWidth004
179  * @tc.desc: Design width of over limit value in manifest can be parsed correctly.
180  * @tc.type: FUNC
181  */
182 HWTEST_F(ManifestWindowTest, ManifestParseDesignWidth004, TestSize.Level0)
183 {
184     /**
185      * @tc.steps: step1. Construct ManifestWindow and parse json with over limit value.
186      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
187      */
188     AssertParseMenifest(MANIFEST_DESIGN_WIDTH_OVER_LIMIT, DESIGN_WIDTH_MAX, false);
189 }
190 
191 /**
192  * @tc.name: ManifestParseDesignWidth005
193  * @tc.desc: Design width of value not number in manifest can be parsed correctly.
194  * @tc.type: FUNC
195  */
196 HWTEST_F(ManifestWindowTest, ManifestParseDesignWidth005, TestSize.Level0)
197 {
198     /**
199      * @tc.steps: step1. Construct ManifestWindow and parse json with value not number.
200      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
201      */
202     AssertParseMenifest(MANIFEST_DESIGN_WIDTH_FORMAT_ERR, DESIGN_WIDTH_DEFAULT, false);
203 }
204 
205 /**
206  * @tc.name: ManifestParseDesignWidth006
207  * @tc.desc: No design width config in manifest can be parsed correctly.
208  * @tc.type: FUNC
209  */
210 HWTEST_F(ManifestWindowTest, ManifestParseDesignWidth006, TestSize.Level0)
211 {
212     /**
213      * @tc.steps: step1. Construct ManifestWindow and parse json with no design width.
214      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
215      */
216     AssertParseMenifest(MANIFEST_DESIGN_WIDTH_NONE, DESIGN_WIDTH_DEFAULT, false);
217 }
218 
219 /**
220  * @tc.name: ManifestParseAutoDesignWidth001
221  * @tc.desc: Auto design width in manifest can be parsed correctly.
222  * @tc.type: FUNC
223  */
224 HWTEST_F(ManifestWindowTest, ManifestParseAutoDesignWidth001, TestSize.Level0)
225 {
226     /**
227      * @tc.steps: step1. Construct ManifestWindow and parse json with autoDesignWidth = true.
228      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
229      */
230     AssertParseMenifest(MANIFEST_AUTO_DESIGN_WIDTH_TRUE, DESIGN_WIDTH_DEFAULT, true);
231 }
232 
233 /**
234  * @tc.name: ManifestParseAutoDesignWidth002
235  * @tc.desc: Auto design width of "false" in manifest can be parsed correctly.
236  * @tc.type: FUNC
237  */
238 HWTEST_F(ManifestWindowTest, ManifestParseAutoDesignWidth002, TestSize.Level0)
239 {
240     /**
241      * @tc.steps: step1. Construct ManifestWindow and parse json with autoDesignWidth = false.
242      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
243      */
244     AssertParseMenifest(MANIFEST_AUTO_DESIGN_WIDTH_FALSE, DESIGN_WIDTH_DEFAULT, false);
245 }
246 
247 /**
248  * @tc.name: ManifestParseAutoDesignWidth003
249  * @tc.desc: Auto design width with wrong format in manifest can be parsed correctly.
250  * @tc.type: FUNC
251  */
252 HWTEST_F(ManifestWindowTest, ManifestParseAutoDesignWidth003, TestSize.Level0)
253 {
254     /**
255      * @tc.steps: step1. Construct ManifestWindow and parse json with autoDesignWidth of wrong format.
256      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
257      */
258     AssertParseMenifest(MANIFEST_AUTO_DESIGN_WIDTH_FORMAT_ERR, DESIGN_WIDTH_DEFAULT, false);
259 }
260 
261 /**
262  * @tc.name: ManifestParseAutoDesignWidth004
263  * @tc.desc: Auto design width not config in manifest can be parsed correctly.
264  * @tc.type: FUNC
265  */
266 HWTEST_F(ManifestWindowTest, ManifestParseAutoDesignWidth004, TestSize.Level0)
267 {
268     /**
269      * @tc.steps: step1. Construct ManifestWindow and parse json with autoDesignWidth not config.
270      * @tc.expected: step1. "designWidth" and "autoDesignWidth" can be parsed correctly.
271      */
272     AssertParseMenifest(MANIFEST_AUTO_DESIGN_WIDTH_NONE, DESIGN_WIDTH_DEFAULT, false);
273 }
274 
275 } // namespace OHOS::Ace::Framework
276