Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: tests/compiler/dart2js_native/native_novel_html_test.dart

Issue 2379173002: Add native_testing library to mock @Native classes (Closed)
Patch Set: xxx Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 import "dart:_js_helper"; 5 import 'native_testing.dart';
6 import "package:expect/expect.dart";
7 6
8 // Test to see if novel HTML tags are interpreted as HTMLElement. 7 // Test to see if novel HTML tags are interpreted as HTMLElement.
9 8
10 @Native("HTMLElement") 9 @Native("HTMLElement")
11 class Element { 10 class Element {
12 String dartMethod(int x) => 'dartMethod(${nativeMethod(x+1)})'; 11 String dartMethod(int x) => 'dartMethod(${nativeMethod(x+1)})';
13 String nativeMethod(int x) native ; 12 String nativeMethod(int x) native ;
14 } 13 }
15 14
16 makeE() native ; 15 makeE() native ;
17 makeF() native ; 16 makeF() native ;
18 17
19 void setup() native """ 18 void setup() native """
20 // A novel HTML element. 19 // A novel HTML element.
21 function HTMLGoofyElement(){} 20 function HTMLGoofyElement(){}
22 HTMLGoofyElement.prototype.nativeMethod = function(a) { 21 HTMLGoofyElement.prototype.nativeMethod = function(a) {
23 return 'Goofy.nativeMethod(' + a + ')'; 22 return 'Goofy.nativeMethod(' + a + ')';
24 }; 23 };
25 makeE = function(){return new HTMLGoofyElement}; 24 makeE = function(){return new HTMLGoofyElement};
26 25
27 // A non-HTML element with a misleading name. 26 // A non-HTML element with a misleading name.
28 function HTMLFakeyElement(){} 27 function HTMLFakeyElement(){}
29 HTMLFakeyElement.prototype.nativeMethod = function(a) { 28 HTMLFakeyElement.prototype.nativeMethod = function(a) {
30 return 'Fakey.nativeMethod(' + a + ')'; 29 return 'Fakey.nativeMethod(' + a + ')';
31 }; 30 };
32 makeF = function(){return new HTMLFakeyElement}; 31 makeF = function(){return new HTMLFakeyElement};
33 32
34 // Make the HTMLGoofyElement look like a real host object. 33 self.nativeConstructor(HTMLGoofyElement);
35 var theRealObjectToString = Object.prototype.toString;
36 Object.prototype.toString = function() {
37 if (this instanceof HTMLGoofyElement) return '[object HTMLGoofyElement]';
38 return theRealObjectToString.call(this);
39 }
40 """; 34 """;
41 35
42 main() { 36 main() {
37 nativeTesting();
43 setup(); 38 setup();
44 39
45 var e = makeE(); 40 var e = makeE();
46 Expect.equals('Goofy.nativeMethod(10)', e.nativeMethod(10)); 41 Expect.equals('Goofy.nativeMethod(10)', e.nativeMethod(10));
47 Expect.equals('dartMethod(Goofy.nativeMethod(11))', e.dartMethod(10)); 42 Expect.equals('dartMethod(Goofy.nativeMethod(11))', e.dartMethod(10));
48 43
49 var f = makeF(); 44 var f = makeF();
50 Expect.throws(() => f.nativeMethod(20), (e) => e is NoSuchMethodError, 45 Expect.throws(() => f.nativeMethod(20), (e) => e is NoSuchMethodError,
51 'fake HTML Element must not run Dart method on native class'); 46 'fake HTML Element must not run Dart method on native class');
52 Expect.throws(() => f.dartMethod(20), (e) => e is NoSuchMethodError, 47 Expect.throws(() => f.dartMethod(20), (e) => e is NoSuchMethodError,
53 'fake HTML Element must not run native method on native class'); 48 'fake HTML Element must not run native method on native class');
54 } 49 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698