Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

/* Any copyright is dedicated to the Public Domain.
"use strict";
/**
* Tests utility functions contained in `source-utils.js`
*/
const sourceUtils = require("resource://devtools/client/shared/source-utils.js");
const CHROME_URLS = [
];
const CONTENT_URLS = [
];
// Test `sourceUtils.parseURL`
add_task(async function () {
let parsed = sourceUtils.parseURL("https://foo.com:8888/boo/bar.js?q=query");
equal(parsed.fileName, "bar.js", "parseURL parsed valid fileName");
equal(parsed.host, "foo.com:8888", "parseURL parsed valid host");
equal(parsed.hostname, "foo.com", "parseURL parsed valid hostname");
equal(parsed.port, "8888", "parseURL parsed valid port");
equal(
parsed.href,
"parseURL parsed valid href"
);
parsed = sourceUtils.parseURL("https://foo.com");
equal(
parsed.host,
"foo.com",
"parseURL parsed valid host when no port given"
);
equal(
parsed.hostname,
"foo.com",
"parseURL parsed valid hostname when no port given"
);
equal(
sourceUtils.parseURL("self-hosted"),
null,
"parseURL returns `null` for invalid URLs"
);
});
// Test `sourceUtils.isContentScheme`.
add_task(async function () {
for (const url of CHROME_URLS) {
ok(
!sourceUtils.isContentScheme(url),
`${url} correctly identified as not content scheme`
);
}
for (const url of CONTENT_URLS) {
ok(
sourceUtils.isContentScheme(url),
`${url} correctly identified as content scheme`
);
}
});
// Test `sourceUtils.isChromeScheme`.
add_task(async function () {
for (const url of CHROME_URLS) {
ok(
sourceUtils.isChromeScheme(url),
`${url} correctly identified as chrome scheme`
);
}
for (const url of CONTENT_URLS) {
ok(
!sourceUtils.isChromeScheme(url),
`${url} correctly identified as not chrome scheme`
);
}
});
// Test `sourceUtils.isWASM`.
add_task(async function () {
ok(
sourceUtils.isWASM("wasm-function[66240] (?:13870536)"),
"wasm function correctly identified"
);
ok(
!sourceUtils.isWASM(CHROME_URLS[0]),
`A chrome url does not identify as wasm.`
);
});
// Test `sourceUtils.isDataScheme`.
add_task(async function () {
const dataURI = "data:text/html;charset=utf-8,<!DOCTYPE html></html>";
ok(
sourceUtils.isDataScheme(dataURI),
`${dataURI} correctly identified as data scheme`
);
for (const url of CHROME_URLS) {
ok(
!sourceUtils.isDataScheme(url),
`${url} correctly identified as not data scheme`
);
}
for (const url of CONTENT_URLS) {
ok(
!sourceUtils.isDataScheme(url),
`${url} correctly identified as not data scheme`
);
}
});
// Test `sourceUtils.getSourceNames`.
add_task(async function () {
testAbbreviation(
"boo.js",
"example.com"
);
});
// Test `sourceUtils.getSourceNames`.
add_task(async function () {
// Check length
const longMalformedURL = `example.com${new Array(100)
.fill("/a")
.join("")}/file.js`;
Assert.lessOrEqual(
sourceUtils.getSourceNames(longMalformedURL).short.length,
100,
"`short` names are capped at 100 characters"
);
testAbbreviation("self-hosted", "self-hosted", "self-hosted");
testAbbreviation("", "(unknown)", "(unknown)");
// Test shortening data URIs, stripping mime/charset
testAbbreviation(
"data:text/html;charset=utf-8,<!DOCTYPE html></html>",
"data:<!DOCTYPE html></html>",
"data:text/html;charset=utf-8,<!DOCTYPE html></html>"
);
const longDataURI = `data:image/png;base64,${new Array(100)
.fill("a")
.join("")}`;
const longDataURIShort = sourceUtils.getSourceNames(longDataURI).short;
// Test shortening data URIs and that the `short` result is capped
Assert.lessOrEqual(
longDataURIShort.length,
100,
"`short` names are capped at 100 characters for data URIs"
);
equal(
longDataURIShort.substr(0, 10),
"data:aaaaa",
"truncated data URI short names still have `data:...`"
);
// Test simple URL and cache retrieval by calling the same input multiple times.
testAbbreviation(testUrl, "boo.js", testUrl, "example.com");
testAbbreviation(testUrl, "boo.js", testUrl, "example.com");
// Check query and hash and port
testAbbreviation(
"baz.js",
"example.com:8888"
);
// Trailing "/" with nothing beyond host
testAbbreviation(
"/",
"example.com"
);
// Trailing "/"
testAbbreviation(
"bar",
"example.com"
);
// Non-extension ending
testAbbreviation(
"bar",
"example.com"
);
// Check query
testAbbreviation(
"foo.js",
"example.com"
);
// Check query with trailing slash
testAbbreviation(
"foo",
"example.com"
);
});
// Test for source mapped file name
add_task(async function () {
const { getSourceMappedFile } = sourceUtils;
const source = "baz.js";
const output = getSourceMappedFile(source);
equal(output, "baz.js", "correctly formats file name");
// Test for OSX file path
const source1 = "/foo/bar/baz.js";
const output1 = getSourceMappedFile(source1);
equal(output1, "baz.js", "correctly formats Linux file path");
// Test for Windows file path
const source2 = "Z:\\foo\\bar\\baz.js";
const output2 = getSourceMappedFile(source2);
equal(output2, "baz.js", "correctly formats Windows file path");
});
function testAbbreviation(source, short, long, host) {
const results = sourceUtils.getSourceNames(source);
equal(results.short, short, `${source} has correct "short" name`);
equal(results.long, long, `${source} has correct "long" name`);
equal(results.host, host, `${source} has correct "host" name`);
}