// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information.
//
//
module CorsicaTests {
"use strict";
var PrivateLegacyAppBar = WinJS.UI._LegacyAppBar;
var AppBarCommand = WinJS.UI.AppBarCommand;
var Flyout = WinJS.UI.Flyout;
var _Constants = Helper.require("WinJS/Controls/_LegacyAppBar/_Constants");
export class AppBarCommandTests {
// Test AppBarCommand Instantiation
testAppBarCommandInstantiation = function () {
// Get the AppBarCommand element from the DOM
LiveUnit.LoggingCore.logComment("Attempt to Instantiate the AppBarCommand element");
var AppBarCommandElement = document.createElement('hr');
document.body.appendChild(AppBarCommandElement);
var AppBarCommand = new WinJS.UI.AppBarCommand(AppBarCommandElement, { type: 'separator' });
LiveUnit.LoggingCore.logComment("AppBarCommand has been instantiated.");
LiveUnit.Assert.isNotNull(AppBarCommand, "AppBarCommand element should not be null when instantiated.");
// We have no functions
document.body.removeChild(AppBarCommandElement);
}
// Test AppBarCommand Instantiation with null element
testAppBarCommandNullInstantiation = function () {
LiveUnit.LoggingCore.logComment("Attempt to Instantiate the AppBarCommand with null element");
var AppBarCommand = new WinJS.UI.AppBarCommand(null, { type: 'separator' });
LiveUnit.Assert.isNotNull(AppBarCommand, "AppBarCommand instantiation was null when sent a null AppBarCommand element.");
}
// Test AppBarCommand Instantiation with no options
testAppBarCommandEmptyInstantiation = function () {
LiveUnit.LoggingCore.logComment("Attempt to Instantiate the AppBarCommand with empty constructor");
var AppBarCommand = new WinJS.UI.AppBarCommand();
LiveUnit.Assert.isNotNull(AppBarCommand, "AppBarCommand instantiation was null when sent a Empty AppBarCommand element.");
}
// Test multiple instantiation of the same AppBarCommand DOM element
testAppBarCommandMultipleInstantiation() {
AppBarCommandTests.prototype.testAppBarCommandMultipleInstantiation["LiveUnit.ExpectedException"] = { message: "Invalid argument: Controls may only be instantiated one time for each DOM element" };
// Get the AppBarCommand element from the DOM
LiveUnit.LoggingCore.logComment("Attempt to Instantiate the AppBarCommand element");
var AppBarCommandElement = document.createElement('hr');
document.body.appendChild(AppBarCommandElement);
var AppBarCommand = new WinJS.UI.AppBarCommand(AppBarCommandElement, { type: 'separator' });
LiveUnit.LoggingCore.logComment("AppBarCommand has been instantiated.");
LiveUnit.Assert.isNotNull(AppBarCommand, "AppBarCommand element should not be null when instantiated.");
var error;
try {
new WinJS.UI.AppBarCommand(AppBarCommandElement, { type: 'separator' });
} catch (e) {
error = e;
} finally {
document.body.removeChild(AppBarCommandElement);
throw error;
}
}
// Test AppBarCommand parameters
testAppBarCommandParams = function () {
function testGoodInitOption(paramName, value) {
LiveUnit.LoggingCore.logComment("Testing creating a AppBarCommand using good parameter " + paramName + "=" + value);
var options = { type: 'button', label: 'test', icon: 'test.png' };
options[paramName] = value;
var AppBarCommand = new WinJS.UI.AppBarCommand(null, options);
LiveUnit.Assert.isNotNull(AppBarCommand);
}
function testBadInitOption(paramName, value, expectedName, expectedMessage) {
LiveUnit.LoggingCore.logComment("Testing creating a AppBarCommand using bad parameter " + paramName + "=" + value);
var options = { type: 'button', label: 'test', icon: 'test.png' };
options[paramName] = value;
try {
new WinJS.UI.AppBarCommand(null, options);
LiveUnit.Assert.fail("Expected creating AppBarCommand with " + paramName + "=" + value + " to throw an exception");
} catch (exception) {
LiveUnit.LoggingCore.logComment(exception.message);
LiveUnit.Assert.isTrue(exception !== null);
LiveUnit.Assert.isTrue(exception.name === expectedName);
LiveUnit.Assert.isTrue(exception.message === expectedMessage);
}
}
LiveUnit.LoggingCore.logComment("Testing id");
testGoodInitOption("id", "ralph");
testGoodInitOption("id", "fred");
testGoodInitOption("id", -1);
testGoodInitOption("id", 12);
testGoodInitOption("id", {});
LiveUnit.LoggingCore.logComment("Testing type");
testGoodInitOption("type", "button");
testGoodInitOption("type", "flyout");
testGoodInitOption("type", "toggle");
testGoodInitOption("type", "separator");
LiveUnit.LoggingCore.logComment("Testing label");
testGoodInitOption("label", "test");
testGoodInitOption("label", "a");
testGoodInitOption("label", -1);
testGoodInitOption("label", 12);
testGoodInitOption("label", {});
LiveUnit.LoggingCore.logComment("Testing icon");
testGoodInitOption("icon", "test");
testGoodInitOption("icon", "b");
testGoodInitOption("icon", "");
testGoodInitOption("icon", -1);
testGoodInitOption("icon", 12);
testGoodInitOption("icon", {});
var priorityExceptionName = "WinJS.UI.AppBarCommand.BadPriority";
var priorityExceptionMessage = "Invalid argument: the priority of an AppBarCommand must be a non-negative integer";
LiveUnit.LoggingCore.logComment("Testing priority");
testBadInitOption("priority", "test", priorityExceptionName, priorityExceptionMessage);
testBadInitOption("priority", "", priorityExceptionName, priorityExceptionMessage);
testBadInitOption("priority", -1, priorityExceptionName, priorityExceptionMessage);
testBadInitOption("priority", {}, priorityExceptionName, priorityExceptionMessage);
testGoodInitOption("priority", undefined);
testGoodInitOption("priority", 0);
testGoodInitOption("priority", 12);
LiveUnit.LoggingCore.logComment("Testing tooltip");
testGoodInitOption("tooltip", "test");
testGoodInitOption("tooltip", "");
testGoodInitOption("tooltip", -1);
testGoodInitOption("tooltip", 12);
testGoodInitOption("tooltip", {});
LiveUnit.LoggingCore.logComment("Testing flyout");
testGoodInitOption("flyout", "test");
testGoodInitOption("flyout", "");
testGoodInitOption("flyout", "");
testGoodInitOption("flyout", { id: "test" });
testGoodInitOption("flyout", { uniqueId: "test" });
testGoodInitOption("flyout", { element: { id: "test" } });
testGoodInitOption("flyout", { element: { uniqueId: "test" } });
LiveUnit.LoggingCore.logComment("Testing section");
testGoodInitOption("section", "global"); // Deprecated, use primary
testGoodInitOption("section", "selection"); // Deprecated, use secondary
testGoodInitOption("section", "primary");
testGoodInitOption("section", "secondary");
testGoodInitOption("section", -1);
testGoodInitOption("section", 12);
testGoodInitOption("section", {});
LiveUnit.LoggingCore.logComment("Testing disabled");
testGoodInitOption("disabled", true);
testGoodInitOption("disabled", false);
testGoodInitOption("disabled", -1);
testGoodInitOption("disabled", "what");
testGoodInitOption("disabled", {});
LiveUnit.LoggingCore.logComment("Testing selected");
testGoodInitOption("selected", true);
testGoodInitOption("selected", false);
testGoodInitOption("selected", -1);
testGoodInitOption("selected", "what");
testGoodInitOption("selected", {});
// TODO: Still need to test click
LiveUnit.LoggingCore.logComment("Testing element");
}
testDefaultAppBarCommandParameters = function () {
// Get the AppBarCommand element from the DOM
LiveUnit.LoggingCore.logComment("Attempt to Instantiate the AppBarCommand element");
var AppBarCommand = new WinJS.UI.AppBarCommand(null, { label: 'test', icon: 'test.png' });
LiveUnit.LoggingCore.logComment("AppBarCommand has been instantiated.");
LiveUnit.Assert.isNotNull(AppBarCommand, "AppBarCommand element should not be null when instantiated.");
LiveUnit.Assert.isNotNull(AppBarCommand.element, "Verifying that element is not null");
LiveUnit.Assert.areEqual("primary", AppBarCommand.section, "Verifying that section is 'primary'");
LiveUnit.Assert.areEqual("", AppBarCommand.id, "Verifying that id is empty string");
LiveUnit.Assert.areEqual("button", AppBarCommand.type, "Verifying that type is 'button'");
LiveUnit.Assert.areEqual("test", AppBarCommand.label, "Verifying that label is 'test'");
LiveUnit.Assert.areEqual("test.png", AppBarCommand.icon, "Verifying that icon is 'test.png'");
// Tooltip should be set if label was set
LiveUnit.Assert.areEqual("test", AppBarCommand.tooltip, "Verifying that tooltip is 'test'");
LiveUnit.Assert.isFalse(AppBarCommand.disabled, "Verifying that disabled is false");
LiveUnit.Assert.isFalse(AppBarCommand.hidden, "Verifying that hidden is false");
LiveUnit.Assert.isFalse(AppBarCommand.selected, "Verifying that selected is false");
}
// Simple Property tests
testSimpleAppBarCommandProperties = function () {
// Get the AppBarCommand element from the DOM
LiveUnit.LoggingCore.logComment("Attempt to Instantiate the AppBarCommand element");
var appBarCommand = new WinJS.UI.AppBarCommand(null, { label: 'test', icon: 'test.png', type: 'toggle', extraClass: 'extra' });
LiveUnit.LoggingCore.logComment("AppBarCommand has been instantiated.");
LiveUnit.Assert.isNotNull(appBarCommand, "AppBarCommand element should not be null when instantiated.");
// Verify initial state of toggle command.
LiveUnit.Assert.areEqual("checkbox", appBarCommand.element.getAttribute("role"),
"Narrator requires 'checkbox' role in order to announce updates to the toggled state");
LiveUnit.Assert.areEqual(false, appBarCommand.selected, "Verifying that selected is false");
LiveUnit.Assert.areEqual(appBarCommand.selected.toString(), appBarCommand.element.getAttribute("aria-checked"),
"aria-checked should map to 'selected' state");
// Cycle selected
appBarCommand.selected = true;
LiveUnit.Assert.areEqual(true, appBarCommand.selected, "Verifying that selected is true");
LiveUnit.Assert.areEqual(appBarCommand.selected.toString(), appBarCommand.element.getAttribute("aria-checked"),
"aria-checked should map to 'selected' state");
appBarCommand.selected = false;
LiveUnit.Assert.areEqual(false, appBarCommand.selected, "Verifying that selected goes back to false");
LiveUnit.Assert.areEqual(appBarCommand.selected.toString(), appBarCommand.element.getAttribute("aria-checked"),
"aria-checked should map to 'selected' state");
// Cycle extra class
LiveUnit.Assert.areEqual("extra", appBarCommand.extraClass, "Verifying that extraClass is 'extra'");
LiveUnit.Assert.isTrue(appBarCommand.element.classList.contains("extra"), "Verifying that extraClass is 'extra'");
appBarCommand.extraClass = "somethingElse";
LiveUnit.Assert.areEqual("somethingElse", appBarCommand.extraClass, "Verifying that extraClass is 'somethingElse");
LiveUnit.Assert.isTrue(appBarCommand.element.classList.contains("somethingElse"), "Verifying that className is 'somethingElse");
appBarCommand.extraClass = "another";
LiveUnit.Assert.areEqual("another", appBarCommand.extraClass, "Verifying that extraClass is 'another'");
LiveUnit.Assert.isTrue(appBarCommand.element.classList.contains("another"), "Verifying that className is 'another'");
// Check flyout with empty id
var fakeDomObject: any = { uniqueID: 'unique' };
appBarCommand.flyout = fakeDomObject;
LiveUnit.Assert.areEqual("unique", fakeDomObject.id, "Verifying that id is set to 'unique' from uniqueID");
LiveUnit.Assert.areEqual("unique", appBarCommand.element.getAttribute("aria-owns"), "Verifying that aria-owns is set by flyout setter");
}
// Hidden Property tests
testHiddenProperty = function () {
LiveUnit.LoggingCore.logComment("Attempt to test hidden property on appbarcommand");
var AppBarElement = document.createElement("div");
document.body.appendChild(AppBarElement);
LiveUnit.LoggingCore.logComment("Attempt to Instantiate the AppBar element");
var AppBar = new PrivateLegacyAppBar(AppBarElement, { commands: { type: 'separator', id: 'sep' } });
LiveUnit.LoggingCore.logComment("set commands");
AppBar.commands = [{ id: 'cmdA', label: 'One', icon: 'back', section: 'primary', tooltip: 'Test glyph by name' }];
var commandVisibilityChangedCount = 0;
AppBarElement.addEventListener("commandvisibilitychanged", function () {
commandVisibilityChangedCount++;
});
var cmd = AppBar.getCommandById("cmdA");
cmd.hidden = true;
LiveUnit.Assert.areEqual(true, cmd.hidden, "verify the command is now hidden");
LiveUnit.Assert.areEqual(1, commandVisibilityChangedCount, "commandvisibilitychanged event should have been fired");
cmd.hidden = false;
LiveUnit.Assert.areEqual(false, cmd.hidden, "verify the command is now visible");
LiveUnit.Assert.areEqual(2, commandVisibilityChangedCount, "commandvisibilitychanged event should have been fired");
AppBar.close();
document.body.removeChild(AppBarElement);
}
// Tests for dispose members and requirements
testAppBarCommandDispose = function () {
var flyout = new Flyout();
LiveUnit.Assert.isFalse(flyout._disposed);
var button = document.createElement("button");
var abc = new AppBarCommand(button, { type: "flyout", flyout: flyout });
LiveUnit.Assert.isTrue(abc.dispose);
LiveUnit.Assert.isTrue(abc.element.classList.contains("win-disposable"));
LiveUnit.Assert.isFalse(abc._disposed);
LiveUnit.Assert.isFalse(abc._tooltipControl._disposed);
// Double dispose sentinel
var sentinel: any = document.createElement("div");
sentinel.disposed = false;
WinJS.Utilities.addClass(sentinel, "win-disposable");
button.appendChild(sentinel);
sentinel.dispose = function () {
if (sentinel.disposed) {
LiveUnit.Assert.fail("Unexpected double dispose occured.");
}
sentinel.disposed = true;
};
abc.dispose();
LiveUnit.Assert.isTrue(sentinel.disposed);
LiveUnit.Assert.isTrue(abc._disposed);
LiveUnit.Assert.isTrue(abc._tooltipControl._disposed);
LiveUnit.Assert.isFalse(flyout._disposed,
"AppBarCommands do not instantiate the flyout and are not responsible for disposing it.");
abc.dispose();
};
// Tests firstElementFocus and lastElementFocus properties of content type AppBarCommand
testContentAppBarCommandFocusProperties = function () {
var element = document.createElement("div");
var HTMLString = "" +
"