| 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/css/MediaQuery.h" | 6 #include "core/css/MediaQuery.h" |
| 7 | 7 |
| 8 #include "core/css/MediaList.h" | 8 #include "core/css/MediaList.h" |
| 9 #include "wtf/PassOwnPtr.h" | 9 #include "wtf/PassOwnPtr.h" |
| 10 #include "wtf/text/StringBuilder.h" | 10 #include "wtf/text/StringBuilder.h" |
| 11 | 11 |
| 12 #include <gtest/gtest.h> | 12 #include <gtest/gtest.h> |
| 13 | 13 |
| 14 namespace WebCore { | 14 namespace WebCore { |
| 15 | 15 |
| 16 typedef struct { | 16 typedef struct { |
| 17 const char* input; | 17 const char* input; |
| 18 const char* output; | 18 const char* output; |
| 19 bool shouldWorkOnOldParser; |
| 19 } TestCase; | 20 } TestCase; |
| 20 | 21 |
| 22 static void testMediaQuery(TestCase test, MediaQuerySet& querySet, bool oldParse
r) |
| 23 { |
| 24 StringBuilder output; |
| 25 size_t j = 0; |
| 26 while (j < querySet.queryVector().size()) { |
| 27 String queryText = querySet.queryVector()[j]->cssText(); |
| 28 output.append(queryText); |
| 29 ++j; |
| 30 if (j >= querySet.queryVector().size()) |
| 31 break; |
| 32 output.append(", "); |
| 33 } |
| 34 if (!oldParser || test.shouldWorkOnOldParser) { |
| 35 if (test.output) |
| 36 ASSERT_STREQ(test.output, output.toString().ascii().data()); |
| 37 else |
| 38 ASSERT_STREQ(test.input, output.toString().ascii().data()); |
| 39 } |
| 40 } |
| 41 |
| 21 TEST(MediaQueryParserTest, Basic) | 42 TEST(MediaQueryParserTest, Basic) |
| 22 { | 43 { |
| 23 // The first string represents the input string. | 44 // The first string represents the input string. |
| 24 // The second string represents the output string, if present. | 45 // The second string represents the output string, if present. |
| 25 // Otherwise, the output string is identical to the first string. | 46 // Otherwise, the output string is identical to the first string. |
| 26 TestCase testCases[] = { | 47 TestCase testCases[] = { |
| 27 {"screen", 0}, | 48 {"screen", 0, true}, |
| 28 {"screen and (color)", 0}, | 49 {"screen and (color)", 0, true}, |
| 29 {"all and (min-width:500px)", "(min-width: 500px)"}, | 50 {"all and (min-width:500px)", "(min-width: 500px)", true}, |
| 30 {"all and (min-width:/*bla*/500px)", "(min-width: 500px)"}, | 51 {"all and (min-width:/*bla*/500px)", "(min-width: 500px)", true}, |
| 31 {"(min-width:500px)", "(min-width: 500px)"}, | 52 {"(min-width:500px)", "(min-width: 500px)", true}, |
| 32 {"screen and (color), projection and (color)", 0}, | 53 {"screen and (color), projection and (color)", 0, true}, |
| 33 {"not screen and (color)", 0}, | 54 {"not screen and (color)", 0, true}, |
| 34 {"only screen and (color)", 0}, | 55 {"only screen and (color)", 0, true}, |
| 35 {"screen and (color), projection and (color)", 0}, | 56 {"screen and (color), projection and (color)", 0, true}, |
| 36 {"aural and (device-aspect-ratio: 16/9)", 0}, | 57 {"aural and (device-aspect-ratio: 16/9)", 0, true}, |
| 37 {"speech and (min-device-width: 800px)", 0}, | 58 {"speech and (min-device-width: 800px)", 0, true}, |
| 38 {"example", 0}, | 59 {"example", 0, true}, |
| 39 {"screen and (max-weight: 3kg) and (color), (monochrome)", "not all, (mo
nochrome)"}, | 60 {"screen and (max-weight: 3kg) and (color), (monochrome)", "not all, (mo
nochrome)", true}, |
| 40 {"(min-width: -100px)", "not all"}, | 61 {"(min-width: -100px)", "not all", true}, |
| 41 {"(example, all,), speech", "not all, speech"}, | 62 {"(example, all,), speech", "not all, speech", true}, |
| 42 {"&test, screen", "not all, screen"}, | 63 {"&test, screen", "not all, screen", true}, |
| 43 {"print and (min-width: 25cm)", 0}, | 64 {"print and (min-width: 25cm)", 0, true}, |
| 44 {"screen and (min-width: 400px) and (max-width: 700px)", "screen and (ma
x-width: 700px) and (min-width: 400px)"}, | 65 {"screen and (min-width: 400px) and (max-width: 700px)", "screen and (ma
x-width: 700px) and (min-width: 400px)", true}, |
| 45 {"screen and (device-width: 800px)", 0}, | 66 {"screen and (device-width: 800px)", 0, true}, |
| 46 {"screen and (device-height: 60em)", 0}, | 67 {"screen and (device-height: 60em)", 0, true}, |
| 47 {"screen and (device-aspect-ratio: 16/9)", 0}, | 68 {"screen and (device-aspect-ratio: 16/9)", 0, true}, |
| 48 {"(device-aspect-ratio: 16.0/9.0)", "not all"}, | 69 {"(device-aspect-ratio: 16.0/9.0)", "not all", true}, |
| 49 {"(device-aspect-ratio: 16/ 9)", "(device-aspect-ratio: 16/9)"}, | 70 {"(device-aspect-ratio: 16/ 9)", "(device-aspect-ratio: 16/9)", true}, |
| 50 {"(device-aspect-ratio: 16/\r9)", "(device-aspect-ratio: 16/9)"}, | 71 {"(device-aspect-ratio: 16/\r9)", "(device-aspect-ratio: 16/9)", true}, |
| 51 {"all and (color)", "(color)"}, | 72 {"all and (color)", "(color)", true}, |
| 52 {"all and (min-color: 1)", "(min-color: 1)"}, | 73 {"all and (min-color: 1)", "(min-color: 1)", true}, |
| 53 {"all and (min-color: 1.0)", "not all"}, | 74 {"all and (min-color: 1.0)", "not all", true}, |
| 54 {"all and (min-color: 2)", "(min-color: 2)"}, | 75 {"all and (min-color: 2)", "(min-color: 2)", true}, |
| 55 {"all and (color-index)", "(color-index)"}, | 76 {"all and (color-index)", "(color-index)", true}, |
| 56 {"all and (min-color-index: 1)", "(min-color-index: 1)"}, | 77 {"all and (min-color-index: 1)", "(min-color-index: 1)", true}, |
| 57 {"all and (monochrome)", "(monochrome)"}, | 78 {"all and (monochrome)", "(monochrome)", true}, |
| 58 {"all and (min-monochrome: 1)", "(min-monochrome: 1)"}, | 79 {"all and (min-monochrome: 1)", "(min-monochrome: 1)", true}, |
| 59 {"all and (min-monochrome: 2)", "(min-monochrome: 2)"}, | 80 {"all and (min-monochrome: 2)", "(min-monochrome: 2)", true}, |
| 60 {"print and (monochrome)", 0}, | 81 {"print and (monochrome)", 0, true}, |
| 61 {"handheld and (grid) and (max-width: 15em)", 0}, | 82 {"handheld and (grid) and (max-width: 15em)", 0, true}, |
| 62 {"handheld and (grid) and (max-device-height: 7em)", 0}, | 83 {"handheld and (grid) and (max-device-height: 7em)", 0, true}, |
| 63 {"screen and (max-width: 50%)", "not all"}, | 84 {"screen and (max-width: 50%)", "not all", true}, |
| 64 {"screen and (max-WIDTH: 500px)", "screen and (max-width: 500px)"}, | 85 {"screen and (max-WIDTH: 500px)", "screen and (max-width: 500px)", true}
, |
| 65 {"screen and (max-width: 24.4em)", 0}, | 86 {"screen and (max-width: 24.4em)", 0, true}, |
| 66 {"screen and (max-width: 24.4EM)", "screen and (max-width: 24.4em)"}, | 87 {"screen and (max-width: 24.4EM)", "screen and (max-width: 24.4em)", tru
e}, |
| 67 {"screen and (max-width: blabla)", "not all"}, | 88 {"screen and (max-width: blabla)", "not all", true}, |
| 68 {"screen and (max-width: 1)", "not all"}, | 89 {"screen and (max-width: 1)", "not all", true}, |
| 69 {"screen and (max-width: 0)", 0}, | 90 {"screen and (max-width: 0)", 0, true}, |
| 70 {"screen and (max-width: 1deg)", "not all"}, | 91 {"screen and (max-width: 1deg)", "not all", true}, |
| 71 {"handheld and (min-width: 20em), \nscreen and (min-width: 20em)", "hand
held and (min-width: 20em), screen and (min-width: 20em)"}, | 92 {"handheld and (min-width: 20em), \nscreen and (min-width: 20em)", "hand
held and (min-width: 20em), screen and (min-width: 20em)", true}, |
| 72 {"print and (min-resolution: 300dpi)", 0}, | 93 {"print and (min-resolution: 300dpi)", 0, true}, |
| 73 {"print and (min-resolution: 118dpcm)", 0}, | 94 {"print and (min-resolution: 118dpcm)", 0, true}, |
| 74 {"(resolution: 0.83333333333333333333dppx)", "(resolution: 0.83333333333
33334dppx)"}, | 95 {"(resolution: 0.83333333333333333333dppx)", "(resolution: 0.83333333333
33334dppx)", true}, |
| 75 {"(resolution: 2.4dppx)", 0}, | 96 {"(resolution: 2.4dppx)", 0, true}, |
| 76 {"all and(color)", "not all"}, | 97 {"all and(color)", "not all", true}, |
| 77 {"all and (", "not all"}, | 98 {"all and (", "not all", true}, |
| 78 {"test;,all", "not all, all"}, | 99 {"test;,all", "not all, all", true}, |
| 79 // {"(color:20example)", "not all"}, | 100 {"(color:20example)", "not all", false}, |
| 80 {"not braille", 0}, | 101 {"not braille", 0, true}, |
| 81 {",screen", "not all, screen"}, | 102 {",screen", "not all, screen", true}, |
| 82 {",all", "not all, all"}, | 103 {",all", "not all, all", true}, |
| 83 {",,all,,", "not all, not all, all, not all, not all"}, | 104 {",,all,,", "not all, not all, all, not all, not all", true}, |
| 84 {",,all,, ", "not all, not all, all, not all, not all"}, | 105 {",,all,, ", "not all, not all, all, not all, not all", true}, |
| 85 {",screen,,&invalid,,", "not all, screen, not all, not all, not all, not
all"}, | 106 {",screen,,&invalid,,", "not all, screen, not all, not all, not all, not
all", true}, |
| 86 {",screen,,(invalid,),,", "not all, screen, not all, not all, not all, n
ot all"}, | 107 {",screen,,(invalid,),,", "not all, screen, not all, not all, not all, n
ot all", true}, |
| 87 {",(all,),,", "not all, not all, not all, not all"}, | 108 {",(all,),,", "not all, not all, not all, not all", true}, |
| 88 {",", "not all, not all"}, | 109 {",", "not all, not all", true}, |
| 89 {" ", ""}, | 110 {" ", "", true}, |
| 90 {"(color", "(color)"}, | 111 {"(color", "(color)", true}, |
| 91 {"(min-color: 2", "(min-color: 2)"}, | 112 {"(min-color: 2", "(min-color: 2)", true}, |
| 92 {"(orientation: portrait)", 0}, | 113 {"(orientation: portrait)", 0, true}, |
| 93 {"tv and (scan: progressive)", 0}, | 114 {"tv and (scan: progressive)", 0, true}, |
| 94 {"(pointer: coarse)", 0}, | 115 {"(pointer: coarse)", 0, true}, |
| 95 {"(min-orientation:portrait)", "not all"}, | 116 {"(min-orientation:portrait)", "not all", true}, |
| 96 {"all and (orientation:portrait)", "(orientation: portrait)"}, | 117 {"all and (orientation:portrait)", "(orientation: portrait)", true}, |
| 97 {"all and (orientation:landscape)", "(orientation: landscape)"}, | 118 {"all and (orientation:landscape)", "(orientation: landscape)", true}, |
| 98 {"NOT braille, tv AND (max-width: 200px) and (min-WIDTH: 100px) and (ori
entation: landscape), (color)", | 119 {"NOT braille, tv AND (max-width: 200px) and (min-WIDTH: 100px) and (ori
entation: landscape), (color)", |
| 99 "not braille, tv and (max-width: 200px) and (min-width: 100px) and (
orientation: landscape), (color)"}, | 120 "not braille, tv and (max-width: 200px) and (min-width: 100px) and (
orientation: landscape), (color)", true}, |
| 100 {"(max-width: 700px), (max-width: 700px)", "(max-width: 700px), (max-wid
th: 700px)"}, | 121 {"(max-width: 700px), (max-width: 700px)", "(max-width: 700px), (max-wid
th: 700px)", true}, |
| 101 {"(max-width: 800px()), (max-width: 800px)", "not all, (max-width: 800px
)"}, | 122 {"(max-width: 800px()), (max-width: 800px)", "not all, (max-width: 800px
)", true}, |
| 102 {"(max-width: 900px(()), (max-width: 900px)", "not all"}, | 123 {"(max-width: 900px(()), (max-width: 900px)", "not all", true}, |
| 103 {"(max-width: 600px(())))), (max-width: 600px)", "not all, (max-width: 6
00px)"}, | 124 {"(max-width: 600px(())))), (max-width: 600px)", "not all, (max-width: 6
00px)", true}, |
| 104 {"(max-width: 500px(((((((((())))), (max-width: 500px)", "not all"}, | 125 {"(max-width: 500px(((((((((())))), (max-width: 500px)", "not all", true
}, |
| 105 {"(max-width: 800px[]), (max-width: 800px)", "not all, (max-width: 800px
)"}, | 126 {"(max-width: 800px[]), (max-width: 800px)", "not all, (max-width: 800px
)", true}, |
| 106 {"(max-width: 900px[[]), (max-width: 900px)", "not all"}, | 127 {"(max-width: 900px[[]), (max-width: 900px)", "not all", true}, |
| 107 {"(max-width: 600px[[]]]]), (max-width: 600px)", "not all, (max-width: 6
00px)"}, | 128 {"(max-width: 600px[[]]]]), (max-width: 600px)", "not all, (max-width: 6
00px)", true}, |
| 108 {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", "not all"}, | 129 {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", "not all", true
}, |
| 109 {"(max-width: 800px{}), (max-width: 800px)", "not all, (max-width: 800px
)"}, | 130 {"(max-width: 800px{}), (max-width: 800px)", "not all, (max-width: 800px
)", true}, |
| 110 {"(max-width: 900px{{}), (max-width: 900px)", "not all"}, | 131 {"(max-width: 900px{{}), (max-width: 900px)", "not all", true}, |
| 111 {"(max-width: 600px{{}}}}), (max-width: 600px)", "not all, (max-width: 6
00px)"}, | 132 {"(max-width: 600px{{}}}}), (max-width: 600px)", "not all, (max-width: 6
00px)", true}, |
| 112 {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", "not all"}, | 133 {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", "not all", true
}, |
| 113 {"[(), (max-width: 900px)", "not all"}, | 134 {"[(), (max-width: 400px)", "not all", true}, |
| 114 {"[{}, (max-width: 900px)", "not all"}, | 135 {"[{}, (max-width: 500px)", "not all", true}, |
| 115 {"[{]}], (max-width: 900px)", "not all, (max-width: 900px)"}, | 136 {"[{]}], (max-width: 900px)", "not all, (max-width: 900px)", true}, |
| 116 {"[{[]{}{{{}}}}], (max-width: 900px)", "not all, (max-width: 900px)"}, | 137 {"[{[]{}{{{}}}}], (max-width: 900px)", "not all, (max-width: 900px)", tr
ue}, |
| 117 {"[{[}], (max-width: 900px)", "not all"}, | 138 {"[{[}], (max-width: 900px)", "not all", true}, |
| 118 {"[({)}], (max-width: 900px)", "not all"}, | 139 {"[({)}], (max-width: 900px)", "not all", true}, |
| 119 {"[]((), (max-width: 900px)", "not all"}, | 140 {"[]((), (max-width: 900px)", "not all", true}, |
| 120 {"[](()), (max-width: 900px)", "not all, (max-width: 900px)"}, | 141 {"((), (max-width: 900px)", "not all", true}, |
| 121 {"all an[isdfs bla())()]icalc(i)(()), (max-width: 900px)", "not all, (ma
x-width: 900px)"}, | 142 {"(foo(), (max-width: 900px)", "not all", true}, |
| 122 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 900px)", "not all"}, | 143 {"[](()), (max-width: 900px)", "not all, (max-width: 900px)", true}, |
| 123 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 900px)", "not all"}, | 144 {"all an[isdfs bla())()]icalc(i)(()), (max-width: 400px)", "not all, (ma
x-width: 400px)", true}, |
| 124 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 900px)", "not all, (m
ax-width: 900px)"}, | 145 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", "not all", tru
e}, |
| 146 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", "not all", tr
ue}, |
| 147 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", "not all, (m
ax-width: 800px)", true}, |
| 125 {0, 0} // Do not remove the terminator line. | 148 {0, 0} // Do not remove the terminator line. |
| 126 }; | 149 }; |
| 127 | 150 |
| 128 for (unsigned i = 0; testCases[i].input; ++i) { | 151 for (unsigned i = 0; testCases[i].input; ++i) { |
| 129 RefPtrWillBeRawPtr<MediaQuerySet> querySet = MediaQuerySet::create(testC
ases[i].input); | 152 RefPtrWillBeRawPtr<MediaQuerySet> oldParserQuerySet = MediaQuerySet::cre
ate(testCases[i].input); |
| 130 StringBuilder output; | 153 RefPtrWillBeRawPtr<MediaQuerySet> threadSafeQuerySet = MediaQuerySet::cr
eateOffMainThread(testCases[i].input); |
| 131 size_t j = 0; | 154 testMediaQuery(testCases[i], *oldParserQuerySet, true); |
| 132 while (j < querySet->queryVector().size()) { | 155 testMediaQuery(testCases[i], *threadSafeQuerySet, false); |
| 133 String queryText = querySet->queryVector()[j]->cssText(); | |
| 134 output.append(queryText); | |
| 135 ++j; | |
| 136 if (j >= querySet->queryVector().size()) | |
| 137 break; | |
| 138 output.append(", "); | |
| 139 } | |
| 140 if (testCases[i].output) | |
| 141 ASSERT_STREQ(testCases[i].output, output.toString().ascii().data()); | |
| 142 else | |
| 143 ASSERT_STREQ(testCases[i].input, output.toString().ascii().data()); | |
| 144 } | 156 } |
| 145 } | 157 } |
| 146 | 158 |
| 147 } // namespace | 159 } // namespace |
| OLD | NEW |