Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

/* Any copyright is dedicated to the Public Domain.
"use strict";
ChromeUtils.defineESModuleGetters(this, {
ShellService: "resource:///modules/ShellService.sys.mjs",
TaskbarTabsPin: "resource:///modules/taskbartabs/TaskbarTabsPin.sys.mjs",
TaskbarTabsRegistry:
"resource:///modules/taskbartabs/TaskbarTabsRegistry.sys.mjs",
});
// We want to mock the native XPCOM interfaces of the initialized
// `ShellService.shellService`, but those interfaces are frozen. Instead we
// proxy `ShellService.shellService` and mock it.
const mockNativeShellService = {
...ShellService.shellService,
createWindowsIcon: sinon.stub().resolves(),
createShortcut: sinon.stub().resolves("dummy_path"),
deleteShortcut: sinon.stub().resolves("dummy_path"),
pinShortcutToTaskbar: sinon.stub().resolves(),
getTaskbarTabShortcutPath: sinon
.stub()
.returns(FileTestUtils.getTempFile().parent.path),
unpinShortcutFromTaskbar: sinon.stub(),
};
sinon.stub(ShellService, "shellService").value(mockNativeShellService);
registerCleanupFunction(() => {
sinon.restore();
});
// Favicons are written to the profile directory, ensure it exists.
do_get_profile();
const faviconService = Cc[
"@mozilla.org/browser/favicon-service;1"
].createInstance(Ci.nsIFaviconService);
const kFaviconUri = Services.io.newFileURI(do_get_file("favicon-normal16.png"));
let faviconThrows = false;
let mockFaviconService = {
QueryInterface: ChromeUtils.generateQI(["nsIFaviconService"]),
getFaviconForPage: sinon.stub().callsFake(async () => {
if (faviconThrows) {
return null;
}
return { dataURI: kFaviconUri };
}),
get defaultFavicon() {
return faviconService.defaultFavicon;
},
};
let defaultIconSpy = sinon.spy(mockFaviconService, "defaultFavicon", ["get"]);
function shellPinCalled() {
ok(
mockNativeShellService.createWindowsIcon.called,
`Icon creation should have been called.`
);
ok(
mockNativeShellService.createShortcut.called,
`Shortcut creation should have been called.`
);
ok(
mockNativeShellService.pinShortcutToTaskbar.called,
`Pin to taskbar should have been called.`
);
}
function shellUnpinCalled() {
ok(
mockNativeShellService.unpinShortcutFromTaskbar.called,
`Unpin from taskbar should have been called.`
);
}
MockRegistrar.register(
"@mozilla.org/browser/favicon-service;1",
mockFaviconService
);
const url = Services.io.newURI("https://www.test.com");
const userContextId = 0;
const registry = new TaskbarTabsRegistry();
const taskbarTab = registry.findOrCreateTaskbarTab(url, userContextId);
add_task(async function test_pin_existing_favicon() {
sinon.resetHistory();
faviconThrows = false;
await TaskbarTabsPin.pinTaskbarTab(taskbarTab);
ok(
mockFaviconService.getFaviconForPage.calledOnce,
"The favicon for the page should have attempted to be retrieved."
);
ok(
defaultIconSpy.get.notCalled,
"The default icon should not be used when a favicon exists for the page."
);
shellPinCalled();
});
add_task(async function test_pin_missing_favicon() {
sinon.resetHistory();
faviconThrows = true;
await TaskbarTabsPin.pinTaskbarTab(taskbarTab);
ok(
mockFaviconService.getFaviconForPage.calledOnce,
"The favicon for the page should have attempted to be retrieved."
);
ok(
defaultIconSpy.get.called,
"The default icon should be used when a favicon does not exist for the page."
);
});
add_task(async function test_pin_location() {
sinon.resetHistory();
await TaskbarTabsPin.pinTaskbarTab(taskbarTab);
const spy = mockNativeShellService.createShortcut;
ok(spy.calledOnce, "A shortcut was created");
Assert.equal(
spy.firstCall.args[6],
"Programs",
"The shortcut went into the Start Menu folder"
);
Assert.equal(
spy.firstCall.args[7].split("\\", 2)[1],
"Test.lnk",
"The shortcut should be in a subdirectory and have a default name"
);
});
add_task(async function test_pin_location_dos_name() {
const parsedURI = Services.io.newURI("https://aux.test");
const invalidTaskbarTab = registry.findOrCreateTaskbarTab(parsedURI, 0);
sinon.resetHistory();
await TaskbarTabsPin.pinTaskbarTab(invalidTaskbarTab);
const spy = mockNativeShellService.createShortcut;
ok(spy.calledOnce, "A shortcut was created");
Assert.equal(
spy.firstCall.args[6],
"Programs",
"The shortcut went into the Start Menu folder"
);
// 'Untitled' is the default selected by the MIME code, since
// AUX is a reserved name on Windows.
Assert.equal(
spy.firstCall.args[7].split("\\", 2)[1],
"Untitled.lnk",
"The shortcut should be in a subdirectory and have a default name"
);
registry.removeTaskbarTab(invalidTaskbarTab);
});
add_task(async function test_unpin() {
sinon.resetHistory();
await TaskbarTabsPin.unpinTaskbarTab(taskbarTab);
shellUnpinCalled();
});