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/rendering/RenderOverflow.h" 33 34 #include "platform/geometry/LayoutRect.h" 35 36 #include <gtest/gtest.h> 37 38 using namespace blink; 39 40 namespace blink { 41 42 // FIXME: Move this somewhere more generic. 43 void PrintTo(const LayoutRect& rect, std::ostream* os) 44 { 45 *os << "LayoutRect(" 46 << rect.x().toFloat() << ", " 47 << rect.y().toFloat() << ", " 48 << rect.width().toFloat() << ", " 49 << rect.height().toFloat() << ")"; 50 } 51 52 } // namespace blink 53 54 namespace { 55 56 LayoutRect initialLayoutOverflow() 57 { 58 return LayoutRect(10, 10, 80, 80); 59 } 60 61 LayoutRect initialVisualOverflow() 62 { 63 return LayoutRect(0, 0, 100, 100); 64 } 65 66 class RenderOverflowTest : public testing::Test { 67 protected: 68 RenderOverflowTest() : m_overflow(initialLayoutOverflow(), initialVisualOverflow()) { } 69 RenderOverflow m_overflow; 70 }; 71 72 TEST_F(RenderOverflowTest, InitialOverflowRects) 73 { 74 EXPECT_EQ(initialLayoutOverflow(), m_overflow.layoutOverflowRect()); 75 EXPECT_EQ(initialVisualOverflow(), m_overflow.visualOverflowRect()); 76 EXPECT_TRUE(m_overflow.contentsVisualOverflowRect().isEmpty()); 77 } 78 79 TEST_F(RenderOverflowTest, AddLayoutOverflowOutsideExpandsRect) 80 { 81 m_overflow.addLayoutOverflow(LayoutRect(0, 10, 30, 10)); 82 EXPECT_EQ(LayoutRect(0, 10, 90, 80), m_overflow.layoutOverflowRect()); 83 } 84 85 TEST_F(RenderOverflowTest, AddLayoutOverflowInsideDoesNotAffectRect) 86 { 87 m_overflow.addLayoutOverflow(LayoutRect(50, 50, 10, 20)); 88 EXPECT_EQ(initialLayoutOverflow(), m_overflow.layoutOverflowRect()); 89 } 90 91 TEST_F(RenderOverflowTest, AddLayoutOverflowEmpty) 92 { 93 // This test documents the existing behavior so that we are aware when/if 94 // it changes. It would also be reasonable for addLayoutOverflow to be 95 // a no-op in this situation. 96 m_overflow.addLayoutOverflow(LayoutRect(200, 200, 0, 0)); 97 EXPECT_EQ(LayoutRect(10, 10, 190, 190), m_overflow.layoutOverflowRect()); 98 } 99 100 TEST_F(RenderOverflowTest, AddLayoutOverflowDoesNotAffectVisualOverflow) 101 { 102 m_overflow.addLayoutOverflow(LayoutRect(300, 300, 300, 300)); 103 EXPECT_EQ(initialVisualOverflow(), m_overflow.visualOverflowRect()); 104 } 105 106 TEST_F(RenderOverflowTest, AddLayoutOverflowDoesNotAffectContentsVisualOverflow) 107 { 108 m_overflow.addLayoutOverflow(LayoutRect(300, 300, 300, 300)); 109 EXPECT_TRUE(m_overflow.contentsVisualOverflowRect().isEmpty()); 110 } 111 112 TEST_F(RenderOverflowTest, AddVisualOverflowOutsideExpandsRect) 113 { 114 m_overflow.addVisualOverflow(LayoutRect(150, -50, 10, 10)); 115 EXPECT_EQ(LayoutRect(0, -50, 160, 150), m_overflow.visualOverflowRect()); 116 } 117 118 TEST_F(RenderOverflowTest, AddVisualOverflowInsideDoesNotAffectRect) 119 { 120 m_overflow.addVisualOverflow(LayoutRect(0, 10, 90, 90)); 121 EXPECT_EQ(initialVisualOverflow(), m_overflow.visualOverflowRect()); 122 } 123 124 TEST_F(RenderOverflowTest, AddVisualOverflowEmpty) 125 { 126 // This test documents the existing behavior so that we are aware when/if 127 // it changes. It would also be reasonable for addVisualOverflow to be 128 // a no-op in this situation. 129 m_overflow.addVisualOverflow(LayoutRect(200, 200, 0, 0)); 130 EXPECT_EQ(LayoutRect(0, 0, 200, 200), m_overflow.visualOverflowRect()); 131 } 132 133 TEST_F(RenderOverflowTest, AddVisualOverflowDoesNotAffectLayoutOverflow) 134 { 135 m_overflow.addVisualOverflow(LayoutRect(300, 300, 300, 300)); 136 EXPECT_EQ(initialLayoutOverflow(), m_overflow.layoutOverflowRect()); 137 } 138 139 TEST_F(RenderOverflowTest, AddVisualOverflowDoesNotAffectContentsVisualOverflow) 140 { 141 m_overflow.addVisualOverflow(LayoutRect(300, 300, 300, 300)); 142 EXPECT_TRUE(m_overflow.contentsVisualOverflowRect().isEmpty()); 143 } 144 145 TEST_F(RenderOverflowTest, AddContentsVisualOverflowFirstCall) 146 { 147 m_overflow.addContentsVisualOverflow(LayoutRect(0, 0, 10, 10)); 148 EXPECT_EQ(LayoutRect(0, 0, 10, 10), m_overflow.contentsVisualOverflowRect()); 149 } 150 151 TEST_F(RenderOverflowTest, AddContentsVisualOverflowUnitesRects) 152 { 153 m_overflow.addContentsVisualOverflow(LayoutRect(0, 0, 10, 10)); 154 m_overflow.addContentsVisualOverflow(LayoutRect(80, 80, 10, 10)); 155 EXPECT_EQ(LayoutRect(0, 0, 90, 90), m_overflow.contentsVisualOverflowRect()); 156 } 157 158 TEST_F(RenderOverflowTest, AddContentsVisualOverflowRectWithinRect) 159 { 160 m_overflow.addContentsVisualOverflow(LayoutRect(0, 0, 10, 10)); 161 m_overflow.addContentsVisualOverflow(LayoutRect(2, 2, 5, 5)); 162 EXPECT_EQ(LayoutRect(0, 0, 10, 10), m_overflow.contentsVisualOverflowRect()); 163 } 164 165 TEST_F(RenderOverflowTest, AddContentsVisualOverflowEmpty) 166 { 167 // This test documents the existing behavior so that we are aware when/if 168 // it changes. It would also be reasonable for addContentsVisualOverflow to 169 // expand in this situation. 170 m_overflow.addContentsVisualOverflow(LayoutRect(0, 0, 10, 10)); 171 m_overflow.addContentsVisualOverflow(LayoutRect(20, 20, 0, 0)); 172 EXPECT_EQ(LayoutRect(0, 0, 10, 10), m_overflow.contentsVisualOverflowRect()); 173 } 174 175 TEST_F(RenderOverflowTest, MoveAffectsLayoutOverflow) 176 { 177 m_overflow.move(500, 100); 178 EXPECT_EQ(LayoutRect(510, 110, 80, 80), m_overflow.layoutOverflowRect()); 179 } 180 181 TEST_F(RenderOverflowTest, MoveAffectsVisualOverflow) 182 { 183 m_overflow.move(500, 100); 184 EXPECT_EQ(LayoutRect(500, 100, 100, 100), m_overflow.visualOverflowRect()); 185 } 186 187 TEST_F(RenderOverflowTest, MoveAffectsContentsVisualOverflow) 188 { 189 m_overflow.addContentsVisualOverflow(LayoutRect(0, 0, 10, 10)); 190 m_overflow.move(500, 100); 191 EXPECT_EQ(LayoutRect(500, 100, 10, 10), m_overflow.contentsVisualOverflowRect()); 192 } 193 194 } // namespace 195