// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "config.h" #include "core/dom/TreeScope.h" #include "core/dom/Document.h" #include "core/dom/Element.h" #include "core/dom/shadow/ShadowRoot.h" #include using namespace blink; namespace { TEST(TreeScopeTest, CommonAncestorOfSameTrees) { RefPtrWillBeRawPtr document = Document::create(); EXPECT_EQ(document.get(), document->commonAncestorTreeScope(*document)); RefPtrWillBeRawPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtrWillBeRawPtr shadowRoot = html->createShadowRoot(ASSERT_NO_EXCEPTION); EXPECT_EQ(shadowRoot.get(), shadowRoot->commonAncestorTreeScope(*shadowRoot)); } TEST(TreeScopeTest, CommonAncestorOfInclusiveTrees) { // document // | : Common ancestor is document. // shadowRoot RefPtrWillBeRawPtr document = Document::create(); RefPtrWillBeRawPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtrWillBeRawPtr shadowRoot = html->createShadowRoot(ASSERT_NO_EXCEPTION); EXPECT_EQ(document.get(), document->commonAncestorTreeScope(*shadowRoot)); EXPECT_EQ(document.get(), shadowRoot->commonAncestorTreeScope(*document)); } TEST(TreeScopeTest, CommonAncestorOfSiblingTrees) { // document // / \ : Common ancestor is document. // A B RefPtrWillBeRawPtr document = Document::create(); RefPtrWillBeRawPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtrWillBeRawPtr head = document->createElement("head", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(head); RefPtrWillBeRawPtr body = document->createElement("body", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(body); RefPtrWillBeRawPtr shadowRootA = head->createShadowRoot(ASSERT_NO_EXCEPTION); RefPtrWillBeRawPtr shadowRootB = body->createShadowRoot(ASSERT_NO_EXCEPTION); EXPECT_EQ(document.get(), shadowRootA->commonAncestorTreeScope(*shadowRootB)); EXPECT_EQ(document.get(), shadowRootB->commonAncestorTreeScope(*shadowRootA)); } TEST(TreeScopeTest, CommonAncestorOfTreesAtDifferentDepths) { // document // / \ : Common ancestor is document. // Y B // / // A RefPtrWillBeRawPtr document = Document::create(); RefPtrWillBeRawPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtrWillBeRawPtr head = document->createElement("head", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(head); RefPtrWillBeRawPtr body = document->createElement("body", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(body); RefPtrWillBeRawPtr shadowRootY = head->createShadowRoot(ASSERT_NO_EXCEPTION); RefPtrWillBeRawPtr shadowRootB = body->createShadowRoot(ASSERT_NO_EXCEPTION); RefPtrWillBeRawPtr divInY = document->createElement("div", nullAtom, ASSERT_NO_EXCEPTION); shadowRootY->appendChild(divInY); RefPtrWillBeRawPtr shadowRootA = divInY->createShadowRoot(ASSERT_NO_EXCEPTION); EXPECT_EQ(document.get(), shadowRootA->commonAncestorTreeScope(*shadowRootB)); EXPECT_EQ(document.get(), shadowRootB->commonAncestorTreeScope(*shadowRootA)); } TEST(TreeScopeTest, CommonAncestorOfTreesInDifferentDocuments) { RefPtrWillBeRawPtr document1 = Document::create(); RefPtrWillBeRawPtr document2 = Document::create(); EXPECT_EQ(0, document1->commonAncestorTreeScope(*document2)); EXPECT_EQ(0, document2->commonAncestorTreeScope(*document1)); } } // namespace