summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/interactions/browser_interactions_typing.js
blob: 6561a4d334ae33e774939bb34ff98fe3b409f890 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests reporting of typing interactions.
 */

"use strict";

const TEST_URL =
  "https://example.com/browser/browser/components/places/tests/browser/keyword_form.html";
const TEST_URL2 = "https://example.com/browser";
const TEST_URL3 =
  "https://example.com/browser/browser/base/content/test/contextMenu/subtst_contextmenu_input.html";

const sentence = "The quick brown fox jumps over the lazy dog.";
const sentenceFragments = [
  "The quick",
  " brown fox",
  " jumps over the lazy dog.",
];
const longSentence =
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ut purus a libero cursus scelerisque. In hac habitasse platea dictumst. Quisque posuere ante sed consequat volutpat.";

// For tests where it matters reduces the maximum time between keypresses to a length that we can
// afford to delay the test by.
const PREF_TYPING_DELAY = "browser.places.interactions.typing_timeout_ms";
const POST_TYPING_DELAY = 150;

async function sendTextToInput(browser, text) {
  await SpecialPowers.spawn(browser, [], function () {
    const input = content.document.querySelector(
      "#form1 > input[name='search']"
    );
    input.focus();
    input.value = ""; // Reset to later verify that the provided text matches the value
  });

  EventUtils.sendString(text);

  await SpecialPowers.spawn(browser, [{ text }], async function (args) {
    await ContentTaskUtils.waitForCondition(
      () =>
        content.document.querySelector("#form1 > input[name='search']").value ==
        args.text,
      "Text has been set on input"
    );
  });
}

add_task(async function test_load_and_navigate_away_no_keypresses() {
  await Interactions.reset();
  await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
    BrowserTestUtils.loadURIString(browser, TEST_URL2);
    await BrowserTestUtils.browserLoaded(browser, false, TEST_URL2);

    await assertDatabaseValues([
      {
        url: TEST_URL,
        keypresses: 0,
        exactTypingTime: 0,
      },
    ]);

    BrowserTestUtils.loadURIString(browser, "about:blank");
    await BrowserTestUtils.browserLoaded(browser, false, "about:blank");

    await assertDatabaseValues([
      {
        url: TEST_URL,
        keypresses: 0,
        exactTypingTime: 0,
      },
      {
        url: TEST_URL2,
        keypresses: 0,
        exactTypingTime: 0,
      },
    ]);
  });
});

add_task(async function test_load_type_and_navigate_away() {
  await Interactions.reset();

  await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
    await sendTextToInput(browser, sentence);

    BrowserTestUtils.loadURIString(browser, TEST_URL2);
    await BrowserTestUtils.browserLoaded(browser, false, TEST_URL2);

    await assertDatabaseValues([
      {
        url: TEST_URL,
        keypresses: sentence.length,
        typingTimeIsGreaterThan: 0,
      },
    ]);

    BrowserTestUtils.loadURIString(browser, "about:blank");
    await BrowserTestUtils.browserLoaded(browser, false, "about:blank");

    await assertDatabaseValues([
      {
        url: TEST_URL,
        keypresses: sentence.length,
        typingTimeIsGreaterThan: 0,
      },
      {
        url: TEST_URL2,
        keypresses: 0,
        exactTypingTime: 0,
      },
    ]);
  });
});

add_task(async function test_no_typing_close_tab() {
  await Interactions.reset();
  await BrowserTestUtils.withNewTab(TEST_URL, async browser => {});

  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: 0,
      exactTypingTime: 0,
    },
  ]);
});

add_task(async function test_typing_close_tab() {
  await Interactions.reset();

  await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
    await sendTextToInput(browser, sentence);
  });

  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: sentence.length,
      typingTimeIsGreaterThan: 0,
    },
  ]);
});

add_task(async function test_single_key_typing_and_delay() {
  await Interactions.reset();

  Services.prefs.setIntPref(PREF_TYPING_DELAY, POST_TYPING_DELAY);

  try {
    await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
      // Single keystrokes with a delay between each, are not considered typing
      const text = ["T", "h", "e"];

      for (let i = 0; i < text.length; i++) {
        await sendTextToInput(browser, text[i]);

        // We do need to wait here because typing is defined as a series of keystrokes followed by a delay.
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        await new Promise(r => setTimeout(r, POST_TYPING_DELAY * 2));
      }
    });
  } finally {
    Services.prefs.clearUserPref(PREF_TYPING_DELAY);
  }

  // Since we typed single keys with delays between each, there should be no typing added to the database
  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: 0,
      exactTypingTime: 0,
    },
  ]);
});

add_task(async function test_double_key_typing_and_delay() {
  await Interactions.reset();

  // Test three 2-key typing bursts.
  const text = ["Ab", "cd", "ef"];

  const testStartTime = Cu.now();

  Services.prefs.setIntPref(PREF_TYPING_DELAY, POST_TYPING_DELAY);

  try {
    await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
      for (let i = 0; i < text.length; i++) {
        await sendTextToInput(browser, text[i]);

        // We do need to wait here because typing is defined as a series of keystrokes followed by a delay.
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        await new Promise(r => setTimeout(r, POST_TYPING_DELAY * 2));
      }
    });
  } finally {
    Services.prefs.clearUserPref(PREF_TYPING_DELAY);
  }

  // All keys should be recorded
  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: text.reduce(
        (accumulator, current) => accumulator + current.length,
        0
      ),
      typingTimeIsLessThan: Cu.now() - testStartTime,
    },
  ]);
});

