| 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/parser/MediaQueryTokenizer.h" | 6 #include "core/css/parser/MediaQueryTokenizer.h" |
| 7 | 7 |
| 8 #include "core/css/parser/MediaQueryBlockWatcher.h" |
| 8 #include "wtf/PassOwnPtr.h" | 9 #include "wtf/PassOwnPtr.h" |
| 9 #include <gtest/gtest.h> | 10 #include <gtest/gtest.h> |
| 10 | 11 |
| 11 namespace WebCore { | 12 namespace WebCore { |
| 12 | 13 |
| 13 typedef struct { | 14 typedef struct { |
| 14 const char* input; | 15 const char* input; |
| 15 const char* output; | 16 const char* output; |
| 16 } TestCase; | 17 } TestCase; |
| 17 | 18 |
| 19 typedef struct { |
| 20 const char* input; |
| 21 const unsigned maxLevel; |
| 22 const unsigned finalLevel; |
| 23 } BlockTestCase; |
| 24 |
| 18 TEST(MediaQueryTokenizerTest, Basic) | 25 TEST(MediaQueryTokenizerTest, Basic) |
| 19 { | 26 { |
| 20 TestCase testCases[] = { | 27 TestCase testCases[] = { |
| 21 { "(max-width: 50px)", "(max-width: 50px)" }, | 28 { "(max-width: 50px)", "(max-width: 50px)" }, |
| 22 { "(max-width: 50\\70\\78)", "(max-width: 50px)" }, | 29 { "(max-width: 50\\70\\78)", "(max-width: 50px)" }, |
| 23 { "(max-width: /* comment */50px)", "(max-width: 50px)" }, | 30 { "(max-width: /* comment */50px)", "(max-width: 50px)" }, |
| 24 { "(max-width: /** *commen*t */60px)", "(max-width: 60px)" }, | 31 { "(max-width: /** *commen*t */60px)", "(max-width: 60px)" }, |
| 25 { "(max-width: /** *commen*t **/70px)", "(max-width: 70px)" }, | 32 { "(max-width: /** *commen*t **/70px)", "(max-width: 70px)" }, |
| 26 { "(max-width: /** *commen*t **//**/80px)", "(max-width: 80px)" }, | 33 { "(max-width: /** *commen*t **//**/80px)", "(max-width: 80px)" }, |
| 27 { "(max-width: /*/ **/90px)", "(max-width: 90px)" }, | 34 { "(max-width: /*/ **/90px)", "(max-width: 90px)" }, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 44 for (int i = 0; testCases[i].input; ++i) { | 51 for (int i = 0; testCases[i].input; ++i) { |
| 45 Vector<MediaQueryToken> tokens; | 52 Vector<MediaQueryToken> tokens; |
| 46 MediaQueryTokenizer::tokenize(testCases[i].input, tokens); | 53 MediaQueryTokenizer::tokenize(testCases[i].input, tokens); |
| 47 StringBuilder output; | 54 StringBuilder output; |
| 48 for (size_t j = 0; j < tokens.size(); ++j) | 55 for (size_t j = 0; j < tokens.size(); ++j) |
| 49 output.append(tokens[j].textForUnitTests()); | 56 output.append(tokens[j].textForUnitTests()); |
| 50 ASSERT_STREQ(testCases[i].output, output.toString().ascii().data()); | 57 ASSERT_STREQ(testCases[i].output, output.toString().ascii().data()); |
| 51 } | 58 } |
| 52 } | 59 } |
| 53 | 60 |
| 61 TEST(MediaQueryTokenizerBlockTest, Basic) |
| 62 { |
| 63 BlockTestCase testCases[] = { |
| 64 {"(max-width: 800px()), (max-width: 800px)", 2, 0}, |
| 65 {"(max-width: 900px(()), (max-width: 900px)", 3, 1}, |
| 66 {"(max-width: 600px(())))), (max-width: 600px)", 3, 0}, |
| 67 {"(max-width: 500px(((((((((())))), (max-width: 500px)", 11, 6}, |
| 68 {"(max-width: 800px[]), (max-width: 800px)", 2, 0}, |
| 69 {"(max-width: 900px[[]), (max-width: 900px)", 3, 2}, |
| 70 {"(max-width: 600px[[]]]]), (max-width: 600px)", 3, 0}, |
| 71 {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", 11, 7}, |
| 72 {"(max-width: 800px{}), (max-width: 800px)", 2, 0}, |
| 73 {"(max-width: 900px{{}), (max-width: 900px)", 3, 2}, |
| 74 {"(max-width: 600px{{}}}}), (max-width: 600px)", 3, 0}, |
| 75 {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", 11, 7}, |
| 76 {"[(), (max-width: 400px)", 2, 1}, |
| 77 {"[{}, (max-width: 500px)", 2, 1}, |
| 78 {"[{]}], (max-width: 900px)", 2, 0}, |
| 79 {"[{[]{}{{{}}}}], (max-width: 900px)", 5, 0}, |
| 80 {"[{[}], (max-width: 900px)", 3, 2}, |
| 81 {"[({)}], (max-width: 900px)", 3, 2}, |
| 82 {"[]((), (max-width: 900px)", 2, 1}, |
| 83 {"((), (max-width: 900px)", 2, 1}, |
| 84 {"(foo(), (max-width: 900px)", 2, 1}, |
| 85 {"[](()), (max-width: 900px)", 2, 0}, |
| 86 {"all an[isdfs bla())(i())]icalc(i)(()), (max-width: 400px)", 3, 0}, |
| 87 {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", 4, 2}, |
| 88 {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", 4, 1}, |
| 89 {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", 4, 0}, |
| 90 {0, 0, 0} // Do not remove the terminator line. |
| 91 }; |
| 92 for (int i = 0; testCases[i].input; ++i) { |
| 93 Vector<MediaQueryToken> tokens; |
| 94 MediaQueryTokenizer::tokenize(testCases[i].input, tokens); |
| 95 MediaQueryBlockWatcher blockWatcher; |
| 96 |
| 97 unsigned maxLevel = 0; |
| 98 unsigned level = 0; |
| 99 for (size_t j = 0; j < tokens.size(); ++j) { |
| 100 blockWatcher.handleToken(tokens[j]); |
| 101 level = blockWatcher.blockLevel(); |
| 102 maxLevel = std::max(level, maxLevel); |
| 103 } |
| 104 ASSERT_EQ(testCases[i].maxLevel, maxLevel); |
| 105 ASSERT_EQ(testCases[i].finalLevel, level); |
| 106 } |
| 107 } |
| 108 |
| 54 void testToken(UChar c, MediaQueryTokenType tokenType) | 109 void testToken(UChar c, MediaQueryTokenType tokenType) |
| 55 { | 110 { |
| 56 Vector<MediaQueryToken> tokens; | 111 Vector<MediaQueryToken> tokens; |
| 57 StringBuilder input; | 112 StringBuilder input; |
| 58 input.append(c); | 113 input.append(c); |
| 59 MediaQueryTokenizer::tokenize(input.toString(), tokens); | 114 MediaQueryTokenizer::tokenize(input.toString(), tokens); |
| 60 ASSERT_EQ(tokens[0].type(), tokenType); | 115 ASSERT_EQ(tokens[0].type(), tokenType); |
| 61 } | 116 } |
| 62 | 117 |
| 63 TEST(MediaQueryTokenizerCodepointsTest, Basic) | 118 TEST(MediaQueryTokenizerCodepointsTest, Basic) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 testToken(c, EOFToken); | 152 testToken(c, EOFToken); |
| 98 else if (c > SCHAR_MAX) | 153 else if (c > SCHAR_MAX) |
| 99 testToken(c, IdentToken); | 154 testToken(c, IdentToken); |
| 100 else | 155 else |
| 101 testToken(c, DelimiterToken); | 156 testToken(c, DelimiterToken); |
| 102 } | 157 } |
| 103 testToken(USHRT_MAX, IdentToken); | 158 testToken(USHRT_MAX, IdentToken); |
| 104 } | 159 } |
| 105 | 160 |
| 106 } // namespace | 161 } // namespace |
| OLD | NEW |