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/MediaQueryEvaluator.h" | 6 #include "core/css/MediaQueryEvaluator.h" |
7 | 7 |
| 8 #include "core/MediaTypeNames.h" |
8 #include "core/css/MediaList.h" | 9 #include "core/css/MediaList.h" |
9 #include "core/css/MediaValuesCached.h" | 10 #include "core/css/MediaValuesCached.h" |
| 11 #include "core/frame/FrameView.h" |
| 12 #include "core/testing/DummyPageHolder.h" |
10 #include "wtf/PassOwnPtr.h" | 13 #include "wtf/PassOwnPtr.h" |
11 #include "wtf/text/StringBuilder.h" | 14 #include "wtf/text/StringBuilder.h" |
12 | 15 |
13 #include <gtest/gtest.h> | 16 #include <gtest/gtest.h> |
14 | 17 |
15 namespace WebCore { | 18 namespace WebCore { |
16 | 19 |
17 typedef struct { | 20 typedef struct { |
18 const char* input; | 21 const char* input; |
19 const bool output; | 22 const bool output; |
20 } TestCase; | 23 } TestCase; |
21 | 24 |
22 TEST(MediaQueryEvaluatorTest, Basic) | 25 TestCase screenTestCases[] = { |
| 26 {"", 1}, |
| 27 {" ", 1}, |
| 28 {"screen", 1}, |
| 29 {"screen and (color)", 1}, |
| 30 {"not screen and (color)", 0}, |
| 31 {"screen and (device-aspect-ratio: 16/9)", 0}, |
| 32 {"screen and (device-aspect-ratio: 1/1)", 1}, |
| 33 {"all and (min-color: 2)", 1}, |
| 34 {"all and (min-color: 32)", 0}, |
| 35 {"all and (min-color-index: 0)", 1}, |
| 36 {"all and (min-color-index: 1)", 0}, |
| 37 {"all and (monochrome)", 0}, |
| 38 {"all and (min-monochrome: 0)", 1}, |
| 39 {"all and (grid: 0)", 1}, |
| 40 {"(resolution: 2dppx)", 1}, |
| 41 {"(resolution: 1dppx)", 0}, |
| 42 {"(orientation: portrait)", 1}, |
| 43 {"(orientation: landscape)", 0}, |
| 44 {"tv and (scan: progressive)", 0}, |
| 45 {"(pointer: coarse)", 0}, |
| 46 {"(pointer: fine)", 1}, |
| 47 {"(hover: 1)", 1}, |
| 48 {"(hover: 0)", 0}, |
| 49 {0, 0} // Do not remove the terminator line. |
| 50 }; |
| 51 |
| 52 TestCase viewportTestCases[] = { |
| 53 {"all and (min-width: 500px)", 1}, |
| 54 {"(min-width: 500px)", 1}, |
| 55 {"(min-width: 501px)", 0}, |
| 56 {"(max-width: 500px)", 1}, |
| 57 {"(max-width: 499px)", 0}, |
| 58 {"(width: 500px)", 1}, |
| 59 {"(width: 501px)", 0}, |
| 60 {"(min-height: 500px)", 1}, |
| 61 {"(min-height: 501px)", 0}, |
| 62 {"(max-height: 500px)", 1}, |
| 63 {"(max-height: 499px)", 0}, |
| 64 {"(height: 500px)", 1}, |
| 65 {"(height: 501px)", 0}, |
| 66 {"screen and (min-width: 400px) and (max-width: 700px)", 1}, |
| 67 {0, 0} // Do not remove the terminator line. |
| 68 }; |
| 69 |
| 70 TestCase printTestCases[] = { |
| 71 {"print and (min-resolution: 1dppx)", 1}, |
| 72 {"print and (min-resolution: 118dpcm)", 1}, |
| 73 {"print and (min-resolution: 119dpcm)", 0}, |
| 74 {0, 0} // Do not remove the terminator line. |
| 75 }; |
| 76 |
| 77 void testMQEvaluator(TestCase* testCases, const MediaQueryEvaluator& mediaQueryE
valuator) |
23 { | 78 { |
24 // The first string represents the input string. | 79 for (unsigned i = 0; testCases[i].input; ++i) { |
25 // The second string represents the output string, if present. | 80 RefPtrWillBeRawPtr<MediaQuerySet> querySet = MediaQuerySet::create(testC
ases[i].input); |
26 // Otherwise, the output string is identical to the first string. | 81 ASSERT_EQ(testCases[i].output, mediaQueryEvaluator.eval(querySet.get()))
; |
27 TestCase screenTestCases[] = { | 82 } |
28 {"", 1}, | 83 } |
29 {" ", 1}, | |
30 {"screen", 1}, | |
31 {"screen and (color)", 1}, | |
32 {"all and (min-width: 500px)", 1}, | |
33 {"not screen and (color)", 0}, | |
34 {"(min-width: 500px)", 1}, | |
35 {"(min-width: 501px)", 0}, | |
36 {"(max-width: 500px)", 1}, | |
37 {"(max-width: 499px)", 0}, | |
38 {"(width: 500px)", 1}, | |
39 {"(width: 501px)", 0}, | |
40 {"(min-height: 500px)", 1}, | |
41 {"(min-height: 501px)", 0}, | |
42 {"(max-height: 500px)", 1}, | |
43 {"(max-height: 499px)", 0}, | |
44 {"(height: 500px)", 1}, | |
45 {"(height: 501px)", 0}, | |
46 {"screen and (min-width: 400px) and (max-width: 700px)", 1}, | |
47 {"screen and (device-aspect-ratio: 16/9)", 0}, | |
48 {"screen and (device-aspect-ratio: 1/1)", 1}, | |
49 {"all and (min-color: 2)", 1}, | |
50 {"all and (min-color: 32)", 0}, | |
51 {"all and (min-color-index: 0)", 1}, | |
52 {"all and (min-color-index: 1)", 0}, | |
53 {"all and (monochrome)", 0}, | |
54 {"all and (min-monochrome: 0)", 1}, | |
55 {"all and (grid: 0)", 1}, | |
56 {"(resolution: 2dppx)", 1}, | |
57 {"(resolution: 1dppx)", 0}, | |
58 {"(orientation: portrait)", 1}, | |
59 {"(orientation: landscape)", 0}, | |
60 {"tv and (scan: progressive)", 0}, | |
61 {"(pointer: coarse)", 0}, | |
62 {"(pointer: fine)", 1}, | |
63 {"(hover: 1)", 1}, | |
64 {"(hover: 0)", 0}, | |
65 {0, 0} // Do not remove the terminator line. | |
66 }; | |
67 TestCase printTestCases[] = { | |
68 {"print and (min-resolution: 1dppx)", 1}, | |
69 {"print and (min-resolution: 118dpcm)", 0}, | |
70 {0, 0} // Do not remove the terminator line. | |
71 }; | |
72 | 84 |
| 85 TEST(MediaQueryEvaluatorTest, Cached) |
| 86 { |
73 MediaValuesCached::MediaValuesCachedData data; | 87 MediaValuesCached::MediaValuesCachedData data; |
74 data.viewportWidth = 500; | 88 data.viewportWidth = 500; |
75 data.viewportHeight = 500; | 89 data.viewportHeight = 500; |
76 data.deviceWidth = 500; | 90 data.deviceWidth = 500; |
77 data.deviceHeight = 500; | 91 data.deviceHeight = 500; |
78 data.devicePixelRatio = 2.0; | 92 data.devicePixelRatio = 2.0; |
79 data.colorBitsPerComponent = 24; | 93 data.colorBitsPerComponent = 24; |
80 data.monochromeBitsPerComponent = 0; | 94 data.monochromeBitsPerComponent = 0; |
81 data.pointer = MediaValues::MousePointer; | 95 data.pointer = MediaValues::MousePointer; |
82 data.defaultFontSize = 16; | 96 data.defaultFontSize = 16; |
83 data.threeDEnabled = true; | 97 data.threeDEnabled = true; |
84 data.scanMediaType = false; | 98 data.mediaType = MediaTypeNames::screen; |
85 data.screenMediaType = true; | |
86 data.printMediaType = false; | |
87 data.strictMode = true; | 99 data.strictMode = true; |
88 RefPtr<MediaValues> mediaValues = MediaValuesCached::create(data); | 100 RefPtr<MediaValues> mediaValues = MediaValuesCached::create(data); |
89 | 101 |
90 for (unsigned i = 0; screenTestCases[i].input; ++i) { | 102 MediaQueryEvaluator mediaQueryEvaluator(*mediaValues); |
91 RefPtrWillBeRawPtr<MediaQuerySet> querySet = MediaQuerySet::create(scree
nTestCases[i].input); | 103 testMQEvaluator(screenTestCases, mediaQueryEvaluator); |
92 MediaQueryEvaluator mediaQueryEvaluator("screen", *mediaValues); | 104 testMQEvaluator(viewportTestCases, mediaQueryEvaluator); |
93 ASSERT_EQ(screenTestCases[i].output, mediaQueryEvaluator.eval(querySet.g
et())); | 105 |
94 } | 106 data.mediaType = MediaTypeNames::print; |
95 for (unsigned i = 0; printTestCases[i].input; ++i) { | 107 mediaValues = MediaValuesCached::create(data); |
96 RefPtrWillBeRawPtr<MediaQuerySet> querySet = MediaQuerySet::create(print
TestCases[i].input); | 108 MediaQueryEvaluator printMediaQueryEvaluator(*mediaValues); |
97 MediaQueryEvaluator mediaQueryEvaluator("print", *mediaValues); | 109 testMQEvaluator(printTestCases, printMediaQueryEvaluator); |
98 ASSERT_EQ(printTestCases[i].output, mediaQueryEvaluator.eval(querySet.ge
t())); | |
99 } | |
100 } | 110 } |
101 | 111 |
| 112 TEST(MediaQueryEvaluatorTest, Dynamic) |
| 113 { |
| 114 OwnPtr<DummyPageHolder> pageHolder = DummyPageHolder::create(IntSize(500, 50
0)); |
| 115 pageHolder->frameView().setMediaType(MediaTypeNames::screen); |
| 116 |
| 117 MediaQueryEvaluator mediaQueryEvaluator(&pageHolder->frame()); |
| 118 testMQEvaluator(viewportTestCases, mediaQueryEvaluator); |
| 119 pageHolder->frameView().setMediaType(MediaTypeNames::print); |
| 120 testMQEvaluator(printTestCases, mediaQueryEvaluator); |
| 121 } |
102 } // namespace | 122 } // namespace |
OLD | NEW |