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/SizesCalcParser.h" | 6 #include "core/css/parser/SizesCalcParser.h" |
7 | 7 |
8 #include "core/MediaTypeNames.h" | 8 #include "core/MediaTypeNames.h" |
9 #include "core/css/CSSPrimitiveValue.h" | 9 #include "core/css/CSSPrimitiveValue.h" |
10 #include "core/css/MediaValuesCached.h" | 10 #include "core/css/MediaValuesCached.h" |
11 #include "core/css/parser/CSSParser.h" | 11 #include "core/css/parser/CSSParser.h" |
12 #include "core/css/parser/MediaQueryTokenizer.h" | 12 #include "core/css/parser/MediaQueryTokenizer.h" |
13 | 13 |
14 #include <gtest/gtest.h> | 14 #include <gtest/gtest.h> |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 struct TestCase { | 18 struct TestCase { |
19 const char* input; | 19 const char* input; |
20 const unsigned output; | 20 const float output; |
21 const bool valid; | 21 const bool valid; |
22 const bool dontRunInCSSCalc; | 22 const bool dontRunInCSSCalc; |
23 const bool viewportDependant; | 23 const bool viewportDependant; |
24 }; | 24 }; |
25 | 25 |
26 static void initLengthArray(CSSLengthArray& lengthArray) | 26 static void initLengthArray(CSSLengthArray& lengthArray) |
27 { | 27 { |
28 lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount); | 28 lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount); |
29 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i) | 29 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i) |
30 lengthArray.at(i) = 0; | 30 lengthArray.at(i) = 0; |
31 } | 31 } |
32 | 32 |
33 static void verifyCSSCalc(String text, double value, bool valid, unsigned fontSi
ze, unsigned viewportWidth, unsigned viewportHeight) | 33 static void verifyCSSCalc(String text, double value, bool valid, unsigned fontSi
ze, unsigned viewportWidth, unsigned viewportHeight) |
34 { | 34 { |
35 CSSLengthArray lengthArray; | 35 CSSLengthArray lengthArray; |
36 initLengthArray(lengthArray); | 36 initLengthArray(lengthArray); |
37 RefPtrWillBeRawPtr<CSSValue> cssValue = CSSParser::parseSingleValue(CSSPrope
rtyLeft, text); | 37 RefPtrWillBeRawPtr<CSSValue> cssValue = CSSParser::parseSingleValue(CSSPrope
rtyLeft, text); |
38 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(cssValue.get()); | 38 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(cssValue.get()); |
39 if (primitiveValue) | 39 if (primitiveValue) |
40 primitiveValue->accumulateLengthArray(lengthArray); | 40 primitiveValue->accumulateLengthArray(lengthArray); |
41 else | 41 else |
42 ASSERT_EQ(valid, false); | 42 ASSERT_EQ(valid, false); |
43 int length = lengthArray.at(CSSPrimitiveValue::UnitTypePixels); | 43 float length = lengthArray.at(CSSPrimitiveValue::UnitTypePixels); |
44 length += lengthArray.at(CSSPrimitiveValue::UnitTypeFontSize) * fontSize; | 44 length += lengthArray.at(CSSPrimitiveValue::UnitTypeFontSize) * fontSize; |
45 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportWidth) * viewpor
tWidth / 100.0; | 45 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportWidth) * viewpor
tWidth / 100.0; |
46 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportHeight) * viewpo
rtHeight / 100.0; | 46 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportHeight) * viewpo
rtHeight / 100.0; |
47 ASSERT_EQ(value, length); | 47 ASSERT_EQ(value, length); |
48 } | 48 } |
49 | 49 |
50 | 50 |
51 TEST(SizesCalcParserTest, Basic) | 51 TEST(SizesCalcParserTest, Basic) |
52 { | 52 { |
53 TestCase testCases[] = { | 53 TestCase testCases[] = { |
54 {"calc(500px + 10em)", 660, true, false, false}, | 54 {"calc(500px + 10em)", 660, true, false, false}, |
| 55 {"calc(500px / 8)", 62.5, true, false, false}, |
55 {"calc(500px + 2 * 10em)", 820, true, false, false}, | 56 {"calc(500px + 2 * 10em)", 820, true, false, false}, |
56 {"calc(500px + 2*10em)", 820, true, false, false}, | 57 {"calc(500px + 2*10em)", 820, true, false, false}, |
57 {"calc(500px + 0.5*10em)", 580, true, false, false}, | 58 {"calc(500px + 0.5*10em)", 580, true, false, false}, |
58 {"calc(500px + (0.5*10em + 13px))", 593, true, false, false}, | 59 {"calc(500px + (0.5*10em + 13px))", 593, true, false, false}, |
59 {"calc(100vw + (0.5*10em + 13px))", 593, true, false, true}, | 60 {"calc(100vw + (0.5*10em + 13px))", 593, true, false, true}, |
60 {"calc(100vh + (0.5*10em + 13px))", 736, true, false, true}, | 61 {"calc(100vh + (0.5*10em + 13px))", 736, true, false, true}, |
61 {"calc(100vh + calc(0.5*10em + 13px))", 736, true, true, true}, // CSSCa
lculationValue does not parse internal "calc(". | 62 {"calc(100vh + calc(0.5*10em + 13px))", 736, true, true, true}, // CSSCa
lculationValue does not parse internal "calc(". |
62 {"calc(100vh + (50%*10em + 13px))", 0, false, false, true}, | 63 {"calc(100vh + (50%*10em + 13px))", 0, false, false, true}, |
63 {"calc(50em+13px)", 0, false, false, false}, | 64 {"calc(50em+13px)", 0, false, false, false}, |
64 {"calc(50em-13px)", 0, false, false, false}, | 65 {"calc(50em-13px)", 0, false, false, false}, |
65 {"calc(500px + 10)", 0, false, false, false}, | 66 {"calc(500px + 10)", 0, false, false, false}, |
66 {"calc(500 + 10)", 0, false, false, false}, | 67 {"calc(500 + 10)", 0, false, false, false}, |
67 {"calc(500px + 10s)", 0, false, true, false}, // This test ASSERTs in CS
SCalculationValue. | 68 {"calc(500px + 10s)", 0, false, true, false}, // This test ASSERTs in CS
SCalculationValue. |
68 {"calc(500px + 1cm)", 537, true, false, false}, | 69 {"calc(500px + 1cm)", 537.795276, true, false, false}, |
69 {"calc(500px - 10s)", 0, false, true, false}, // This test ASSERTs in CS
SCalculationValue. | 70 {"calc(500px - 10s)", 0, false, true, false}, // This test ASSERTs in CS
SCalculationValue. |
70 {"calc(500px - 1cm)", 462, true, false, false}, | 71 {"calc(500px - 1cm)", 462.204724, true, false, false}, |
71 {"calc(500px - 1vw)", 495, true, false, true}, | 72 {"calc(500px - 1vw)", 495, true, false, true}, |
72 {"calc(50px*10)", 500, true, false, false}, | 73 {"calc(50px*10)", 500, true, false, false}, |
73 {"calc(50px*10px)", 0, false, false, false}, | 74 {"calc(50px*10px)", 0, false, false, false}, |
74 {"calc(50px/10px)", 0, false, false, false}, | 75 {"calc(50px/10px)", 0, false, false, false}, |
75 {"calc(500px/10)", 50, true, false, false}, | 76 {"calc(500px/10)", 50, true, false, false}, |
76 {"calc(500/10)", 0, false, false, false}, | 77 {"calc(500/10)", 0, false, false, false}, |
77 {"calc(500px/0.5)", 1000, true, false, false}, | 78 {"calc(500px/0.5)", 1000, true, false, false}, |
78 {"calc(500px/.5)", 1000, true, false, false}, | 79 {"calc(500px/.5)", 1000, true, false, false}, |
79 {"calc(500/0)", 0, false, false, false}, | 80 {"calc(500/0)", 0, false, false, false}, |
80 {"calc(500px/0)", 0, false, false, false}, | 81 {"calc(500px/0)", 0, false, false, false}, |
81 {"calc(-500px/10)", 0, true, true, false}, // CSSCalculationValue does n
ot clamp negative values to 0. | 82 {"calc(-500px/10)", 0, true, true, false}, // CSSCalculationValue does n
ot clamp negative values to 0. |
82 {"calc(((4) * ((10px))))", 40, true, false, false}, | 83 {"calc(((4) * ((10px))))", 40, true, false, false}, |
83 {"calc(50px / 0)", 0, false, false, false}, | 84 {"calc(50px / 0)", 0, false, false, false}, |
84 {"calc(50px / (10 + 10))", 2, true, false, false}, | 85 {"calc(50px / (10 + 10))", 2.5, true, false, false}, |
85 {"calc(50px / (10 - 10))", 0, false, false, false}, | 86 {"calc(50px / (10 - 10))", 0, false, false, false}, |
86 {"calc(50px / (10 * 10))", 0, true, false, false}, | 87 {"calc(50px / (10 * 10))", 0.5, true, false, false}, |
87 {"calc(50px / (10 / 10))", 50, true, false, false}, | 88 {"calc(50px / (10 / 10))", 50, true, false, false}, |
88 {"calc(200px*)", 0, false, false, false}, | 89 {"calc(200px*)", 0, false, false, false}, |
89 {"calc(+ +200px)", 0, false, false, false}, | 90 {"calc(+ +200px)", 0, false, false, false}, |
90 {"calc()", 0, false, false, false}, | 91 {"calc()", 0, false, false, false}, |
91 {"calc(100px + + +100px)", 0, false, false, false}, | 92 {"calc(100px + + +100px)", 0, false, false, false}, |
92 {"calc(200px 200px)", 0, false, false, false}, | 93 {"calc(200px 200px)", 0, false, false, false}, |
93 {"calc(100px * * 2)", 0, false, false, false}, | 94 {"calc(100px * * 2)", 0, false, false, false}, |
94 {"calc(100px @ 2)", 0, false, false, false}, | 95 {"calc(100px @ 2)", 0, false, false, false}, |
95 {"calc(1 flim 2)", 0, false, false, false}, | 96 {"calc(1 flim 2)", 0, false, false, false}, |
96 {"calc(100px @ 2)", 0, false, false, false}, | 97 {"calc(100px @ 2)", 0, false, false, false}, |
(...skipping 30 matching lines...) Expand all Loading... |
127 } | 128 } |
128 | 129 |
129 for (unsigned i = 0; testCases[i].input; ++i) { | 130 for (unsigned i = 0; testCases[i].input; ++i) { |
130 if (testCases[i].dontRunInCSSCalc) | 131 if (testCases[i].dontRunInCSSCalc) |
131 continue; | 132 continue; |
132 verifyCSSCalc(testCases[i].input, testCases[i].output, testCases[i].vali
d, data.defaultFontSize, data.viewportWidth, data.viewportHeight); | 133 verifyCSSCalc(testCases[i].input, testCases[i].output, testCases[i].vali
d, data.defaultFontSize, data.viewportWidth, data.viewportHeight); |
133 } | 134 } |
134 } | 135 } |
135 | 136 |
136 } // namespace | 137 } // namespace |
OLD | NEW |