summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_getSubmission_params.js
blob: 1be81a2e31c40204398cdba50834c80662262f49 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_setup(async function () {
  await SearchTestUtils.useTestEngines("simple-engines");
  await AddonTestUtils.promiseStartupManager();
  await Services.search.init();
});

const searchTerms = "fxsearch";
function checkSubstitution(url, prefix, engine, template, expected) {
  url.template = prefix + template;
  equal(engine.getSubmission(searchTerms).uri.spec, prefix + expected);
}

add_task(async function test_paramSubstitution() {
  let prefix = "https://example.com/?sourceId=Mozilla-search&search=";
  let engine = await Services.search.getEngineByName("Simple Engine");
  let url = engine.wrappedJSObject._getURLOfType("text/html");
  equal(url.getSubmission("foo", engine).uri.spec, prefix + "foo");
  // Reset the engine parameters so we can have a clean template to use for
  // the subsequent tests.
  url.params = [];

  let check = checkSubstitution.bind(this, url, prefix, engine);

  // The same parameter can be used more than once.
  check("{searchTerms}/{searchTerms}", searchTerms + "/" + searchTerms);

  // Optional parameters are replaced if we known them.
  check("{searchTerms?}", searchTerms);
  check("{unknownOptional?}", "");
  check("{unknownRequired}", "{unknownRequired}");

  check("{language}", Services.locale.requestedLocale);
  check("{language?}", Services.locale.requestedLocale);

  engine.wrappedJSObject._queryCharset = "UTF-8";
  check("{inputEncoding}", "UTF-8");
  check("{inputEncoding?}", "UTF-8");
  check("{outputEncoding}", "UTF-8");
  check("{outputEncoding?}", "UTF-8");

  // 'Unsupported' parameters with hard coded values used only when the parameter is required.
  check("{count}", "20");
  check("{count?}", "");
  check("{startIndex}", "1");
  check("{startIndex?}", "");
  check("{startPage}", "1");
  check("{startPage?}", "");

  check("{moz:locale}", Services.locale.requestedLocale);
});

add_task(async function test_mozParamsFailForNonAppProvided() {
  await SearchTestUtils.installSearchExtension();

  let prefix = "https://example.com/?q=";
  let engine = await Services.search.getEngineByName("Example");
  let url = engine.wrappedJSObject._getURLOfType("text/html");
  equal(url.getSubmission("foo", engine).uri.spec, prefix + "foo");
  // Reset the engine parameters so we can have a clean template to use for
  // the subsequent tests.
  url.params = [];

  let check = checkSubstitution.bind(this, url, prefix, engine);

  // Test moz: parameters (only supported for built-in engines, ie _isDefault == true).
  check("{moz:locale}", "{moz:locale}");

  await promiseAfterSettings();
});