Chromium Code Reviews| 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 DirtyLayoutAndAssertLifecycle(document); | |
| 72 document.getElementById("stickyChild")->getBoundingClientRect(); | |
| 73 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 74 document.Lifecycle().GetState()); | |
| 75 } | |
| 76 | |
| 77 // Verifies that calling scrollIntoView* cleans compositor inputs. Cleaning the | |
| 78 // compositor inputs is required so that position:sticky elements and their | |
| 79 // descendants have the correct location. | |
| 80 TEST(ElementTest, ScrollIntoViewUpdatesCompositorInputs) { | |
| 81 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); | |
| 82 Document& document = page_holder->GetDocument(); | |
| 83 | |
| 84 document.View()->UpdateAllLifecyclePhases(); | |
| 85 EXPECT_EQ(DocumentLifecycle::kPaintClean, document.Lifecycle().GetState()); | |
| 86 | |
| 87 document.body()->setInnerHTML( | |
| 88 "<div id='ancestor'>" | |
| 89 " <div id='sticky' style='position:sticky;'>" | |
| 90 " <div id='stickyChild'></div>" | |
| 91 " </div>" | |
| 92 " <div id='nonSticky'></div>" | |
| 93 "</div>"); | |
| 94 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, | |
| 95 document.Lifecycle().GetState()); | |
| 96 | |
| 97 // Scrolling any element that is not affected by a sticky element should only | |
| 98 // advance us to layout clean. | |
| 99 document.getElementById("ancestor")->scrollIntoView(); | |
| 100 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 101 | |
| 102 document.getElementById("nonSticky")->scrollIntoView(); | |
| 103 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 104 | |
| 105 document.getElementById("ancestor")->scrollIntoViewIfNeeded(); | |
| 106 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 107 | |
| 108 document.getElementById("nonSticky")->scrollIntoViewIfNeeded(); | |
| 109 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 110 | |
| 111 // However, scrolling based on either the sticky element or it's descendents | |
| 112 // should clean compositing inputs as well. | |
| 113 document.getElementById("sticky")->scrollIntoView(); | |
| 114 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 115 document.Lifecycle().GetState()); | |
| 116 | |
| 117 DirtyLayoutAndAssertLifecycle(document); | |
| 118 document.getElementById("stickyChild")->scrollIntoView(); | |
| 119 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 120 document.Lifecycle().GetState()); | |
| 121 | |
| 122 DirtyLayoutAndAssertLifecycle(document); | |
| 123 document.getElementById("sticky")->scrollIntoViewIfNeeded(); | |
| 124 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 125 document.Lifecycle().GetState()); | |
| 126 | |
| 127 DirtyLayoutAndAssertLifecycle(document); | |
| 128 document.getElementById("stickyChild")->scrollIntoViewIfNeeded(); | |
| 129 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 130 document.Lifecycle().GetState()); | |
| 131 } | |
| 132 | |
| 133 // Verifies that calling offsetTop/offsetLeft cleans compositor inputs. | |
| 134 // Cleaning the compositor inputs is required so that position:sticky elements | |
| 135 // and their descendants have the correct location. | |
| 136 TEST(ElementTest, OffsetTopAndLeftUpdateCompositorInputs) { | |
| 137 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); | |
| 138 Document& document = page_holder->GetDocument(); | |
| 139 | |
| 140 document.View()->UpdateAllLifecyclePhases(); | |
| 141 EXPECT_EQ(DocumentLifecycle::kPaintClean, document.Lifecycle().GetState()); | |
| 142 | |
| 143 document.body()->setInnerHTML( | |
| 144 "<div id='ancestor'>" | |
| 145 " <div id='sticky' style='position:sticky;'>" | |
| 146 " <div id='stickyChild'></div>" | |
| 147 " </div>" | |
| 148 " <div id='nonSticky'></div>" | |
| 149 "</div>"); | |
| 150 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, | |
| 151 document.Lifecycle().GetState()); | |
| 152 | |
| 153 // Asking for any element that is not affected by a sticky element should only | |
| 154 // advance us to layout clean. | |
| 155 document.getElementById("ancestor")->OffsetTop(); | |
| 156 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 157 | |
| 158 document.getElementById("ancestor")->OffsetLeft(); | |
| 159 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 160 | |
| 161 document.getElementById("nonSticky")->OffsetTop(); | |
| 162 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 163 | |
| 164 document.getElementById("nonSticky")->OffsetLeft(); | |
| 165 EXPECT_EQ(DocumentLifecycle::kLayoutClean, document.Lifecycle().GetState()); | |
| 166 | |
| 167 // However, asking for either the sticky element or it's descendents should | |
| 168 // clean compositing inputs as well. | |
| 169 document.getElementById("sticky")->OffsetTop(); | |
| 170 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 171 document.Lifecycle().GetState()); | |
| 172 | |
| 173 DirtyLayoutAndAssertLifecycle(document); | |
| 174 document.getElementById("sticky")->OffsetLeft(); | |
| 175 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 176 document.Lifecycle().GetState()); | |
| 177 | |
| 178 DirtyLayoutAndAssertLifecycle(document); | |
| 179 document.getElementById("stickyChild")->OffsetTop(); | |
| 180 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 181 document.Lifecycle().GetState()); | |
| 182 | |
| 183 DirtyLayoutAndAssertLifecycle(document); | |
| 184 document.getElementById("stickyChild")->OffsetLeft(); | |
| 185 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 186 document.Lifecycle().GetState()); | |
| 187 } | |
| 188 | |
| 189 TEST(ElementTest, OffsetTopAndLeftCorrectForStickyElementsAfterInsertion) { | |
| 190 std::unique_ptr<DummyPageHolder> page_holder = DummyPageHolder::Create(); | |
| 191 Document& document = page_holder->GetDocument(); | |
| 192 | |
| 193 document.body()->setInnerHTML( | |
| 194 "<style>body { margin: 0 }" | |
| 195 "#scroller { overflow: scroll; height: 100px; width: 100px; }" | |
| 196 "#sticky { height: 25px; position: sticky; top: 0; left: 25px; }" | |
| 197 "#padding { height: 500px; width: 300px; }</style>" | |
| 198 "<div id='scroller'><div id='writer'></div><div id='sticky'></div>" | |
| 199 "<div id='padding'></div></div>"); | |
| 200 document.View()->UpdateAllLifecyclePhases(); | |
| 201 | |
| 202 Element* scroller = document.getElementById("scroller"); | |
| 203 Element* writer = document.getElementById("writer"); | |
| 204 Element* sticky = document.getElementById("sticky"); | |
| 205 | |
| 206 ASSERT_TRUE(scroller); | |
| 207 ASSERT_TRUE(writer); | |
| 208 ASSERT_TRUE(sticky); | |
| 209 | |
| 210 scroller->scrollTo(0, 200.0); | |
| 211 | |
| 212 // After scrolling, the position:sticky element should be offset to stay at | |
| 213 // the top of the viewport. | |
| 214 EXPECT_EQ(scroller->scrollTop(), sticky->OffsetTop()); | |
| 215 | |
| 216 // Insert a new <div> above the sticky. This will dirty layout. | |
| 217 writer->setInnerHTML("<div style='height: 100px;'></div>"); | |
| 218 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, | |
| 219 document.Lifecycle().GetState()); | |
| 220 | |
| 221 // Querying the new offset of the sticky element should now cause both layout | |
| 222 // and compositing inputs to be clean and should return the correct offset. | |
| 223 int offset_top = sticky->OffsetTop(); | |
| 224 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 225 document.Lifecycle().GetState()); | |
| 226 EXPECT_FALSE(sticky->GetLayoutBoxModelObject() | |
| 227 ->Layer() | |
| 228 ->NeedsCompositingInputsUpdate()); | |
| 229 EXPECT_EQ(scroller->scrollTop(), offset_top); | |
| 230 | |
| 231 scroller->scrollTo(50.0, 0); | |
| 232 | |
| 233 // After scrolling, the position:sticky element should be offset to stay 25 | |
| 234 // pixels from the left of the viewport. | |
| 235 EXPECT_EQ(scroller->scrollLeft() + 25, sticky->OffsetLeft()); | |
| 236 | |
| 237 // Insert a new <div> above the sticky. This will dirty layout. | |
| 238 writer->setInnerHTML("<div style='width: 700px;'></div>"); | |
| 239 EXPECT_EQ(DocumentLifecycle::kVisualUpdatePending, | |
| 240 document.Lifecycle().GetState()); | |
| 241 | |
| 242 // Again getting the offset should cause layout and compositing to be clean. | |
| 243 int offset_left = sticky->OffsetLeft(); | |
| 244 EXPECT_EQ(DocumentLifecycle::kCompositingInputsClean, | |
| 245 document.Lifecycle().GetState()); | |
| 246 EXPECT_FALSE(sticky->GetLayoutBoxModelObject() | |
| 247 ->Layer() | |
| 248 ->NeedsCompositingInputsUpdate()); | |
| 249 EXPECT_EQ(scroller->scrollLeft() + 25, offset_left); | |
| 250 } | |
| 251 | |
|
flackr
2017/04/27 19:20:51
Can you test isInStickySubtree addition (which of
| |
| 26 } // namespace blink | 252 } // namespace blink |
| OLD | NEW |