summaryrefslogtreecommitdiffstats
path: root/docshell/test/unit/test_URIFixup_external_protocol_fallback.js
blob: 0846070b7ad4ad36b9312efa82d71818c8ef2850 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// Test whether fallback mechanism is working if don't trust nsIExternalProtocolService.

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);
const { MockRegistrar } = ChromeUtils.importESModule(
  "resource://testing-common/MockRegistrar.sys.mjs"
);

add_task(async function setup() {
  info(
    "Prepare mock nsIExternalProtocolService whose externalProtocolHandlerExists returns always true"
  );
  const externalProtocolService = Cc[
    "@mozilla.org/uriloader/external-protocol-service;1"
  ].getService(Ci.nsIExternalProtocolService);
  const mockId = MockRegistrar.register(
    "@mozilla.org/uriloader/external-protocol-service;1",
    {
      getProtocolHandlerInfo: scheme =>
        externalProtocolService.getProtocolHandlerInfo(scheme),
      externalProtocolHandlerExists: () => true,
      QueryInterface: ChromeUtils.generateQI(["nsIExternalProtocolService"]),
    }
  );
  const mockExternalProtocolService = Cc[
    "@mozilla.org/uriloader/external-protocol-service;1"
  ].getService(Ci.nsIExternalProtocolService);
  Assert.ok(
    mockExternalProtocolService.externalProtocolHandlerExists("__invalid__"),
    "Mock service is working"
  );

  info("Register new dummy protocol");
  const dummyProtocolHandlerInfo =
    externalProtocolService.getProtocolHandlerInfo("dummy");
  const handlerService = Cc[
    "@mozilla.org/uriloader/handler-service;1"
  ].getService(Ci.nsIHandlerService);
  handlerService.store(dummyProtocolHandlerInfo);

  info("Prepare test search engine");
  await setupSearchService();
  await addTestEngines();
  await Services.search.setDefault(
    Services.search.getEngineByName(kSearchEngineID),
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  registerCleanupFunction(() => {
    handlerService.remove(dummyProtocolHandlerInfo);
    MockRegistrar.unregister(mockId);
  });
});

add_task(function basic() {
  const testData = [
    {
      input: "mailto:test@example.com",
      expected:
        isSupportedInHandlerService("mailto") ||
        // Thunderbird IS a mailto handler, it doesn't have handlers.
        AppConstants.MOZ_APP_NAME == "thunderbird"
          ? "mailto:test@example.com"
          : "http://mailto:test@example.com/",
    },
    {
      input: "keyword:search",
      expected: "https://www.example.org/?search=keyword%3Asearch",
    },
    {
      input: "dummy:protocol",
      expected: "dummy:protocol",
    },
  ];

  for (const { input, expected } of testData) {
    assertFixup(input, expected);
  }
});

function assertFixup(input, expected) {
  const { preferredURI } = Services.uriFixup.getFixupURIInfo(
    input,
    Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS
  );
  Assert.equal(preferredURI.spec, expected);
}

function isSupportedInHandlerService(scheme) {
  const externalProtocolService = Cc[
    "@mozilla.org/uriloader/external-protocol-service;1"
  ].getService(Ci.nsIExternalProtocolService);
  const handlerService = Cc[
    "@mozilla.org/uriloader/handler-service;1"
  ].getService(Ci.nsIHandlerService);
  return handlerService.exists(
    externalProtocolService.getProtocolHandlerInfo(scheme)
  );
}