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
|
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>MozFiveStar Tests</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<link
rel="stylesheet"
href="chrome://mochikit/content/tests/SimpleTest/test.css"
/>
<script
type="module"
src="chrome://global/content/elements/moz-five-star.mjs"
></script>
</head>
<body>
<p id="display"></p>
<div id="content" style="max-width: fit-content">
<moz-five-star label="Label" rating="2.5"></moz-five-star>
<moz-five-star rating="2" selectable></moz-five-star>
</div>
<script class="testbody" type="application/javascript">
const { BrowserTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/BrowserTestUtils.sys.mjs"
);
async function testRatingValue(
mozFiveStar,
value,
ratingRounded,
expectation
) {
is(mozFiveStar.rating, value, "Rating property is set to value");
if (mozFiveStar.ownerDocument.hasPendingL10nMutations) {
await BrowserTestUtils.waitForEvent(
mozFiveStar.ownerDocument,
"L10nMutationsFinished"
);
}
let starsString = Array.from(mozFiveStar.starEls)
.map(star => star.getAttribute("fill"))
.join(",");
is(starsString, expectation, `Rendering of rating ${value}`);
let ratingsString = Array.from(mozFiveStar.starEls)
.map(star => star.getAttribute("rating"))
.join(",");
is("1,2,3,4,5", ratingsString, `Rendering of rating attributes`);
}
add_task(async function testMozFiveStar() {
const mozFiveStar = document.querySelector("moz-five-star");
ok(mozFiveStar, "moz-five-star is rendered");
const stars = mozFiveStar.starEls;
ok(stars, "moz-five-star has stars");
is(stars.length, 5, "moz-five-star stars count is 5");
const rating = mozFiveStar.rating;
ok(rating, "moz-five-star has a rating");
is(rating, 2.5, "moz-five-star rating is 2.5");
});
add_task(async function testMozFiveStarsDisplay() {
const mozFiveStar = document.querySelector("moz-five-star");
ok(mozFiveStar, "moz-five-star is rendered");
async function testRating(rating, ratingRounded, expectation) {
mozFiveStar.rating = rating;
await mozFiveStar.updateComplete;
await testRatingValue(
mozFiveStar,
rating,
ratingRounded,
expectation
);
is(
mozFiveStar.starsWrapperEl.title,
`Rated ${ratingRounded} out of 5`,
"Rendered title must contain at most one fractional digit"
);
let isImage =
mozFiveStar.starsWrapperEl.getAttribute("role") == "img" ||
mozFiveStar.starsWrapperEl.getAttribute("role") == "image";
ok(
isImage,
"Rating element is an image for the title to be announced"
);
}
await testRating(0.0, "0", "empty,empty,empty,empty,empty");
await testRating(0.249, "0.2", "empty,empty,empty,empty,empty");
await testRating(0.25, "0.3", "half,empty,empty,empty,empty");
await testRating(0.749, "0.7", "half,empty,empty,empty,empty");
await testRating(0.99, "1", "full,empty,empty,empty,empty");
await testRating(1.0, "1", "full,empty,empty,empty,empty");
await testRating(2, "2", "full,full,empty,empty,empty");
await testRating(3.0, "3", "full,full,full,empty,empty");
await testRating(4.001, "4", "full,full,full,full,empty");
await testRating(4.249, "4.2", "full,full,full,full,empty");
await testRating(4.25, "4.3", "full,full,full,full,half");
await testRating(4.749, "4.7", "full,full,full,full,half");
await testRating(4.89, "4.9", "full,full,full,full,full");
await testRating(5.0, "5", "full,full,full,full,full");
});
add_task(async function testMozFiveStarsSelectable() {
const selectableMozFiveStar = document.querySelector(
"moz-five-star[selectable]"
);
const selectedEvents = [];
selectableMozFiveStar.addEventListener("select", e =>
selectedEvents.push(e)
);
await selectableMozFiveStar.updateComplete;
await testRatingValue(
selectableMozFiveStar,
2,
"2",
"full,full,empty,empty,empty"
);
const fifthStar = Array.from(selectableMozFiveStar.starEls).at(4);
fifthStar.click();
is(selectedEvents.length, 1, "dispatched one event");
const selectedEvent = selectedEvents.at(0);
is(selectedEvent.type, "select", "dispatched select event");
is(selectedEvent.detail.rating, 5, "dispatched selected rating number");
await selectableMozFiveStar.updateComplete;
await testRatingValue(
selectableMozFiveStar,
5,
"5",
"full,full,full,full,full"
);
});
add_task(async function testMozFiveStarsSelectableHover() {
function assertFilledBg(star) {
ok(
getComputedStyle(star).backgroundImage.includes("#full"),
"bg is filled"
);
}
function assertNotFilledBg(star) {
ok(
getComputedStyle(star).backgroundImage.includes("#empty"),
"bg is not filled"
);
}
const selectableMozFiveStar = document.querySelector(
"moz-five-star[selectable]"
);
selectableMozFiveStar.rating = 1;
await selectableMozFiveStar.updateComplete;
const [firstStar, secondStar, thirdStar, fourthStar, fifthStar] =
selectableMozFiveStar.starEls;
assertFilledBg(firstStar);
assertNotFilledBg(secondStar, thirdStar, fourthStar, fifthStar);
synthesizeMouseAtCenter(fourthStar, { type: "mouseover" });
assertFilledBg(firstStar, secondStar, thirdStar, fourthStar);
assertNotFilledBg(fifthStar);
});
</script>
</body>
</html>
|