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
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
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.
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/rendering/style/RenderStyle.h"
38 #include "core/rendering/style/StyleInheritedData.h"
40 #include <gtest/gtest.h>
45 void PrintTo(const CSSLengthArray& lengthArray, ::std::ostream* os)
47 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
48 *os << lengthArray.at(i) << ' ';
53 using namespace blink;
57 void testAccumulatePixelsAndPercent(const CSSToLengthConversionData& conversionData, PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> expression, float expectedPixels, float expectedPercent)
59 PixelsAndPercent value(0, 0);
60 expression->accumulatePixelsAndPercent(conversionData, value);
61 EXPECT_EQ(expectedPixels, value.pixels);
62 EXPECT_EQ(expectedPercent, value.percent);
65 void initLengthArray(CSSLengthArray& lengthArray)
67 lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount);
68 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
69 lengthArray.at(i) = 0;
72 CSSLengthArray& setLengthArray(CSSLengthArray& lengthArray, String text)
74 initLengthArray(lengthArray);
75 RefPtrWillBeRawPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::create();
76 propertySet->setProperty(CSSPropertyLeft, text);
77 toCSSPrimitiveValue(propertySet->getPropertyCSSValue(CSSPropertyLeft).get())->accumulateLengthArray(lengthArray);
81 bool lengthArraysEqual(CSSLengthArray& a, CSSLengthArray& b)
83 for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i) {
84 if (a.at(i) != b.at(i))
90 TEST(CSSCalculationValue, AccumulatePixelsAndPercent)
92 RefPtr<RenderStyle> style = RenderStyle::createDefaultStyle();
93 style->setEffectiveZoom(5);
94 CSSToLengthConversionData conversionData(style.get(), style.get(), 0);
96 testAccumulatePixelsAndPercent(conversionData,
97 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::CSS_PX), true),
100 testAccumulatePixelsAndPercent(conversionData,
101 CSSCalcValue::createExpressionNode(
102 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::CSS_PX), true),
103 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(20, CSSPrimitiveValue::CSS_PX), true),
107 testAccumulatePixelsAndPercent(conversionData,
108 CSSCalcValue::createExpressionNode(
109 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(1, CSSPrimitiveValue::CSS_IN), true),
110 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(2, CSSPrimitiveValue::CSS_NUMBER), true),
114 testAccumulatePixelsAndPercent(conversionData,
115 CSSCalcValue::createExpressionNode(
116 CSSCalcValue::createExpressionNode(
117 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(50, CSSPrimitiveValue::CSS_PX), true),
118 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(0.25, CSSPrimitiveValue::CSS_NUMBER), false),
120 CSSCalcValue::createExpressionNode(
121 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(20, CSSPrimitiveValue::CSS_PX), true),
122 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(40, CSSPrimitiveValue::CSS_PERCENTAGE), false),
128 TEST(CSSCalculationValue, RefCount)
130 RefPtr<CalculationValue> calc = CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll);
131 Length lengthA(calc);
132 EXPECT_EQ(calc->refCount(), 2);
136 EXPECT_EQ(calc->refCount(), 3);
138 Length lengthC(calc);
140 EXPECT_EQ(calc->refCount(), 4);
142 Length lengthD(CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll));
144 EXPECT_EQ(calc->refCount(), 5);
147 TEST(CSSCalculationValue, RefCountLeak)
149 RefPtr<CalculationValue> calc = CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll);
150 Length lengthA(calc);
152 Length lengthB = lengthA;
153 for (int i = 0; i < 100; ++i)
155 EXPECT_EQ(calc->refCount(), 3);
157 Length lengthC(lengthA);
158 for (int i = 0; i < 100; ++i)
160 EXPECT_EQ(calc->refCount(), 4);
162 Length lengthD(calc);
163 for (int i = 0; i < 100; ++i)
165 EXPECT_EQ(calc->refCount(), 5);
168 EXPECT_EQ(calc->refCount(), 4);
171 TEST(CSSCalculationValue, AddToLengthUnitValues)
173 CSSLengthArray expectation, actual;
174 initLengthArray(expectation);
175 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "0")));
177 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 10;
178 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "10px")));
180 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 0;
181 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = 20;
182 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "20%%")));
184 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 30;
185 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = -40;
186 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc(30px - 40%%)")));
188 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 90;
189 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = 10;
190 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc(1in + 10%% - 6px)")));
192 expectation.at(CSSPrimitiveValue::UnitTypePixels) = 15;
193 expectation.at(CSSPrimitiveValue::UnitTypeFontSize) = 20;
194 expectation.at(CSSPrimitiveValue::UnitTypePercentage) = -40;
195 EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc((1 * 2) * (5px + 20em / 2) - 80%% / (3 - 1) + 5px)")));