Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / parser / MediaQueryTokenizerTest.cpp
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
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/css/parser/MediaQueryTokenizer.h"
7
8 #include "core/css/parser/MediaQueryBlockWatcher.h"
9 #include "wtf/PassOwnPtr.h"
10 #include <gtest/gtest.h>
11
12 namespace WebCore {
13
14 typedef struct {
15     const char* input;
16     const char* output;
17 } TestCase;
18
19 typedef struct {
20     const char* input;
21     const unsigned maxLevel;
22     const unsigned finalLevel;
23 } BlockTestCase;
24
25 TEST(MediaQueryTokenizerTest, Basic)
26 {
27     TestCase testCases[] = {
28         { "(max-width: 50px)", "(max-width: 50px)" },
29         { "(max-width: 1e+2px)", "(max-width: 100px)" },
30         { "(max-width: 1e2px)", "(max-width: 100px)" },
31         { "(max-width: 1000e-1px)", "(max-width: 100px)" },
32         { "(max-width: 50\\70\\78)", "(max-width: 50px)" },
33         { "(max-width: /* comment */50px)", "(max-width: 50px)" },
34         { "(max-width: /** *commen*t */60px)", "(max-width: 60px)" },
35         { "(max-width: /** *commen*t **/70px)", "(max-width: 70px)" },
36         { "(max-width: /** *commen*t **//**/80px)", "(max-width: 80px)" },
37         { "(max-width: /*/ **/90px)", "(max-width: 90px)" },
38         { "(max-width: /*/ **/*100px)", "(max-width: *100px)" },
39         { "(max-width: 110px/*)", "(max-width: 110px" },
40         { "(max-width: 120px)/*", "(max-width: 120px)" },
41         { "(max-width: 130px)/**", "(max-width: 130px)" },
42         { "(max-width: /***/140px)/**/", "(max-width: 140px)" },
43         { "(max-width: '40px')", "(max-width: 40px)" },
44         { "(max-width: '40px", "(max-width: 40px" },
45         { "(max-width: '40px\n", "(max-width:  " },
46         { "(max-width: '40px\\", "(max-width: 40px" },
47         { "(max-width: '40px\\\n", "(max-width: 40px" },
48         { "(max-width: '40px\\\n')", "(max-width: 40px)" },
49         { "(max-width: '40\\70\\78')", "(max-width: 40px)" },
50         { "(max-width: '40\\\npx')", "(max-width: 40px)" },
51         { 0, 0 } // Do not remove the terminator line.
52     };
53
54     for (int i = 0; testCases[i].input; ++i) {
55         Vector<MediaQueryToken> tokens;
56         MediaQueryTokenizer::tokenize(testCases[i].input, tokens);
57         StringBuilder output;
58         for (size_t j = 0; j < tokens.size(); ++j)
59             output.append(tokens[j].textForUnitTests());
60         ASSERT_STREQ(testCases[i].output, output.toString().ascii().data());
61     }
62 }
63
64 TEST(MediaQueryTokenizerBlockTest, Basic)
65 {
66     BlockTestCase testCases[] = {
67         {"(max-width: 800px()), (max-width: 800px)", 2, 0},
68         {"(max-width: 900px(()), (max-width: 900px)", 3, 1},
69         {"(max-width: 600px(())))), (max-width: 600px)", 3, 0},
70         {"(max-width: 500px(((((((((())))), (max-width: 500px)", 11, 6},
71         {"(max-width: 800px[]), (max-width: 800px)", 2, 0},
72         {"(max-width: 900px[[]), (max-width: 900px)", 3, 2},
73         {"(max-width: 600px[[]]]]), (max-width: 600px)", 3, 0},
74         {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", 11, 7},
75         {"(max-width: 800px{}), (max-width: 800px)", 2, 0},
76         {"(max-width: 900px{{}), (max-width: 900px)", 3, 2},
77         {"(max-width: 600px{{}}}}), (max-width: 600px)", 3, 0},
78         {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", 11, 7},
79         {"[(), (max-width: 400px)", 2, 1},
80         {"[{}, (max-width: 500px)", 2, 1},
81         {"[{]}], (max-width: 900px)", 2, 0},
82         {"[{[]{}{{{}}}}], (max-width: 900px)", 5, 0},
83         {"[{[}], (max-width: 900px)", 3, 2},
84         {"[({)}], (max-width: 900px)", 3, 2},
85         {"[]((), (max-width: 900px)", 2, 1},
86         {"((), (max-width: 900px)", 2, 1},
87         {"(foo(), (max-width: 900px)", 2, 1},
88         {"[](()), (max-width: 900px)", 2, 0},
89         {"all an[isdfs bla())(i())]icalc(i)(()), (max-width: 400px)", 3, 0},
90         {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", 4, 2},
91         {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", 4, 1},
92         {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", 4, 0},
93         {0, 0, 0} // Do not remove the terminator line.
94     };
95     for (int i = 0; testCases[i].input; ++i) {
96         Vector<MediaQueryToken> tokens;
97         MediaQueryTokenizer::tokenize(testCases[i].input, tokens);
98         MediaQueryBlockWatcher blockWatcher;
99
100         unsigned maxLevel = 0;
101         unsigned level = 0;
102         for (size_t j = 0; j < tokens.size(); ++j) {
103             blockWatcher.handleToken(tokens[j]);
104             level = blockWatcher.blockLevel();
105             maxLevel = std::max(level, maxLevel);
106         }
107         ASSERT_EQ(testCases[i].maxLevel, maxLevel);
108         ASSERT_EQ(testCases[i].finalLevel, level);
109     }
110 }
111
112 void testToken(UChar c, MediaQueryTokenType tokenType)
113 {
114     Vector<MediaQueryToken> tokens;
115     StringBuilder input;
116     input.append(c);
117     MediaQueryTokenizer::tokenize(input.toString(), tokens);
118     ASSERT_EQ(tokens[0].type(), tokenType);
119 }
120
121 TEST(MediaQueryTokenizerCodepointsTest, Basic)
122 {
123     for (UChar c = 0; c <= 1000; ++c) {
124         if (isASCIIDigit(c))
125             testToken(c, NumberToken);
126         else if (isASCIIAlpha(c))
127             testToken(c, IdentToken);
128         else if (c == '_')
129             testToken(c, IdentToken);
130         else if (c == '\r' || c == ' ' || c == '\n' || c == '\t' || c == '\f')
131             testToken(c, WhitespaceToken);
132         else if (c == '(')
133             testToken(c, LeftParenthesisToken);
134         else if (c == ')')
135             testToken(c, RightParenthesisToken);
136         else if (c == '[')
137             testToken(c, LeftBracketToken);
138         else if (c == ']')
139             testToken(c, RightBracketToken);
140         else if (c == '{')
141             testToken(c, LeftBraceToken);
142         else if (c == '}')
143             testToken(c, RightBraceToken);
144         else if (c == '.' || c == '+' || c == '-' || c == '/' || c == '\\')
145             testToken(c, DelimiterToken);
146         else if (c == '\'' || c == '"')
147             testToken(c, StringToken);
148         else if (c == ',')
149             testToken(c, CommaToken);
150         else if (c == ':')
151             testToken(c, ColonToken);
152         else if (c == ';')
153             testToken(c, SemicolonToken);
154         else if (!c)
155             testToken(c, EOFToken);
156         else if (c > SCHAR_MAX)
157             testToken(c, IdentToken);
158         else
159             testToken(c, DelimiterToken);
160     }
161     testToken(USHRT_MAX, IdentToken);
162 }
163
164 } // namespace