Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSCalculationValueTest.cpp
1 /*
2  * Copyright (c) 2013, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "core/css/CSSCalculationValue.h"
33
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"
39
40 #include <gtest/gtest.h>
41
42
43 namespace blink {
44
45 void PrintTo(const CSSLengthArray& lengthArray, ::std::ostream* os)
46 {
47     for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
48         *os << lengthArray.at(i) << ' ';
49 }
50
51 }
52
53 using namespace blink;
54
55 namespace {
56
57 void testAccumulatePixelsAndPercent(const CSSToLengthConversionData& conversionData, PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> expression, float expectedPixels, float expectedPercent)
58 {
59     PixelsAndPercent value(0, 0);
60     expression->accumulatePixelsAndPercent(conversionData, value);
61     EXPECT_EQ(expectedPixels, value.pixels);
62     EXPECT_EQ(expectedPercent, value.percent);
63 }
64
65 void initLengthArray(CSSLengthArray& lengthArray)
66 {
67     lengthArray.resize(CSSPrimitiveValue::LengthUnitTypeCount);
68     for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
69         lengthArray.at(i) = 0;
70 }
71
72 CSSLengthArray& setLengthArray(CSSLengthArray& lengthArray, String text)
73 {
74     initLengthArray(lengthArray);
75     RefPtrWillBeRawPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::create();
76     propertySet->setProperty(CSSPropertyLeft, text);
77     toCSSPrimitiveValue(propertySet->getPropertyCSSValue(CSSPropertyLeft).get())->accumulateLengthArray(lengthArray);
78     return lengthArray;
79 }
80
81 bool lengthArraysEqual(CSSLengthArray& a, CSSLengthArray& b)
82 {
83     for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i) {
84         if (a.at(i) != b.at(i))
85             return false;
86     }
87     return true;
88 }
89
90 TEST(CSSCalculationValue, AccumulatePixelsAndPercent)
91 {
92     RefPtr<RenderStyle> style = RenderStyle::createDefaultStyle();
93     style->setEffectiveZoom(5);
94     CSSToLengthConversionData conversionData(style.get(), style.get(), 0);
95
96     testAccumulatePixelsAndPercent(conversionData,
97         CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::CSS_PX), true),
98         50, 0);
99
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),
104             CalcAdd),
105         150, 0);
106
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),
111             CalcMultiply),
112         960, 0);
113
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),
119                 CalcMultiply),
120             CSSCalcValue::createExpressionNode(
121                 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(20, CSSPrimitiveValue::CSS_PX), true),
122                 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(40, CSSPrimitiveValue::CSS_PERCENTAGE), false),
123                 CalcSubtract),
124             CalcSubtract),
125         -37.5, 40);
126 }
127
128 TEST(CSSCalculationValue, RefCount)
129 {
130     RefPtr<CalculationValue> calc = CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll);
131     Length lengthA(calc);
132     EXPECT_EQ(calc->refCount(), 2);
133
134     Length lengthB;
135     lengthB = lengthA;
136     EXPECT_EQ(calc->refCount(), 3);
137
138     Length lengthC(calc);
139     lengthC = lengthA;
140     EXPECT_EQ(calc->refCount(), 4);
141
142     Length lengthD(CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll));
143     lengthD = lengthA;
144     EXPECT_EQ(calc->refCount(), 5);
145 }
146
147 TEST(CSSCalculationValue, RefCountLeak)
148 {
149     RefPtr<CalculationValue> calc = CalculationValue::create(PixelsAndPercent(1, 2), ValueRangeAll);
150     Length lengthA(calc);
151
152     Length lengthB = lengthA;
153     for (int i = 0; i < 100; ++i)
154         lengthB = lengthA;
155     EXPECT_EQ(calc->refCount(), 3);
156
157     Length lengthC(lengthA);
158     for (int i = 0; i < 100; ++i)
159         lengthC = lengthA;
160     EXPECT_EQ(calc->refCount(), 4);
161
162     Length lengthD(calc);
163     for (int i = 0; i < 100; ++i)
164         lengthD = lengthA;
165     EXPECT_EQ(calc->refCount(), 5);
166
167     lengthD = Length();
168     EXPECT_EQ(calc->refCount(), 4);
169 }
170
171 TEST(CSSCalculationValue, AddToLengthUnitValues)
172 {
173     CSSLengthArray expectation, actual;
174     initLengthArray(expectation);
175     EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "0")));
176
177     expectation.at(CSSPrimitiveValue::UnitTypePixels) = 10;
178     EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "10px")));
179
180     expectation.at(CSSPrimitiveValue::UnitTypePixels) = 0;
181     expectation.at(CSSPrimitiveValue::UnitTypePercentage) = 20;
182     EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "20%%")));
183
184     expectation.at(CSSPrimitiveValue::UnitTypePixels) = 30;
185     expectation.at(CSSPrimitiveValue::UnitTypePercentage) = -40;
186     EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc(30px - 40%%)")));
187
188     expectation.at(CSSPrimitiveValue::UnitTypePixels) = 90;
189     expectation.at(CSSPrimitiveValue::UnitTypePercentage) = 10;
190     EXPECT_TRUE(lengthArraysEqual(expectation, setLengthArray(actual, "calc(1in + 10%% - 6px)")));
191
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)")));
196 }
197
198 }