summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_inspector-search-front.html
blob: e0f8f77e8e0e03dcd6592fb7392d79db4c0f288c (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=835896
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 835896</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript;version=1.8" src="inspector-helpers.js"></script>
  <script type="application/javascript;version=1.8">
window.onload = function() {
  const Cu = Components.utils;
  const {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
  const promise = require("promise");
  const {InspectorFront} = require("devtools/shared/fronts/inspector");
  const {console} = Cu.import("resource://gre/modules/Console.jsm", {});

  SimpleTest.waitForExplicitFinish();

  let walkerFront = null;
  let inspectee = null;
  let inspector = null;

  // WalkerFront specific tests.  These aren't to excercise search
  // edge cases so much as to test the state the Front maintains between
  // searches.
  // See also test_inspector-search.html

  addAsyncTest(function* setup() {
    info ("Setting up inspector and walker actors.");

    let url = document.getElementById("inspectorContent").href;

    yield new promise(resolve => {
      attachURL(url, function(err, client, tab, doc) {
        inspectee = doc;
        inspector = InspectorFront(client, tab);
        resolve();
      });
    });

    walkerFront = yield inspector.getWalker();
    ok(walkerFront, "getWalker() should return an actor.");

    runNextTest();
  });

  addAsyncTest(function* testWalkerFrontDefaults() {
    info ("Testing search API using WalkerFront.");
    let nodes = yield walkerFront.querySelectorAll(walkerFront.rootNode, "h2");
    let fronts = yield nodes.items();

    let frontResult = yield walkerFront.search("");
    ok(!frontResult, "Null result on front when searching for ''");

    let results = yield walkerFront.search("h2");
    isDeeply(results, {
      node: fronts[0],
      type: "search",
      resultsIndex: 0,
      resultsLength: 3
    }, "Default options work");

    results = yield walkerFront.search("h2", { });
    isDeeply(results, {
      node: fronts[1],
      type: "search",
      resultsIndex: 1,
      resultsLength: 3
    }, "Search works with empty options");

    // Clear search data to remove result state on the front
    yield walkerFront.search("");
    runNextTest();
  });

  addAsyncTest(function* testMultipleSearches() {
    info ("Testing search API using WalkerFront (reverse=false)");
    let nodes = yield walkerFront.querySelectorAll(walkerFront.rootNode, "h2");
    let fronts = yield nodes.items();

    let results = yield walkerFront.search("h2");
    isDeeply(results, {
      node: fronts[0],
      type: "search",
      resultsIndex: 0,
      resultsLength: 3
    }, "Search works with multiple results (reverse=false)");

    results = yield walkerFront.search("h2");
    isDeeply(results, {
      node: fronts[1],
      type: "search",
      resultsIndex: 1,
      resultsLength: 3
    }, "Search works with multiple results (reverse=false)");

    results = yield walkerFront.search("h2");
    isDeeply(results, {
      node: fronts[2],
      type: "search",
      resultsIndex: 2,
      resultsLength: 3
    }, "Search works with multiple results (reverse=false)");

    results = yield walkerFront.search("h2");
    isDeeply(results, {
      node: fronts[0],
      type: "search",
      resultsIndex: 0,
      resultsLength: 3
    }, "Search works with multiple results (reverse=false)");

    // Clear search data to remove result state on the front
    yield walkerFront.search("");
    runNextTest();
  });

  addAsyncTest(function* testMultipleSearchesReverse() {
    info ("Testing search API using WalkerFront (reverse=true)");
    let nodes = yield walkerFront.querySelectorAll(walkerFront.rootNode, "h2");
    let fronts = yield nodes.items();

    let results = yield walkerFront.search("h2", {reverse: true});
    isDeeply(results, {
      node: fronts[2],
      type: "search",
      resultsIndex: 2,
      resultsLength: 3
    }, "Search works with multiple results (reverse=true)");

    results = yield walkerFront.search("h2", {reverse: true});
    isDeeply(results, {
      node: fronts[1],
      type: "search",
      resultsIndex: 1,
      resultsLength: 3
    }, "Search works with multiple results (reverse=true)");

    results = yield walkerFront.search("h2", {reverse: true});
    isDeeply(results, {
      node: fronts[0],
      type: "search",
      resultsIndex: 0,
      resultsLength: 3
    }, "Search works with multiple results (reverse=true)");

    results = yield walkerFront.search("h2", {reverse: true});
    isDeeply(results, {
      node: fronts[2],
      type: "search",
      resultsIndex: 2,
      resultsLength: 3
    }, "Search works with multiple results (reverse=true)");

    results = yield walkerFront.search("h2", {reverse: false});
    isDeeply(results, {
      node: fronts[0],
      type: "search",
      resultsIndex: 0,
      resultsLength: 3
    }, "Search works with multiple results (reverse=false)");

    // Clear search data to remove result state on the front
    yield walkerFront.search("");
    runNextTest();
  });


  addAsyncTest(function* testBackwardsCompat() {
    info ("Simulating a server that doesn't have the new search functionality.");
    walkerFront.traits.textSearch = false;
    let front = yield walkerFront.querySelector(walkerFront.rootNode, "h1");

    let results = yield walkerFront.search("h1");
    isDeeply(results, {
      node: front,
      type: "selector",
      resultsIndex: 0,
      resultsLength: 1
    }, "Only querySelectorAll results being returned");

    // Clear search data to remove result state on the front
    yield walkerFront.search("");

    // Reset the normal textSearch behavior
    walkerFront.traits.textSearch = true;

    results = yield walkerFront.search("h1");
    isDeeply(results, {
      node: front,
      type: "search",
      resultsIndex: 0,
      resultsLength: 3
    }, "Other results being included");

    // Clear search data to remove result state on the front
    yield walkerFront.search("");
    runNextTest();
  });

  runNextTest();
};
  </script>
</head>
<body>
<a id="inspectorContent" target="_blank" href="inspector-search-data.html">Test Document</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>