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.
6 #include "core/dom/TreeScope.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h"
10 #include "core/dom/shadow/ShadowRoot.h"
11 #include <gtest/gtest.h>
13 using namespace blink;
17 TEST(TreeScopeTest, CommonAncestorOfSameTrees)
19 RefPtrWillBeRawPtr<Document> document = Document::create();
20 EXPECT_EQ(document.get(), document->commonAncestorTreeScope(*document));
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));
28 TEST(TreeScopeTest, CommonAncestorOfInclusiveTrees)
31 // | : Common ancestor is document.
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);
39 EXPECT_EQ(document.get(), document->commonAncestorTreeScope(*shadowRoot));
40 EXPECT_EQ(document.get(), shadowRoot->commonAncestorTreeScope(*document));
43 TEST(TreeScopeTest, CommonAncestorOfSiblingTrees)
46 // / \ : Common ancestor is document.
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);
57 RefPtrWillBeRawPtr<ShadowRoot> shadowRootA = head->createShadowRoot(ASSERT_NO_EXCEPTION);
58 RefPtrWillBeRawPtr<ShadowRoot> shadowRootB = body->createShadowRoot(ASSERT_NO_EXCEPTION);
60 EXPECT_EQ(document.get(), shadowRootA->commonAncestorTreeScope(*shadowRootB));
61 EXPECT_EQ(document.get(), shadowRootB->commonAncestorTreeScope(*shadowRootA));
64 TEST(TreeScopeTest, CommonAncestorOfTreesAtDifferentDepths)
67 // / \ : Common ancestor is document.
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);
80 RefPtrWillBeRawPtr<ShadowRoot> shadowRootY = head->createShadowRoot(ASSERT_NO_EXCEPTION);
81 RefPtrWillBeRawPtr<ShadowRoot> shadowRootB = body->createShadowRoot(ASSERT_NO_EXCEPTION);
83 RefPtrWillBeRawPtr<Element> divInY = document->createElement("div", nullAtom, ASSERT_NO_EXCEPTION);
84 shadowRootY->appendChild(divInY);
85 RefPtrWillBeRawPtr<ShadowRoot> shadowRootA = divInY->createShadowRoot(ASSERT_NO_EXCEPTION);
87 EXPECT_EQ(document.get(), shadowRootA->commonAncestorTreeScope(*shadowRootB));
88 EXPECT_EQ(document.get(), shadowRootB->commonAncestorTreeScope(*shadowRootA));
91 TEST(TreeScopeTest, CommonAncestorOfTreesInDifferentDocuments)
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));