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

const kDefaultEngineName = "engine1";

add_setup(async function () {
  useHttpServer();
  await AddonTestUtils.promiseStartupManager();
  await SearchTestUtils.useTestEngines("data1");
  Assert.ok(!Services.search.isInitialized);
  Services.prefs.setBoolPref(
    "browser.search.removeEngineInfobar.enabled",
    false
  );
});

// Check that the default engine matches the defaultenginename pref
add_task(async function test_defaultEngine() {
  await Services.search.init();
  await SearchTestUtils.promiseNewSearchEngine({
    url: `${gDataUrl}engine.xml`,
  });

  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);
});

// Setting the search engine should be persisted across restarts.
add_task(async function test_persistAcrossRestarts() {
  // Set the engine through the API.
  await Services.search.setDefault(
    Services.search.getEngineByName(kTestEngineName),
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  Assert.equal(Services.search.defaultEngine.name, kTestEngineName);
  await promiseAfterSettings();

  // Check that the a hash was saved.
  let metadata = await promiseGlobalMetadata();
  Assert.equal(metadata.defaultEngineIdHash.length, 44);

  // Re-init and check the engine is still the same.
  Services.search.wrappedJSObject.reset();
  await Services.search.init(true);
  Assert.equal(Services.search.defaultEngine.name, kTestEngineName);

  // Cleanup (set the engine back to default).
  Services.search.resetToAppDefaultEngine();
  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);
});

// An engine set without a valid hash should be ignored.
add_task(async function test_ignoreInvalidHash() {
  // Set the engine through the API.
  await Services.search.setDefault(
    Services.search.getEngineByName(kTestEngineName),
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  Assert.equal(Services.search.defaultEngine.name, kTestEngineName);
  await promiseAfterSettings();

  // Then mess with the file (make the hash invalid).
  let metadata = await promiseGlobalMetadata();
  metadata.defaultEngineIdHash = "invalid";
  await promiseSaveGlobalMetadata(metadata);

  // Re-init the search service, and check that the json file is ignored.
  Services.search.wrappedJSObject.reset();
  await Services.search.init(true);
  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);
});

// Resetting the engine to the default should remove the saved value.
add_task(async function test_settingToDefault() {
  // Set the engine through the API.
  await Services.search.setDefault(
    Services.search.getEngineByName(kTestEngineName),
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  Assert.equal(Services.search.defaultEngine.name, kTestEngineName);
  await promiseAfterSettings();

  // Check that the current engine was saved.
  let metadata = await promiseGlobalMetadata();
  let currentEngine = Services.search.getEngineByName(kTestEngineName);
  Assert.equal(metadata.defaultEngineId, currentEngine.id);

  // Then set the engine back to the default through the API.
  await Services.search.setDefault(
    Services.search.getEngineByName(kDefaultEngineName),
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  await promiseAfterSettings();

  // Check that the current engine is no longer saved in the JSON file.
  metadata = await promiseGlobalMetadata();
  Assert.equal(metadata.defaultEngineId, "");
});

add_task(async function test_resetToOriginalDefaultEngine() {
  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);

  await Services.search.setDefault(
    Services.search.getEngineByName(kTestEngineName),
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  Assert.equal(Services.search.defaultEngine.name, kTestEngineName);
  await promiseAfterSettings();

  Services.search.resetToAppDefaultEngine();
  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);
  await promiseAfterSettings();
});

add_task(async function test_fallback_kept_after_restart() {
  // Set current engine to a default engine that isn't the original default.
  let builtInEngines = await Services.search.getAppProvidedEngines();
  let nonDefaultBuiltInEngine;
  for (let engine of builtInEngines) {
    if (engine.name != kDefaultEngineName) {
      nonDefaultBuiltInEngine = engine;
      break;
    }
  }
  await Services.search.setDefault(
    nonDefaultBuiltInEngine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  Assert.equal(
    Services.search.defaultEngine.name,
    nonDefaultBuiltInEngine.name
  );
  await promiseAfterSettings();

  // Remove that engine...
  await Services.search.removeEngine(nonDefaultBuiltInEngine);
  // The engine being a default (built-in) one, it should be hidden
  // rather than actually removed.
  Assert.ok(nonDefaultBuiltInEngine.hidden);

  // Using the defaultEngine getter should force a fallback to the
  // original default engine.
  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);

  // Restoring the default engines should unhide our built-in test
  // engine, but not change the value of defaultEngine.
  Services.search.restoreDefaultEngines();
  Assert.ok(!nonDefaultBuiltInEngine.hidden);
  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);
  await promiseAfterSettings();

  // After a restart, the defaultEngine value should still be unchanged.
  Services.search.wrappedJSObject.reset();
  await Services.search.init(true);
  Assert.equal(Services.search.defaultEngine.name, kDefaultEngineName);
});