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.
6 #include "core/css/parser/SizesCalcParser.h"
8 #include "core/MediaTypeNames.h"
9 #include "core/css/CSSPrimitiveValue.h"
10 #include "core/css/MediaValuesCached.h"
11 #include "core/css/parser/CSSParser.h"
12 #include "core/css/parser/MediaQueryTokenizer.h"
14 #include <gtest/gtest.h>
20 const unsigned output;
22 const bool dontRunInCSSCalc;
23 const bool viewportDependant;
26 static void initLengthArray(CSSLengthArray& lengthArray)
28 lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount);
29 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
30 lengthArray.at(i) = 0;
33 static void verifyCSSCalc(String text, double value, bool valid, unsigned fontSize, unsigned viewportWidth, unsigned viewportHeight)
35 CSSLengthArray lengthArray;
36 initLengthArray(lengthArray);
37 RefPtrWillBeRawPtr<CSSValue> cssValue = CSSParser::parseSingleValue(CSSPropertyLeft, text);
38 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(cssValue.get());
40 primitiveValue->accumulateLengthArray(lengthArray);
42 ASSERT_EQ(valid, false);
43 int length = lengthArray.at(CSSPrimitiveValue::UnitTypePixels);
44 length += lengthArray.at(CSSPrimitiveValue::UnitTypeFontSize) * fontSize;
45 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportWidth) * viewportWidth / 100.0;
46 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportHeight) * viewportHeight / 100.0;
47 ASSERT_EQ(value, length);
51 TEST(SizesCalcParserTest, Basic)
53 TestCase testCases[] = {
54 {"calc(500px + 10em)", 660, true, false, false},
55 {"calc(500px + 2 * 10em)", 820, true, false, false},
56 {"calc(500px + 2*10em)", 820, true, false, false},
57 {"calc(500px + 0.5*10em)", 580, true, false, false},
58 {"calc(500px + (0.5*10em + 13px))", 593, true, false, false},
59 {"calc(100vw + (0.5*10em + 13px))", 593, true, false, true},
60 {"calc(100vh + (0.5*10em + 13px))", 736, true, false, true},
61 {"calc(100vh + calc(0.5*10em + 13px))", 736, true, true, true}, // CSSCalculationValue does not parse internal "calc(".
62 {"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},
65 {"calc(500px + 10)", 0, false, false, false},
66 {"calc(500 + 10)", 0, false, false, false},
67 {"calc(500px + 10s)", 0, false, true, false}, // This test ASSERTs in CSSCalculationValue.
68 {"calc(500px + 1cm)", 537, true, false, false},
69 {"calc(500px - 10s)", 0, false, true, false}, // This test ASSERTs in CSSCalculationValue.
70 {"calc(500px - 1cm)", 462, true, false, false},
71 {"calc(500px - 1vw)", 495, true, false, true},
72 {"calc(50px*10)", 500, true, false, false},
73 {"calc(50px*10px)", 0, false, false, false},
74 {"calc(50px/10px)", 0, false, false, false},
75 {"calc(500px/10)", 50, true, false, false},
76 {"calc(500/10)", 0, false, false, false},
77 {"calc(500px/0.5)", 1000, true, false, false},
78 {"calc(500px/.5)", 1000, true, false, false},
79 {"calc(500/0)", 0, false, false, false},
80 {"calc(500px/0)", 0, false, false, false},
81 {"calc(-500px/10)", 0, true, true, false}, // CSSCalculationValue does not clamp negative values to 0.
82 {"calc(((4) * ((10px))))", 40, true, false, false},
83 {"calc(50px / 0)", 0, false, false, false},
84 {"calc(50px / (10 + 10))", 2, true, false, false},
85 {"calc(50px / (10 - 10))", 0, false, false, false},
86 {"calc(50px / (10 * 10))", 0, true, false, false},
87 {"calc(50px / (10 / 10))", 50, true, false, false},
88 {"calc(200px*)", 0, false, false, false},
89 {"calc(+ +200px)", 0, false, false, false},
90 {"calc()", 0, false, false, false},
91 {"calc(100px + + +100px)", 0, false, false, false},
92 {"calc(200px 200px)", 0, false, false, false},
93 {"calc(100px * * 2)", 0, false, false, false},
94 {"calc(100px @ 2)", 0, false, false, false},
95 {"calc(1 flim 2)", 0, false, false, false},
96 {"calc(100px @ 2)", 0, false, false, false},
97 {"calc(1 flim 2)", 0, false, false, false},
98 {"calc(1 flim (2))", 0, false, false, false},
99 {0, 0, true, false, false} // Do not remove the terminator line.
103 MediaValuesCached::MediaValuesCachedData data;
104 data.viewportWidth = 500;
105 data.viewportHeight = 643;
106 data.deviceWidth = 500;
107 data.deviceHeight = 643;
108 data.devicePixelRatio = 2.0;
109 data.colorBitsPerComponent = 24;
110 data.monochromeBitsPerComponent = 0;
111 data.primaryPointerType = PointerTypeFine;
112 data.defaultFontSize = 16;
113 data.threeDEnabled = true;
114 data.mediaType = MediaTypeNames::screen;
115 data.strictMode = true;
116 RefPtr<MediaValues> mediaValues = MediaValuesCached::create(data);
118 for (unsigned i = 0; testCases[i].input; ++i) {
119 Vector<MediaQueryToken> tokens;
120 MediaQueryTokenizer::tokenize(testCases[i].input, tokens);
121 SizesCalcParser calcParser(tokens.begin(), tokens.end(), mediaValues);
122 ASSERT_EQ(testCases[i].valid, calcParser.isValid());
123 if (calcParser.isValid()) {
124 ASSERT_EQ(testCases[i].output, calcParser.result());
125 ASSERT_EQ(testCases[i].viewportDependant, calcParser.viewportDependant());
129 for (unsigned i = 0; testCases[i].input; ++i) {
130 if (testCases[i].dontRunInCSSCalc)
132 verifyCSSCalc(testCases[i].input, testCases[i].output, testCases[i].valid, data.defaultFontSize, data.viewportWidth, data.viewportHeight);