1 // Copyright 2014 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/dom/TreeScope.h" 7 8 #include "core/dom/Document.h" 9 #include "core/dom/Element.h" 10 #include "core/dom/shadow/ShadowRoot.h" 11 #include <gtest/gtest.h> 12 13 using namespace blink; 14 15 namespace { 16 17 TEST(TreeScopeTest, CommonAncestorOfSameTrees) 18 { 19 RefPtrWillBeRawPtr<Document> document = Document::create(); 20 EXPECT_EQ(document.get(), document->commonAncestorTreeScope(*document)); 21 22 RefPtrWillBeRawPtr<Element> html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); 23 document->appendChild(html, ASSERT_NO_EXCEPTION); 24 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = html->createShadowRoot(ASSERT_NO_EXCEPTION); 25 EXPECT_EQ(shadowRoot.get(), shadowRoot->commonAncestorTreeScope(*shadowRoot)); 26 } 27 28 TEST(TreeScopeTest, CommonAncestorOfInclusiveTrees) 29 { 30 // document 31 // | : Common ancestor is document. 32 // shadowRoot 33 34 RefPtrWillBeRawPtr<Document> document = Document::create(); 35 RefPtrWillBeRawPtr<Element> html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); 36 document->appendChild(html, ASSERT_NO_EXCEPTION); 37 RefPtrWillBeRawPtr<ShadowRoot> shadowRoot = html->createShadowRoot(ASSERT_NO_EXCEPTION); 38 39 EXPECT_EQ(document.get(), document->commonAncestorTreeScope(*shadowRoot)); 40 EXPECT_EQ(document.get(), shadowRoot->commonAncestorTreeScope(*document)); 41 } 42 43 TEST(TreeScopeTest, CommonAncestorOfSiblingTrees) 44 { 45 // document 46 // / \ : Common ancestor is document. 47 // A B 48 49 RefPtrWillBeRawPtr<Document> document = Document::create(); 50 RefPtrWillBeRawPtr<Element> html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); 51 document->appendChild(html, ASSERT_NO_EXCEPTION); 52 RefPtrWillBeRawPtr<Element> head = document->createElement("head", nullAtom, ASSERT_NO_EXCEPTION); 53 html->appendChild(head); 54 RefPtrWillBeRawPtr<Element> body = document->createElement("body", nullAtom, ASSERT_NO_EXCEPTION); 55 html->appendChild(body); 56 57 RefPtrWillBeRawPtr<ShadowRoot> shadowRootA = head->createShadowRoot(ASSERT_NO_EXCEPTION); 58 RefPtrWillBeRawPtr<ShadowRoot> shadowRootB = body->createShadowRoot(ASSERT_NO_EXCEPTION); 59 60 EXPECT_EQ(document.get(), shadowRootA->commonAncestorTreeScope(*shadowRootB)); 61 EXPECT_EQ(document.get(), shadowRootB->commonAncestorTreeScope(*shadowRootA)); 62 } 63 64 TEST(TreeScopeTest, CommonAncestorOfTreesAtDifferentDepths) 65 { 66 // document 67 // / \ : Common ancestor is document. 68 // Y B 69 // / 70 // A 71 72 RefPtrWillBeRawPtr<Document> document = Document::create(); 73 RefPtrWillBeRawPtr<Element> html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); 74 document->appendChild(html, ASSERT_NO_EXCEPTION); 75 RefPtrWillBeRawPtr<Element> head = document->createElement("head", nullAtom, ASSERT_NO_EXCEPTION); 76 html->appendChild(head); 77 RefPtrWillBeRawPtr<Element> body = document->createElement("body", nullAtom, ASSERT_NO_EXCEPTION); 78 html->appendChild(body); 79 80 RefPtrWillBeRawPtr<ShadowRoot> shadowRootY = head->createShadowRoot(ASSERT_NO_EXCEPTION); 81 RefPtrWillBeRawPtr<ShadowRoot> shadowRootB = body->createShadowRoot(ASSERT_NO_EXCEPTION); 82 83 RefPtrWillBeRawPtr<Element> divInY = document->createElement("div", nullAtom, ASSERT_NO_EXCEPTION); 84 shadowRootY->appendChild(divInY); 85 RefPtrWillBeRawPtr<ShadowRoot> shadowRootA = divInY->createShadowRoot(ASSERT_NO_EXCEPTION); 86 87 EXPECT_EQ(document.get(), shadowRootA->commonAncestorTreeScope(*shadowRootB)); 88 EXPECT_EQ(document.get(), shadowRootB->commonAncestorTreeScope(*shadowRootA)); 89 } 90 91 TEST(TreeScopeTest, CommonAncestorOfTreesInDifferentDocuments) 92 { 93 RefPtrWillBeRawPtr<Document> document1 = Document::create(); 94 RefPtrWillBeRawPtr<Document> document2 = Document::create(); 95 EXPECT_EQ(0, document1->commonAncestorTreeScope(*document2)); 96 EXPECT_EQ(0, document2->commonAncestorTreeScope(*document1)); 97 } 98 99 } // namespace 100