| OLD | NEW |
| 1 #library('InnerFrameTest'); | 1 #library('InnerFrameTest'); |
| 2 #import('../../pkg/unittest/unittest.dart'); | 2 #import('../../pkg/unittest/unittest.dart'); |
| 3 #import('../../pkg/unittest/html_config.dart'); | 3 #import('../../pkg/unittest/html_config.dart'); |
| 4 #import('dart:html'); | 4 #import('dart:html'); |
| 5 | 5 |
| 6 main() { | 6 main() { |
| 7 if (window != window.top) { | 7 if (window != window.top) { |
| 8 // Child frame. | 8 // Child frame. |
| 9 | 9 |
| 10 // The child's frame should not be able to access its parent's | 10 // The child's frame should not be able to access its parent's |
| 11 // document. | 11 // document. |
| 12 | 12 |
| 13 // Check window.frameElement. | 13 window.on.message.add((Event e) { |
| 14 try { | 14 switch (e.data) { |
| 15 var parentDocument = window.frameElement.document; | 15 case 'frameElement': { |
| 16 var div = parentDocument.$dom_createElement("div"); | 16 // Check window.frameElement. |
| 17 div.id = "illegalFrameElement"; | 17 try { |
| 18 parentDocument.body.nodes.add(div); | 18 var parentDocument = window.frameElement.document; |
| 19 Expect.fail('Should not reach here.'); | 19 var div = parentDocument.$dom_createElement("div"); |
| 20 } on NoSuchMethodError catch (e) { | 20 div.id = "illegalFrameElement"; |
| 21 // Expected. | 21 parentDocument.body.nodes.add(div); |
| 22 } | 22 Expect.fail('Should not reach here.'); |
| 23 | 23 } on NoSuchMethodError catch (e) { |
| 24 // Check window.top. | 24 // Expected. |
| 25 try { | 25 window.top.postMessage('pass_frameElement', '*'); |
| 26 final top = window.top; | 26 } catch (e) { |
| 27 var parentDocument = top.document; | 27 window.top.postMessage('fail_frameElement', '*'); |
| 28 var div = parentDocument.$dom_createElement("div"); | 28 } |
| 29 div.id = "illegalTop"; | 29 return; |
| 30 parentDocument.body.nodes.add(div); | 30 } |
| 31 Expect.fail('Should not reach here.'); | 31 case 'top': { |
| 32 } catch (e) { | 32 // Check window.top. |
| 33 // Expected. | 33 try { |
| 34 // TODO(vsm): Enforce this is a NoSuchMethodError. | 34 final top = window.top; |
| 35 } | 35 var parentDocument = top.document; |
| 36 return; | 36 var div = parentDocument.$dom_createElement("div"); |
| 37 div.id = "illegalTop"; |
| 38 parentDocument.body.nodes.add(div); |
| 39 Expect.fail('Should not reach here.'); |
| 40 } on NoSuchMethodError catch (e) { |
| 41 // Expected. |
| 42 window.top.postMessage('pass_top', '*'); |
| 43 } catch (e) { |
| 44 window.top.postMessage('fail_top', '*'); |
| 45 } |
| 46 return; |
| 47 } |
| 48 case 'parent': { |
| 49 // Check window.parent. |
| 50 try { |
| 51 final parent = window.parent; |
| 52 var parentDocument = parent.document; |
| 53 var div = parentDocument.$dom_createElement("div"); |
| 54 div.id = "illegalParent"; |
| 55 parentDocument.body.nodes.add(div); |
| 56 Expect.fail('Should not reach here.'); |
| 57 } on NoSuchMethodError catch (e) { |
| 58 // Expected. |
| 59 window.top.postMessage('pass_parent', '*'); |
| 60 } catch (e) { |
| 61 window.top.postMessage('fail_parent', '*'); |
| 62 } |
| 63 return; |
| 64 } |
| 65 } |
| 66 }); |
| 37 } | 67 } |
| 38 | 68 |
| 39 // Parent / test frame | 69 // Parent / test frame |
| 40 useHtmlConfiguration(); | 70 useHtmlConfiguration(); |
| 41 | 71 |
| 42 final iframe = new Element.tag('iframe'); | 72 final iframe = new Element.tag('iframe'); |
| 43 iframe.src = window.location.href; | 73 iframe.src = window.location.href; |
| 74 var child; |
| 44 | 75 |
| 45 test('prepare', () { | 76 test('prepare', () { |
| 46 iframe.on.load.add(expectAsync1((e) {})); | 77 iframe.on.load.add(expectAsync1((e) { child = iframe.contentWindow;})); |
| 47 document.body.nodes.add(iframe); | 78 document.body.nodes.add(iframe); |
| 48 }); | 79 }); |
| 49 | 80 |
| 81 final validate = (testName, verify) { |
| 82 final expectedVerify = expectAsync0(verify); |
| 83 window.on.message.add((e) { |
| 84 guardAsync(() { |
| 85 if (e.data == 'pass_$testName') { |
| 86 expectedVerify(); |
| 87 } |
| 88 expect(e.data != 'fail_$testName'); |
| 89 }); |
| 90 }); |
| 91 child.postMessage(testName, '*'); |
| 92 }; |
| 93 |
| 50 test('frameElement', () { | 94 test('frameElement', () { |
| 51 var div = document.query('#illegalFrameElement'); | 95 validate('frameElement', () { |
| 96 var div = document.query('#illegalFrameElement'); |
| 52 | 97 |
| 53 // Ensure that this parent frame was not modified by its child. | 98 // Ensure that this parent frame was not modified by its child. |
| 54 Expect.isNull(div); | 99 expect(div, isNull); |
| 100 }); |
| 55 }); | 101 }); |
| 56 | 102 |
| 57 test('top', () { | 103 test('top', () { |
| 58 var div = document.query('#illegalTop'); | 104 validate('top', () { |
| 105 var div = document.query('#illegalTop'); |
| 59 | 106 |
| 60 // Ensure that this parent frame was not modified by its child. | 107 // Ensure that this parent frame was not modified by its child. |
| 61 Expect.isNull(div); | 108 expect(div, isNull); |
| 109 }); |
| 110 }); |
| 111 |
| 112 test('parent', () { |
| 113 validate('parent', () { |
| 114 var div = document.query('#illegalParent'); |
| 115 |
| 116 // Ensure that this parent frame was not modified by its child. |
| 117 expect(div, isNull); |
| 118 }); |
| 62 }); | 119 }); |
| 63 } | 120 } |
| OLD | NEW |