origin-from-messageevent.window.js (3074B)
1 // META: title=`Origin.from(MessageEvent)` 2 // META: script=/common/get-host-info.sub.js 3 4 test(t => { 5 const e = new MessageEvent("message", { origin: get_host_info().ORIGIN }); 6 assert_throws_js(TypeError, _ => Origin.from(e)); 7 }, "Constructed `MessageEvent` objects have no real origins."); 8 9 async_test(t => { 10 const el = document.createElement('iframe'); 11 el.src = "/html/browsers/windows/resources/message-parent.html" 12 window.addEventListener("message", t.step_func(e => { 13 if (e.source === el.contentWindow) { 14 const origin = Origin.from(e); 15 assert_true(!!origin); 16 assert_false(origin.opaque); 17 assert_true(origin.isSameOrigin(Origin.from(get_host_info().ORIGIN))); 18 t.done(); 19 } 20 })); 21 document.body.appendChild(el); 22 }, `Origin.from(MessageEvent) returns a tuple origin for messages from same-origin frames.`); 23 24 async_test(t => { 25 const el = document.createElement('iframe'); 26 el.src = get_host_info().REMOTE_ORIGIN + "/html/browsers/windows/resources/message-parent.html" 27 window.addEventListener("message", t.step_func(e => { 28 if (e.source === el.contentWindow) { 29 const origin = Origin.from(e); 30 assert_true(!!origin); 31 assert_false(origin.opaque); 32 assert_true(origin.isSameOrigin(Origin.from(get_host_info().REMOTE_ORIGIN))); 33 t.done(); 34 } 35 })); 36 document.body.appendChild(el); 37 }, `Origin.from(MessageEvent) returns a tuple origin for messages from cross-origin frames.`); 38 39 async_test(t => { 40 const el = document.createElement('iframe'); 41 el.src = get_host_info().REMOTE_ORIGIN + "/html/browsers/windows/resources/message-parent.html" 42 el.sandbox = "allow-scripts"; 43 window.addEventListener("message", t.step_func(e => { 44 if (e.source === el.contentWindow) { 45 const origin = Origin.from(e); 46 assert_true(!!origin); 47 assert_true(origin.opaque); 48 assert_false(origin.isSameOrigin(Origin.from(get_host_info().REMOTE_ORIGIN))); 49 t.done(); 50 } 51 })); 52 document.body.appendChild(el); 53 }, `Origin.from(MessageEvent) returns an opaque origin for messages from sandboxed frames.`); 54 55 async_test(t => { 56 const w = window.open("/html/browsers/windows/resources/post-to-opener.html"); 57 window.addEventListener("message", t.step_func(e => { 58 if (e.source === w) { 59 const origin = Origin.from(e); 60 assert_true(!!origin); 61 assert_false(origin.opaque); 62 assert_true(origin.isSameOrigin(Origin.from(get_host_info().ORIGIN))); 63 t.done(); 64 } 65 })); 66 }, `Origin.from(MessageEvent) returns a tuple origin for same-origin windows.`); 67 68 async_test(t => { 69 const w = window.open(get_host_info().REMOTE_ORIGIN + "/html/browsers/windows/resources/post-to-opener.html"); 70 window.addEventListener("message", t.step_func(e => { 71 if (e.source === w) { 72 const origin = Origin.from(e); 73 assert_true(!!origin); 74 assert_false(origin.opaque); 75 assert_true(origin.isSameOrigin(Origin.from(get_host_info().REMOTE_ORIGIN))); 76 t.done(); 77 } 78 })); 79 }, `Origin.from(MessageEvent) returns a tuple origin for cross-origin windows.`);