|
| 1 | +// Copyright (c) 2022, 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 | +// Make sure `Object` methods work as expected with `dart:html` and interop |
| 6 | +// types. The expectations here aren't guarantees that they should work a |
| 7 | +// particular way, but rather a way to monitor regressions/changes. |
| 8 | + |
| 9 | +@JS() |
| 10 | +library object_members_test; |
| 11 | + |
| 12 | +import 'package:js/js.dart'; |
| 13 | +import 'package:expect/minitest.dart'; |
| 14 | + |
| 15 | +import 'dart:html'; |
| 16 | +import 'dart:_interceptors' show JSObject; |
| 17 | + |
| 18 | +@JS() |
| 19 | +external void eval(String code); |
| 20 | + |
| 21 | +@JS() |
| 22 | +class JSClass { |
| 23 | + external JSClass(); |
| 24 | +} |
| 25 | + |
| 26 | +void main() { |
| 27 | + eval(r''' |
| 28 | + function JSClass() {} |
| 29 | + '''); |
| 30 | + |
| 31 | + // `dart:html` type. |
| 32 | + var div = document.createElement('div'); |
| 33 | + expect(div == div, true); |
| 34 | + expect(div == DomPointReadOnly(), false); |
| 35 | + // Ensure that we get a random hash for each new instance. It should be |
| 36 | + // improbable for this to fail across many runs if the hash is |
| 37 | + // non-deterministic. |
| 38 | + var hashCode = div.hashCode; |
| 39 | + var attempts = 0; |
| 40 | + var maxAttempts = 1000; |
| 41 | + while (div.hashCode == hashCode && attempts < maxAttempts) { |
| 42 | + div = document.createElement('div'); |
| 43 | + attempts++; |
| 44 | + } |
| 45 | + expect(attempts > 0 && attempts != maxAttempts, isTrue); |
| 46 | + expect(div.toString, isNotNull); |
| 47 | + expect(div.toString(), 'div'); |
| 48 | + expect(div.noSuchMethod, isNotNull); |
| 49 | + var noSuchMethodErrorThrown = true; |
| 50 | + try { |
| 51 | + (div as dynamic).triggerNoSuchMethod(); |
| 52 | + noSuchMethodErrorThrown = false; |
| 53 | + } catch (_) {} |
| 54 | + expect(noSuchMethodErrorThrown, isTrue); |
| 55 | + expect(div.runtimeType, DivElement); |
| 56 | + |
| 57 | + // `toString` for `dart:html` types that do not have an overridden `toString` |
| 58 | + // should look up the type through the proto. |
| 59 | + expect(window.navigator.toString(), "Instance of 'Navigator'"); |
| 60 | + |
| 61 | + // Interop type. |
| 62 | + var js = JSClass(); |
| 63 | + expect(js == js, true); |
| 64 | + expect(js == JSClass(), false); |
| 65 | + // TODO(srujzs): Modify this once interop has random hash codes. |
| 66 | + hashCode = js.hashCode; |
| 67 | + expect(hashCode, 0); |
| 68 | + expect(hashCode, js.hashCode); |
| 69 | + expect(js.toString, isNotNull); |
| 70 | + // Should forward to underlying `toString` call. |
| 71 | + expect(js.toString(), '[object Object]'); |
| 72 | + expect(js.noSuchMethod, isNotNull); |
| 73 | + noSuchMethodErrorThrown = true; |
| 74 | + try { |
| 75 | + (js as dynamic).triggerNoSuchMethod(); |
| 76 | + noSuchMethodErrorThrown = false; |
| 77 | + } catch (_) {} |
| 78 | + expect(noSuchMethodErrorThrown, isTrue); |
| 79 | + expect(js.runtimeType, JSObject); |
| 80 | +} |
0 commit comments