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

/**
 * Tests the hasEngineWithURL() method of the nsIBrowserSearchService.
 */
function run_test() {
  do_print("Setting up test");

  updateAppInfo();
  useHttpServer();

  do_print("Test starting");
  run_next_test();
}


// Return a discreet, cloned copy of an (engine) object.
function getEngineClone(engine) {
  return JSON.parse(JSON.stringify(engine));
}

// Check whether and engine does or doesn't exist.
function checkEngineState(exists, engine) {
  do_check_eq(exists, Services.search.hasEngineWithURL(engine.method,
                                                       engine.formURL,
                                                       engine.queryParams));
}

// Add a search engine for testing.
function addEngineWithParams(engine) {
  Services.search.addEngineWithDetails(engine.name, null, null, null,
                                       engine.method, engine.formURL);

  let addedEngine = Services.search.getEngineByName(engine.name);
  for (let param of engine.queryParams) {
    addedEngine.addParam(param.name, param.value, null);
  }
}

// Main test.
add_task(function* test_hasEngineWithURL() {
  // Avoid deprecated synchronous initialization.
  yield asyncInit();

  // Setup various Engine definitions for method tests.
  let UNSORTED_ENGINE = {
    name: "mySearch Engine",
    method: "GET",
    formURL: "https://totallyNotRealSearchEngine.com/",
    queryParams: [
      { name: "DDs", value: "38s" },
      { name: "DCs", value: "39s" },
      { name: "DDs", value: "39s" },
      { name: "DDs", value: "38s" },
      { name: "DDs", value: "37s" },
      { name: "DDs", value: "38s" },
      { name: "DEs", value: "38s" },
      { name: "DCs", value: "38s" },
      { name: "DEs", value: "37s" },
    ],
  };

  // Same as UNSORTED_ENGINE, but sorted.
  let SORTED_ENGINE = {
    name: "mySearch Engine",
    method: "GET",
    formURL: "https://totallyNotRealSearchEngine.com/",
    queryParams: [
      { name: "DCs", value: "38s" },
      { name: "DCs", value: "39s" },
      { name: "DDs", value: "37s" },
      { name: "DDs", value: "38s" },
      { name: "DDs", value: "38s" },
      { name: "DDs", value: "38s" },
      { name: "DDs", value: "39s" },
      { name: "DEs", value: "37s" },
      { name: "DEs", value: "38s" },
    ],
  };

  // Unique variations of the SORTED_ENGINE.
  let SORTED_ENGINE_METHOD_CHANGE = getEngineClone(SORTED_ENGINE);
  SORTED_ENGINE_METHOD_CHANGE.method = "PoST";

  let SORTED_ENGINE_FORMURL_CHANGE = getEngineClone(SORTED_ENGINE);
  SORTED_ENGINE_FORMURL_CHANGE.formURL = "http://www.ahighrpowr.com/"

  let SORTED_ENGINE_QUERYPARM_CHANGE = getEngineClone(SORTED_ENGINE);
  SORTED_ENGINE_QUERYPARM_CHANGE.queryParams = [];

  let SORTED_ENGINE_NAME_CHANGE = getEngineClone(SORTED_ENGINE);
  SORTED_ENGINE_NAME_CHANGE.name += " 2";


  // First ensure neither the unsorted engine, nor the same engine
  // with a pre-sorted list of query parms matches.
  checkEngineState(false, UNSORTED_ENGINE);
  do_print("The unsorted version of the test engine does not exist.");
  checkEngineState(false, SORTED_ENGINE);
  do_print("The sorted version of the test engine does not exist.");

  // Ensure variations of the engine definition do not match.
  checkEngineState(false, SORTED_ENGINE_METHOD_CHANGE);
  checkEngineState(false, SORTED_ENGINE_FORMURL_CHANGE);
  checkEngineState(false, SORTED_ENGINE_QUERYPARM_CHANGE);
  do_print("There are no modified versions of the sorted test engine.");

  // Note that this method doesn't check name variations.
  checkEngineState(false, SORTED_ENGINE_NAME_CHANGE);
  do_print("There is no NAME modified version of the sorted test engine.");


  // Add the unsorted engine and it's queryParams.
  addEngineWithParams(UNSORTED_ENGINE);
  do_print("The unsorted engine has been added.");


  // Then, ensure we find a match for the unsorted engine, and for the
  // same engine with a pre-sorted list of query parms.
  checkEngineState(true, UNSORTED_ENGINE);
  do_print("The unsorted version of the test engine now exists.");
  checkEngineState(true, SORTED_ENGINE);
  do_print("The sorted version of the same test engine also now exists.");

  // Ensure variations of the engine definition still do not match.
  checkEngineState(false, SORTED_ENGINE_METHOD_CHANGE);
  checkEngineState(false, SORTED_ENGINE_FORMURL_CHANGE);
  checkEngineState(false, SORTED_ENGINE_QUERYPARM_CHANGE);
  do_print("There are still no modified versions of the sorted test engine.");

  // Note that this method still doesn't check name variations.
  checkEngineState(true, SORTED_ENGINE_NAME_CHANGE);
  do_print("There IS now a NAME modified version of the sorted test engine.");
});