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

"use strict";

add_task(async function test_utf8_extension() {
  const mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
  let someMIME = mimeService.getFromTypeAndExtension(
    "application/x-nonsense",
    ".тест"
  );
  Assert.stringContains(someMIME.description, "тест");
  // primary extension isn't set on macOS or android, see bug 1721181
  if (AppConstants.platform != "macosx" && AppConstants.platform != "android") {
    Assert.equal(someMIME.primaryExtension, ".тест");
  }
});

function getLocalHandlers(mimeInfo) {
  try {
    const appList = mimeInfo?.possibleLocalHandlers || [];
    return appList;
  } catch (err) {
    // if the mime info on this platform doesn't support getting local handlers,
    // we don't need to test
    if (err.result == Cr.NS_ERROR_NOT_IMPLEMENTED) {
      return [];
    }

    // otherwise, throw the err because the test is broken
    throw err;
  }
}

add_task(async function test_default_executable() {
  if (AppConstants.platform == "linux") {
    return;
  }

  const mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
  let mimeInfo = mimeService.getFromTypeAndExtension("text/html", "html");
  if (mimeInfo !== undefined) {
    if (mimeInfo.hasDefaultHandler) {
      let defaultExecutableFile = mimeInfo.defaultExecutable;
      if (defaultExecutableFile) {
        if (AppConstants.platform == "win") {
          Assert.ok(
            defaultExecutableFile.leafName.endsWith(".exe"),
            "Default browser on Windows should end with .exe"
          );
        }
      }

      let foundDefaultInList = false;

      let appList = getLocalHandlers(mimeInfo);
      if (!appList.length) {
        return;
      }

      for (let index = 0; index < appList.length; index++) {
        let app = appList.queryElementAt(index, Ci.nsILocalHandlerApp);
        let executablePath = app.executable.path;

        if (executablePath == defaultExecutableFile.path) {
          foundDefaultInList = true;
          break;
        }
      }

      Assert.ok(
        foundDefaultInList,
        "The default browser must be returned in the list of executables from the mime info"
      );
    } else {
      Assert.throws(
        () => mimeInfo.defaultExecutable,
        /NS_ERROR_FAILURE/,
        "Fetching the defaultExecutable should generate an exception; this line should never be reached"
      );
    }
  }
});

add_task(async function test_pretty_name_for_edge() {
  if (AppConstants.platform == "win" && !AppConstants.IS_ESR) {
    const mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);

    let mimeInfo = mimeService.getFromTypeAndExtension("text/html", "html");
    let appList = getLocalHandlers(mimeInfo);
    for (let index = 0; index < appList.length; index++) {
      let app = appList.queryElementAt(index, Ci.nsILocalHandlerApp);
      if (app) {
        let executableName = app.executable?.displayName;
        if (executableName) {
          let prettyName = await app.prettyNameAsync();

          // Hardcode Edge, as an extra test, when it's installed
          if (executableName == "msedge.exe") {
            Assert.equal(
              prettyName,
              "Microsoft Edge",
              "The generated pretty name for MS Edge should match the expectation."
            );
          }

          // The pretty name should always be something nicer than the executable name.
          // This isn't testing that's nice, but should be good enough to validate that
          // something other than the executable is found.
          Assert.notEqual(executableName, prettyName);
        }
      }
    }
  }
});

add_task(async function test_pretty_names_match_names_on_non_windows() {
  if (AppConstants.platform != "win") {
    const mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);

    let mimeInfo = mimeService.getFromTypeAndExtension("text/html", "html");
    let appList = getLocalHandlers(mimeInfo);
    for (let index = 0; index < appList.length; index++) {
      let app = appList.queryElementAt(index, Ci.nsILocalHandlerApp);
      if (app) {
        if (app.executable) {
          let name = app.executable.name;
          let prettyName = await app.prettyNameAsync();

          Assert.equal(
            prettyName,
            name,
            "On platforms other than windows, the prettyName and the name of file handlers should be the same."
          );
        }
      }
    }
  }
});