chromium/third_party/blink/web_tests/external/wpt/trusted-types/block-string-assignment-to-DOMWindowTimers-setTimeout-setInterval.html

<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>

<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
<body>
<script>
  // setTimeout tests
  // TrustedScript assignments do not throw.
  async_test(t => {
    window.timeoutTest = t;
    let policy = createScript_policy(window, 'timeout');
    let script = policy.createScript("window.timeoutTest.done();");
    setTimeout(script);
  }, "window.setTimeout assigned via policy (successful Script transformation).");

  // String assignments throw.
  test(t => {
    window.timeoutTestString = t.unreached_func();
    assert_throws_js(TypeError, _ => {
      setTimeout("window.timeoutTestString();");
    });
  }, "`window.setTimeout(string)` throws.");

  // Null assignment throws.
  test(t => {
    assert_throws_js(TypeError, _ => {
      setTimeout(null);
    });
  }, "`window.setTimeout(null)` throws.");

  // setInterval tests
  // TrustedScript assignments do not throw.
  async_test(t => {
    window.intervalTest = t;
    let policy = createScript_policy(window, 'script');
    let script = policy.createScript("window.intervalTest.done();");
    setInterval(script);
  }, "window.setInterval assigned via policy (successful Script transformation).");

  // String assignments throw.
  test(t => {
    window.intervalTestString = t.unreached_func();
    assert_throws_js(TypeError, _ => {
      setInterval("window.intervalTestString()");
    });
  }, "`window.setInterval(string)` throws.");

  // Null assignment throws.
  test(t => {
    assert_throws_js(TypeError, _ => {
      setInterval(null);
    });
  }, "`window.setInterval(null)` throws.");

  let policy = window.trustedTypes.createPolicy("default", { createScript: (x, _, sink) => {
    if (x === "timeoutTestString") {
      assert_equals(sink, 'Window setTimeout');
    } else if (x === "intervalTestString") {
      assert_equals(sink, 'Window setInterval');
    }
    return "0";
  }});
  // After default policy creation string assignment implicitly calls createScript.
  test(t => {
    setTimeout(INPUTS.SCRIPT);
    setInterval(INPUTS.SCRIPT);
  }, "`setTimeout(string)`, `setInterval(string)` via default policy (successful Script transformation).");
  // After default policy creation null assignment implicitly calls createScript.
  test(t => {
    setTimeout(null);
    setInterval(null);
  }, "`setTimeout(null)`, `setInterval(null)` via default policy (successful Script transformation).");
</script>