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>
15 TEST(TreeScopeTest
, CommonAncestorOfSameTrees
)
17 RefPtrWillBeRawPtr
<Document
> document
= Document::create();
18 EXPECT_EQ(document
.get(), document
->commonAncestorTreeScope(*document
));
20 RefPtrWillBeRawPtr
<Element
> html
= document
->createElement("html", nullAtom
, ASSERT_NO_EXCEPTION
);
21 document
->appendChild(html
, ASSERT_NO_EXCEPTION
);
22 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= html
->createShadowRootInternal(ShadowRootType::OpenByDefault
, ASSERT_NO_EXCEPTION
);
23 EXPECT_EQ(shadowRoot
.get(), shadowRoot
->commonAncestorTreeScope(*shadowRoot
));
26 TEST(TreeScopeTest
, CommonAncestorOfInclusiveTrees
)
29 // | : Common ancestor is document.
32 RefPtrWillBeRawPtr
<Document
> document
= Document::create();
33 RefPtrWillBeRawPtr
<Element
> html
= document
->createElement("html", nullAtom
, ASSERT_NO_EXCEPTION
);
34 document
->appendChild(html
, ASSERT_NO_EXCEPTION
);
35 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= html
->createShadowRootInternal(ShadowRootType::OpenByDefault
, ASSERT_NO_EXCEPTION
);
37 EXPECT_EQ(document
.get(), document
->commonAncestorTreeScope(*shadowRoot
));
38 EXPECT_EQ(document
.get(), shadowRoot
->commonAncestorTreeScope(*document
));
41 TEST(TreeScopeTest
, CommonAncestorOfSiblingTrees
)
44 // / \ : Common ancestor is document.
47 RefPtrWillBeRawPtr
<Document
> document
= Document::create();
48 RefPtrWillBeRawPtr
<Element
> html
= document
->createElement("html", nullAtom
, ASSERT_NO_EXCEPTION
);
49 document
->appendChild(html
, ASSERT_NO_EXCEPTION
);
50 RefPtrWillBeRawPtr
<Element
> head
= document
->createElement("head", nullAtom
, ASSERT_NO_EXCEPTION
);
51 html
->appendChild(head
);
52 RefPtrWillBeRawPtr
<Element
> body
= document
->createElement("body", nullAtom
, ASSERT_NO_EXCEPTION
);
53 html
->appendChild(body
);
55 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRootA
= head
->createShadowRootInternal(ShadowRootType::OpenByDefault
, ASSERT_NO_EXCEPTION
);
56 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRootB
= body
->createShadowRootInternal(ShadowRootType::OpenByDefault
, ASSERT_NO_EXCEPTION
);
58 EXPECT_EQ(document
.get(), shadowRootA
->commonAncestorTreeScope(*shadowRootB
));
59 EXPECT_EQ(document
.get(), shadowRootB
->commonAncestorTreeScope(*shadowRootA
));
62 TEST(TreeScopeTest
, CommonAncestorOfTreesAtDifferentDepths
)
65 // / \ : Common ancestor is document.
70 RefPtrWillBeRawPtr
<Document
> document
= Document::create();
71 RefPtrWillBeRawPtr
<Element
> html
= document
->createElement("html", nullAtom
, ASSERT_NO_EXCEPTION
);
72 document
->appendChild(html
, ASSERT_NO_EXCEPTION
);
73 RefPtrWillBeRawPtr
<Element
> head
= document
->createElement("head", nullAtom
, ASSERT_NO_EXCEPTION
);
74 html
->appendChild(head
);
75 RefPtrWillBeRawPtr
<Element
> body
= document
->createElement("body", nullAtom
, ASSERT_NO_EXCEPTION
);
76 html
->appendChild(body
);
78 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRootY
= head
->createShadowRootInternal(ShadowRootType::OpenByDefault
, ASSERT_NO_EXCEPTION
);
79 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRootB
= body
->createShadowRootInternal(ShadowRootType::OpenByDefault
, ASSERT_NO_EXCEPTION
);
81 RefPtrWillBeRawPtr
<Element
> divInY
= document
->createElement("div", nullAtom
, ASSERT_NO_EXCEPTION
);
82 shadowRootY
->appendChild(divInY
);
83 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRootA
= divInY
->createShadowRootInternal(ShadowRootType::OpenByDefault
, ASSERT_NO_EXCEPTION
);
85 EXPECT_EQ(document
.get(), shadowRootA
->commonAncestorTreeScope(*shadowRootB
));
86 EXPECT_EQ(document
.get(), shadowRootB
->commonAncestorTreeScope(*shadowRootA
));
89 TEST(TreeScopeTest
, CommonAncestorOfTreesInDifferentDocuments
)
91 RefPtrWillBeRawPtr
<Document
> document1
= Document::create();
92 RefPtrWillBeRawPtr
<Document
> document2
= Document::create();
93 EXPECT_EQ(0, document1
->commonAncestorTreeScope(*document2
));
94 EXPECT_EQ(0, document2
->commonAncestorTreeScope(*document1
));