Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: Source/core/css/parser/SizesCalcParserTest.cpp

Issue 369423002: Have srcset respond to viewport changes (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Oilpan comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/StylePropertySet.h" 11 #include "core/css/StylePropertySet.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 WebCore { 16 namespace WebCore {
17 17
18 struct TestCase { 18 struct TestCase {
19 const char* input; 19 const char* input;
20 const unsigned output; 20 const unsigned output;
21 const bool valid; 21 const bool valid;
22 const bool dontRunInCSSCalc; 22 const bool dontRunInCSSCalc;
23 const bool viewportDependant;
23 }; 24 };
24 25
25 static void initLengthArray(CSSLengthArray& lengthArray) 26 static void initLengthArray(CSSLengthArray& lengthArray)
26 { 27 {
27 lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount); 28 lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount);
28 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i) 29 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
29 lengthArray.at(i) = 0; 30 lengthArray.at(i) = 0;
30 } 31 }
31 32
32 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)
(...skipping 12 matching lines...) Expand all
45 length += lengthArray.at(CSSPrimitiveValue::UnitTypeFontSize) * fontSize; 46 length += lengthArray.at(CSSPrimitiveValue::UnitTypeFontSize) * fontSize;
46 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportWidth) * viewpor tWidth / 100.0; 47 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportWidth) * viewpor tWidth / 100.0;
47 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportHeight) * viewpo rtHeight / 100.0; 48 length += lengthArray.at(CSSPrimitiveValue::UnitTypeViewportHeight) * viewpo rtHeight / 100.0;
48 ASSERT_EQ(value, length); 49 ASSERT_EQ(value, length);
49 } 50 }
50 51
51 52
52 TEST(SizesCalcParserTest, Basic) 53 TEST(SizesCalcParserTest, Basic)
53 { 54 {
54 TestCase testCases[] = { 55 TestCase testCases[] = {
55 {"calc(500px + 10em)", 660, true, false}, 56 {"calc(500px + 10em)", 660, true, false, false},
56 {"calc(500px + 2 * 10em)", 820, true, false}, 57 {"calc(500px + 2 * 10em)", 820, true, false, false},
57 {"calc(500px + 2*10em)", 820, true, false}, 58 {"calc(500px + 2*10em)", 820, true, false, false},
58 {"calc(500px + 0.5*10em)", 580, true, false}, 59 {"calc(500px + 0.5*10em)", 580, true, false, false},
59 {"calc(500px + (0.5*10em + 13px))", 593, true, false}, 60 {"calc(500px + (0.5*10em + 13px))", 593, true, false, false},
60 {"calc(100vw + (0.5*10em + 13px))", 593, true, false}, 61 {"calc(100vw + (0.5*10em + 13px))", 593, true, false, true},
61 {"calc(100vh + (0.5*10em + 13px))", 736, true, false}, 62 {"calc(100vh + (0.5*10em + 13px))", 736, true, false, true},
62 {"calc(100vh + calc(0.5*10em + 13px))", 736, true, true}, // CSSCalculat ionValue does not parse internal "calc(". 63 {"calc(100vh + calc(0.5*10em + 13px))", 736, true, true, true}, // CSSCa lculationValue does not parse internal "calc(".
63 {"calc(100vh + (50%*10em + 13px))", 0, false, false}, 64 {"calc(100vh + (50%*10em + 13px))", 0, false, false, true},
64 {"calc(50em+13px)", 0, false, false}, 65 {"calc(50em+13px)", 0, false, false, false},
65 {"calc(50em-13px)", 0, false, false}, 66 {"calc(50em-13px)", 0, false, false, false},
66 {"calc(500px + 10)", 0, false, false}, 67 {"calc(500px + 10)", 0, false, false, false},
67 {"calc(500 + 10)", 0, false, false}, 68 {"calc(500 + 10)", 0, false, false, false},
68 {"calc(500px + 10s)", 0, false, true}, // This test ASSERTs in CSSCalcul ationValue. 69 {"calc(500px + 10s)", 0, false, true, false}, // This test ASSERTs in CS SCalculationValue.
69 {"calc(500px + 1cm)", 537, true, false}, 70 {"calc(500px + 1cm)", 537, true, false, false},
70 {"calc(500px - 10s)", 0, false, true}, // This test ASSERTs in CSSCalcul ationValue. 71 {"calc(500px - 10s)", 0, false, true, false}, // This test ASSERTs in CS SCalculationValue.
71 {"calc(500px - 1cm)", 462, true, false}, 72 {"calc(500px - 1cm)", 462, true, false, false},
72 {"calc(50px*10)", 500, true, false}, 73 {"calc(500px - 1vw)", 495, true, false, true},
73 {"calc(50px*10px)", 0, false, false}, 74 {"calc(50px*10)", 500, true, false, false},
74 {"calc(50px/10px)", 0, false, false}, 75 {"calc(50px*10px)", 0, false, false, false},
75 {"calc(500px/10)", 50, true, false}, 76 {"calc(50px/10px)", 0, false, false, false},
76 {"calc(500/10)", 0, false, false}, 77 {"calc(500px/10)", 50, true, false, false},
77 {"calc(500px/0.5)", 1000, true, false}, 78 {"calc(500/10)", 0, false, false, false},
78 {"calc(500px/.5)", 1000, true, false}, 79 {"calc(500px/0.5)", 1000, true, false, false},
79 {"calc(500/0)", 0, false, false}, 80 {"calc(500px/.5)", 1000, true, false, false},
80 {"calc(500px/0)", 0, false, false}, 81 {"calc(500/0)", 0, false, false, false},
81 {"calc(-500px/10)", 0, true, true}, // CSSCalculationValue does not clam p negative values to 0. 82 {"calc(500px/0)", 0, false, false, false},
82 {"calc(((4) * ((10px))))", 40, true, false}, 83 {"calc(-500px/10)", 0, true, true, false}, // CSSCalculationValue does n ot clamp negative values to 0.
83 {"calc(50px / 0)", 0, false, false}, 84 {"calc(((4) * ((10px))))", 40, true, false, false},
84 {"calc(50px / (10 + 10))", 2, true, false}, 85 {"calc(50px / 0)", 0, false, false, false},
85 {"calc(50px / (10 - 10))", 0, false, false}, 86 {"calc(50px / (10 + 10))", 2, true, false, false},
86 {"calc(50px / (10 * 10))", 0, true, false}, 87 {"calc(50px / (10 - 10))", 0, false, false, false},
87 {"calc(50px / (10 / 10))", 50, true, false}, 88 {"calc(50px / (10 * 10))", 0, true, false, false},
88 {"calc(200px*)", 0, false, false}, 89 {"calc(50px / (10 / 10))", 50, true, false, false},
89 {"calc(+ +200px)", 0, false, false}, 90 {"calc(200px*)", 0, false, false, false},
90 {"calc()", 0, false, false}, 91 {"calc(+ +200px)", 0, false, false, false},
91 {"calc(100px + + +100px)", 0, false, false}, 92 {"calc()", 0, false, false, false},
92 {"calc(200px 200px)", 0, false, false}, 93 {"calc(100px + + +100px)", 0, false, false, false},
93 {"calc(100px * * 2)", 0, false, false}, 94 {"calc(200px 200px)", 0, false, false, false},
94 {"calc(100px @ 2)", 0, false, false}, 95 {"calc(100px * * 2)", 0, false, false, false},
95 {"calc(1 flim 2)", 0, false, false}, 96 {"calc(100px @ 2)", 0, false, false, false},
96 {"calc(100px @ 2)", 0, false, false}, 97 {"calc(1 flim 2)", 0, false, false, false},
97 {"calc(1 flim 2)", 0, false, false}, 98 {"calc(100px @ 2)", 0, false, false, false},
98 {"calc(1 flim (2))", 0, false, false}, 99 {"calc(1 flim 2)", 0, false, false, false},
99 {0, 0, true, false} // Do not remove the terminator line. 100 {"calc(1 flim (2))", 0, false, false, false},
101 {0, 0, true, false, false} // Do not remove the terminator line.
100 }; 102 };
101 103
102 104
103 MediaValuesCached::MediaValuesCachedData data; 105 MediaValuesCached::MediaValuesCachedData data;
104 data.viewportWidth = 500; 106 data.viewportWidth = 500;
105 data.viewportHeight = 643; 107 data.viewportHeight = 643;
106 data.deviceWidth = 500; 108 data.deviceWidth = 500;
107 data.deviceHeight = 643; 109 data.deviceHeight = 643;
108 data.devicePixelRatio = 2.0; 110 data.devicePixelRatio = 2.0;
109 data.colorBitsPerComponent = 24; 111 data.colorBitsPerComponent = 24;
110 data.monochromeBitsPerComponent = 0; 112 data.monochromeBitsPerComponent = 0;
111 data.pointer = MediaValues::MousePointer; 113 data.pointer = MediaValues::MousePointer;
112 data.defaultFontSize = 16; 114 data.defaultFontSize = 16;
113 data.threeDEnabled = true; 115 data.threeDEnabled = true;
114 data.mediaType = MediaTypeNames::screen; 116 data.mediaType = MediaTypeNames::screen;
115 data.strictMode = true; 117 data.strictMode = true;
116 RefPtr<MediaValues> mediaValues = MediaValuesCached::create(data); 118 RefPtr<MediaValues> mediaValues = MediaValuesCached::create(data);
117 119
118 for (unsigned i = 0; testCases[i].input; ++i) { 120 for (unsigned i = 0; testCases[i].input; ++i) {
119 Vector<MediaQueryToken> tokens; 121 Vector<MediaQueryToken> tokens;
120 MediaQueryTokenizer::tokenize(testCases[i].input, tokens); 122 MediaQueryTokenizer::tokenize(testCases[i].input, tokens);
121 unsigned output; 123 SizesCalcParser calcParser(tokens.begin(), tokens.end(), mediaValues);
122 bool valid = SizesCalcParser::parse(tokens.begin(), tokens.end(), mediaV alues, output); 124 ASSERT_EQ(testCases[i].valid, calcParser.isValid());
123 ASSERT_EQ(testCases[i].valid, valid); 125 if (calcParser.isValid()) {
124 if (valid) 126 ASSERT_EQ(testCases[i].output, calcParser.result());
125 ASSERT_EQ(testCases[i].output, output); 127 ASSERT_EQ(testCases[i].viewportDependant, calcParser.viewportDependa nt());
128 }
126 } 129 }
127 130
128 for (unsigned i = 0; testCases[i].input; ++i) { 131 for (unsigned i = 0; testCases[i].input; ++i) {
129 if (testCases[i].dontRunInCSSCalc) 132 if (testCases[i].dontRunInCSSCalc)
130 continue; 133 continue;
131 verifyCSSCalc(testCases[i].input, testCases[i].output, testCases[i].vali d, data.defaultFontSize, data.viewportWidth, data.viewportHeight); 134 verifyCSSCalc(testCases[i].input, testCases[i].output, testCases[i].vali d, data.defaultFontSize, data.viewportWidth, data.viewportHeight);
132 } 135 }
133 } 136 }
134 137
135 } // namespace 138 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698