summaryrefslogtreecommitdiffstats
path: root/toolkit/components/viewsource/test/browser/browser_validatefilename.js
blob: 57699df733bd696ab152ef7e214da4e1875fc1b8 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function () {
  let tests = [
    {
      uri: "data:text/html,Test",
      basename: "Untitled",
    },
    {
      uri: "data:text/html,<title>Hello There</title>Test",
      basename: "Hello There",
    },
  ];

  for (let test of tests) {
    await BrowserTestUtils.withNewTab(test.uri, async browser => {
      let doc = {
        characterSet: browser.characterSet,
        contentType: browser.documentContentType,
        title: browser.contentTitle,
      };

      let fl = gViewSourceUtils.getTemporaryFile(
        browser.currentURI,
        doc,
        "text/html"
      );
      // Some versions of Windows will crop the extension to three characters so allow both forms.
      ok(
        fl.leafName == test.basename + ".htm" ||
          fl.leafName == test.basename + ".html",
        "filename title for " + test.basename + " html"
      );

      doc.contentType = "application/xhtml+xml";
      fl = gViewSourceUtils.getTemporaryFile(
        browser.currentURI,
        doc,
        "application/xhtml+xml"
      );
      ok(
        fl.leafName == test.basename + ".xht" ||
          fl.leafName == test.basename + ".xhtml",
        "filename title for " + test.basename + " xhtml"
      );
    });
  }

  let fl = gViewSourceUtils.getTemporaryFile(
    Services.io.newURI("http://www.example.com/simple"),
    null,
    "text/html"
  );
  ok(
    fl.leafName == "simple.htm" || fl.leafName == "simple.html",
    "filename title for simple"
  );

  fl = gViewSourceUtils.getTemporaryFile(
    Services.io.newURI("http://www.example.com/samplefile.txt"),
    null,
    "text/html"
  );
  is(fl.leafName, "samplefile.txt", "filename title for samplefile");
});