summaryrefslogtreecommitdiffstats
path: root/uriloader/exthandler/tests/unit/test_defaults_handlerService.js
blob: 47fd18a642a487e3d93b91e061b55499ce9a6e87 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

XPCOMUtils.defineLazyServiceGetter(
  this,
  "gExternalProtocolService",
  "@mozilla.org/uriloader/external-protocol-service;1",
  "nsIExternalProtocolService"
);
ChromeUtils.defineESModuleGetters(this, {
  kHandlerList: "resource://gre/modules/handlers/HandlerList.sys.mjs",
});

add_task(async function test_check_defaults_get_added() {
  let protocols = Object.keys(kHandlerList.default.schemes);
  for (let protocol of protocols) {
    let protocolHandlerCount =
      kHandlerList.default.schemes[protocol].handlers.length;
    Assert.ok(
      gHandlerService.wrappedJSObject._store.data.schemes[protocol].stubEntry,
      `Expect stub for ${protocol}`
    );
    let info = gExternalProtocolService.getProtocolHandlerInfo(protocol, {});
    Assert.ok(
      info,
      `Should be able to get protocol handler info for ${protocol}`
    );
    let handlers = Array.from(
      info.possibleApplicationHandlers.enumerate(Ci.nsIHandlerApp)
    );
    handlers = handlers.filter(h => h instanceof Ci.nsIWebHandlerApp);
    Assert.equal(
      handlers.length,
      protocolHandlerCount,
      `Default web handlers for ${protocol} should match`
    );
    let { alwaysAskBeforeHandling, preferredAction } = info;
    // Actually store something, pretending there was a change:
    let infoToWrite = gExternalProtocolService.getProtocolHandlerInfo(
      protocol,
      {}
    );
    gHandlerService.store(infoToWrite);
    ok(
      !gHandlerService.wrappedJSObject._store.data.schemes[protocol].stubEntry,
      "Expect stub entry info to go away"
    );

    let newInfo = gExternalProtocolService.getProtocolHandlerInfo(protocol, {});
    Assert.equal(
      alwaysAskBeforeHandling,
      newInfo.alwaysAskBeforeHandling,
      protocol + " - always ask shouldn't change"
    );
    Assert.equal(
      preferredAction,
      newInfo.preferredAction,
      protocol + " - preferred action shouldn't change"
    );
    await deleteHandlerStore();
  }
});

add_task(async function test_check_default_modification() {
  Assert.ok(
    true,
    JSON.stringify(gHandlerService.wrappedJSObject._store.data.schemes.mailto)
  );
  Assert.ok(
    gHandlerService.wrappedJSObject._store.data.schemes.mailto.stubEntry,
    "Expect stub for mailto"
  );
  let mailInfo = gExternalProtocolService.getProtocolHandlerInfo("mailto", {});
  mailInfo.alwaysAskBeforeHandling = false;
  mailInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
  gHandlerService.store(mailInfo);
  Assert.ok(
    !gHandlerService.wrappedJSObject._store.data.schemes.mailto.stubEntry,
    "Stub entry should be removed immediately."
  );
  let newMail = gExternalProtocolService.getProtocolHandlerInfo("mailto", {});
  Assert.equal(newMail.preferredAction, Ci.nsIHandlerInfo.useSystemDefault);
  Assert.equal(newMail.alwaysAskBeforeHandling, false);
  await deleteHandlerStore();
});

add_task(async function test_migrations() {
  const kTestData = [
    ["A", "http://compose.mail.yahoo.co.jp/ym/Compose?To=%s"],
    ["B", "http://www.inbox.lv/rfc2368/?value=%s"],
    ["C", "http://poczta.interia.pl/mh/?mailto=%s"],
    ["D", "http://win.mail.ru/cgi-bin/sentmsg?mailto=%s"],
  ];
  // Set up the test handlers. This doesn't use prefs like the previous test,
  // because we now refuse to import insecure handler prefs. They can only
  // exist if they were added into the handler store before this restriction
  // was created (bug 1526890).
  gHandlerService.wrappedJSObject._injectDefaultProtocolHandlers();
  let handler = gExternalProtocolService.getProtocolHandlerInfo("mailto");
  while (handler.possibleApplicationHandlers.length) {
    handler.possibleApplicationHandlers.removeElementAt(0);
  }
  for (let [name, uriTemplate] of kTestData) {
    let app = Cc["@mozilla.org/uriloader/web-handler-app;1"].createInstance(
      Ci.nsIWebHandlerApp
    );
    app.uriTemplate = uriTemplate;
    app.name = name;
    handler.possibleApplicationHandlers.appendElement(app);
  }
  gHandlerService.store(handler);

  // Now migrate them:
  Services.prefs.setCharPref("browser.handlers.migrations", "blah,secure-mail");
  gHandlerService.wrappedJSObject._migrateProtocolHandlersIfNeeded();

  // Now check the result:
  handler = gExternalProtocolService.getProtocolHandlerInfo("mailto");

  let expectedURIs = new Set([
    "https://mail.yahoo.co.jp/compose/?To=%s",
    "https://mail.inbox.lv/compose?to=%s",
    "https://poczta.interia.pl/mh/?mailto=%s",
    "https://e.mail.ru/cgi-bin/sentmsg?mailto=%s",
  ]);

  let possibleApplicationHandlers = Array.from(
    handler.possibleApplicationHandlers.enumerate(Ci.nsIWebHandlerApp)
  );
  // Set iterators are stable to deletion, so this works:
  for (let expected of expectedURIs) {
    for (let app of possibleApplicationHandlers) {
      if (app instanceof Ci.nsIWebHandlerApp && app.uriTemplate == expected) {
        Assert.ok(true, "Found handler with URI " + expected);
        // ... even when we remove items.
        expectedURIs.delete(expected);
        break;
      }
    }
  }
  Assert.equal(expectedURIs.size, 0, "Should have seen all the expected URIs.");

  for (let app of possibleApplicationHandlers) {
    if (app instanceof Ci.nsIWebHandlerApp) {
      Assert.ok(
        !kTestData.some(n => n[1] == app.uriTemplate),
        "Should not be any of the original handlers"
      );
    }
  }

  Assert.ok(
    !handler.preferredApplicationHandler,
    "Shouldn't have preferred handler initially."
  );
  await deleteHandlerStore();
});