OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 import 'dart:_foreign_helper' show JS; |
| 7 import 'dart:_js_helper' show Creates, setNativeSubclassDispatchRecord; |
| 8 import 'dart:_interceptors' show |
| 9 findInterceptorForType, findConstructorForWebComponentType; |
| 10 |
| 11 // Test that subclasses of native classes can be initialized by calling the |
| 12 // 'upgrade' constructor. |
| 13 |
| 14 var trace = []; |
| 15 |
| 16 var log; |
| 17 |
| 18 class A native "A" { |
| 19 final a1 = log(101); // Only initialized IF named constructor called. |
| 20 final a2; // Initialized by native constructor. |
| 21 final a3; // Initialized only by A.two. |
| 22 var a4 = log(104); |
| 23 |
| 24 A.one(); |
| 25 |
| 26 A.two() : a3 = log(103) { |
| 27 log('body(A.two)'); |
| 28 log(a4 += increment); |
| 29 } |
| 30 |
| 31 A.three(x, this.a4) { |
| 32 log('body(A.three)'); |
| 33 log(a4 = '($a4, $x)'); |
| 34 } |
| 35 |
| 36 get increment => 10; |
| 37 } |
| 38 |
| 39 class B extends A { |
| 40 final b1; |
| 41 final b2 = log(202); |
| 42 var b3; |
| 43 |
| 44 B.one() : super.one(); |
| 45 |
| 46 B.two() : b1 = log(201), super.two(), b3 = log(203) { |
| 47 log('body(B.two)'); |
| 48 } |
| 49 |
| 50 B.three([x]) : super.three(205, x); |
| 51 |
| 52 get increment => 20; |
| 53 } |
| 54 |
| 55 |
| 56 makeB() native; |
| 57 |
| 58 @Creates('=Object') |
| 59 getBPrototype() native; |
| 60 |
| 61 void setup() native r""" |
| 62 function B() { this.a2 = 102; } |
| 63 |
| 64 makeB = function(){return new B;}; |
| 65 |
| 66 getBPrototype = function(){return B.prototype;}; |
| 67 """; |
| 68 |
| 69 |
| 70 test_one() { |
| 71 trace = []; |
| 72 var constructor = findConstructorForWebComponentType(B, 'one'); |
| 73 Expect.isNotNull(constructor); |
| 74 Expect.isNull(findConstructorForWebComponentType(B, 'Missing')); |
| 75 |
| 76 var b = makeB(); |
| 77 Expect.isTrue(b is B); |
| 78 // Call constructor to initialize native object. |
| 79 var b2 = JS('', '#(#)', constructor, b); |
| 80 Expect.identical(b, b2); |
| 81 Expect.isTrue(b is B); |
| 82 |
| 83 Expect.equals(101, b.a1); |
| 84 Expect.equals(102, b.a2); |
| 85 Expect.equals(null, b.a3); |
| 86 Expect.equals(104, b.a4); |
| 87 Expect.equals(null, b.b1); |
| 88 Expect.equals(202, b.b2); |
| 89 Expect.equals(null, b.b3); |
| 90 |
| 91 Expect.equals('[202, 101, 104]', '$trace'); |
| 92 } |
| 93 |
| 94 test_two() { |
| 95 trace = []; |
| 96 var constructor = findConstructorForWebComponentType(B, 'two'); |
| 97 Expect.isNotNull(constructor); |
| 98 |
| 99 var b = makeB(); |
| 100 Expect.isTrue(b is B); |
| 101 // Call constructor to initialize native object. |
| 102 JS('', '#(#)', constructor, b); |
| 103 Expect.isTrue(b is B); |
| 104 |
| 105 Expect.equals(101, b.a1); |
| 106 Expect.equals(102, b.a2); |
| 107 Expect.equals(103, b.a3); |
| 108 Expect.equals(124, b.a4); |
| 109 Expect.equals(201, b.b1); |
| 110 Expect.equals(202, b.b2); |
| 111 Expect.equals(203, b.b3); |
| 112 |
| 113 Expect.equals( |
| 114 '[202, 201, 101, 104, 103, 203, body(A.two), 124, body(B.two)]', |
| 115 '$trace'); |
| 116 } |
| 117 |
| 118 test_three() { |
| 119 trace = []; |
| 120 var constructor = findConstructorForWebComponentType(B, 'three'); |
| 121 Expect.isNotNull(constructor); |
| 122 |
| 123 var b = makeB(); |
| 124 Expect.isTrue(b is B); |
| 125 // Call constructor to initialize native object. |
| 126 // |
| 127 // Since the constructor takes some optional arguments that are not passed, it |
| 128 // is as though the web components runtime explicitly passed `null` for all |
| 129 // parameters. |
| 130 // |
| 131 // TODO(sra): The constructor returned by findConstructorForWebComponentType |
| 132 // should be a function that fills in the default values. |
| 133 JS('', '#(#)', constructor, b); |
| 134 Expect.isTrue(b is B); |
| 135 |
| 136 Expect.equals(101, b.a1); |
| 137 Expect.equals(102, b.a2); |
| 138 Expect.equals(null, b.a3); |
| 139 Expect.equals('(null, 205)', b.a4); |
| 140 Expect.equals(null, b.b1); |
| 141 Expect.equals(202, b.b2); |
| 142 Expect.equals(null, b.b3); |
| 143 print(trace); |
| 144 Expect.equals('[202, 101, 104, body(A.three), (null, 205)]', '$trace'); |
| 145 } |
| 146 |
| 147 test_new() { |
| 148 trace = []; |
| 149 checkThrows(action, description) { |
| 150 Expect.throws(action, (e) => true, "'$description must fail'"); |
| 151 } |
| 152 |
| 153 checkThrows(() => new B.one(), 'new B.one()'); |
| 154 checkThrows(() => new B.two(), 'new B.two()'); |
| 155 checkThrows(() => new B.three(), 'new B.three()'); |
| 156 checkThrows(() => new B.three(1), 'new B.three(1)'); |
| 157 checkThrows(() => new B.three([]), 'new B.three([])'); |
| 158 } |
| 159 |
| 160 var inscrutable; |
| 161 |
| 162 main() { |
| 163 setup(); |
| 164 inscrutable = (x) => x; |
| 165 log = (message) { |
| 166 trace.add('$message'); |
| 167 return message; |
| 168 }; |
| 169 |
| 170 setNativeSubclassDispatchRecord(getBPrototype(), findInterceptorForType(B)); |
| 171 |
| 172 test_one(); |
| 173 test_two(); |
| 174 test_three(); |
| 175 test_new(); |
| 176 } |
OLD | NEW |