| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/editing/VisiblePosition.h" | 6 #include "core/editing/VisiblePosition.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/editing/EditingTestBase.h" |
| 9 #include "core/dom/Range.h" | |
| 10 #include "core/dom/Text.h" | |
| 11 #include "core/html/HTMLElement.h" | |
| 12 #include "core/testing/CoreTestHelpers.h" | |
| 13 #include "core/testing/DummyPageHolder.h" | |
| 14 #include <gtest/gtest.h> | |
| 15 | 9 |
| 16 namespace blink { | 10 namespace blink { |
| 17 | 11 |
| 18 namespace { | 12 class VisiblePositionTest : public EditingTestBase { |
| 19 | |
| 20 Position positionInDOMTree(Node& anchor, int offset) | |
| 21 { | |
| 22 return Position(&anchor, offset, Position::PositionIsOffsetInAnchor); | |
| 23 } | |
| 24 | |
| 25 PositionInComposedTree positionInComposedTree(Node& anchor, int offset) | |
| 26 { | |
| 27 return PositionInComposedTree(&anchor, offset, PositionInComposedTree::Posit
ionIsOffsetInAnchor); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 class VisiblePositionTest : public ::testing::Test { | |
| 33 protected: | |
| 34 void SetUp() override; | |
| 35 | |
| 36 Document& document() const { return m_dummyPageHolder->document(); } | |
| 37 | |
| 38 static PassRefPtrWillBeRawPtr<ShadowRoot> createShadowRootForElementWithIDAn
dSetInnerHTML(TreeScope&, const char* hostElementID, const char* shadowRootConte
nt); | |
| 39 | |
| 40 void setBodyContent(const char*); | |
| 41 PassRefPtrWillBeRawPtr<ShadowRoot> setShadowContent(const char*); | |
| 42 | |
| 43 private: | |
| 44 OwnPtr<DummyPageHolder> m_dummyPageHolder; | |
| 45 }; | 13 }; |
| 46 | 14 |
| 47 void VisiblePositionTest::SetUp() | |
| 48 { | |
| 49 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); | |
| 50 } | |
| 51 | |
| 52 PassRefPtrWillBeRawPtr<ShadowRoot> VisiblePositionTest::createShadowRootForEleme
ntWithIDAndSetInnerHTML(TreeScope& scope, const char* hostElementID, const char*
shadowRootContent) | |
| 53 { | |
| 54 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = scope.getElementById(AtomicStrin
g::fromUTF8(hostElementID))->createShadowRoot(ASSERT_NO_EXCEPTION); | |
| 55 shadowRoot->setInnerHTML(String::fromUTF8(shadowRootContent), ASSERT_NO_EXCE
PTION); | |
| 56 return shadowRoot.release(); | |
| 57 } | |
| 58 | |
| 59 void VisiblePositionTest::setBodyContent(const char* bodyContent) | |
| 60 { | |
| 61 document().body()->setInnerHTML(String::fromUTF8(bodyContent), ASSERT_NO_EXC
EPTION); | |
| 62 } | |
| 63 | |
| 64 PassRefPtrWillBeRawPtr<ShadowRoot> VisiblePositionTest::setShadowContent(const c
har* shadowContent) | |
| 65 { | |
| 66 return createShadowRootForElementWithIDAndSetInnerHTML(document(), "host", s
hadowContent); | |
| 67 } | |
| 68 | |
| 69 TEST_F(VisiblePositionTest, ShadowDistributedNodes) | 15 TEST_F(VisiblePositionTest, ShadowDistributedNodes) |
| 70 { | 16 { |
| 71 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</
b>33</p>"; | 17 const char* bodyContent = "<p id='host'>00<b id='one'>11</b><b id='two'>22</
b>33</p>"; |
| 72 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two>
</content><span id='s5'>55</span><content select=#one></content><span id='s6'>66
</span></a>"; | 18 const char* shadowContent = "<a><span id='s4'>44</span><content select=#two>
</content><span id='s5'>55</span><content select=#one></content><span id='s6'>66
</span></a>"; |
| 73 setBodyContent(bodyContent); | 19 setBodyContent(bodyContent); |
| 74 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent); | 20 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent); |
| 75 | 21 |
| 76 RefPtrWillBeRawPtr<Element> body = document().body(); | 22 RefPtrWillBeRawPtr<Element> body = document().body(); |
| 77 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE
PTION); | 23 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE
PTION); |
| 78 RefPtrWillBeRawPtr<Element> two = body->querySelector("#two", ASSERT_NO_EXCE
PTION); | 24 RefPtrWillBeRawPtr<Element> two = body->querySelector("#two", ASSERT_NO_EXCE
PTION); |
| 79 RefPtrWillBeRawPtr<Element> four = shadowRoot->querySelector("#s4", ASSERT_N
O_EXCEPTION); | 25 RefPtrWillBeRawPtr<Element> four = shadowRoot->querySelector("#s4", ASSERT_N
O_EXCEPTION); |
| 80 RefPtrWillBeRawPtr<Element> five = shadowRoot->querySelector("#s5", ASSERT_N
O_EXCEPTION); | 26 RefPtrWillBeRawPtr<Element> five = shadowRoot->querySelector("#s5", ASSERT_N
O_EXCEPTION); |
| 81 | 27 |
| 82 EXPECT_EQ(positionInDOMTree(*one->firstChild(), 0), canonicalPositionOf(posi
tionInDOMTree(*one, 0))); | 28 EXPECT_EQ(positionInDOMTree(*one->firstChild(), 0), canonicalPositionOf(posi
tionInDOMTree(*one, 0))); |
| 83 EXPECT_EQ(positionInDOMTree(*one->firstChild(), 2), canonicalPositionOf(posi
tionInDOMTree(*two, 0))); | 29 EXPECT_EQ(positionInDOMTree(*one->firstChild(), 2), canonicalPositionOf(posi
tionInDOMTree(*two, 0))); |
| 84 | 30 |
| 85 EXPECT_EQ(positionInComposedTree(*five->firstChild(), 2), canonicalPositionO
f(positionInComposedTree(*one, 0))); | 31 EXPECT_EQ(positionInComposedTree(*five->firstChild(), 2), canonicalPositionO
f(positionInComposedTree(*one, 0))); |
| 86 EXPECT_EQ(positionInComposedTree(*four->firstChild(), 2), canonicalPositionO
f(positionInComposedTree(*two, 0))); | 32 EXPECT_EQ(positionInComposedTree(*four->firstChild(), 2), canonicalPositionO
f(positionInComposedTree(*two, 0))); |
| 87 } | 33 } |
| 88 | 34 |
| 89 } // namespace blink | 35 } // namespace blink |
| OLD | NEW |