add_task(async function test_typing_and_delay() {
  await Interactions.reset();

  const testStartTime = Cu.now();

  Services.prefs.setIntPref(PREF_TYPING_DELAY, POST_TYPING_DELAY);

  try {
    await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
      for (let i = 0; i < sentenceFragments.length; i++) {
        await sendTextToInput(browser, sentenceFragments[i]);

        // We do need to wait here because typing is defined as a series of keystrokes followed by a delay.
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        await new Promise(r => setTimeout(r, POST_TYPING_DELAY * 2));
      }
    });
  } finally {
    Services.prefs.clearUserPref(PREF_TYPING_DELAY);
  }

  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: sentenceFragments.reduce(
        (accumulator, current) => accumulator + current.length,
        0
      ),
      typingTimeIsGreaterThan: 0,
      typingTimeIsLessThan: Cu.now() - testStartTime,
    },
  ]);
});

add_task(async function test_typing_and_reload() {
  await Interactions.reset();

  const testStartTime = Cu.now();

  await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
    await sendTextToInput(browser, sentenceFragments[0]);

    info("reload");
    browser.reload();
    await BrowserTestUtils.browserLoaded(browser, false, TEST_URL);

    // First typing should have been recorded
    await assertDatabaseValues([
      {
        url: TEST_URL,
        keypresses: sentenceFragments[0].length,
        typingTimeIsGreaterThan: 0,
      },
    ]);

    await sendTextToInput(browser, sentenceFragments[1]);

    info("reload");
    browser.reload();
    await BrowserTestUtils.browserLoaded(browser, false, TEST_URL);

    // Second typing should have been recorded
    await assertDatabaseValues([
      {
        url: TEST_URL,
        keypresses: sentenceFragments[0].length,
        typingTimeIsGreaterThan: 0,
        typingTimeIsLessThan: Cu.now() - testStartTime,
      },
      {
        url: TEST_URL,
        keypresses: sentenceFragments[1].length,
        typingTimeIsGreaterThan: 0,
        typingTimeIsLessThan: Cu.now() - testStartTime,
      },
    ]);
  });
}).skip(); // Bug 1749328 - intermittent failure: dropping the 2nd interaction after the 2nd reload

add_task(async function test_switch_tabs_no_typing() {
  await Interactions.reset();

  let tab1 = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: TEST_URL,
  });

  let tab2 = BrowserTestUtils.addTab(gBrowser, TEST_URL2);
  await BrowserTestUtils.browserLoaded(tab2.linkedBrowser, false, TEST_URL2);

  info("Switch to second tab");
  gBrowser.selectedTab = tab2;

  // Only the interaction of the first tab should be recorded so far, and with no typing
  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: 0,
      exactTypingTime: 0,
    },
  ]);

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

add_task(async function test_typing_switch_tabs() {
  await Interactions.reset();

  let tab1 = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: TEST_URL,
  });

  await sendTextToInput(tab1.linkedBrowser, sentence);

  let tab2 = BrowserTestUtils.addTab(gBrowser, TEST_URL3);
  await BrowserTestUtils.browserLoaded(tab2.linkedBrowser, false, TEST_URL3);

  info("Switch to second tab");
  await BrowserTestUtils.switchTab(gBrowser, tab2);

  // Only the interaction of the first tab should be recorded so far
  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: sentence.length,
      typingTimeIsGreaterThan: 0,
    },
  ]);

  const tab1TyingTime = await getDatabaseValue(TEST_URL, "typingTime");

  info("Switch back to first tab");
  await BrowserTestUtils.switchTab(gBrowser, tab1);

  // The interaction of the second tab should now be recorded (no typing)
  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: sentence.length,
      exactTypingTime: tab1TyingTime,
    },
    {
      url: TEST_URL3,
      keypresses: 0,
      exactTypingTime: 0,
    },
  ]);

  info("Switch back to the second tab");
  await BrowserTestUtils.switchTab(gBrowser, tab2);

  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: sentence.length,
      exactTypingTime: tab1TyingTime,
    },
    {
      url: TEST_URL3,
      keypresses: 0,
      exactTypingTime: 0,
    },
  ]);

  // Typing into the second tab
  await SpecialPowers.spawn(tab2.linkedBrowser, [], function () {
    const input = content.document.getElementById("input_text");
    input.focus();
  });
  await EventUtils.sendString(longSentence);
  await TestUtils.waitForTick();

  info("Switch back to first tab");
  await BrowserTestUtils.switchTab(gBrowser, tab1);

  // The interaction of the second tab should now also be recorded (with typing)
  await assertDatabaseValues([
    {
      url: TEST_URL,
      keypresses: sentence.length,
      exactTypingTime: tab1TyingTime,
    },
    {
      url: TEST_URL3,
      keypresses: longSentence.length,
      typingTimeIsGreaterThan: 0,
    },
  ]);

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});