Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / css / CSSCalculationValueTest.cpp
blob278764a07e6e033bcd74bbf0524c0995387a2425
1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
32 #include "core/css/CSSCalculationValue.h"
34 #include "core/css/CSSPrimitiveValue.h"
35 #include "core/css/CSSToLengthConversionData.h"
36 #include "core/css/StylePropertySet.h"
37 #include "core/style/ComputedStyle.h"
38 #include "core/style/StyleInheritedData.h"
40 #include <gtest/gtest.h>
43 namespace blink {
45 void PrintTo(const CSSLengthArray& lengthArray, ::std::ostream* os)
47 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
48 *os << lengthArray.at(i) << ' ';
51 namespace {
53 void testAccumulatePixelsAndPercent(const CSSToLengthConversionData& conversionData, PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> expression, float expectedPixels, float expectedPercent)
55 PixelsAndPercent value(0, 0);
56 expression->accumulatePixelsAndPercent(conversionData, value);
57 EXPECT_EQ(expectedPixels, value.pixels);
58 EXPECT_EQ(expectedPercent, value.percent);
61 void initLengthArray(CSSLengthArray& lengthArray)
63 lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount);
64 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
65 lengthArray.at(i) = 0;
68 CSSLengthArray& setLengthArray(CSSLengthArray& lengthArray, String text)
70 initLengthArray(lengthArray);
71 RefPtrWillBeRawPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::create(HTMLQuirksMode);
72 propertySet->setProperty(CSSPropertyLeft, text);
73 toCSSPrimitiveValue(propertySet->getPropertyCSSValue(CSSPropertyLeft).get())->accumulateLengthArray(lengthArray);
74 return lengthArray;
77 bool lengthArraysEqual(CSSLengthArray& a, CSSLengthArray& b)
79 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i) {
80 if (a.at(i) != b.at(i))
81 return false;
83 return true;
86 TEST(CSSCalculationValue, AccumulatePixelsAndPercent)
88 RefPtr<ComputedStyle> style = ComputedStyle::create();
89 style->setEffectiveZoom(5);
90 CSSToLengthConversionData conversionData(style.get(), style.get(), nullptr, style->effectiveZoom());
92 testAccumulatePixelsAndPercent(conversionData,
93 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::UnitType::Pixels), true),
94 50, 0);
96 testAccumulatePixelsAndPercent(conversionData,
97 CSSCalcValue::createExpressionNode(
98 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::UnitType::Pixels), true),
99 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(20, CSSPrimitiveValue::UnitType::Pixels), true),
100 CalcAdd),
101 150, 0);
103 testAccumulatePixelsAndPercent(conversionData,
104 CSSCalcValue::createExpressionNode(
105 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(1, CSSPrimitiveValue::UnitType::Inches), true),
106 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(2, CSSPrimitiveValue::UnitType::Number), true),
107 CalcMultiply),
108 960, 0);
110 testAccumulatePixelsAndPercent(conversionData,
111 CSSCalcValue::createExpressionNode(
112 CSSCalcValue::createExpressionNode(
113 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(50, CSSPrimitiveValue::UnitType::Pixels), true),
114 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(0.25, CSSPrimitiveValue::UnitType::Number), false),
115 CalcMultiply),
116 CSSCalcValue::createExpressionNode(
117 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(20, CSSPrimitiveValue::UnitType::Pixels), true),
118 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(40, CSSPrimitiveValue::UnitType::Percentage), false),
119 CalcSubtract),
120 CalcSubtract),
121 -37.5, 40);
124 TEST(CSSCalculationValue, RefCount)
126 RefPtr<CalculationValue> calc = CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll);
127 Length lengthA(calc);
128 EXPECT_EQ(calc->refCount(), 2);
130 Length lengthB;
131 lengthB = lengthA;
132 EXPECT_EQ(calc->refCount(), 3);
134 Length lengthC(calc);
135 lengthC = lengthA;
136 EXPECT_EQ(calc->refCount(), 4);
138 Length lengthD(CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll));
139 lengthD = lengthA;
140 EXPECT_EQ(calc->refCount(), 5);
143 TEST(CSSCalculationValue, RefCountLeak)
145 RefPtr<CalculationValue> calc = CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll);
146 Length lengthA(calc);
148 Length lengthB = lengthA;
149 for (int i = 0; i < 100; ++i)
150 lengthB = lengthA;
151 EXPECT_EQ(calc->refCount(), 3);
153 Length lengthC(lengthA);
154 for (int i = 0; i < 100; ++i)
155 lengthC = lengthA;
156 EXPECT_EQ(calc->refCount(), 4);
158 Length lengthD(calc);
159 for (int i = 0; i < 100; ++i)
160 lengthD = lengthA;
161 EXPECT_EQ(calc->refCount(), 5);
163 lengthD = Length();
164 EXPECT_EQ(calc->refCount(), 4);
167 TEST(CSSCalculationValue, AddToLengthUnitValues)
169 CSSLengthArray expectation, actual;
170 initLengthArray(expectation);
171 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "0")));
173 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 10;
174 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "10px")));
176 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 0;
177 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = 20;
178 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "20%")));
180 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 30;
181 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = -40;
182 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc(30px - 40%)")));
184 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 90;
185 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = 10;
186 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc(1in + 10% - 6px)")));
188 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 15;
189 expectation.at(CSSPrimitiveValue::UnitTypeFontSize) = 20;
190 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = -40;
191 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc((1 * 2) * (5px + 20em / 2) - 80% / (3 - 1) + 5px)")));
194 } // anonymous namespace
196 } // namespace blink