|
| 1 | +<!DOCTYPE HTML> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>MozButton Tests</title> |
| 6 | + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
| 7 | + <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script> |
| 8 | + <script type="module" src="chrome://global/content/elements/moz-button.mjs"></script> |
| 9 | + <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> |
| 10 | + <link rel="stylesheet" href="chrome://global/skin/design-system/tokens-brand.css"> |
| 11 | + <link rel="stylesheet" href="chrome://global/skin/design-system/text-and-typography.css"> |
| 12 | +<style> |
| 13 | +.four::part(button), |
| 14 | +.five::part(button), |
| 15 | +.six::part(button) { |
| 16 | + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' width='16' height='16' fill='context-fill' fill-opacity='context-fill-opacity'%3E%3Cpath d='M3 7 1.5 7l-.5.5L1 9l.5.5 1.5 0 .5-.5 0-1.5z'/%3E%3Cpath d='m8.75 7-1.5 0-.5.5 0 1.5.5.5 1.5 0 .5-.5 0-1.5z'/%3E%3Cpath d='M14.5 7 13 7l-.5.5 0 1.5.5.5 1.5 0L15 9l0-1.5z'/%3E%3C/svg%3E"); |
| 17 | +} |
| 18 | +</style> |
| 19 | + <script> |
| 20 | + function normalizeColor(val, computedStyles) { |
| 21 | + if (val.includes("currentColor")) { |
| 22 | + val = val.replaceAll("currentColor", computedStyles.color); |
| 23 | + } |
| 24 | + if (val.startsWith("light-dark")) { |
| 25 | + let [, light, dark] = val.match(/light-dark\(([^,]+),\s*([^)]+)\)/); |
| 26 | + if (light && dark) { |
| 27 | + val = window.matchMedia("(prefers-color-scheme: dark)").matches ? dark : light; |
| 28 | + } |
| 29 | + } |
| 30 | + try { |
| 31 | + let { r, g, b, a } = InspectorUtils.colorToRGBA(val); |
| 32 | + return `rgba(${r}, ${g}, ${b}, ${a})`; |
| 33 | + } catch (e) { |
| 34 | + info(val); |
| 35 | + throw e; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + function assertButtonPropertiesMatch(el, propertyToCssVar) { |
| 40 | + let elStyles = getComputedStyle(el.buttonEl); |
| 41 | + for (let [property, cssVar] of Object.entries(propertyToCssVar)) { |
| 42 | + let propertyVal = elStyles[property]; |
| 43 | + let cssVarVal = cssVar.startsWith("--") ? elStyles.getPropertyValue(cssVar) : cssVar; |
| 44 | + if (propertyVal.startsWith("rgb") || propertyVal.startsWith("#") || propertyVal.startsWith("color")) { |
| 45 | + propertyVal = normalizeColor(propertyVal, elStyles); |
| 46 | + cssVarVal = normalizeColor(cssVarVal, elStyles); |
| 47 | + } |
| 48 | + info(`${propertyVal} == ${cssVarVal}`); |
| 49 | + is(propertyVal, cssVarVal, `${property} should be ${cssVar}`); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + add_task(async function testButtonTypes() { |
| 54 | + let [...buttons] = document.querySelectorAll("moz-button"); |
| 55 | + let [one, two, three, four, five, six] = buttons; |
| 56 | + |
| 57 | + await Promise.all(buttons.map(btn => btn.updateComplete)); |
| 58 | + |
| 59 | + is(one.textContent, "Test button", "Text is set"); |
| 60 | + is(two.buttonEl.textContent.trim(), "Test button", "Text is set"); |
| 61 | + is(three.textContent, "Test button", "Text is set"); |
| 62 | + |
| 63 | + assertButtonPropertiesMatch(one, { |
| 64 | + backgroundColor: "--button-background-color", |
| 65 | + color: "--button-text-color", |
| 66 | + height: "--button-min-height", |
| 67 | + }); |
| 68 | + assertButtonPropertiesMatch(two, { |
| 69 | + backgroundColor: "--button-background-color", |
| 70 | + color: "--button-text-color", |
| 71 | + height: "--button-min-height", |
| 72 | + }); |
| 73 | + assertButtonPropertiesMatch(three, { |
| 74 | + backgroundColor: "--button-background-color-primary", |
| 75 | + color: "--button-text-color-primary", |
| 76 | + height: "--button-min-height", |
| 77 | + }); |
| 78 | + |
| 79 | + assertButtonPropertiesMatch(four, { |
| 80 | + width: "--button-size-icon", |
| 81 | + height: "--button-size-icon", |
| 82 | + backgroundColor: "--button-background-color", |
| 83 | + fill: "--button-text-color", |
| 84 | + }); |
| 85 | + assertButtonPropertiesMatch(five, { |
| 86 | + width: "--button-size-icon", |
| 87 | + height: "--button-size-icon", |
| 88 | + backgroundColor: "transparent", |
| 89 | + fill: "--button-text-color", |
| 90 | + }); |
| 91 | + assertButtonPropertiesMatch(six, { |
| 92 | + width: "--button-size-icon", |
| 93 | + height: "--button-size-icon", |
| 94 | + backgroundColor: "transparent", |
| 95 | + fill: "--button-text-color", |
| 96 | + }); |
| 97 | + |
| 98 | + buttons.forEach(btn => (btn.size = "small")); |
| 99 | + |
| 100 | + await Promise.all(buttons.map(btn => btn.updateComplete)); |
| 101 | + |
| 102 | + assertButtonPropertiesMatch(one, { |
| 103 | + height: "--button-min-height-small", |
| 104 | + }); |
| 105 | + assertButtonPropertiesMatch(two, { |
| 106 | + height: "--button-min-height-small", |
| 107 | + }); |
| 108 | + assertButtonPropertiesMatch(three, { |
| 109 | + height: "--button-min-height-small", |
| 110 | + }); |
| 111 | + assertButtonPropertiesMatch(four, { |
| 112 | + width: "--button-size-icon-small", |
| 113 | + height: "--button-size-icon-small", |
| 114 | + }); |
| 115 | + assertButtonPropertiesMatch(five, { |
| 116 | + width: "--button-size-icon-small", |
| 117 | + height: "--button-size-icon-small", |
| 118 | + }); |
| 119 | + assertButtonPropertiesMatch(six, { |
| 120 | + width: "--button-size-icon-small", |
| 121 | + height: "--button-size-icon-small", |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + add_task(async function testA11yAttributes() { |
| 126 | + let button = document.querySelector("moz-button"); |
| 127 | + |
| 128 | + async function testProperty(propName, jsPropName = propName) { |
| 129 | + let propValue = `${propName} value`; |
| 130 | + ok(!button.buttonEl.hasAttribute(propName), `No ${propName} on inner button`); |
| 131 | + button.setAttribute(propName, propValue); |
| 132 | + |
| 133 | + await button.updateComplete; |
| 134 | + |
| 135 | + ok(!button.hasAttribute(propName), `moz-button ${propName} cleared`); |
| 136 | + is(button.buttonEl.getAttribute(propName), propValue, `${propName} added to inner button`); |
| 137 | + |
| 138 | + button[jsPropName] = null; |
| 139 | + await button.updateComplete; |
| 140 | + |
| 141 | + ok(!button.buttonEl.hasAttribute(propName), `${propName} cleared by setting property`); |
| 142 | + } |
| 143 | + |
| 144 | + await testProperty("title"); |
| 145 | + await testProperty("aria-label", "ariaLabel"); |
| 146 | + }); |
| 147 | + |
| 148 | + </script> |
| 149 | +</head> |
| 150 | +<body> |
| 151 | + <moz-button class="one">Test button</moz-button> |
| 152 | + <moz-button class="two" label="Test button"></moz-button> |
| 153 | + <moz-button class="three" type="primary">Test button</moz-button> |
| 154 | + <moz-button class="four" type="icon"></moz-button> |
| 155 | + <moz-button class="five" type="icon ghost"></moz-button> |
| 156 | + <moz-button class="six" type="ghost icon"></moz-button> |
| 157 | +</body> |
| 158 | +</html> |
0 commit comments