summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/svg/svg_use_element_test.cc
blob: e26665fc54a8c27546000b65493b9f21d3d69024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2019 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 "third_party/blink/renderer/core/svg/svg_element.h"

#include "third_party/blink/renderer/core/dom/dom_implementation.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/svg/svg_use_element.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"

namespace blink {

using LifecycleUpdateReason = DocumentUpdateReason;

class SVGUseElementTest : public PageTestBase {};

TEST_F(SVGUseElementTest, InstanceInvalidatedWhenNonAttachedTargetRemoved) {
  GetDocument().body()->setInnerHTML(R"HTML(
    <style></style>
    <svg>
        <unknown>
          <g id="parent">
            <a id="target">
          </g>
          <use id="use" href="#parent">
        </unknown>
    </svg>
  )HTML");
  GetDocument().View()->UpdateAllLifecyclePhases(LifecycleUpdateReason::kTest);

  // Remove #target.
  ASSERT_TRUE(GetDocument().getElementById("target"));
  GetDocument().getElementById("target")->remove();

  // This should cause a rebuild of the <use> shadow tree.
  GetDocument().View()->UpdateAllLifecyclePhases(LifecycleUpdateReason::kTest);

  // There should be no instance for #target anymore, since that element was
  // removed.
  auto* use = To<SVGUseElement>(GetDocument().getElementById("use"));
  ASSERT_TRUE(use);
  ASSERT_TRUE(use->GetShadowRoot());
  ASSERT_FALSE(use->GetShadowRoot()->getElementById("target"));
}

TEST_F(SVGUseElementTest,
       InstanceInvalidatedWhenNonAttachedTargetMovedInDocument) {
  GetDocument().body()->setInnerHTML(R"HTML(
    <svg>
      <use id="use" href="#path"/>
      <textPath id="path">
        <textPath>
          <a id="target" systemLanguage="th"></a>
        </textPath>
      </textPath>
    </svg>
  )HTML");
  GetDocument().View()->UpdateAllLifecyclePhases(LifecycleUpdateReason::kTest);

  // Move #target in the document (leaving it still "connected").
  Element* target = GetDocument().getElementById("target");
  ASSERT_TRUE(target);
  GetDocument().body()->appendChild(target);

  // This should cause a rebuild of the <use> shadow tree.
  GetDocument().View()->UpdateAllLifecyclePhases(LifecycleUpdateReason::kTest);

  // There should be no instance for #target anymore, since that element was
  // removed.
  auto* use = To<SVGUseElement>(GetDocument().getElementById("use"));
  ASSERT_TRUE(use);
  ASSERT_TRUE(use->GetShadowRoot());
  ASSERT_FALSE(use->GetShadowRoot()->getElementById("target"));
}

TEST_F(SVGUseElementTest, NullInstanceRootWhenNotConnectedToDocument) {
  GetDocument().body()->setInnerHTML(R"HTML(
    <svg>
      <defs>
        <rect id="r" width="100" height="100" fill="blue"/>
      </defs>
      <use id="target" href="#r"/>
    </svg>
  )HTML");
  GetDocument().View()->UpdateAllLifecyclePhases(LifecycleUpdateReason::kTest);

  auto* target = To<SVGUseElement>(GetDocument().getElementById("target"));
  ASSERT_TRUE(target);
  ASSERT_TRUE(target->InstanceRoot());

  target->remove();

  ASSERT_FALSE(target->InstanceRoot());
}

TEST_F(SVGUseElementTest, NullInstanceRootWhenConnectedToInactiveDocument) {
  GetDocument().body()->setInnerHTML(R"HTML(
    <svg>
      <defs>
        <rect id="r" width="100" height="100" fill="blue"/>
      </defs>
      <use id="target" href="#r"/>
    </svg>
  )HTML");
  GetDocument().View()->UpdateAllLifecyclePhases(LifecycleUpdateReason::kTest);

  auto* target = To<SVGUseElement>(GetDocument().getElementById("target"));
  ASSERT_TRUE(target);
  ASSERT_TRUE(target->InstanceRoot());

  Document* other_document =
      GetDocument().implementation().createHTMLDocument();
  other_document->body()->appendChild(target);

  ASSERT_FALSE(target->InstanceRoot());
}

TEST_F(SVGUseElementTest, NullInstanceRootWhenShadowTreePendingRebuild) {
  GetDocument().body()->setInnerHTML(R"HTML(
    <svg>
      <defs>
        <rect id="r" width="100" height="100" fill="blue"/>
      </defs>
      <use id="target" href="#r"/>
    </svg>
  )HTML");
  GetDocument().View()->UpdateAllLifecyclePhases(LifecycleUpdateReason::kTest);

  auto* target = To<SVGUseElement>(GetDocument().getElementById("target"));
  ASSERT_TRUE(target);
  ASSERT_TRUE(target->InstanceRoot());

  GetDocument().getElementById("r")->setAttribute(html_names::kWidthAttr, "50");

  ASSERT_FALSE(target->InstanceRoot());
}

}  // namespace blink