1/*
2 * Copyright (C) 2013 Adobe Systems Incorporated. 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 *    copyright notice, this list of conditions and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 *    copyright notice, this list of conditions and the following
13 *    disclaimer in the documentation and/or other materials
14 *    provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31
32#include "core/rendering/shapes/BoxShape.h"
33
34#include "platform/geometry/RoundedRect.h"
35
36#include <gtest/gtest.h>
37
38namespace blink {
39
40class BoxShapeTest : public ::testing::Test {
41protected:
42    BoxShapeTest() { }
43
44    PassOwnPtr<Shape> createBoxShape(const RoundedRect& bounds, float shapeMargin)
45    {
46        return Shape::createLayoutBoxShape(bounds, TopToBottomWritingMode, shapeMargin);
47    }
48};
49
50} // namespace blink
51
52namespace {
53
54using namespace blink;
55
56#define TEST_EXCLUDED_INTERVAL(shapePtr, lineTop, lineHeight, expectedLeft, expectedRight) \
57{                                                                                          \
58    LineSegment segment = shapePtr->getExcludedInterval(lineTop, lineHeight);                           \
59    EXPECT_TRUE(segment.isValid); \
60    if (segment.isValid) {                                                             \
61        EXPECT_FLOAT_EQ(expectedLeft, segment.logicalLeft);                              \
62        EXPECT_FLOAT_EQ(expectedRight, segment.logicalRight);                            \
63    }                                                                                      \
64}
65
66#define TEST_NO_EXCLUDED_INTERVAL(shapePtr, lineTop, lineHeight) \
67{                                                                \
68    LineSegment segment = shapePtr->getExcludedInterval(lineTop, lineHeight); \
69    EXPECT_FALSE(segment.isValid); \
70}
71
72/* The BoxShape is based on a 100x50 rectangle at 0,0. The shape-margin value is 10,
73 * so the shapeMarginBoundingBox rectangle is 120x70 at -10,-10:
74 *
75 *   -10,-10   110,-10
76 *       +--------+
77 *       |        |
78 *       +--------+
79 *   -10,60     60,60
80 */
81TEST_F(BoxShapeTest, zeroRadii)
82{
83    OwnPtr<Shape> shape = createBoxShape(RoundedRect(0, 0, 100, 50), 10);
84    EXPECT_FALSE(shape->isEmpty());
85
86    EXPECT_EQ(LayoutRect(-10, -10, 120, 70), shape->shapeMarginLogicalBoundingBox());
87
88    // A BoxShape's bounds include the top edge but not the bottom edge.
89    // Similarly a "line", specified as top,height to the overlap methods,
90    // is defined as top <= y < top + height.
91
92    EXPECT_TRUE(shape->lineOverlapsShapeMarginBounds(-9, 1));
93    EXPECT_TRUE(shape->lineOverlapsShapeMarginBounds(-10, 0));
94    EXPECT_TRUE(shape->lineOverlapsShapeMarginBounds(-10, 200));
95    EXPECT_TRUE(shape->lineOverlapsShapeMarginBounds(5, 10));
96    EXPECT_TRUE(shape->lineOverlapsShapeMarginBounds(59, 1));
97
98    EXPECT_FALSE(shape->lineOverlapsShapeMarginBounds(-12, 2));
99    EXPECT_FALSE(shape->lineOverlapsShapeMarginBounds(60, 1));
100    EXPECT_FALSE(shape->lineOverlapsShapeMarginBounds(100, 200));
101
102    TEST_EXCLUDED_INTERVAL(shape, -9, 1, -10, 110);
103    TEST_EXCLUDED_INTERVAL(shape, -10, 0, -10, 110);
104    TEST_EXCLUDED_INTERVAL(shape, -10, 200, -10, 110);
105    TEST_EXCLUDED_INTERVAL(shape, 5, 10, -10, 110);
106    TEST_EXCLUDED_INTERVAL(shape, 59, 1, -10, 110);
107
108    TEST_NO_EXCLUDED_INTERVAL(shape, -12, 2);
109    TEST_NO_EXCLUDED_INTERVAL(shape, 60, 1);
110    TEST_NO_EXCLUDED_INTERVAL(shape, 100, 200);
111}
112
113/* BoxShape geometry for this test. Corner radii are in parens, x and y intercepts
114 * for the elliptical corners are noted. The rectangle itself is at 0,0 with width and height 100.
115 *
116 *         (10, 15)  x=10      x=90 (10, 20)
117 *                (--+---------+--)
118 *           y=15 +--|         |-+ y=20
119 *                |               |
120 *                |               |
121 *           y=85 + -|         |- + y=70
122 *                (--+---------+--)
123 *       (25, 15)  x=25      x=80  (20, 30)
124 */
125TEST_F(BoxShapeTest, getIntervals)
126{
127    const RoundedRect::Radii cornerRadii(IntSize(10, 15), IntSize(10, 20), IntSize(25, 15), IntSize(20, 30));
128    OwnPtr<Shape> shape = createBoxShape(RoundedRect(IntRect(0, 0, 100, 100), cornerRadii), 0);
129    EXPECT_FALSE(shape->isEmpty());
130
131    EXPECT_EQ(LayoutRect(0, 0, 100, 100), shape->shapeMarginLogicalBoundingBox());
132
133    TEST_EXCLUDED_INTERVAL(shape, 10, 95, 0, 100);
134    TEST_EXCLUDED_INTERVAL(shape, 5, 25, 0, 100);
135    TEST_EXCLUDED_INTERVAL(shape, 15, 6, 0, 100);
136    TEST_EXCLUDED_INTERVAL(shape, 20, 50, 0, 100);
137    TEST_EXCLUDED_INTERVAL(shape, 69, 5, 0, 100);
138    TEST_EXCLUDED_INTERVAL(shape, 85, 10, 0, 97.320511f);
139}
140
141} // namespace
142