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/MediaQuery.h"
7
8#include "core/css/MediaList.h"
9#include "wtf/PassOwnPtr.h"
10#include "wtf/text/StringBuilder.h"
11
12#include <gtest/gtest.h>
13
14namespace blink {
15
16typedef struct {
17    const char* input;
18    const char* output;
19    bool shouldWorkOnOldParser;
20} TestCase;
21
22static void testMediaQuery(TestCase test, MediaQuerySet& querySet, bool oldParser)
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.appendLiteral(", ");
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
42TEST(MediaQuerySetTest, Basic)
43{
44    // The first string represents the input string.
45    // The second string represents the output string, if present.
46    // Otherwise, the output string is identical to the first string.
47    TestCase testCases[] = {
48        {"", 0, true},
49        {" ", "", true},
50        {"screen", 0, true},
51        {"screen and (color)", 0, true},
52        {"all and (min-width:500px)", "(min-width: 500px)", true},
53        {"all and (min-width:/*bla*/500px)", "(min-width: 500px)", true},
54        {"(min-width:500px)", "(min-width: 500px)", true},
55        {"screen and (color), projection and (color)", 0, true},
56        {"not screen and (color)", 0, true},
57        {"only screen and (color)", 0, true},
58        {"screen and (color), projection and (color)", 0, true},
59        {"aural and (device-aspect-ratio: 16/9)", 0, true},
60        {"speech and (min-device-width: 800px)", 0, true},
61        {"example", 0, true},
62        {"screen and (max-weight: 3kg) and (color), (monochrome)", "not all, (monochrome)", true},
63        {"(min-width: -100px)", "not all", true},
64        {"(example, all,), speech", "not all, speech", true},
65        {"&test, screen", "not all, screen", true},
66        {"print and (min-width: 25cm)", 0, true},
67        {"screen and (min-width: 400px) and (max-width: 700px)", "screen and (max-width: 700px) and (min-width: 400px)", true},
68        {"screen and (device-width: 800px)", 0, true},
69        {"screen and (device-height: 60em)", 0, true},
70        {"screen and (device-height: 60rem)", 0, true},
71        {"screen and (device-height: 60ch)", 0, true},
72        {"screen and (device-aspect-ratio: 16/9)", 0, true},
73        {"(device-aspect-ratio: 16.0/9.0)", "not all", true},
74        {"(device-aspect-ratio: 16/ 9)", "(device-aspect-ratio: 16/9)", true},
75        {"(device-aspect-ratio: 16/\r9)", "(device-aspect-ratio: 16/9)", true},
76        {"all and (color)", "(color)", true},
77        {"all and (min-color: 1)", "(min-color: 1)", true},
78        {"all and (min-color: 1.0)", "not all", true},
79        {"all and (min-color: 2)", "(min-color: 2)", true},
80        {"all and (color-index)", "(color-index)", true},
81        {"all and (min-color-index: 1)", "(min-color-index: 1)", true},
82        {"all and (monochrome)", "(monochrome)", true},
83        {"all and (min-monochrome: 1)", "(min-monochrome: 1)", true},
84        {"all and (min-monochrome: 2)", "(min-monochrome: 2)", true},
85        {"print and (monochrome)", 0, true},
86        {"handheld and (grid) and (max-width: 15em)", 0, true},
87        {"handheld and (grid) and (max-device-height: 7em)", 0, true},
88        {"screen and (max-width: 50%)", "not all", true},
89        {"screen and (max-WIDTH: 500px)", "screen and (max-width: 500px)", true},
90        {"screen and (max-width: 24.4em)", 0, true},
91        {"screen and (max-width: 24.4EM)", "screen and (max-width: 24.4em)", true},
92        {"screen and (max-width: blabla)", "not all", true},
93        {"screen and (max-width: 1)", "not all", true},
94        {"screen and (max-width: 0)", 0, true},
95        {"screen and (max-width: 1deg)", "not all", true},
96        {"handheld and (min-width: 20em), \nscreen and (min-width: 20em)", "handheld and (min-width: 20em), screen and (min-width: 20em)", true},
97        {"print and (min-resolution: 300dpi)", 0, true},
98        {"print and (min-resolution: 118dpcm)", 0, true},
99        {"(resolution: 0.83333333333333333333dppx)", "(resolution: 0.833333333333333dppx)", true},
100        {"(resolution: 2.4dppx)", 0, true},
101        {"all and(color)", "not all", true},
102        {"all and (", "not all", true},
103        {"test;,all", "not all, all", true},
104        {"(color:20example)", "not all", false},
105        {"not braille", 0, true},
106        {",screen", "not all, screen", true},
107        {",all", "not all, all", true},
108        {",,all,,", "not all, not all, all, not all, not all", true},
109        {",,all,, ", "not all, not all, all, not all, not all", true},
110        {",screen,,&invalid,,", "not all, screen, not all, not all, not all, not all", true},
111        {",screen,,(invalid,),,", "not all, screen, not all, not all, not all, not all", true},
112        {",(all,),,", "not all, not all, not all, not all", true},
113        {",", "not all, not all", true},
114        {"  ", "", true},
115        {"(color", "(color)", true},
116        {"(min-color: 2", "(min-color: 2)", true},
117        {"(orientation: portrait)", 0, true},
118        {"tv and (scan: progressive)", 0, true},
119        {"(pointer: coarse)", 0, true},
120        {"(min-orientation:portrait)", "not all", true},
121        {"all and (orientation:portrait)", "(orientation: portrait)", true},
122        {"all and (orientation:landscape)", "(orientation: landscape)", true},
123        {"NOT braille, tv AND (max-width: 200px) and (min-WIDTH: 100px) and (orientation: landscape), (color)",
124            "not braille, tv and (max-width: 200px) and (min-width: 100px) and (orientation: landscape), (color)", true},
125        {"(m\\61x-width: 300px)", "(max-width: 300px)", true},
126        {"(max-width: 400\\70\\78)", "(max-width: 400px)", false},
127        {"(max-width: 500\\0070\\0078)", "(max-width: 500px)", false},
128        {"(max-width: 600\\000070\\000078)", "(max-width: 600px)", false},
129        {"(max-width: 700px), (max-width: 700px)", "(max-width: 700px), (max-width: 700px)", true},
130        {"(max-width: 800px()), (max-width: 800px)", "not all, (max-width: 800px)", true},
131        {"(max-width: 900px(()), (max-width: 900px)", "not all", true},
132        {"(max-width: 600px(())))), (max-width: 600px)", "not all, (max-width: 600px)", true},
133        {"(max-width: 500px(((((((((())))), (max-width: 500px)", "not all", true},
134        {"(max-width: 800px[]), (max-width: 800px)", "not all, (max-width: 800px)", true},
135        {"(max-width: 900px[[]), (max-width: 900px)", "not all", true},
136        {"(max-width: 600px[[]]]]), (max-width: 600px)", "not all, (max-width: 600px)", true},
137        {"(max-width: 500px[[[[[[[[[[]]]]), (max-width: 500px)", "not all", true},
138        {"(max-width: 800px{}), (max-width: 800px)", "not all, (max-width: 800px)", true},
139        {"(max-width: 900px{{}), (max-width: 900px)", "not all", true},
140        {"(max-width: 600px{{}}}}), (max-width: 600px)", "not all, (max-width: 600px)", true},
141        {"(max-width: 500px{{{{{{{{{{}}}}), (max-width: 500px)", "not all", true},
142        {"[(), (max-width: 400px)", "not all", true},
143        {"[{}, (max-width: 500px)", "not all", true},
144        {"[{]}], (max-width: 900px)", "not all, (max-width: 900px)", true},
145        {"[{[]{}{{{}}}}], (max-width: 900px)", "not all, (max-width: 900px)", true},
146        {"[{[}], (max-width: 900px)", "not all", true},
147        {"[({)}], (max-width: 900px)", "not all", true},
148        {"[]((), (max-width: 900px)", "not all", true},
149        {"((), (max-width: 900px)", "not all", true},
150        {"(foo(), (max-width: 900px)", "not all", true},
151        {"[](()), (max-width: 900px)", "not all, (max-width: 900px)", true},
152        {"all an[isdfs bla())()]icalc(i)(()), (max-width: 400px)", "not all, (max-width: 400px)", true},
153        {"all an[isdfs bla())(]icalc(i)(()), (max-width: 500px)", "not all", true},
154        {"all an[isdfs bla())(]icalc(i)(())), (max-width: 600px)", "not all", true},
155        {"all an[isdfs bla())(]icalc(i)(()))], (max-width: 800px)", "not all, (max-width: 800px)", true},
156        {"(max-width: '40px')", "not all", true},
157        {"('max-width': 40px)", "not all", true},
158        {"'\"'\", (max-width: 900px)", "not all", true},
159        {"'\"\"\"', (max-width: 900px)", "not all, (max-width: 900px)", true},
160        {"\"'\"', (max-width: 900px)", "not all", true},
161        {"\"'''\", (max-width: 900px)", "not all, (max-width: 900px)", true},
162        {0, 0} // Do not remove the terminator line.
163    };
164
165    for (unsigned i = 0; testCases[i].input; ++i) {
166        RefPtrWillBeRawPtr<MediaQuerySet> oldParserQuerySet = MediaQuerySet::create(testCases[i].input);
167        RefPtrWillBeRawPtr<MediaQuerySet> threadSafeQuerySet = MediaQuerySet::createOffMainThread(testCases[i].input);
168        testMediaQuery(testCases[i], *oldParserQuerySet, true);
169        testMediaQuery(testCases[i], *threadSafeQuerySet, false);
170    }
171}
172
173} // namespace
174