summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/document-metadata/the-meta-element/color-scheme/support/compute-root-color-scheme.js
blob: 74cbf895ced3d9b7f4783be993d5da274ca081f3 (plain)
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
'use strict';

function assert_root_color_scheme(expected_used_scheme, description) {
  function get_used_root_color_scheme() {
    let light = get_system_color("only light", "CanvasText");
    let dark = get_system_color("only dark", "CanvasText");
    assert_not_equals(light, dark, "CanvasText system color should be different with light and dark color schemes");
    let root = getComputedStyle(document.documentElement).color;
    assert_in_array(root, [light, dark], "Root color scheme should be either light or dark, or the text needs to be extended for newer color-schemes");
    return root == light ? "light" : "dark";
  }

  function get_system_color(scheme, color) {
    let div = document.createElement("div");
    div.style.color = color;
    div.style.colorScheme = scheme;

    document.documentElement.appendChild(div);
    let computed = getComputedStyle(div).color;
    div.remove();
    return computed;
  }

  test(() => {
    assert_equals(get_used_root_color_scheme(), expected_used_scheme);
    assert_equals(getComputedStyle(document.documentElement).colorScheme, "normal", "Root element's color-scheme should be 'normal'");
  }, description);
}