OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/css/MediaQuery.h" | 5 #include "core/css/MediaQuery.h" |
6 | 6 |
7 #include "core/css/MediaList.h" | 7 #include "core/css/MediaList.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "wtf/text/StringBuilder.h" | 9 #include "wtf/text/StringBuilder.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
13 typedef struct { | 13 typedef struct { |
14 const char* input; | 14 const char* input; |
15 const char* output; | 15 const char* output; |
16 bool shouldWorkOnOldParser; | |
17 } TestCase; | 16 } TestCase; |
18 | 17 |
19 static void testMediaQuery(TestCase test, | 18 static void testMediaQuery(TestCase test, MediaQuerySet& querySet) { |
20 MediaQuerySet& querySet, | |
21 bool oldParser) { | |
22 StringBuilder output; | 19 StringBuilder output; |
23 size_t j = 0; | 20 size_t j = 0; |
24 while (j < querySet.queryVector().size()) { | 21 while (j < querySet.queryVector().size()) { |
25 String queryText = querySet.queryVector()[j]->cssText(); | 22 String queryText = querySet.queryVector()[j]->cssText(); |
26 output.append(queryText); | 23 output.append(queryText); |
27 ++j; | 24 ++j; |
28 if (j >= querySet.queryVector().size()) | 25 if (j >= querySet.queryVector().size()) |
29 break; | 26 break; |
30 output.append(", "); | 27 output.append(", "); |
31 } | 28 } |
32 if (!oldParser || test.shouldWorkOnOldParser) { | 29 if (test.output) |
33 if (test.output) | 30 ASSERT_STREQ(test.output, output.toString().ascii().data()); |
34 ASSERT_STREQ(test.output, output.toString().ascii().data()); | 31 else |
35 else | 32 ASSERT_STREQ(test.input, output.toString().ascii().data()); |
36 ASSERT_STREQ(test.input, output.toString().ascii().data()); | |
37 } | |
38 } | 33 } |
39 | 34 |
40 TEST(MediaQuerySetTest, Basic) { | 35 TEST(MediaQuerySetTest, Basic) { |
41 // The first string represents the input string. | 36 // The first string represents the input string. |
42 // The second string represents the output string, if present. | 37 // The second string represents the output string, if present. |
43 // Otherwise, the output string is identical to the first string. | 38 // Otherwise, the output string is identical to the first string. |
44 TestCase testCases[] = { | 39 TestCase testCases[] = { |
45 {"", 0, true}, | 40 {"", nullptr}, |
46 {" ", "", true}, | 41 {" ", ""}, |
47 {"screen", 0, true}, | 42 {"screen", nullptr}, |
48 {"screen and (color)", 0, true}, | 43 {"screen and (color)", nullptr}, |
49 {"all and (min-width:500px)", "(min-width: 500px)", true}, | 44 {"all and (min-width:500px)", "(min-width: 500px)"}, |
50 {"all and (min-width:/*bla*/500px)", "(min-width: 500px)", true}, | 45 {"all and (min-width:/*bla*/500px)", "(min-width: 500px)"}, |
51 {"(min-width:500px)", "(min-width: 500px)", true}, | 46 {"(min-width:500px)", "(min-width: 500px)"}, |
52 {"screen and (color), projection and (color)", 0, true}, | 47 {"screen and (color), projection and (color)", nullptr}, |
53 {"not screen and (color)", 0, true}, | 48 {"not screen and (color)", nullptr}, |
54 {"only screen and (color)", 0, true}, | 49 {"only screen and (color)", nullptr}, |
55 {"screen and (color), projection and (color)", 0, true}, | 50 {"screen and (color), projection and (color)", nullptr}, |
56 {"aural and (device-aspect-ratio: 16/9)", 0, true}, | 51 {"aural and (device-aspect-ratio: 16/9)", nullptr}, |
57 {"speech and (min-device-width: 800px)", 0, true}, | 52 {"speech and (min-device-width: 800px)", nullptr}, |
58 {"example", 0, true}, | 53 {"example", nullptr}, |
59 {"screen and (max-weight: 3kg) and (color), (monochrome)", | 54 {"screen and (max-weight: 3kg) and (color), (monochrome)", |
60 "not all, (monochrome)", true}, | 55 "not all, (monochrome)"}, |
61 {"(min-width: -100px)", "not all", true}, | 56 {"(min-width: -100px)", "not all"}, |
62 {"(example, all,), speech", "not all, speech", true}, | 57 {"(example, all,), speech", "not all, speech"}, |
63 {"&test, screen", "not all, screen", true}, | 58 {"&test, screen", "not all, screen"}, |
64 {"print and (min-width: 25cm)", 0, true}, | 59 {"print and (min-width: 25cm)", nullptr}, |
65 {"screen and (min-width: 400px) and (max-width: 700px)", | 60 {"screen and (min-width: 400px) and (max-width: 700px)", |
66 "screen and (max-width: 700px) and (min-width: 400px)", true}, | 61 "screen and (max-width: 700px) and (min-width: 400px)"}, |
67 {"screen and (device-width: 800px)", 0, true}, | 62 {"screen and (device-width: 800px)", nullptr}, |
68 {"screen and (device-height: 60em)", 0, true}, | 63 {"screen and (device-height: 60em)", nullptr}, |
69 {"screen and (device-height: 60rem)", 0, true}, | 64 {"screen and (device-height: 60rem)", nullptr}, |
70 {"screen and (device-height: 60ch)", 0, true}, | 65 {"screen and (device-height: 60ch)", nullptr}, |
71 {"screen and (device-aspect-ratio: 16/9)", 0, true}, | 66 {"screen and (device-aspect-ratio: 16/9)", nullptr}, |
72 {"(device-aspect-ratio: 16.0/9.0)", "not all", true}, | 67 {"(device-aspect-ratio: 16.0/9.0)", "not all"}, |
73 {"(device-aspect-ratio: 16/ 9)", "(device-aspect-ratio: 16/9)", true}, | 68 {"(device-aspect-ratio: 16/ 9)", "(device-aspect-ratio: 16/9)"}, |
74 {"(device-aspect-ratio: 16/\r9)", "(device-aspect-ratio: 16/9)", true}, | 69 {"(device-aspect-ratio: 16/\r9)", "(device-aspect-ratio: 16/9)"}, |
75 {"all and (color)", "(color)", true}, | 70 {"all and (color)", "(color)"}, |
76 {"all and (min-color: 1)", "(min-color: 1)", true}, | 71 {"all and (min-color: 1)", "(min-color: 1)"}, |
77 {"all and (min-color: 1.0)", "not all", true}, | 72 {"all and (min-color: 1.0)", "not all"}, |
78 {"all and (min-color: 2)", "(min-color: 2)", true}, | 73 {"all and (min-color: 2)", "(min-color: 2)"}, |
79 {"all and (color-index)", "(color-index)", true}, | 74 {"all and (color-index)", "(color-index)"}, |
80 {"all and (min-color-index: 1)", "(min-color-index: 1)", true}, | 75 {"all and (min-color-index: 1)", "(min-color-index: 1)"}, |
81 {"all and (monochrome)", "(monochrome)", true}, | 76 {"all and (monochrome)", "(monochrome)"}, |
82 {"all and (min-monochrome: 1)", "(min-monochrome: 1)", true}, | 77 {"all and (min-monochrome: 1)", "(min-monochrome: 1)"}, |
83 {"all and (min-monochrome: 2)", "(min-monochrome: 2)", true}, | 78 {"all and (min-monochrome: 2)", "(min-monochrome: 2)"}, |
84 {"print and (monochrome)", 0, true}, | 79 {"print and (monochrome)", nullptr}, |
85 {"handheld and (grid) and (max-width: 15em)", 0, true}, | 80 {"handheld and (grid) and (max-width: 15em)", nullptr}, |
86 {"handheld and (grid) and (max-device-height: 7em)", 0, true}, | 81 {"handheld and (grid) and (max-device-height: 7em)", nullptr}, |
87 {"screen and (max-width: 50%)", "not all", true}, | 82 {"screen and (max-width: 50%)", "not all"}, |
88 {"screen and (max-WIDTH: 500px)", "screen and (max-width: 500px)", true}, | 83 {"screen and (max-WIDTH: 500px)", "screen and (max-width: 500px)"}, |
89 {"screen and (max-width: 24.4em)", 0, true}, | 84 {"screen and (max-width: 24.4em)", nullptr}, |
90 {"screen and (max-width: 24.4EM)", "screen and (max-width: 24.4em)", | 85 {"screen and (max-width: 24.4EM)", "screen and (max-width: 24.4em)"}, |
91 true}, | 86 {"screen and (max-width: blabla)", "not all"}, |
92 {"screen and (max-width: blabla)", "not all", true}, | 87 {"screen and (max-width: 1)", "not all"}, |
93 {"screen and (max-width: 1)", "not all", true}, | 88 {"screen and (max-width: 0)", nullptr}, |
94 {"screen and (max-width: 0)", 0, true}, | 89 {"screen and (max-width: 1deg)", "not all"}, |
95 {"screen and (max-width: 1deg)", "not all", true}, | |
96 {"handheld and (min-width: 20em), \nscreen and (min-width: 20em)", | 90 {"handheld and (min-width: 20em), \nscreen and (min-width: 20em)", |
97 "handheld and (min-width: 20em), screen and (min-width: 20em)", true}, | 91 "handheld and (min-width: 20em), screen and (min-width: 20em)"}, |
98 {"print and (min-resolution: 300dpi)", 0, true}, | 92 {"print and (min-resolution: 300dpi)", nullptr}, |
99 {"print and (min-resolution: 118dpcm)", 0, true}, | 93 {"print and (min-resolution: 118dpcm)", nullptr}, |
100 {"(resolution: 0.83333333333333333333dppx)", | 94 {"(resolution: 0.83333333333333333333dppx)", |
101 "(resolution: 0.833333333333333dppx)", true}, | 95 "(resolution: 0.833333333333333dppx)"}, |
102 {"(resolution: 2.4dppx)", 0, true}, | 96 {"(resolution: 2.4dppx)", nullptr}, |
103 {"all and(color)", "not all", true}, | 97 {"all and(color)", "not all"}, |
104 {"all and (", "not all", true}, | 98 {"all and (", "not all"}, |
105 {"test;,all", "not all, all", true}, | 99 {"test;,all", "not all, all"}, |
106 {"(color:20example)", "not all", false}, | 100 {"(color:20example)", "not all"}, |
107 {"not braille", 0, true}, | 101 {"not braille", nullptr}, |
108 {",screen", "not all, screen", true}, | 102 {",screen", "not all, screen"}, |
109 {",all", "not all, all", true}, | 103 {",all", "not all, all"}, |
110 {",,all,,", "not all, not all, all, not all, not all", true}, | 104 {",,all,,", "not all, not all, all, not all, not all"}, |
111 {",,all,, ", "not all, not all, all, not all, not all", true}, | 105 {",,all,, ", "not all, not all, all, not all, not all"}, |
112 {",screen,,&invalid,,", | 106 {",screen,,&invalid,,", |
113 "not all, screen, not all, not all, not all, not all", true}, | 107 "not all, screen, not all, not all, not all, not all"}, |
114 {",screen,,(invalid,),,", | 108 {",screen,,(invalid,),,", |
115 "not all, screen, not all, not all, not all, not all", true}, | 109 "not all, screen, not all, not all, not all, not all"}, |
116 {",(all,),,", "not all, not all, not all, not all", true}, | 110 {",(all,),,", "not all, not all, not all, not all"}, |
117 {",", "not all, not all", true}, | 111 {",", "not all, not all"}, |
118 {" ", "", true}, | 112 {" ", ""}, |
119 {"(color", "(color)", true}, | 113 {"(color", "(color)"}, |
120 {"(min-color: 2", "(min-color: 2)", true}, | 114 {"(min-color: 2", "(min-color: 2)"}, |
121 {"(orientation: portrait)", 0, true}, | 115 {"(orientation: portrait)", nullptr}, |
122 {"tv and (scan: progressive)", 0, true}, | 116 {"tv and (scan: progressive)", nullptr}, |
123 {"(pointer: coarse)", 0, true}, | 117 {"(pointer: coarse)", nullptr}, |
124 {"(min-orientation:portrait)", "not all", true}, | 118 {"(min-orientation:portrait)", "not all"}, |
125 {"all and (orientation:portrait)", "(orientation: portrait)", true}, | 119 {"all and (orientation:portrait)", "(orientation: portrait)"}, |
126 {"all and (orientation:landscape)", "(orientation: landscape)", true}, | 120 {"all and (orientation:landscape)", "(orientation: landscape)"}, |
127 {"NOT braille, tv AND (max-width: 200px) and (min-WIDTH: 100px) and " | 121 {"NOT braille, tv AND (max-width: 200px) and (min-WIDTH: 100px) and " |
128 "(orientation: landscape), (color)", | 122 "(orientation: landscape), (color)", |
129 "not braille, tv and (max-width: 200px) and (min-width: 100px) and " | 123 "not braille, tv and (max-width: 200px) and (min-width: 100px) and " |
130 "(orientation: landscape), (color)", | 124 "(orientation: landscape), (color)"}, |
131 true}, | 125 {"(m\\61x-width: 300px)", "(max-width: 300px)"}, |
132 {"(m\\61x-width: 300px)", "(max-width: 300px)", true}, | 126 {"(max-width: 400\\70\\78)", "(max-width: 400px)"}, |
133 {"(max-width: 400\\70\\78)", "(max-width: 400px)", false}, | 127 {"(max-width: 500\\0070\\0078)", "(max-width: 500px)"}, |
134 {"(max-width: 500\\0070\\0078)", "(max-width: 500px)", false}, | 128 {"(max-width: 600\\000070\\000078)", "(max-width: 600px)"}, |
135 {"(max-width: 600\\000070\\000078)", "(max-width: 600px)", false}, | |
136 {"(max-width: 700px), (max-width: 700px)", | 129 {"(max-width: 700px), (max-width: 700px)", |
137 "(max-width: 700px), (max-width: 700px)", true}, | 130 "(max-width: 700px), (max-width: 700px)"}, |
138 {"(max-width: 800px()), (max-width: 800px)", | 131 {"(max-width: 800px()), (max-width: 800px)", |
139 "not all, (max-width: 800px)", true}, | 132 "not all, (max-width: 800px)"}, |
140 {"(max-width: 900px(()), (max-width: 900px)", "not all", true}, | 133 {"(max-width: 900px(()), (max-width: 900px)", "not all"}, |
141 {"(max-width: 600px(())))), (max-width: 600px)", | 134 {"(max-width: 600px(())))), (max-width: 600px)", |
142 "not all, (max-width: 600px)", true}, | 135 "not all, (max-width: 600px)"}, |
143 {"(max-width: 500px(((((((((())))), (max-width: 500px)", "not all", true}, | 136 {"(max-width: 500px(((((((((())))), (max-width: 500px)", "not all"}, |
144 {"(max-width: 800px[]), (max-width: 800px)", | 137 {"(max-width: 800px[]), (max-width: 800px)", |
145 "not all, (max-width: 800px)", true}, | 138 "not all, (max-width: 800px)"}, |
146 {"(max-width: 900px[[]), (max-width: 900px)", "not all", true}, | 139 {"(max-width: 900px[[]), (max-width: 900px)", "not all"}, |
147 {"(max-width: 600px[[]]]]), (max-width: 600px)", | 140 {"(max-width: 600px[[]]]]), (max-width: 600px)", |
148 "not all, (max-width: 600px)", true}, | 141 "not all, (max-width: 600px)"}, |
149 {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", "not all", true}, | 142 {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", "not all"}, |
150 {"(max-width: 800px{}), (max-width: 800px)", | 143 {"(max-width: 800px{}), (max-width: 800px)", |
151 "not all, (max-width: 800px)", true}, | 144 "not all, (max-width: 800px)"}, |
152 {"(max-width: 900px{{}), (max-width: 900px)", "not all", true}, | 145 {"(max-width: 900px{{}), (max-width: 900px)", "not all"}, |
153 {"(max-width: 600px{{}}}}), (max-width: 600px)", | 146 {"(max-width: 600px{{}}}}), (max-width: 600px)", |
154 "not all, (max-width: 600px)", true}, | 147 "not all, (max-width: 600px)"}, |
155 {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", "not all", true}, | 148 {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", "not all"}, |
156 {"[(), (max-width: 400px)", "not all", true}, | 149 {"[(), (max-width: 400px)", "not all"}, |
157 {"[{}, (max-width: 500px)", "not all", true}, | 150 {"[{}, (max-width: 500px)", "not all"}, |
158 {"[{]}], (max-width: 900px)", "not all, (max-width: 900px)", true}, | 151 {"[{]}], (max-width: 900px)", "not all, (max-width: 900px)"}, |
159 {"[{[]{}{{{}}}}], (max-width: 900px)", "not all, (max-width: 900px)", | 152 {"[{[]{}{{{}}}}], (max-width: 900px)", "not all, (max-width: 900px)"}, |
160 true}, | 153 {"[{[}], (max-width: 900px)", "not all"}, |
161 {"[{[}], (max-width: 900px)", "not all", true}, | 154 {"[({)}], (max-width: 900px)", "not all"}, |
162 {"[({)}], (max-width: 900px)", "not all", true}, | 155 {"[]((), (max-width: 900px)", "not all"}, |
163 {"[]((), (max-width: 900px)", "not all", true}, | 156 {"((), (max-width: 900px)", "not all"}, |
164 {"((), (max-width: 900px)", "not all", true}, | 157 {"(foo(), (max-width: 900px)", "not all"}, |
165 {"(foo(), (max-width: 900px)", "not all", true}, | 158 {"[](()), (max-width: 900px)", "not all, (max-width: 900px)"}, |
166 {"[](()), (max-width: 900px)", "not all, (max-width: 900px)", true}, | |
167 {"all an[isdfs bla())()]icalc(i)(()), (max-width: 400px)", | 159 {"all an[isdfs bla())()]icalc(i)(()), (max-width: 400px)", |
168 "not all, (max-width: 400px)", true}, | 160 "not all, (max-width: 400px)"}, |
169 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", "not all", | 161 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", "not all"}, |
170 true}, | 162 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", "not all"}, |
171 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", "not all", | |
172 true}, | |
173 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", | 163 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", |
174 "not all, (max-width: 800px)", true}, | 164 "not all, (max-width: 800px)"}, |
175 {"(max-width: '40px')", "not all", true}, | 165 {"(max-width: '40px')", "not all"}, |
176 {"('max-width': 40px)", "not all", true}, | 166 {"('max-width': 40px)", "not all"}, |
177 {"'\"'\", (max-width: 900px)", "not all", true}, | 167 {"'\"'\", (max-width: 900px)", "not all"}, |
178 {"'\"\"\"', (max-width: 900px)", "not all, (max-width: 900px)", true}, | 168 {"'\"\"\"', (max-width: 900px)", "not all, (max-width: 900px)"}, |
179 {"\"'\"', (max-width: 900px)", "not all", true}, | 169 {"\"'\"', (max-width: 900px)", "not all"}, |
180 {"\"'''\", (max-width: 900px)", "not all, (max-width: 900px)", true}, | 170 {"\"'''\", (max-width: 900px)", "not all, (max-width: 900px)"}, |
181 {"not not", "not all", true}, | 171 {"not not", "not all"}, |
182 {"not and", "not all", true}, | 172 {"not and", "not all"}, |
183 {"not only", "not all", true}, | 173 {"not only", "not all"}, |
184 {"not or", "not all", true}, | 174 {"not or", "not all"}, |
185 {"only not", "not all", true}, | 175 {"only not", "not all"}, |
186 {"only and", "not all", true}, | 176 {"only and", "not all"}, |
187 {"only only", "not all", true}, | 177 {"only only", "not all"}, |
188 {"only or", "not all", true}, | 178 {"only or", "not all"}, |
189 {"not (orientation)", "not all", true}, | 179 {"not (orientation)", "not all"}, |
190 {"only (orientation)", "not all", true}, | 180 {"only (orientation)", "not all"}, |
191 {0, 0} // Do not remove the terminator line. | 181 {nullptr, nullptr} // Do not remove the terminator line. |
192 }; | 182 }; |
193 | 183 |
194 for (unsigned i = 0; testCases[i].input; ++i) { | 184 for (unsigned i = 0; testCases[i].input; ++i) { |
195 MediaQuerySet* oldParserQuerySet = | 185 MediaQuerySet* querySet = MediaQuerySet::create(testCases[i].input); |
196 MediaQuerySet::create(testCases[i].input); | 186 testMediaQuery(testCases[i], *querySet); |
197 MediaQuerySet* threadSafeQuerySet = | |
198 MediaQuerySet::createOffMainThread(testCases[i].input); | |
199 testMediaQuery(testCases[i], *oldParserQuerySet, true); | |
200 testMediaQuery(testCases[i], *threadSafeQuerySet, false); | |
201 } | 187 } |
202 } | 188 } |
203 | 189 |
204 } // namespace blink | 190 } // namespace blink |
OLD | NEW |