summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/hypertext/test_update.html
blob: 23ac8b2c8db2333be870dd4870aaaf4a37530dec (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html>
<head>
  <title>nsIHyper>TextAccessible in dynamic tests</title>
  <link rel="stylesheet" type="text/css"
        href="chrome://mochikit/content/tests/SimpleTest/test.css" />

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <script type="application/javascript"
          src="../common.js"></script>
  <script type="application/javascript"
          src="../events.js"></script>

  <script type="application/javascript">
    const kLinksCount = 128;
    function addLinks(aContainerID)
    {
      this.containerNode = getNode(aContainerID);

      this.eventSeq = [
        new invokerChecker(EVENT_REORDER, this.containerNode)
      ];

      this.invoke = function addLinks_invoke()
      {
        for (var jdx = 0; jdx < kLinksCount; jdx++) {
          var a = document.createElement("a");
          a.setAttribute("href", "mozilla.org");
          a.textContent = "mozilla";
          this.containerNode.appendChild(a);

          var span = document.createElement("span");
          span.textContent = " text ";
          this.containerNode.appendChild(span);
        }
      }

      this.finalCheck = function addLinks_finalCheck()
      {
        // getLinkAt and getLinkIndex.
        var htAcc = getAccessible(this.containerNode, [nsIAccessibleHyperText]);
        for (var jdx = 0; jdx < kLinksCount; jdx++) {
          var link = htAcc.getLinkAt(jdx);
          ok(link, "No link at index " + jdx + " for '" + aContainerID + "'");

          var linkIdx = htAcc.getLinkIndex(link);
          is(linkIdx, jdx, "Wrong link index for '" + aContainerID + "'!");
        }
      }

      this.getID = function addLinks_getID()
      {
        return "Add links for '" + aContainerID + "'";
      }
    }

    function updateText(aContainerID)
    {
      this.containerNode = getNode(aContainerID);
      this.container = getAccessible(this.containerNode, nsIAccessibleHyperText);
      this.text = this.container.firstChild;
      this.textNode = this.text.DOMNode;
      this.textLen = this.textNode.data.length;

      this.eventSeq = [
        new invokerChecker(EVENT_TEXT_INSERTED, this.containerNode)
      ];

      this.invoke = function updateText_invoke()
      {
        is(this.container.getLinkIndexAtOffset(this.textLen), 0,
           "Wrong intial text offsets!");

        this.text.DOMNode.appendData(" my");
      }

      this.finalCheck = function updateText_finalCheck()
      {
        is(this.container.getLinkIndexAtOffset(this.textLen), -1,
           "Text offsets weren't updated!");
      }

      this.getID = function updateText_getID()
      {
        return "update text for '" + aContainerID + "'";
      }
    }

    /**
     * Text offsets must be updated when hypertext child is removed.
     */
    function removeChild(aContainerID, aChildID, aInitialText, aFinalText)
    {
      this.containerNode = getNode(aContainerID);
      this.container = getAccessible(this.containerNode, nsIAccessibleText);
      this.childNode = getNode(aChildID);

      // Call first to getText so offsets are cached
      is(this.container.getText(0, -1), aInitialText,
         "Wrong text before child removal");

      this.eventSeq = [
        new invokerChecker(EVENT_REORDER, this.containerNode)
      ];

      this.invoke = function removeChild_invoke()
      {
        this.containerNode.removeChild(this.childNode);
      }

      this.finalCheck = function removeChild_finalCheck()
      {
        is(this.container.getText(0, -1), aFinalText,
           "Wrong text after child removal");
        is(this.container.characterCount, aFinalText.length,
           "Wrong text after child removal");
      }

      this.getID = function removeChild_getID()
      {
        return "check text after removing child from '" + aContainerID + "'";
      }
    }

    function removeFirstChild(aContainer)
    {
      this.ht = getAccessible(aContainer, [ nsIAccessibleHyperText ]);
      this.eventSeq = [
        new invokerChecker(EVENT_REORDER, aContainer)
      ];

      this.invoke = function removeFirstChild_invoke()
      {
        is(this.ht.linkCount, 2, "Wrong embedded objects count before removal");

        getNode(aContainer).removeChild(getNode(aContainer).firstElementChild);
      }

      this.finalCheck = function removeFirstChild_finalCheck()
      {
        // check list index before link count
        is(this.ht.getLinkIndex(this.ht.firstChild), 0, "Wrong child index");
        is(this.ht.linkCount, 1, "Wrong embedded objects count after removal");
      }

      this.getID = function removeFirstChild_getID()
      {
        return "Remove first child and check embedded object indeces";
      }
    }

    function removeLastChild(aContainer)
    {
      this.ht = getAccessible(aContainer, [ nsIAccessibleHyperText ]);
      this.eventSeq = [
        new invokerChecker(EVENT_REORDER, aContainer)
      ];

      this.invoke = function removeLastChild_invoke()
      {
        is(this.ht.linkCount, 1, "Wrong embedded objects count before removal");

        getNode(aContainer).removeChild(getNode(aContainer).lastElementChild);
      }

      this.finalCheck = function removeLastChild_finalCheck()
      {
        is(this.ht.linkCount, 0, "Wrong embedded objects count after removal");

        var link = null;
        try {
          link = this.ht.getLinkAt(0);
        } catch (e) { }
        ok(!link, "No embedded object is expected");
      }

      this.getID = function removeLastChild_getID()
      {
        return "Remove last child and try its embedded object";
      }
    }

    //gA11yEventDumpToConsole = true; // debug stuff

    var gQueue = null;
    function doTest()
    {
      gQueue = new eventQueue();
      gQueue.push(new addLinks("p1"));
      gQueue.push(new updateText("p2"));
      gQueue.push(new removeChild("div1","div2",
                                  "hello my good friend", "hello friend"));
      gQueue.push(new removeFirstChild("c4"));
      gQueue.push(new removeLastChild("c5"));

      gQueue.invoke(); // Will call SimpleTest.finish();
    }

    SimpleTest.waitForExplicitFinish();
    addA11yLoadEvent(doTest);
  </script>
</head>
<body>

  <a target="_blank"
     title="Cache links within hypertext accessible"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=572394">
    Mozilla Bug 572394
  </a>
  <a target="_blank"
     title="Text offsets don't get updated when text of first child text accessible is changed"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=625009">
    Mozilla Bug 625009
  </a>
  <a target="_blank"
     title="Crash in nsHyperTextAccessible::GetText()"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=630841">
    Mozilla Bug 630841
  </a><br>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test">
  </pre>

  <p id="p1"></p>
  <p id="p2"><b>hello</b><a>friend</a></p>
  <div id="div1">hello<span id="div2"> my<span id="div3"> good</span></span> friend</span></div>
  <form id="c4">
    <label for="c4_input">label</label>
    <input id="c4_input">
  </form>
  <div id="c5">TextLeaf<input id="c5_input"></div>
</body>
</html>