• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 20212022 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 
18 #define private public
19 #include "frameworks/bridge/common/media_query/media_queryer.h"
20 #include "frameworks/bridge/common/media_query/media_query_info.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS::Ace::Framework {
26 namespace {
27 constexpr int32_t ORIENTATION_LANDSCAPE = 1;
28 const std::string MEDIA_FEATURE_INFORMATION = "{\"width\":0,"
29                                               "\"height\":0,"
30                                               "\"aspect-ratio\":1,"
31                                               "\"round-screen\":true,"
32                                               "\"device-width\":100,"
33                                               "\"device-height\":100,"
34                                               "\"resolution\":1,"
35                                               "\"orientation\":\"portrait\","
36                                               "\"device-type\":\"phone\","
37                                               "\"dark-mode\":false,"
38                                               "\"api-version\":10,"
39                                               "\"device-brand\":\"test_brand\"}";
40 
41 const std::string PHONE_MEDIA_INFO = "{\"width\":0,"
42                                      "\"height\":0,"
43                                      "\"aspect-ratio\":1,"
44                                      "\"round-screen\":true,"
45                                      "\"device-width\":100,"
46                                      "\"device-height\":100,"
47                                      "\"resolution\":1,"
48                                      "\"orientation\":\"portrait\","
49                                      "\"device-type\":\"phone\","
50                                      "\"dark-mode\":false,"
51                                      "\"api-version\":9,"
52                                      "\"isInit\":false}";
53 
54 const std::string TV_MEDIA_INFO = "{\"width\":0,"
55                                   "\"height\":0,"
56                                   "\"aspect-ratio\":1,"
57                                   "\"round-screen\":true,"
58                                   "\"device-width\":100,"
59                                   "\"device-height\":100,"
60                                   "\"resolution\":1,"
61                                   "\"orientation\":\"landscape\","
62                                   "\"device-type\":\"tv\","
63                                   "\"dark-mode\":false,"
64                                   "\"api-version\":9,"
65                                   "\"isInit\":false}";
66 
67 const std::string CAR_MEDIA_INFO = "{\"width\":0,"
68                                    "\"height\":0,"
69                                    "\"aspect-ratio\":1,"
70                                    "\"round-screen\":true,"
71                                    "\"device-width\":100,"
72                                    "\"device-height\":100,"
73                                    "\"resolution\":1,"
74                                    "\"orientation\":\"landscape\","
75                                    "\"device-type\":\"car\","
76                                    "\"dark-mode\":false,"
77                                    "\"api-version\":9,"
78                                    "\"isInit\":false}";
79 
80 const std::string WATCH_MEDIA_INFO = "{\"width\":0,"
81                                      "\"height\":0,"
82                                      "\"aspect-ratio\":1,"
83                                      "\"round-screen\":true,"
84                                      "\"device-width\":100,"
85                                      "\"device-height\":100,"
86                                      "\"resolution\":1,"
87                                      "\"orientation\":\"landscape\","
88                                      "\"device-type\":\"wearable\","
89                                      "\"dark-mode\":false,"
90                                      "\"api-version\":9,"
91                                      "\"isInit\":false}";
92 
93 const std::string TABLET_MEDIA_INFO = "{\"width\":0,"
94                                       "\"height\":0,"
95                                       "\"aspect-ratio\":1,"
96                                       "\"round-screen\":true,"
97                                       "\"device-width\":100,"
98                                       "\"device-height\":100,"
99                                       "\"resolution\":1,"
100                                       "\"orientation\":\"landscape\","
101                                       "\"device-type\":\"tablet\","
102                                       "\"dark-mode\":false,"
103                                       "\"api-version\":9,"
104                                       "\"isInit\":false}";
105 } // namespace
106 
107 class MediaQueryTest : public testing::Test {
108 public:
109     static void SetUpTestCase();
110     static void TearDownTestCase();
111     void SetUp();
112     void TearDown();
113 };
114 
SetUpTestCase()115 void MediaQueryTest::SetUpTestCase() {}
TearDownTestCase()116 void MediaQueryTest::TearDownTestCase() {}
SetUp()117 void MediaQueryTest::SetUp() {}
TearDown()118 void MediaQueryTest::TearDown() {}
119 
120 /**
121  * @tc.name: MediaQueryTest001
122  * @tc.desc: Verify that media query condition can match right result.
123  * @tc.type: FUNC
124  * @tc.require: issueI5MWTB
125  */
126 HWTEST_F(MediaQueryTest, MediaQueryTest001, TestSize.Level1)
127 {
128     OHOS::Ace::Framework::MediaFeature mediaFeature = JsonUtil::Create(true);
129     mediaFeature->Put("width", 1500);
130     mediaFeature->Put("device-type", "tv");
131     mediaFeature->Put("round-screen", true);
132 
133     struct ConditionTestCase {
134         std::string condition;
135         bool result;
136     };
137     std::vector<ConditionTestCase> conditions = {
138         { "(max-width: )", false },
139         { "(min-width: 1000)", true },
140         { "(width < 2000)", true },
141         { "(width < 2000vp)", true },
142         { "(width << 2000)", false },
143         { "(1000 < width)", true },
144         { "(1000 < width < 2000)", true },
145         { "(round-screen: true) (device-type:tv)", false },
146         { "screen and (round-screen: true) and (device-type:tv)", true },
147         { "not screen and (round-screen: true) and (device-type:tv)", false },
148         { "not screen and (round-screen: false) and (device-type:tv)", true },
149         { "not screen and (round-screen: true) and (device-type:phone)", true },
150         { "screen and (round-screen: false), (device-type:tv)", true },
151         { "only screen and (1000 < width < 2000), (device-type:phone)", true },
152         { "(device-height < 2000) and (device-width < 2000) and (round-screen: true) and (device-type:phone)", false },
153         { "(device-height > 2000) or (device-width > 2000) or (round-screen: false) or (device-type:tv)", true },
154         { "(round-screen: true) or (device-type:phone)", true },
155         { "(round-screen: true), screen and (1000 < width < 2000), (device-type:phone)", false },
156     };
157 
158     MediaQueryer mediaQueryer;
159     for (const auto& item : conditions) {
160         bool result = mediaQueryer.MatchCondition(item.condition, mediaFeature);
161         ASSERT_EQ(result, item.result) << "condition = " << item.condition;
162     }
163 }
164 
165 /**
166  * @tc.name: MediaQueryTest002
167  * @tc.desc: Verify that media query condition can match right result.
168  * @tc.type: FUNC
169  */
170 HWTEST_F(MediaQueryTest, MediaQueryTest002, TestSize.Level1)
171 {
172     /**
173      * @tc.steps: step1. Input condition is empty.
174      * @tc.expected: step1. Media query condition can not match right result.
175      */
176     OHOS::Ace::Framework::MediaFeature mediaFeature = JsonUtil::Create(true);
177     mediaFeature->Put("device-type:phone", true);
178 
179     SystemProperties systemProperties;
180     systemProperties.InitDeviceInfo(100, 100, 0, 1.0, true);
181     systemProperties.paramDeviceType_ = "phone";
182     systemProperties.apiVersion_ = "10";
183     systemProperties.brand_ = "test_brand";
184     systemProperties.SetDeviceType(DeviceType::PHONE);
185 
186     const std::string nullCondition = "";
187     const std::string conditions = "{\"device-type:phone\":true}";
188     MediaQueryer mediaQueryer;
189     MediaQueryer::QueryHistory query = { conditions, true };
190     mediaQueryer.queryHistories.insert({ conditions, query });
191     bool result = mediaQueryer.MatchCondition(nullCondition, mediaFeature);
192     ASSERT_EQ(result, false);
193     auto mediaFeatureStr = mediaQueryer.GetMediaFeature()->ToString();
194     ASSERT_EQ(mediaFeatureStr, MEDIA_FEATURE_INFORMATION);
195 
196     /**
197      * @tc.steps: step2. Input the matching condition.
198      * @tc.expected: step2. Media query condition can match right result.
199      */
200     result = mediaQueryer.MatchCondition(conditions, mediaFeature);
201     ASSERT_EQ(result, true);
202 
203     /**
204      * @tc.steps: step3. Width and height are not initialized.
205      * And the query condition includes "width" or "height"
206      * @tc.expected: step3. Media query condition can not match right result.
207      */
208     mediaFeature->Put("(min-width: 1000", true);
209     result = mediaQueryer.MatchCondition(conditions, mediaFeature);
210     ASSERT_EQ(result, false);
211 }
212 
213 /**
214  * @tc.name: MediaQueryTest003
215  * @tc.desc: Get media query information as expect.
216  * @tc.type: FUNC
217  */
218 HWTEST_F(MediaQueryTest, MediaQueryTest003, TestSize.Level1)
219 {
220     /**
221      * @tc.steps: step1. Initialize device information.
222      * @tc.expected: step1. Get media query information as expect.
223      */
224     SystemProperties systemProperties;
225     systemProperties.InitDeviceInfo(100, 100, 0, 1.0, true);
226     MediaQueryInfo mediaQueryInfo;
227     systemProperties.paramDeviceType_ = "phone";
228     systemProperties.apiVersion_ = "9";
229     systemProperties.SetDeviceType(DeviceType::PHONE);
230     auto mediaQueryString = mediaQueryInfo.GetMediaQueryInfo();
231     ASSERT_EQ(mediaQueryString, PHONE_MEDIA_INFO);
232 
233     /**
234      * @tc.steps: step2. Device-type is "tv" and device-orientation is "landscape".
235      * @tc.expected: step2. Get media query information as expect.
236      */
237     systemProperties.SetDeviceOrientation(ORIENTATION_LANDSCAPE);
238     systemProperties.SetDeviceType(DeviceType::TV);
239     mediaQueryString = mediaQueryInfo.GetMediaQueryInfo();
240     ASSERT_EQ(mediaQueryString, TV_MEDIA_INFO);
241 
242     /**
243      * @tc.steps: step3. Device-type is "car".
244      * @tc.expected: step3. Get media query information as expect.
245      */
246     systemProperties.SetDeviceType(DeviceType::CAR);
247     mediaQueryString = mediaQueryInfo.GetMediaQueryInfo();
248     ASSERT_EQ(mediaQueryString, CAR_MEDIA_INFO);
249 
250     /**
251      * @tc.steps: step4. Device-type is "wearable".
252      * @tc.expected: step4. Get media query information as expect.
253      */
254     systemProperties.SetDeviceType(DeviceType::WATCH);
255     mediaQueryString = mediaQueryInfo.GetMediaQueryInfo();
256     ASSERT_EQ(mediaQueryString, WATCH_MEDIA_INFO);
257 
258     /**
259      * @tc.steps: step5. Device-type is "tablet".
260      * @tc.expected: step5. Get media query information as expect.
261      */
262     systemProperties.SetDeviceType(DeviceType::TABLET);
263     mediaQueryString = mediaQueryInfo.GetMediaQueryInfo();
264     ASSERT_EQ(mediaQueryString, TABLET_MEDIA_INFO);
265 
266     /**
267      * @tc.steps: step6. Device-type param is "tablet".
268      * @tc.expected: step6. Get media query information as expect.
269      */
270     systemProperties.paramDeviceType_ = "tablet";
271     mediaQueryString = mediaQueryInfo.GetMediaQueryInfo();
272     ASSERT_EQ(mediaQueryString, TABLET_MEDIA_INFO);
273 }
274 } // namespace OHOS::Ace::Framework
275