// 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 "sky/engine/config.h" #include "sky/engine/core/dom/TreeScope.h" #include #include "sky/engine/core/dom/Document.h" #include "sky/engine/core/dom/Element.h" #include "sky/engine/core/dom/shadow/ShadowRoot.h" using namespace blink; namespace { TEST(TreeScopeTest, CommonAncestorOfSameTrees) { RefPtr document = Document::create(); EXPECT_EQ(document.get(), document->commonAncestorTreeScope(*document)); RefPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtr shadowRoot = html->createShadowRoot(ASSERT_NO_EXCEPTION); EXPECT_EQ(shadowRoot.get(), shadowRoot->commonAncestorTreeScope(*shadowRoot)); } TEST(TreeScopeTest, CommonAncestorOfInclusiveTrees) { // document // | : Common ancestor is document. // shadowRoot RefPtr document = Document::create(); RefPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtr 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 RefPtr document = Document::create(); RefPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtr head = document->createElement("head", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(head); RefPtr body = document->createElement("body", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(body); RefPtr shadowRootA = head->createShadowRoot(ASSERT_NO_EXCEPTION); RefPtr 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 RefPtr document = Document::create(); RefPtr html = document->createElement("html", nullAtom, ASSERT_NO_EXCEPTION); document->appendChild(html, ASSERT_NO_EXCEPTION); RefPtr head = document->createElement("head", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(head); RefPtr body = document->createElement("body", nullAtom, ASSERT_NO_EXCEPTION); html->appendChild(body); RefPtr shadowRootY = head->createShadowRoot(ASSERT_NO_EXCEPTION); RefPtr shadowRootB = body->createShadowRoot(ASSERT_NO_EXCEPTION); RefPtr divInY = document->createElement("div", nullAtom, ASSERT_NO_EXCEPTION); shadowRootY->appendChild(divInY); RefPtr shadowRootA = divInY->createShadowRoot(ASSERT_NO_EXCEPTION); EXPECT_EQ(document.get(), shadowRootA->commonAncestorTreeScope(*shadowRootB)); EXPECT_EQ(document.get(), shadowRootB->commonAncestorTreeScope(*shadowRootA)); } TEST(TreeScopeTest, CommonAncestorOfTreesInDifferentDocuments) { RefPtr document1 = Document::create(); RefPtr document2 = Document::create(); EXPECT_EQ(0, document1->commonAncestorTreeScope(*document2)); EXPECT_EQ(0, document2->commonAncestorTreeScope(*document1)); } } // namespace