summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/the-window-object/window-open-windowfeatures-values.html
blob: 32551dd8d70f1e19b906b4ee228fd7807ecbb786 (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
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
<!doctype html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>window.open() windowFeature value parsing</title>
<link rel="author" href="mailto:masonf@chromium.org">
<link rel="help" href="https://html.spec.whatwg.org/multipage/window-object.html#concept-window-open-features-parse-boolean">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
function testValueGeneric(val, expectTrue, property, testFn) {
  const windowFeatureStr = val === "" ? property : `${property}=${val}`;
  async_test(t => {
    const windowName = '' + Math.round(Math.random()*1e12);
    const channel = new BroadcastChannel(windowName);
    channel.onmessage = t.step_func_done(e => {
      // Send message first so if asserts throw the popup is still closed
      channel.postMessage(null);
      testFn(e.data);
    });
    window.open("support/windowFeature-values-target.html?" + windowName, windowName, windowFeatureStr);
  },`Test ${windowFeatureStr}, expected interpretation is ${expectTrue ? 'true' : 'false'}`);
}

function testValueForNoReferrer(val, expectTrue) {
  testValueGeneric(val, expectTrue, "noreferrer", (data) => {
    if (expectTrue) {
      assert_false(data.haveReferrer);
      assert_false(data.haveOpener);
    } else {
      assert_true(data.haveReferrer);
      assert_true(data.haveOpener);
    }
  });
}

function testValueForNoOpener(val, expectTrue) {
  testValueGeneric(val, expectTrue, "noopener", (data) => {
    assert_equals(data.haveOpener, !expectTrue);
  });
}

function testValueForPopup(val, expectTrue) {
  testValueGeneric(val, expectTrue, "popup", (data) => {
    assert_equals(data.isPopup, expectTrue);
  });
}

function testValue(val, expectTrue) {
  const quotes = val === "" ? [''] : ['','"',"'"];
  let noQuotes = true;
  for (const quote of quotes) {
    const thisExpectTrue = expectTrue && noQuotes;
    const thisVal = quote + val + quote;
    testValueForNoReferrer(thisVal, thisExpectTrue);
    testValueForNoOpener(thisVal, thisExpectTrue);
    testValueForPopup(thisVal, thisExpectTrue);
    noQuotes = false;
  }
}

testValue('',true); // Just the parameter means true
testValue('yes',true); // Yes means true
testValue('true',true); // True means true
testValue('foo',false); // If parsing as an integer is an error, false
testValue('0',false);   // 0 is false
testValue('00',false);  // 0 is false
testValue('1',true);    // Non-zero is true
testValue('99999',true);    // Non-zero is true
testValue('-1',true);    // Non-zero is true
testValue('1foo',true); // This parses to 1
testValue('0foo',false); // This parses to 0
</script>