summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_filter-autocomplete.js
blob: 974c18a4f20438a21c46bfb62c4139ccbfe9d10e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Test autocomplete based on filtering flags and requests
 */
const REQUESTS = [
  {
    url: "sjs_content-type-test-server.sjs?fmt=html&res=undefined&text=Sample",
  },
  {
    url:
      "sjs_content-type-test-server.sjs?fmt=html&res=undefined&text=Sample" +
      "&cookies=1",
  },
  { url: "sjs_content-type-test-server.sjs?fmt=css&text=sample" },
  { url: "sjs_content-type-test-server.sjs?fmt=js&text=sample" },
  { url: "sjs_content-type-test-server.sjs?fmt=font" },
  { url: "sjs_content-type-test-server.sjs?fmt=image" },
  { url: "sjs_content-type-test-server.sjs?fmt=audio" },
  { url: "sjs_content-type-test-server.sjs?fmt=video" },
  { url: "sjs_content-type-test-server.sjs?fmt=gzip" },
  { url: "sjs_status-codes-test-server.sjs?sts=304" },
];

function testAutocompleteContents(expected, document) {
  expected.forEach(function (item, i) {
    is(
      document.querySelector(
        `.devtools-autocomplete-listbox .autocomplete-item:nth-child(${i + 1})`
      ).textContent,
      item,
      `${expected[i]} found`
    );
  });
}

add_task(async function () {
  const { monitor } = await initNetMonitor(FILTERING_URL, { requestCount: 1 });
  const { document, store, windowRequire } = monitor.panelWin;
  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");

  store.dispatch(Actions.batchEnable(false));

  info("Starting test... ");

  // Let the requests load completely before the autocomplete tests begin
  // as autocomplete values also rely on the network requests.
  const waitNetwork = waitForNetworkEvents(monitor, REQUESTS.length);
  await performRequestsInContent(REQUESTS);
  await waitNetwork;

  EventUtils.synthesizeMouseAtCenter(
    document.querySelector(".devtools-filterinput"),
    {},
    document.defaultView
  );
  // Empty Mouse click should keep autocomplete hidden
  ok(
    !document.querySelector(".devtools-autocomplete-popup"),
    "Autocomplete Popup still hidden"
  );

  document.querySelector(".devtools-filterinput").focus();

  // Typing numbers that corresponds to status codes should invoke an autocomplete
  EventUtils.sendString("2");
  ok(
    document.querySelector(".devtools-autocomplete-popup"),
    "Autocomplete Popup Created"
  );
  testAutocompleteContents(
    [
      "status-code:200",
      "status-code:201",
      "status-code:202",
      "status-code:203",
      "status-code:204",
      "status-code:205",
      "status-code:206",
    ],
    document
  );
  EventUtils.synthesizeKey("KEY_Enter");
  is(
    document.querySelector(".devtools-filterinput").value,
    "status-code:200",
    "Value correctly set after Enter"
  );
  ok(
    !document.querySelector(".devtools-autocomplete-popup"),
    "Autocomplete Popup hidden after keyboard Enter key"
  );

  // Space separated tokens
  // The last token where autocomplete is available shall generate the popup
  EventUtils.sendString(" s");
  ok(
    document.querySelector(".devtools-autocomplete-popup"),
    "Autocomplete Popup Created"
  );
  testAutocompleteContents(
    [
      "scheme:",
      "set-cookie-domain:",
      "set-cookie-name:",
      "set-cookie-value:",
      "size:",
      "status-code:",
    ],
    document
  );

  EventUtils.sendString("c");
  testAutocompleteContents(["scheme:"], document);
  EventUtils.synthesizeKey("KEY_Tab");
  // Tab selection should hide autocomplete
  ok(
    document.querySelector(".devtools-autocomplete-popup"),
    "Autocomplete Popup alive with content values"
  );
  testAutocompleteContents(["scheme:http"], document);

  EventUtils.synthesizeKey("KEY_Enter");
  is(
    document.querySelector(".devtools-filterinput").value,
    "status-code:200 scheme:http",
    "Value correctly set after Enter"
  );
  ok(
    !document.querySelector(".devtools-autocomplete-popup"),
    "Autocomplete Popup hidden after keyboard Enter key"
  );

  // Space separated tokens
  // The last token where autocomplete is available shall generate the popup
  EventUtils.sendString(" pro");
  testAutocompleteContents(["protocol:"], document);

  // The new value of the text box should be previousTokens + latest value selected
  // First return selects "protocol:"
  EventUtils.synthesizeKey("KEY_Enter");
  // Second return selects "protocol:HTTP/1.1"
  EventUtils.synthesizeKey("KEY_Enter");
  is(
    document.querySelector(".devtools-filterinput").value,
    "status-code:200 scheme:http protocol:HTTP/1.1",
    "Tokenized click generates correct value in input box"
  );

  // Explicitly type in `flag:` renders autocomplete with values
  EventUtils.sendString(" status-code:");
  testAutocompleteContents(["status-code:200", "status-code:304"], document);

  // Typing the exact value closes autocomplete
  EventUtils.sendString("304");
  ok(
    !document.querySelector(".devtools-autocomplete-popup"),
    "Typing the exact value closes autocomplete"
  );

  // Check if mime-type has been correctly parsed out and values also get autocomplete
  EventUtils.sendString(" mime-type:text");
  testAutocompleteContents(
    ["mime-type:text/css", "mime-type:text/html", "mime-type:text/plain"],
    document
  );

  // The negative filter flags
  EventUtils.sendString(" -");
  testAutocompleteContents(
    [
      "-cause:",
      "-domain:",
      "-has-response-header:",
      "-initiator:",
      "-is:",
      "-larger-than:",
      "-method:",
      "-mime-type:",
      "-priority:",
      "-protocol:",
      "-regexp:",
      "-remote-ip:",
      "-scheme:",
      "-set-cookie-domain:",
      "-set-cookie-name:",
      "-set-cookie-value:",
      "-size:",
      "-status-code:",
      "-transferred-larger-than:",
      "-transferred:",
    ],
    document
  );

  // Autocomplete for negative filtering
  EventUtils.sendString("is:");
  testAutocompleteContents(
    ["-is:cached", "-is:from-cache", "-is:running"],
    document
  );

  await teardown(monitor);
});