summaryrefslogtreecommitdiffstats
path: root/toolkit/components/thumbnails/test/test_thumbnails_interfaces.js
blob: eb70e8011b1e57147cf57ba6e1a6d900e604d254 (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
"use strict";

// This is an xpcshell test and gets a browser test env applied, so we
// need to still manually import NetUtil.
// eslint-disable-next-line mozilla/no-redeclare-with-import-autofix
const { NetUtil } = ChromeUtils.importESModule(
  "resource://gre/modules/NetUtil.sys.mjs"
);

// need profile so that PageThumbsStorageService can resolve the path to the underlying file
do_get_profile();

function run_test() {
  // check the protocol handler implements the correct interface
  let handler = Services.io.getProtocolHandler("moz-page-thumb");
  ok(
    handler instanceof Ci.nsIProtocolHandler,
    "moz-page-thumb handler provides a protocol handler interface"
  );

  // create a dummy loadinfo which we can hand to newChannel.
  let dummyURI = Services.io.newURI("https://www.example.com/1");
  let dummyChannel = NetUtil.newChannel({
    uri: dummyURI,
    loadUsingSystemPrincipal: true,
  });
  let dummyLoadInfo = dummyChannel.loadInfo;

  // and check that the error cases work as specified
  let badhost = Services.io.newURI(
    "moz-page-thumb://wronghost/?url=http%3A%2F%2Fwww.mozilla.org%2F"
  );
  Assert.throws(
    () => handler.newChannel(badhost, dummyLoadInfo),
    /NS_ERROR_NOT_AVAILABLE/i,
    "moz-page-thumb object with wrong host must not resolve to a file path"
  );

  let badQuery = Services.io.newURI(
    "moz-page-thumb://thumbnail/http%3A%2F%2Fwww.mozilla.org%2F"
  );
  Assert.throws(
    () => handler.newChannel(badQuery, dummyLoadInfo),
    /NS_ERROR_NOT_AVAILABLE/i,
    "moz-page-thumb object with malformed query parameters must not resolve to a file path"
  );

  let noURL = Services.io.newURI("moz-page-thumb://thumbnail/?badStuff");
  Assert.throws(
    () => handler.newChannel(noURL, dummyLoadInfo),
    /NS_ERROR_NOT_AVAILABLE/i,
    "moz-page-thumb object without a URL parameter must not resolve to a file path"
  );
}