blob: 86b62cf1b765d35ed9c75312f4fad62b2e37ddff [file] [edit]
<!doctype html>
<title>HighlightRegistry has a setlike interface</title>
<link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
test(() => {
assert_not_equals(window.HighlightRegistry, undefined, 'HighlightRegistry is in window');
assert_true(typeof HighlightRegistry === 'function', 'HighlightRegistry is a function');
assert_throws_js(TypeError, function () { var x = new HighlightRegistry(); },
'HighlightRegistry constructor throws');
assert_not_equals(CSS.highlights, undefined, 'CSS.highlights exists');
assert_equals(CSS.highlights.__proto__, window.HighlightRegistry.prototype,
'CSS.highlights and window.HighlightRegistry have same prototype');
assert_equals(CSS.highlights.size, 0, 'HighlightRegistry starts empty');
}, 'HighlightRegistry initializes as it should.');
test(() => {
let customHighlight1 = new Highlight("example1");
let customHighlight2 = new Highlight("example2");
let customHighlight3 = new Highlight("example1");
assert_false(CSS.highlights.has(customHighlight1), 'HighlightRegistry.has returns false when it doesn\'t have the element which is called with as argument.');
CSS.highlights.add(customHighlight1);
assert_true(CSS.highlights.has(customHighlight1), 'HighlightRegistry.has returns true when it has the element which is called with as argument.');
assert_equals(CSS.highlights.size, 1, 'HighlightRegistry.size is 1 after only adding 1 element.');
assert_false(CSS.highlights.delete(customHighlight2), 'HighlightRegistry.delete returns false when trying to delete an element that is not in the set.');
assert_equals(CSS.highlights.size, 1, 'HighlightRegistry.size stays the same after trying to delete a non-existing element.');
CSS.highlights.add(customHighlight2);
assert_true(CSS.highlights.has(customHighlight1), 'HighlightRegistry.has returns true when it is called with the element added first');
assert_true(CSS.highlights.has(customHighlight2), 'HighlightRegistry.has returns true when it is called with the element added second');
assert_equals(CSS.highlights.size, 2, 'HighlightRegistry.size is 2 after only adding two different elements.');
assert_true(CSS.highlights.delete(customHighlight2), 'HighlightRegistry.delete returns true when trying to delete an element that is in the group.');
assert_true(CSS.highlights.has(customHighlight1), 'HighlightRegistry.has returns true when it is called with the element added first');
assert_false(CSS.highlights.has(customHighlight2), 'HighlightRegistry.has returns true when it is called with the element that was deleted');
assert_equals(CSS.highlights.size, 1, 'HighlightRegistry.size decreases in 1 after deleting an existing element.');
assert_false(CSS.highlights.delete(customHighlight2), 'HighlightRegistry.delete returns false when trying to delete an element that was already deleted.');
assert_true(CSS.highlights.has(customHighlight1), 'HighlightRegistry.has returns true when it is called with the element added first');
assert_false(CSS.highlights.has(customHighlight2), 'HighlightRegistry.has returns false when it is called with an element that was deleted twice');
assert_equals(CSS.highlights.size, 1, 'HighlightRegistry.size stays the same after trying to delete the same element for a second time.');
assert_true(CSS.highlights.delete(customHighlight1), 'HighlightRegistry.delete returns true when trying to delete the remaining element');
assert_false(CSS.highlights.has(customHighlight1), 'HighlightRegistry.has returns false when it is called with the element added first and then deleted');
assert_false(CSS.highlights.has(customHighlight2), 'HighlightRegistry.has returns false when it is called with the element added second and then deleted');
assert_equals(CSS.highlights.size, 0, 'HighlightRegistry.size decreases in 1 after deleting an existing element.');
CSS.highlights.add(customHighlight1);
assert_throws_dom('OperationError', () => { CSS.highlights.add(customHighlight3); }, 'add throws when trying to add a Highlight with the same name as one present in the registry');
assert_equals(CSS.highlights.size, 1, 'HighlightRegistry.size keeps the same after an unsuccessful addition of a Highlight with an existing name');
assert_true(CSS.highlights.has(customHighlight1), 'The Highlight that has the same name as the one that was tried to be added later is still in the registry');
assert_false(CSS.highlights.has(customHighlight3), 'The Highlight with same name as an existing one was effectively not added to the registry');
CSS.highlights.clear();
assert_equals(CSS.highlights.size, 0, 'HighlightRegistry.clear empties the set.');
CSS.highlights.clear();
assert_equals(CSS.highlights.size, 0, 'HighlightRegistry.clear called with an empty registry keeps it empty.');
}, 'HighlightRegistry has a setlike interface.');
</script>