Upstream version 5.34.104.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/rendering/style/RenderStyle.h"
37 #include "core/rendering/style/StyleInheritedData.h"
38
39 #include <gtest/gtest.h>
40
41 using namespace WebCore;
42
43 namespace {
44
45 void testExpression(PassRefPtrWillBeRawPtr<CSSCalcExpressionNode> expression, const RenderStyle* style)
46 {
47     EXPECT_TRUE(
48         expression->equals(
49             *CSSCalcValue::createExpressionNode(
50                 expression->toCalcValue(CSSToLengthConversionData(style, style, 0)).get(),
51                 style->effectiveZoom()).get()));
52 }
53
54 TEST(CSSCalculationValue, CreateExpressionNodeFromLength)
55 {
56     RefPtr<RenderStyle> style = RenderStyle::create();
57     RefPtrWillBeRawPtr<CSSCalcExpressionNode> expected;
58     RefPtrWillBeRawPtr<CSSCalcExpressionNode> actual;
59
60     expected = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::CSS_PX), true);
61     actual = CSSCalcValue::createExpressionNode(Length(10, WebCore::Fixed), style->effectiveZoom());
62     EXPECT_TRUE(actual->equals(*expected.get()));
63
64     expected = CSSCalcValue::createExpressionNode(
65         CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::CSS_PX), true),
66         CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(20, CSSPrimitiveValue::CSS_PX), true),
67         CalcAdd);
68     actual = CSSCalcValue::createExpressionNode(
69         Length(CalculationValue::create(
70             adoptPtr(new CalcExpressionBinaryOperation(
71                 adoptPtr(new CalcExpressionLength(Length(10, WebCore::Fixed))),
72                 adoptPtr(new CalcExpressionLength(Length(20, WebCore::Fixed))),
73                 CalcAdd)),
74             ValueRangeAll)),
75         style->effectiveZoom());
76     EXPECT_TRUE(actual->equals(*expected.get()));
77
78     expected = CSSCalcValue::createExpressionNode(
79         CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(30, CSSPrimitiveValue::CSS_PX), true),
80         CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(40, CSSPrimitiveValue::CSS_NUMBER), true),
81         CalcMultiply);
82     actual = CSSCalcValue::createExpressionNode(
83         Length(CalculationValue::create(
84             adoptPtr(new CalcExpressionBinaryOperation(
85                 adoptPtr(new CalcExpressionLength(Length(30, WebCore::Fixed))),
86                 adoptPtr(new CalcExpressionNumber(40)),
87                 CalcMultiply)),
88             ValueRangeAll)),
89         style->effectiveZoom());
90     EXPECT_TRUE(actual->equals(*expected.get()));
91
92     expected = CSSCalcValue::createExpressionNode(
93         CSSCalcValue::createExpressionNode(
94             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(50, CSSPrimitiveValue::CSS_PX), true),
95             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(0.25, CSSPrimitiveValue::CSS_NUMBER), false),
96             CalcMultiply),
97         CSSCalcValue::createExpressionNode(
98             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(60, CSSPrimitiveValue::CSS_PX), true),
99             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(0.75, CSSPrimitiveValue::CSS_NUMBER), false),
100             CalcMultiply),
101         CalcAdd);
102     actual = CSSCalcValue::createExpressionNode(
103         Length(CalculationValue::create(
104             adoptPtr(new CalcExpressionBlendLength(Length(50, WebCore::Fixed), Length(60, WebCore::Fixed), 0.75)),
105             ValueRangeAll)),
106         style->effectiveZoom());
107     EXPECT_TRUE(actual->equals(*expected.get()));
108 }
109
110 TEST(CSSCalculationValue, CreateExpressionNodeFromLengthFromExpressionNode)
111 {
112     RefPtrWillBeRawPtr<CSSCalcExpressionNode> expression;
113     RefPtr<RenderStyle> style = RenderStyle::createDefaultStyle();
114     style->setEffectiveZoom(5);
115
116     testExpression(
117         CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::CSS_PX), true),
118         style.get());
119
120     testExpression(
121         CSSCalcValue::createExpressionNode(
122             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(10, CSSPrimitiveValue::CSS_PX), true),
123             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(20, CSSPrimitiveValue::CSS_PX), true),
124             CalcAdd),
125         style.get());
126
127     testExpression(
128         CSSCalcValue::createExpressionNode(
129             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(30, CSSPrimitiveValue::CSS_PX), true),
130             CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(40, CSSPrimitiveValue::CSS_NUMBER), true),
131             CalcMultiply),
132         style.get());
133
134     testExpression(
135         CSSCalcValue::createExpressionNode(
136             CSSCalcValue::createExpressionNode(
137                 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(50, CSSPrimitiveValue::CSS_PX), true),
138                 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(0.25, CSSPrimitiveValue::CSS_NUMBER), false),
139                 CalcMultiply),
140             CSSCalcValue::createExpressionNode(
141                 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(60, CSSPrimitiveValue::CSS_PX), true),
142                 CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(0.75, CSSPrimitiveValue::CSS_NUMBER), false),
143                 CalcMultiply),
144             CalcAdd),
145         style.get());
146 }
147
148 }