| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Test to see if novel HTML tags are interpreted as HTMLElement. | 5 // Test to see if novel HTML tags are interpreted as HTMLElement. |
| 6 | 6 |
| 7 class Element native "*HTMLElement" { | 7 class Element native "*HTMLElement" { |
| 8 String foo(int x) => '[${bar(x+1)}]'; | 8 String dartMethod(int x) => 'dartMethod(${nativeMethod(x+1)})'; |
| 9 String bar(int x) native; | 9 String nativeMethod(int x) native; |
| 10 } | 10 } |
| 11 | 11 |
| 12 makeE() native; | 12 makeE() native; |
| 13 makeF() native; | 13 makeF() native; |
| 14 | 14 |
| 15 void setup() native """ | 15 void setup() native """ |
| 16 // A novel HTML element. | 16 // A novel HTML element. |
| 17 function HTMLGoofyElement(){} | 17 function HTMLGoofyElement(){} |
| 18 HTMLGoofyElement.prototype.bar = function(a){return 'Goofy.foo(' + a + ')';} | 18 HTMLGoofyElement.prototype.nativeMethod = function(a) { |
| 19 return 'Goofy.nativeMethod(' + a + ')'; |
| 20 }; |
| 19 makeE = function(){return new HTMLGoofyElement}; | 21 makeE = function(){return new HTMLGoofyElement}; |
| 20 | 22 |
| 21 // A non-HTML element with a misleading name. | 23 // A non-HTML element with a misleading name. |
| 22 function HTMLFakeyElement(){} | 24 function HTMLFakeyElement(){} |
| 23 HTMLFakeyElement.prototype.bar = function(a){return 'Fakey.foo(' + a + ')';} | 25 HTMLFakeyElement.prototype.nativeMethod = function(a) { |
| 26 return 'Fakey.nativeMethod(' + a + ')'; |
| 27 }; |
| 24 makeF = function(){return new HTMLFakeyElement}; | 28 makeF = function(){return new HTMLFakeyElement}; |
| 25 | 29 |
| 26 // Make the HTMLGoofyElement look like a real host object. | 30 // Make the HTMLGoofyElement look like a real host object. |
| 27 var theRealObjectToString = Object.prototype.toString; | 31 var theRealObjectToString = Object.prototype.toString; |
| 28 Object.prototype.toString = function() { | 32 Object.prototype.toString = function() { |
| 29 if (this instanceof HTMLGoofyElement) return '[object HTMLGoofyElement]'; | 33 if (this instanceof HTMLGoofyElement) return '[object HTMLGoofyElement]'; |
| 30 return theRealObjectToString.call(this); | 34 return theRealObjectToString.call(this); |
| 31 } | 35 } |
| 32 """; | 36 """; |
| 33 | 37 |
| 34 | 38 |
| 35 main() { | 39 main() { |
| 36 setup(); | 40 setup(); |
| 37 | 41 |
| 38 print(123); | |
| 39 var e = makeE(); | 42 var e = makeE(); |
| 40 Expect.equals('[Goofy.foo(11)]', e.foo(10)); | 43 Expect.equals('Goofy.nativeMethod(10)', e.nativeMethod(10)); |
| 44 Expect.equals('dartMethod(Goofy.nativeMethod(11))', e.dartMethod(10)); |
| 41 | 45 |
| 42 var f = makeF(); | 46 var f = makeF(); |
| 43 expectNoSuchMethod(() => f.foo(20), 'f.foo(20) should fail'); | 47 Expect.throws(() => f.nativeMethod(20), (e) => e is NoSuchMethodError, |
| 48 'fake HTML Element must not run Dart method on native class'); |
| 49 Expect.throws(() => f.dartMethod(20), (e) => e is NoSuchMethodError, |
| 50 'fake HTML Element must not run native method on native class'); |
| 44 } | 51 } |
| 45 | |
| 46 expectNoSuchMethod(action, note) { | |
| 47 bool caught = false; | |
| 48 try { | |
| 49 action(); | |
| 50 } catch (ex) { | |
| 51 caught = true; | |
| 52 Expect.isTrue(ex is NoSuchMethodError, note); | |
| 53 } | |
| 54 Expect.isTrue(caught, note); | |
| 55 } | |
| OLD | NEW |