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

/* Test that user-set metadata isn't lost on engine update */

"use strict";

const KEYWORD = "keyword";
let timerManager;

add_task(async function setup() {
  let server = useHttpServer("");
  server.registerContentType("sjs", "sjs");
  await AddonTestUtils.promiseStartupManager();
  await Services.search.init();

  timerManager = Cc["@mozilla.org/updates/timer-manager;1"].getService(
    Ci.nsIUpdateTimerManager
  );
});

add_task(async function test_installEngine_with_updates_disabled() {
  const engineData = {
    baseURL: gDataUrl,
    name: "test engine",
    method: "GET",
    updateFile: "opensearch/simple.xml",
  };

  Services.prefs.setBoolPref(SearchUtils.BROWSER_SEARCH_PREF + "update", false);
  Assert.ok(
    !("search-engine-update-timer" in timerManager.wrappedJSObject._timers),
    "Should not have registered the update timer already"
  );

  await SearchTestUtils.promiseNewSearchEngine({
    url: `${gDataUrl}data/engineMaker.sjs?${JSON.stringify(engineData)}`,
  });

  Assert.ok(
    Services.search.getEngineByName("test engine"),
    "Should have added the test engine."
  );
  Assert.ok(
    !("search-engine-update-timer" in timerManager.wrappedJSObject._timers),
    "Should have not registered the update timer when updates are disabled"
  );
});

add_task(async function test_installEngine_with_updates_enabled() {
  const engineData = {
    baseURL: gDataUrl,
    name: "original engine",
    method: "GET",
    updateFile: "opensearch/simple.xml",
  };

  Services.prefs.setBoolPref(SearchUtils.BROWSER_SEARCH_PREF + "update", true);

  Assert.ok(
    !("search-engine-update-timer" in timerManager.wrappedJSObject._timers),
    "Should not have registered the update timer already"
  );

  let engine = await SearchTestUtils.promiseNewSearchEngine({
    url: `${gDataUrl}data/engineMaker.sjs?${JSON.stringify(engineData)}`,
  });

  Assert.ok(
    "search-engine-update-timer" in timerManager.wrappedJSObject._timers,
    "Should have registered the update timer"
  );

  engine.alias = KEYWORD;
  await Services.search.moveEngine(engine, 0);

  Assert.ok(
    !!Services.search.getEngineByName("original engine"),
    "Should be able to get the engine by the original name"
  );
  Assert.ok(
    !Services.search.getEngineByName("simple"),
    "Should not be able to get the engine by the new name"
  );
});

add_task(async function test_engineUpdate() {
  const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;

  let promiseUpdate = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );

  // set last update to 8 days ago, since the default interval is 7, then
  // trigger an update
  let engine = Services.search.getEngineByName("original engine");
  engine.wrappedJSObject.setAttr("updateexpir", Date.now() - ONE_DAY_IN_MS * 8);
  Services.search.QueryInterface(Ci.nsITimerCallback).notify(null);

  await promiseUpdate;

  Assert.equal(engine.name, "simple", "Should have updated the engine's name");

  Assert.equal(engine.alias, KEYWORD, "Should have kept the keyword");
  Assert.equal(
    engine.wrappedJSObject.getAttr("order"),
    1,
    "Should have kept the order"
  );

  Assert.ok(
    !!Services.search.getEngineByName("simple"),
    "Should be able to get the engine by the new name"
  );
  Assert.ok(
    !Services.search.getEngineByName("original engine"),
    "Should not be able to get the engine by the old name"
  );
});