| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/dom/Element.h" | 5 #include "core/dom/Element.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 8 #include "core/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
| 9 #include "core/html/HTMLHtmlElement.h" | 10 #include "core/html/HTMLHtmlElement.h" |
| 11 #include "core/layout/LayoutBoxModelObject.h" |
| 12 #include "core/paint/PaintLayer.h" |
| 10 #include "core/testing/DummyPageHolder.h" | 13 #include "core/testing/DummyPageHolder.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include <memory> | |
| 13 | 15 |
| 14 namespace blink { | 16 namespace blink { |
| 15 | 17 |
| 18 namespace { |
| 19 void DirtyLayoutAndAssertLifecycle(Document& document) { |
| 20 // Dirty layout to reset the lifecycle. |
| 21 document.body()->setAttribute("style", "background: red;"); |
| 22 ASSERT_EQ(DocumentLifecycle::kVisualUpdatePending, |
| 23 document.Lifecycle().GetState()); |
| 24 } |
| 25 } // namespace |
| 26 |
| 16 TEST(ElementTest, SupportsFocus) { | 27 TEST(ElementTest, SupportsFocus) { |
| 17 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); | 28 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); |
| 18 Document& document = page_holder->GetDocument(); | 29 Document& document = page_holder->GetDocument(); |
| 19 DCHECK(isHTMLHtmlElement(document.documentElement())); | 30 DCHECK(isHTMLHtmlElement(document.documentElement())); |
| 20 document.setDesignMode("on"); | 31 document.setDesignMode("on"); |
| 21 document.View()->UpdateAllLifecyclePhases(); | 32 document.View()->UpdateAllLifecyclePhases(); |
| 22 EXPECT_TRUE(document.documentElement()->SupportsFocus()) | 33 EXPECT_TRUE(document.documentElement()->SupportsFocus()) |
| 23 << "<html> with designMode=on should be focusable."; | 34 << "<html> with designMode=on should be focusable."; |
| 24 } | 35 } |
| 25 | 36 |
| 37 // Verifies that calling getBoundingClientRect cleans compositor inputs. |
| 38 // Cleaning the compositor inputs is required so that position:sticky elements |
| 39 // and their descendants have the correct location. |
| 40 TEST(ElementTest, GetBoundingClientRectUpdatesCompositorInputs) { |
| 41 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); |
| 42 Document& document = page_holder->GetDocument(); |
| 43 |
| 44 document.View()->UpdateAllLifecyclePhases(); |
| 45 EXPECT_EQ(DocumentLifecycle::kPaintClean, document.Lifecycle().GetState()); |
| 46 |
| 47 document.body()->setInnerHTML( |
| 48 "<div id='ancestor'>" |
| 49 " <div id='sticky' style='position:sticky;'>" |
| 50 " <div id='stickyChild'></div>" |
| 51 " </div>" |
| 52 " <div id='nonSticky'></div>" |
| 53 "</div>"); |
| 54 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, |
| 55 document.Lifecycle().GetState()); |
| 56 |
| 57 // Asking for any element that is not affected by a sticky element should only |
| 58 // advance us to layout clean. |
| 59 document.getElementById("ancestor")->getBoundingClientRect(); |
| 60 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 61 |
| 62 document.getElementById("nonSticky")->getBoundingClientRect(); |
| 63 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 64 |
| 65 // However, asking for either the sticky element or it's descendents should |
| 66 // clean compositing inputs as well. |
| 67 document.getElementById("sticky")->getBoundingClientRect(); |
| 68 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 69 document.Lifecycle().GetState()); |
| 70 |
| 71 // TODO(smcgruer): This fails when it should succeed. The underlying reason is |
| 72 // that doing the update for sticky in StyleWillChange doesn't work. The |
| 73 // Parent() appears not to be set yet. |
| 74 DirtyLayoutAndAssertLifecycle(document); |
| 75 document.getElementById("stickyChild")->getBoundingClientRect(); |
| 76 // EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 77 // document.Lifecycle().GetState()); |
| 78 } |
| 79 |
| 80 // Verifies that calling scrollIntoView* cleans compositor inputs. Cleaning the |
| 81 // compositor inputs is required so that position:sticky elements and their |
| 82 // descendants have the correct location. |
| 83 TEST(ElementTest, ScrollIntoViewUpdatesCompositorInputs) { |
| 84 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); |
| 85 Document& document = page_holder->GetDocument(); |
| 86 |
| 87 document.View()->UpdateAllLifecyclePhases(); |
| 88 EXPECT_EQ(DocumentLifecycle::kPaintClean, document.Lifecycle().GetState()); |
| 89 |
| 90 document.body()->setInnerHTML( |
| 91 "<div id='ancestor'>" |
| 92 " <div id='sticky' style='position:sticky;'>" |
| 93 " <div id='stickyChild'></div>" |
| 94 " </div>" |
| 95 " <div id='nonSticky'></div>" |
| 96 "</div>"); |
| 97 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, |
| 98 document.Lifecycle().GetState()); |
| 99 |
| 100 // Scrolling any element that is not affected by a sticky element should only |
| 101 // advance us to layout clean. |
| 102 document.getElementById("ancestor")->scrollIntoView(); |
| 103 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 104 |
| 105 document.getElementById("nonSticky")->scrollIntoView(); |
| 106 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 107 |
| 108 document.getElementById("ancestor")->scrollIntoViewIfNeeded(); |
| 109 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 110 |
| 111 document.getElementById("nonSticky")->scrollIntoViewIfNeeded(); |
| 112 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 113 |
| 114 // However, scrolling based on either the sticky element or it's descendents |
| 115 // should clean compositing inputs as well. |
| 116 document.getElementById("sticky")->scrollIntoView(); |
| 117 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 118 document.Lifecycle().GetState()); |
| 119 |
| 120 DirtyLayoutAndAssertLifecycle(document); |
| 121 // TODO(smcgruer): This fails when it should succeed. |
| 122 document.getElementById("stickyChild")->scrollIntoView(); |
| 123 // EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 124 // document.Lifecycle().GetState()); |
| 125 |
| 126 DirtyLayoutAndAssertLifecycle(document); |
| 127 document.getElementById("sticky")->scrollIntoViewIfNeeded(); |
| 128 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 129 document.Lifecycle().GetState()); |
| 130 |
| 131 DirtyLayoutAndAssertLifecycle(document); |
| 132 // TODO(smcgruer): This fails when it should succeed. |
| 133 document.getElementById("stickyChild")->scrollIntoViewIfNeeded(); |
| 134 // EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 135 // document.Lifecycle().GetState()); |
| 136 } |
| 137 |
| 138 // Verifies that calling offsetTop/offsetLeft cleans compositor inputs. |
| 139 // Cleaning the compositor inputs is required so that position:sticky elements |
| 140 // and their descendants have the correct location. |
| 141 TEST(ElementTest, OffsetTopAndLeftUpdateCompositorInputs) { |
| 142 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); |
| 143 Document& document = page_holder->GetDocument(); |
| 144 |
| 145 document.View()->UpdateAllLifecyclePhases(); |
| 146 EXPECT_EQ(DocumentLifecycle::kPaintClean, document.Lifecycle().GetState()); |
| 147 |
| 148 document.body()->setInnerHTML( |
| 149 "<div id='ancestor'>" |
| 150 " <div id='sticky' style='position:sticky;'>" |
| 151 " <div id='stickyChild'></div>" |
| 152 " </div>" |
| 153 " <div id='nonSticky'></div>" |
| 154 "</div>"); |
| 155 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, |
| 156 document.Lifecycle().GetState()); |
| 157 |
| 158 // Asking for any element that is not affected by a sticky element should only |
| 159 // advance us to layout clean. |
| 160 document.getElementById("ancestor")->OffsetTop(); |
| 161 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 162 |
| 163 document.getElementById("ancestor")->OffsetLeft(); |
| 164 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 165 |
| 166 document.getElementById("nonSticky")->OffsetTop(); |
| 167 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 168 |
| 169 document.getElementById("nonSticky")->OffsetLeft(); |
| 170 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); |
| 171 |
| 172 // However, asking for either the sticky element or it's descendents should |
| 173 // clean compositing inputs as well. |
| 174 document.getElementById("sticky")->OffsetTop(); |
| 175 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 176 document.Lifecycle().GetState()); |
| 177 |
| 178 DirtyLayoutAndAssertLifecycle(document); |
| 179 document.getElementById("sticky")->OffsetLeft(); |
| 180 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 181 document.Lifecycle().GetState()); |
| 182 |
| 183 DirtyLayoutAndAssertLifecycle(document); |
| 184 // TODO(smcgruer): This fails when it should succeed. |
| 185 document.getElementById("stickyChild")->OffsetTop(); |
| 186 // EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 187 // document.Lifecycle().GetState()); |
| 188 |
| 189 DirtyLayoutAndAssertLifecycle(document); |
| 190 // TODO(smcgruer): This fails when it should succeed. |
| 191 document.getElementById("stickyChild")->OffsetLeft(); |
| 192 // EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 193 // document.Lifecycle().GetState()); |
| 194 } |
| 195 |
| 196 TEST(ElementTest, OffsetTopAndLeftCorrectForStickyElementsAfterInsertion) { |
| 197 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); |
| 198 Document& document = page_holder->GetDocument(); |
| 199 |
| 200 document.body()->setInnerHTML( |
| 201 "<style>body { margin: 0 }" |
| 202 "#scroller { overflow: scroll; height: 100px; width: 100px; }" |
| 203 "#sticky { height: 25px; position: sticky; top: 0; left: 25px; }" |
| 204 "#padding { height: 500px; width: 300px; }</style>" |
| 205 "<div id='scroller'><div id='writer'></div><div id='sticky'></div>" |
| 206 "<div id='padding'></div></div>"); |
| 207 document.View()->UpdateAllLifecyclePhases(); |
| 208 |
| 209 Element* scroller = document.getElementById("scroller"); |
| 210 Element* writer = document.getElementById("writer"); |
| 211 Element* sticky = document.getElementById("sticky"); |
| 212 |
| 213 ASSERT_TRUE(scroller); |
| 214 ASSERT_TRUE(writer); |
| 215 ASSERT_TRUE(sticky); |
| 216 |
| 217 scroller->scrollTo(0, 200.0); |
| 218 |
| 219 // After scrolling, the position:sticky element should be offset to stay at |
| 220 // the top of the viewport. |
| 221 EXPECT_EQ(scroller->scrollTop(), sticky->OffsetTop()); |
| 222 |
| 223 // Insert a new <div> above the sticky. This will dirty layout. |
| 224 writer->setInnerHTML("<div style='height: 100px;'></div>"); |
| 225 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, |
| 226 document.Lifecycle().GetState()); |
| 227 |
| 228 // Querying the new offset of the sticky element should now cause both layout |
| 229 // and compositing inputs to be clean and should return the correct offset. |
| 230 int offset_top = sticky->OffsetTop(); |
| 231 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 232 document.Lifecycle().GetState()); |
| 233 EXPECT_FALSE(sticky->GetLayoutBoxModelObject() |
| 234 ->Layer() |
| 235 ->NeedsCompositingInputsUpdate()); |
| 236 EXPECT_EQ(scroller->scrollTop(), offset_top); |
| 237 |
| 238 scroller->scrollTo(50.0, 0); |
| 239 |
| 240 // After scrolling, the position:sticky element should be offset to stay 25 |
| 241 // pixels from the left of the viewport. |
| 242 EXPECT_EQ(scroller->scrollLeft() + 25, sticky->OffsetLeft()); |
| 243 |
| 244 // Insert a new <div> above the sticky. This will dirty layout. |
| 245 writer->setInnerHTML("<div style='width: 700px;'></div>"); |
| 246 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, |
| 247 document.Lifecycle().GetState()); |
| 248 |
| 249 // Again getting the offset should cause layout and compositing to be clean. |
| 250 int offset_left = sticky->OffsetLeft(); |
| 251 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, |
| 252 document.Lifecycle().GetState()); |
| 253 EXPECT_FALSE(sticky->GetLayoutBoxModelObject() |
| 254 ->Layer() |
| 255 ->NeedsCompositingInputsUpdate()); |
| 256 EXPECT_EQ(scroller->scrollLeft() + 25, offset_left); |
| 257 } |
| 258 |
| 26 } // namespace blink | 259 } // namespace blink |
| OLD | NEW |