Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "core/editing/VisiblePosition.h" | |
| 7 | |
| 8 #include "core/dom/Document.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 | |
| 16 namespace blink { | |
| 17 | |
| 18 namespace { | |
| 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 }; | |
| 46 | |
| 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) | |
|
tkent
2015/06/23 07:56:06
I saw this code multiple times. Can you merge the
| |
| 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) | |
| 70 { | |
| 71 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>"; | |
| 73 setBodyContent(bodyContent); | |
| 74 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = setShadowContent(shadowContent); | |
| 75 | |
| 76 RefPtrWillBeRawPtr<Element> body = document().body(); | |
| 77 RefPtrWillBeRawPtr<Element> one = body->querySelector("#one", ASSERT_NO_EXCE PTION); | |
| 78 RefPtrWillBeRawPtr<Element> two = body->querySelector("#two", ASSERT_NO_EXCE PTION); | |
| 79 RefPtrWillBeRawPtr<Element> four = shadowRoot->querySelector("#s4", ASSERT_N O_EXCEPTION); | |
| 80 RefPtrWillBeRawPtr<Element> five = shadowRoot->querySelector("#s5", ASSERT_N O_EXCEPTION); | |
| 81 | |
| 82 EXPECT_EQ(positionInDOMTree(*one->firstChild(), 0), canonicalPositionOf(posi tionInDOMTree(*one, 0))); | |
| 83 EXPECT_EQ(positionInDOMTree(*one->firstChild(), 2), canonicalPositionOf(posi tionInDOMTree(*two, 0))); | |
| 84 | |
| 85 EXPECT_EQ(positionInComposedTree(*five->firstChild(), 2), canonicalPositionO f(positionInComposedTree(*one, 0))); | |
| 86 EXPECT_EQ(positionInComposedTree(*four->firstChild(), 2), canonicalPositionO f(positionInComposedTree(*two, 0))); | |
| 87 } | |
| 88 | |
| 89 } // namespace blink | |
| OLD | NEW |