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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>MozButton Tests</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<script type="module" src="chrome://global/content/elements/moz-button.mjs"></script>
<link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
<link rel="stylesheet" href="chrome://global/skin/design-system/tokens-brand.css">
<link rel="stylesheet" href="chrome://global/skin/design-system/text-and-typography.css">
<style>
.four::part(button),
.five::part(button),
.six::part(button) {
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");
}
</style>
<script>
const { AddonManager } = ChromeUtils.importESModule(
"resource://gre/modules/AddonManager.sys.mjs"
);
// Always run tests with the light theme for consistency and to avoid from false
// test passes arising from the fact that --icon-color and --button-text-color
// have the same value in the dark theme.
add_setup(async function () {
// Developer Edition enables the wrong theme by default. Make sure
// the ordinary default theme is enabled.
let theme = await AddonManager.getAddonByID("default-theme@mozilla.org");
await theme.enable();
await SpecialPowers.pushPrefEnv({
set: [
["ui.systemUsesDarkTheme", 0],
],
});
// Triggers a refresh to ensure new theme applied.
await new Promise(resolve => requestAnimationFrame(resolve));
ok(window.matchMedia("(prefers-color-scheme: light)").matches, "Light theme is active.");
// Move mouse to the bottom of the test frame to prevent hover on buttons.
let mouseTrap = document.getElementById("mouse-trap");
await synthesizeMouseAtCenter(mouseTrap, { type: "mousemove" });
ok(mouseTrap.matches(":hover"), "The mouse trap is hovered");
});
function normalizeColor(val, computedStyles) {
if (val.includes("currentColor")) {
val = val.replaceAll("currentColor", computedStyles.color);
}
if (val.startsWith("light-dark")) {
let [, light, dark] = val.match(/light-dark\(([^,]+),\s*([^)]+)\)/);
if (light && dark) {
val = window.matchMedia("(prefers-color-scheme: dark)").matches ? dark : light;
}
}
try {
let { r, g, b, a } = InspectorUtils.colorToRGBA(val);
return `rgba(${r}, ${g}, ${b}, ${a})`;
} catch (e) {
info(val);
throw e;
}
}
function assertButtonPropertiesMatch(el, propertyToCssVar) {
let elStyles = el.buttonEl ? getComputedStyle(el.buttonEl) : getComputedStyle(el);
for (let [property, cssVar] of Object.entries(propertyToCssVar)) {
let propertyVal = elStyles[property];
let cssVarVal = cssVar.startsWith("--") ? elStyles.getPropertyValue(cssVar) : cssVar;
if (propertyVal.startsWith("rgb") || propertyVal.startsWith("#") || propertyVal.startsWith("color")) {
propertyVal = normalizeColor(propertyVal, elStyles);
cssVarVal = normalizeColor(cssVarVal, elStyles);
}
info(`${propertyVal} == ${cssVarVal}`);
is(propertyVal, cssVarVal, `${property} should be ${cssVar}`);
}
}
add_task(async function testButtonTypes() {
let [...buttons] = document.querySelectorAll("moz-button");
let [one, two, three, four, five, six, seven] = buttons;
await Promise.all(buttons.map(btn => btn.updateComplete));
is(one.textContent, "Test button", "Text is set");
is(two.buttonEl.textContent.trim(), "Test button", "Text is set");
is(three.textContent, "Test button", "Text is set");
is(seven.buttonEl.textContent.trim(), "Test button", "Text is set");
assertButtonPropertiesMatch(one, {
backgroundColor: "--button-background-color",
color: "--button-text-color",
height: "--button-min-height",
});
assertButtonPropertiesMatch(two, {
backgroundColor: "--button-background-color",
color: "--button-text-color",
height: "--button-min-height",
});
assertButtonPropertiesMatch(three, {
backgroundColor: "--button-background-color-primary",
color: "--button-text-color-primary",
height: "--button-min-height",
});
assertButtonPropertiesMatch(four, {
width: "--button-size-icon",
height: "--button-size-icon",
backgroundColor: "--button-background-color",
fill: "--button-text-color",
});
assertButtonPropertiesMatch(five, {
width: "--button-size-icon",
height: "--button-size-icon",
backgroundColor: "transparent",
fill: "--button-text-color",
});
assertButtonPropertiesMatch(six, {
width: "--button-size-icon",
height: "--button-size-icon",
backgroundColor: "transparent",
fill: "--button-text-color",
});
assertButtonPropertiesMatch(seven, {
backgroundColor: "--button-background-color",
color: "--button-text-color",
height: "--button-min-height",
});
buttons.forEach(btn => (btn.size = "small"));
await Promise.all(buttons.map(btn => btn.updateComplete));
assertButtonPropertiesMatch(one, {
height: "--button-min-height-small",
});
assertButtonPropertiesMatch(two, {
height: "--button-min-height-small",
});
assertButtonPropertiesMatch(three, {
height: "--button-min-height-small",
});
assertButtonPropertiesMatch(four, {
width: "--button-size-icon-small",
height: "--button-size-icon-small",
});
assertButtonPropertiesMatch(five, {
width: "--button-size-icon-small",
height: "--button-size-icon-small",
});
assertButtonPropertiesMatch(six, {
width: "--button-size-icon-small",
height: "--button-size-icon-small",
});
assertButtonPropertiesMatch(seven, {
height: "--button-min-height-small",
});
});
add_task(async function testA11yAttributes() {
let button = document.querySelector("moz-button");
async function testProperty(propName, jsPropName = propName) {
let propValue = `${propName} value`;
ok(!button.buttonEl.hasAttribute(propName), `No ${propName} on inner button`);
button.setAttribute(propName, propValue);
await button.updateComplete;
ok(!button.hasAttribute(propName), `moz-button ${propName} cleared`);
is(button.buttonEl.getAttribute(propName), propValue, `${propName} added to inner button`);
button[jsPropName] = null;
await button.updateComplete;
ok(!button.buttonEl.hasAttribute(propName), `${propName} cleared by setting property`);
}
await testProperty("title");
await testProperty("aria-label", "ariaLabel");
});
add_task(async function testIconButtons() {
let buttons = ["four", "five", "six", "seven", "eight", "nine"].map(
className => document.querySelector(`.${className}`)
);
let [four, five, six, seven, eight, nine] = buttons;
await Promise.all(buttons.map(btn => btn.updateComplete));
function verifyBackgroundIcon(button) {
let img = button.shadowRoot.querySelector("img");
ok(!img, "button does not use an img element to display an icon");
assertButtonPropertiesMatch(button, {
fill: "--button-text-color",
backgroundSize: "--icon-size-default",
});
}
function verifyImageIcon({ button }) {
let img = button.shadowRoot.querySelector("img");
ok(img, "button uses an inner img element to display an icon");
is(img.src, "chrome://global/skin/icons/edit.svg", "button displays the expected icon");
assertButtonPropertiesMatch(img, {
fill: "--button-text-color",
width: "--icon-size-default",
height: "--icon-size-default",
});
}
verifyBackgroundIcon(four);
verifyBackgroundIcon(five);
verifyBackgroundIcon(six);
verifyImageIcon({ button: seven });
verifyImageIcon({ button: eight });
verifyImageIcon({ button: nine });
nine.textContent = "With text";
// Ensure we've painted before checking styles
await new Promise(resolve => requestAnimationFrame(resolve));
verifyImageIcon({ button: nine, isLabelled: true });
nine.textContent = "";
// Ensure we've painted before checking styles
await new Promise(resolve => requestAnimationFrame(resolve));
verifyImageIcon({ button: nine, isLabelled: false });
});
</script>
</head>
<body>
<style>
.wrapper {
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
gap: 64px;
}
</style>
<div class="wrapper">
<div>
<moz-button class="one">Test button</moz-button>
<moz-button class="two" label="Test button"></moz-button>
<moz-button class="three" type="primary">Test button</moz-button>
<moz-button class="four" type="icon"></moz-button>
<moz-button class="five" type="icon ghost"></moz-button>
<moz-button class="six" type="ghost icon"></moz-button>
<moz-button
class="seven"
label="Test button"
iconsrc="chrome://global/skin/icons/edit.svg"
></moz-button>
<moz-button
class="eight"
iconsrc="chrome://global/skin/icons/edit.svg"
></moz-button>
<!-- Used to verify that empty space doesn't get treated as a label -->
<moz-button
class="nine"
type="ghost"
iconsrc="chrome://global/skin/icons/edit.svg"
> </moz-button>
</div>
<button id="mouse-trap">Mouse goes here</button>
</div>
</body>
</html>
|