blob: 56049207964709b85d42438b85cd035b56bb083b [file] [log] [blame]
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'native_testing.dart';
import 'dart:_js_helper' show setNativeSubclassDispatchRecord;
import 'dart:_interceptors'
show
JSObject, // The interface, which may be re-exported by a
// js-interop library.
LegacyJavaScriptObject, // The interceptor abstract class.
PlainJavaScriptObject, // The interceptor concrete class.
UnknownJavaScriptObject, // The interceptor concrete class.
Interceptor;
// Test for JavaScript objects from outside the Dart program. Although we only
// export the interface [JSObject] to user level code, this test makes sure we
// can distinguish plain JavaScript objects from ones with a complex prototype.
@Native('QQ')
class Q {}
makeA() native;
makeB() native;
makeQ() native;
void setup() {
JS('', r"""
(function(){
self.makeA = function(){return {hello: 123};};
function BB(){}
self.makeB = function(){return new BB();};
function QQ(){}
self.makeQ = function(){return new QQ();};
self.nativeConstructor(QQ);
})()""");
applyTestExtensions(['QQ']);
}
class Is<T> {
bool check(x) => x is T;
}
static_test() {
var x = makeA();
Expect.isTrue(x is JSObject);
Expect.isTrue(x is LegacyJavaScriptObject);
Expect.isTrue(x is PlainJavaScriptObject);
Expect.isTrue(x is! UnknownJavaScriptObject);
Expect.equals(JSObject, x.runtimeType);
x = makeB();
Expect.isTrue(x is JSObject);
Expect.isTrue(x is LegacyJavaScriptObject);
Expect.isTrue(x is! PlainJavaScriptObject);
Expect.isTrue(x is UnknownJavaScriptObject);
Expect.equals(JSObject, x.runtimeType);
x = makeQ();
Expect.isFalse(x is JSObject);
Expect.isFalse(x is LegacyJavaScriptObject);
Expect.isFalse(x is PlainJavaScriptObject);
Expect.isFalse(x is UnknownJavaScriptObject);
Expect.isFalse(x.runtimeType == JSObject);
Expect.isTrue(x is Q);
}
dynamic_test() {
var x = makeA();
var isJSObject = new Is<JSObject>().check;
var isLegacyJavaScriptObject = new Is<LegacyJavaScriptObject>().check;
var isPlainJavaScriptObject = new Is<PlainJavaScriptObject>().check;
var isUnknownJavaScriptObject = new Is<UnknownJavaScriptObject>().check;
var isQ = new Is<Q>().check;
Expect.isTrue(isJSObject(x));
Expect.isTrue(isLegacyJavaScriptObject(x));
Expect.isTrue(isPlainJavaScriptObject(x));
Expect.isTrue(!isUnknownJavaScriptObject(x));
Expect.equals(JSObject, x.runtimeType);
x = makeB();
Expect.isTrue(isJSObject(x));
Expect.isTrue(isLegacyJavaScriptObject(x));
Expect.isTrue(!isPlainJavaScriptObject(x));
Expect.isTrue(isUnknownJavaScriptObject(x));
Expect.equals(JSObject, x.runtimeType);
x = makeQ();
Expect.isFalse(isJSObject(x));
Expect.isFalse(isLegacyJavaScriptObject(x));
Expect.isFalse(isPlainJavaScriptObject(x));
Expect.isFalse(isUnknownJavaScriptObject(x));
Expect.isTrue(isQ(x));
Expect.isFalse(x.runtimeType == JSObject);
}
main() {
nativeTesting();
setup();
dynamic_test();
static_test();
}