|
| 1 | +describe("TypeScript-extended native class construction", function () { |
| 2 | + var Holder = com.tns.tests.WrapperHolder; |
| 3 | + var Button1 = com.tns.tests.Button1; |
| 4 | + |
| 5 | + function collectBoth() { |
| 6 | + for (var k = 0; k < 2; k++) { |
| 7 | + gc(); |
| 8 | + java.lang.System.nanoTime(); |
| 9 | + for (var i = 0; i < 3; i++) { |
| 10 | + java.lang.Runtime.getRuntime().gc(); |
| 11 | + java.lang.System.runFinalization(); |
| 12 | + } |
| 13 | + gc(); |
| 14 | + java.lang.System.nanoTime(); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + afterEach(function () { |
| 19 | + Holder.held = null; |
| 20 | + }); |
| 21 | + |
| 22 | + // TypeScript's ES5 output for |
| 23 | + // class EchoButton extends com.tns.tests.Button1 { |
| 24 | + // label = "L" + value; count = 0; |
| 25 | + // constructor(value) { super(value); } |
| 26 | + // echo(s) { ... super.echo(s) ... } describe() { ... } static kind = "echo"; |
| 27 | + // } |
| 28 | + var EchoButton = (function (_super) { |
| 29 | + __extends(EchoButton, _super); |
| 30 | + function EchoButton(value) { |
| 31 | + var _this = _super.call(this, value) || this; |
| 32 | + _this.label = "L" + value; |
| 33 | + _this.count = 0; |
| 34 | + return global.__native(_this); |
| 35 | + } |
| 36 | + EchoButton.prototype.echo = function (s) { |
| 37 | + this.count++; |
| 38 | + this.lastEcho = s; |
| 39 | + return this.label + ":" + _super.prototype.echo.call(this, s); |
| 40 | + }; |
| 41 | + EchoButton.prototype.describe = function () { |
| 42 | + return this.label + "/" + this.count; |
| 43 | + }; |
| 44 | + EchoButton.prototype.superToString = function () { |
| 45 | + return this.super.toString(); |
| 46 | + }; |
| 47 | + EchoButton.kind = "echo"; |
| 48 | + return EchoButton; |
| 49 | + })(com.tns.tests.Button1); |
| 50 | + |
| 51 | + it("is an instance of its class and of every Java class above it", function () { |
| 52 | + var button = new EchoButton(1); |
| 53 | + expect(button instanceof EchoButton).toBe(true); |
| 54 | + expect(button instanceof Button1).toBe(true); |
| 55 | + expect(button instanceof com.tns.tests.ButtonBase).toBe(true); |
| 56 | + expect(button instanceof java.lang.Object).toBe(true); |
| 57 | + expect(new Button1() instanceof EchoButton).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it("shares one prototype chain between instances, with the class prototype on it", function () { |
| 61 | + var a = new EchoButton(1); |
| 62 | + var b = new EchoButton(2); |
| 63 | + var proto = Object.getPrototypeOf(a); |
| 64 | + expect(Object.getPrototypeOf(b)).toBe(proto); |
| 65 | + expect(Object.getPrototypeOf(proto)).toBe(EchoButton.prototype); |
| 66 | + expect(Object.getPrototypeOf(EchoButton.prototype)).toBe(Button1.prototype); |
| 67 | + expect(a.constructor.prototype).toBe(proto); |
| 68 | + expect(a instanceof a.constructor).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it("keeps the constructor's field initializers as own properties of the instance", function () { |
| 72 | + var button = new EchoButton(3); |
| 73 | + expect(Object.keys(button).sort()).toEqual(["count", "label"]); |
| 74 | + expect(button.label).toBe("L3"); |
| 75 | + expect(button.count).toBe(0); |
| 76 | + }); |
| 77 | + |
| 78 | + it("keeps the state of each instance apart", function () { |
| 79 | + var a = new EchoButton(1); |
| 80 | + var b = new EchoButton(2); |
| 81 | + a.extra = "a"; |
| 82 | + a.triggerEcho("x"); |
| 83 | + expect(b.extra).toBeUndefined(); |
| 84 | + expect(a.count).toBe(1); |
| 85 | + expect(b.count).toBe(0); |
| 86 | + expect(b.describe()).toBe("L2/0"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("runs the JS override when Java calls the method, with the instance as this", function () { |
| 90 | + var button = new EchoButton(4); |
| 91 | + expect(button.triggerEcho("hi")).toBe("L4:hi"); |
| 92 | + expect(button.count).toBe(1); |
| 93 | + expect(button.lastEcho).toBe("hi"); |
| 94 | + expect(button.describe()).toBe("L4/1"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("calls the Java implementation through super", function () { |
| 98 | + var button = new EchoButton(5); |
| 99 | + expect(button.echo("s")).toBe("L5:s"); |
| 100 | + var label = button.superToString(); |
| 101 | + expect(label.indexOf("com.tns.tests.Button1_")).not.toBe(-1); |
| 102 | + }); |
| 103 | + |
| 104 | + it("calls inherited Java methods and passes the instance to Java and back", function () { |
| 105 | + var button = new EchoButton(6); |
| 106 | + expect(button.getIMAGE_ID_PROP()).toBe("image id prop"); |
| 107 | + var list = new java.util.ArrayList(); |
| 108 | + list.add(button); |
| 109 | + expect(list.get(0)).toBe(button); |
| 110 | + }); |
| 111 | + |
| 112 | + it("keeps the class's static members and inherits the Java ones", function () { |
| 113 | + expect(EchoButton.kind).toBe("echo"); |
| 114 | + expect(EchoButton.SGetStaticImageId()).toBe("static image id"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("keeps the prototype chain and the expandos of a Java-held instance after a collection", function () { |
| 118 | + var proto = (function () { |
| 119 | + var button = new EchoButton(7); |
| 120 | + button.expando = "kept"; |
| 121 | + Holder.held = button; |
| 122 | + return Object.getPrototypeOf(button); |
| 123 | + })(); |
| 124 | + collectBoth(); |
| 125 | + |
| 126 | + var button = Holder.held; |
| 127 | + expect(button.triggerEcho("r")).toBe("L7:r"); |
| 128 | + expect(Object.getPrototypeOf(button)).toBe(proto); |
| 129 | + expect(button instanceof EchoButton).toBe(true); |
| 130 | + expect(button.expando).toBe("kept"); |
| 131 | + expect(button.describe()).toBe("L7/1"); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("when the constructor forwards its arguments with apply", function () { |
| 135 | + // TypeScript's output for a class without a constructor, and for `super(...args)`. |
| 136 | + var DefaultButton = (function (_super) { |
| 137 | + __extends(DefaultButton, _super); |
| 138 | + function DefaultButton() { |
| 139 | + return _super !== null && _super.apply(this, arguments) || this; |
| 140 | + } |
| 141 | + DefaultButton.prototype.echo = function (s) { |
| 142 | + return "default:" + s; |
| 143 | + }; |
| 144 | + return DefaultButton; |
| 145 | + })(com.tns.tests.Button1); |
| 146 | + var SpreadButton = (function (_super) { |
| 147 | + __extends(SpreadButton, _super); |
| 148 | + function SpreadButton(value) { |
| 149 | + var _this = _super.apply(this, [value]) || this; |
| 150 | + _this.value = value; |
| 151 | + return _this; |
| 152 | + } |
| 153 | + SpreadButton.prototype.echo = function (s) { |
| 154 | + return this.value + ":" + s; |
| 155 | + }; |
| 156 | + return SpreadButton; |
| 157 | + })(com.tns.tests.Button1); |
| 158 | + |
| 159 | + it("constructs the instance with the forwarded arguments", function () { |
| 160 | + var plain = new DefaultButton(); |
| 161 | + expect(plain instanceof DefaultButton).toBe(true); |
| 162 | + expect(plain.triggerEcho("a")).toBe("default:a"); |
| 163 | + |
| 164 | + var spread = new SpreadButton(3); |
| 165 | + expect(spread instanceof SpreadButton).toBe(true); |
| 166 | + expect(Object.keys(spread)).toEqual(["value"]); |
| 167 | + expect(spread.triggerEcho("b")).toBe("3:b"); |
| 168 | + }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments