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/dom/shadow/ComposedTreeTraversal.h" | 6 #include "core/dom/shadow/ComposedTreeTraversal.h" |
7 | 7 |
8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/Element.h" | 10 #include "core/dom/Element.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 class ComposedTreeTraversalTest : public ::testing::Test { | 29 class ComposedTreeTraversalTest : public ::testing::Test { |
30 protected: | 30 protected: |
31 HTMLDocument& document() const; | 31 HTMLDocument& document() const; |
32 | 32 |
33 // Sets |mainHTML| to BODY element with |innerHTML| property and attaches | 33 // Sets |mainHTML| to BODY element with |innerHTML| property and attaches |
34 // shadow root to child with |shadowHTML|, then update distribution for | 34 // shadow root to child with |shadowHTML|, then update distribution for |
35 // calling member functions in |ComposedTreeTraversal|. | 35 // calling member functions in |ComposedTreeTraversal|. |
36 void setupSampleHTML(const char* mainHTML, const char* shadowHTML, unsigned)
; | 36 void setupSampleHTML(const char* mainHTML, const char* shadowHTML, unsigned)
; |
37 | 37 |
| 38 void setupDocumentTree(const char* mainHTML); |
| 39 |
38 void attachV0ShadowRoot(Element& shadowHost, const char* shadowInnerHTML); | 40 void attachV0ShadowRoot(Element& shadowHost, const char* shadowInnerHTML); |
| 41 void attachOpenShadowRoot(Element& shadowHost, const char* shadowInnerHTML); |
39 | 42 |
40 private: | 43 private: |
41 void SetUp() override; | 44 void SetUp() override; |
42 | 45 |
43 RefPtrWillBePersistent<HTMLDocument> m_document; | 46 RefPtrWillBePersistent<HTMLDocument> m_document; |
44 OwnPtr<DummyPageHolder> m_dummyPageHolder; | 47 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
45 }; | 48 }; |
46 | 49 |
47 void ComposedTreeTraversalTest::SetUp() | 50 void ComposedTreeTraversalTest::SetUp() |
48 { | 51 { |
(...skipping 10 matching lines...) Expand all Loading... |
59 void ComposedTreeTraversalTest::setupSampleHTML(const char* mainHTML, const char
* shadowHTML, unsigned index) | 62 void ComposedTreeTraversalTest::setupSampleHTML(const char* mainHTML, const char
* shadowHTML, unsigned index) |
60 { | 63 { |
61 RefPtrWillBeRawPtr<Element> body = document().body(); | 64 RefPtrWillBeRawPtr<Element> body = document().body(); |
62 body->setInnerHTML(String::fromUTF8(mainHTML), ASSERT_NO_EXCEPTION); | 65 body->setInnerHTML(String::fromUTF8(mainHTML), ASSERT_NO_EXCEPTION); |
63 RefPtrWillBeRawPtr<Element> shadowHost = toElement(NodeTraversal::childAt(*b
ody, index)); | 66 RefPtrWillBeRawPtr<Element> shadowHost = toElement(NodeTraversal::childAt(*b
ody, index)); |
64 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = shadowHost->createShadowRootInte
rnal(ShadowRootType::V0, ASSERT_NO_EXCEPTION); | 67 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = shadowHost->createShadowRootInte
rnal(ShadowRootType::V0, ASSERT_NO_EXCEPTION); |
65 shadowRoot->setInnerHTML(String::fromUTF8(shadowHTML), ASSERT_NO_EXCEPTION); | 68 shadowRoot->setInnerHTML(String::fromUTF8(shadowHTML), ASSERT_NO_EXCEPTION); |
66 body->updateDistribution(); | 69 body->updateDistribution(); |
67 } | 70 } |
68 | 71 |
| 72 void ComposedTreeTraversalTest::setupDocumentTree(const char* mainHTML) |
| 73 { |
| 74 RefPtrWillBeRawPtr<Element> body = document().body(); |
| 75 body->setInnerHTML(String::fromUTF8(mainHTML), ASSERT_NO_EXCEPTION); |
| 76 } |
| 77 |
69 void ComposedTreeTraversalTest::attachV0ShadowRoot(Element& shadowHost, const ch
ar* shadowInnerHTML) | 78 void ComposedTreeTraversalTest::attachV0ShadowRoot(Element& shadowHost, const ch
ar* shadowInnerHTML) |
70 { | 79 { |
71 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = shadowHost.createShadowRootInter
nal(ShadowRootType::V0, ASSERT_NO_EXCEPTION); | 80 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = shadowHost.createShadowRootInter
nal(ShadowRootType::V0, ASSERT_NO_EXCEPTION); |
72 shadowRoot->setInnerHTML(String::fromUTF8(shadowInnerHTML), ASSERT_NO_EXCEPT
ION); | 81 shadowRoot->setInnerHTML(String::fromUTF8(shadowInnerHTML), ASSERT_NO_EXCEPT
ION); |
73 document().body()->updateDistribution(); | 82 document().body()->updateDistribution(); |
74 } | 83 } |
75 | 84 |
| 85 void ComposedTreeTraversalTest::attachOpenShadowRoot(Element& shadowHost, const
char* shadowInnerHTML) |
| 86 { |
| 87 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = shadowHost.createShadowRootInter
nal(ShadowRootType::Open, ASSERT_NO_EXCEPTION); |
| 88 shadowRoot->setInnerHTML(String::fromUTF8(shadowInnerHTML), ASSERT_NO_EXCEPT
ION); |
| 89 document().body()->updateDistribution(); |
| 90 } |
| 91 |
76 void testCommonAncestor(Node* expectedResult, const Node& nodeA, const Node& nod
eB) | 92 void testCommonAncestor(Node* expectedResult, const Node& nodeA, const Node& nod
eB) |
77 { | 93 { |
78 Node* result1 = ComposedTreeTraversal::commonAncestor(nodeA, nodeB); | 94 Node* result1 = ComposedTreeTraversal::commonAncestor(nodeA, nodeB); |
79 EXPECT_EQ(expectedResult, result1) << "commonAncestor(" << nodeA.textContent
() << "," << nodeB.textContent() << ")"; | 95 EXPECT_EQ(expectedResult, result1) << "commonAncestor(" << nodeA.textContent
() << "," << nodeB.textContent() << ")"; |
80 Node* result2 = ComposedTreeTraversal::commonAncestor(nodeB, nodeA); | 96 Node* result2 = ComposedTreeTraversal::commonAncestor(nodeB, nodeA); |
81 EXPECT_EQ(expectedResult, result2) << "commonAncestor(" << nodeB.textContent
() << "," << nodeA.textContent() << ")"; | 97 EXPECT_EQ(expectedResult, result2) << "commonAncestor(" << nodeB.textContent
() << "," << nodeA.textContent() << ")"; |
82 } | 98 } |
83 | 99 |
84 // Test case for | 100 // Test case for |
85 // - childAt | 101 // - childAt |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 | 453 |
438 EXPECT_EQ(s21.get(), ComposedTreeTraversal::nextSibling(*m10)); | 454 EXPECT_EQ(s21.get(), ComposedTreeTraversal::nextSibling(*m10)); |
439 EXPECT_EQ(m10.get(), ComposedTreeTraversal::previousSibling(*s21)); | 455 EXPECT_EQ(m10.get(), ComposedTreeTraversal::previousSibling(*s21)); |
440 | 456 |
441 // ComposedTreeTraversal::traverseSiblings does not work for a node which is
not in a document composed tree. | 457 // ComposedTreeTraversal::traverseSiblings does not work for a node which is
not in a document composed tree. |
442 // e.g. The following test fails. The result of ComposedTreeTraversal::previ
ousSibling(*m11)) will be #m10, instead of nullptr. | 458 // e.g. The following test fails. The result of ComposedTreeTraversal::previ
ousSibling(*m11)) will be #m10, instead of nullptr. |
443 // RefPtrWillBeRawPtr<Element> m11 = body->querySelector("#m11", ASSERT_NO_E
XCEPTION); | 459 // RefPtrWillBeRawPtr<Element> m11 = body->querySelector("#m11", ASSERT_NO_E
XCEPTION); |
444 // EXPECT_EQ(nullptr, ComposedTreeTraversal::previousSibling(*m11)); | 460 // EXPECT_EQ(nullptr, ComposedTreeTraversal::previousSibling(*m11)); |
445 } | 461 } |
446 | 462 |
| 463 TEST_F(ComposedTreeTraversalTest, v1Simple) |
| 464 { |
| 465 const char* mainHTML = |
| 466 "<div id='host'>" |
| 467 "<div id='child1' slot='slot1'></div>" |
| 468 "<div id='child2' slot='slot2'></div>" |
| 469 "</div>"; |
| 470 const char* shadowHTML = |
| 471 "<div id='shadow-child1'></div>" |
| 472 "<slot name='slot1'></slot>" |
| 473 "<slot name='slot2'></slot>" |
| 474 "<div id='shadow-child2'></div>"; |
| 475 |
| 476 setupDocumentTree(mainHTML); |
| 477 RefPtrWillBeRawPtr<Element> body = document().body(); |
| 478 RefPtrWillBeRawPtr<Element> host = body->querySelector("#host", ASSERT_NO_EX
CEPTION); |
| 479 RefPtrWillBeRawPtr<Element> child1 = body->querySelector("#child1", ASSERT_N
O_EXCEPTION); |
| 480 RefPtrWillBeRawPtr<Element> child2 = body->querySelector("#child2", ASSERT_N
O_EXCEPTION); |
| 481 |
| 482 attachOpenShadowRoot(*host, shadowHTML); |
| 483 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = host->openShadowRoot(); |
| 484 RefPtrWillBeRawPtr<Element> slot1 = shadowRoot->querySelector("[name=slot1]"
, ASSERT_NO_EXCEPTION); |
| 485 RefPtrWillBeRawPtr<Element> slot2 = shadowRoot->querySelector("[name=slot2]"
, ASSERT_NO_EXCEPTION); |
| 486 RefPtrWillBeRawPtr<Element> shadowChild1 = shadowRoot->querySelector("#shado
w-child1", ASSERT_NO_EXCEPTION); |
| 487 RefPtrWillBeRawPtr<Element> shadowChild2 = shadowRoot->querySelector("#shado
w-child2", ASSERT_NO_EXCEPTION); |
| 488 |
| 489 EXPECT_EQ(shadowChild1.get(), ComposedTreeTraversal::firstChild(*host)); |
| 490 EXPECT_EQ(child1.get(), ComposedTreeTraversal::nextSibling(*shadowChild1)); |
| 491 EXPECT_EQ(child2.get(), ComposedTreeTraversal::nextSibling(*child1)); |
| 492 EXPECT_EQ(shadowChild2.get(), ComposedTreeTraversal::nextSibling(*child2)); |
| 493 } |
| 494 |
| 495 TEST_F(ComposedTreeTraversalTest, v1Redistribution) |
| 496 { |
| 497 const char* mainHTML = |
| 498 "<div id='d1'>" |
| 499 "<div id='d2' slot='d1-s1'></div>" |
| 500 "<div id='d3' slot='d1-s2'></div>" |
| 501 "<div id='d4' slot='nonexistent'></div>" |
| 502 "<div id='d5'></div>" |
| 503 "</div>" |
| 504 "<div id='d6'></div>"; |
| 505 const char* shadowHTML1 = |
| 506 "<div id='d1-1'>" |
| 507 "<div id='d1-2'></div>" |
| 508 "<slot id='d1-s0'></slot>" |
| 509 "<slot name='d1-s1' slot='d1-1-s1'></slot>" |
| 510 "<slot name='d1-s2'></slot>" |
| 511 "<div id='d1-3'></div>" |
| 512 "<div id='d1-4' slot='d1-1-s1'></div>" |
| 513 "</div>"; |
| 514 const char* shadowHTML2 = |
| 515 "<div id='d1-1-1'></div>" |
| 516 "<slot name='d1-1-s1'></slot>" |
| 517 "<slot name='d1-1-s2'></slot>" |
| 518 "<div id='d1-1-2'></div>"; |
| 519 |
| 520 setupDocumentTree(mainHTML); |
| 521 |
| 522 RefPtrWillBeRawPtr<Element> body = document().body(); |
| 523 RefPtrWillBeRawPtr<Element> d1 = body->querySelector("#d1", ASSERT_NO_EXCEPT
ION); |
| 524 RefPtrWillBeRawPtr<Element> d2 = body->querySelector("#d2", ASSERT_NO_EXCEPT
ION); |
| 525 RefPtrWillBeRawPtr<Element> d3 = body->querySelector("#d3", ASSERT_NO_EXCEPT
ION); |
| 526 RefPtrWillBeRawPtr<Element> d4 = body->querySelector("#d4", ASSERT_NO_EXCEPT
ION); |
| 527 RefPtrWillBeRawPtr<Element> d5 = body->querySelector("#d5", ASSERT_NO_EXCEPT
ION); |
| 528 RefPtrWillBeRawPtr<Element> d6 = body->querySelector("#d6", ASSERT_NO_EXCEPT
ION); |
| 529 |
| 530 attachOpenShadowRoot(*d1, shadowHTML1); |
| 531 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot1 = d1->openShadowRoot(); |
| 532 RefPtrWillBeRawPtr<Element> d11 = shadowRoot1->querySelector("#d1-1", ASSERT
_NO_EXCEPTION); |
| 533 RefPtrWillBeRawPtr<Element> d12 = shadowRoot1->querySelector("#d1-2", ASSERT
_NO_EXCEPTION); |
| 534 RefPtrWillBeRawPtr<Element> d13 = shadowRoot1->querySelector("#d1-3", ASSERT
_NO_EXCEPTION); |
| 535 RefPtrWillBeRawPtr<Element> d14 = shadowRoot1->querySelector("#d1-4", ASSERT
_NO_EXCEPTION); |
| 536 RefPtrWillBeRawPtr<Element> d1s0 = shadowRoot1->querySelector("#d1-s0", ASSE
RT_NO_EXCEPTION); |
| 537 RefPtrWillBeRawPtr<Element> d1s1 = shadowRoot1->querySelector("[name=d1-s1]"
, ASSERT_NO_EXCEPTION); |
| 538 RefPtrWillBeRawPtr<Element> d1s2 = shadowRoot1->querySelector("[name=d1-s2]"
, ASSERT_NO_EXCEPTION); |
| 539 |
| 540 attachOpenShadowRoot(*d11, shadowHTML2); |
| 541 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot2 = d11->openShadowRoot(); |
| 542 RefPtrWillBeRawPtr<Element> d111 = shadowRoot2->querySelector("#d1-1-1", ASS
ERT_NO_EXCEPTION); |
| 543 RefPtrWillBeRawPtr<Element> d112 = shadowRoot2->querySelector("#d1-1-2", ASS
ERT_NO_EXCEPTION); |
| 544 RefPtrWillBeRawPtr<Element> d11s1 = shadowRoot2->querySelector("[name=d1-1-s
1]", ASSERT_NO_EXCEPTION); |
| 545 RefPtrWillBeRawPtr<Element> d11s2 = shadowRoot2->querySelector("[name=d1-1-s
2]", ASSERT_NO_EXCEPTION); |
| 546 |
| 547 EXPECT_EQ(d11.get(), ComposedTreeTraversal::next(*d1)); |
| 548 EXPECT_EQ(d111.get(), ComposedTreeTraversal::next(*d11)); |
| 549 EXPECT_EQ(d2.get(), ComposedTreeTraversal::next(*d111)); |
| 550 EXPECT_EQ(d14.get(), ComposedTreeTraversal::next(*d2)); |
| 551 EXPECT_EQ(d112.get(), ComposedTreeTraversal::next(*d14)); |
| 552 EXPECT_EQ(d6.get(), ComposedTreeTraversal::next(*d112)); |
| 553 |
| 554 EXPECT_EQ(d112.get(), ComposedTreeTraversal::previous(*d6)); |
| 555 |
| 556 EXPECT_EQ(d11.get(), ComposedTreeTraversal::parent(*d111)); |
| 557 EXPECT_EQ(d11.get(), ComposedTreeTraversal::parent(*d112)); |
| 558 EXPECT_EQ(d11.get(), ComposedTreeTraversal::parent(*d2)); |
| 559 EXPECT_EQ(d11.get(), ComposedTreeTraversal::parent(*d14)); |
| 560 EXPECT_EQ(nullptr, ComposedTreeTraversal::parent(*d3)); |
| 561 EXPECT_EQ(nullptr, ComposedTreeTraversal::parent(*d4)); |
| 562 } |
| 563 |
447 } // namespace blink | 564 } // namespace blink |
OLD | NEW |