Chromium Code Reviews| Index: LayoutTests/dart/inspector/utils.dart |
| diff --git a/LayoutTests/dart/inspector/utils.dart b/LayoutTests/dart/inspector/utils.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..706a496654fec99f29581fc57227edc40bc20458 |
| --- /dev/null |
| +++ b/LayoutTests/dart/inspector/utils.dart |
| @@ -0,0 +1,325 @@ |
| +// Copyright (c) 2014, 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. |
| + |
| +/** |
| + * Tests for utility class used to extract debugger data defined in |
| + * native_DOMImplementation.dart |
| + */ |
| +library utilsTest; |
| + |
| +import 'package:expect/expect.dart'; |
| +import 'package:unittest/html_config.dart'; |
| +import 'package:unittest/unittest.dart'; |
| +import 'dart:html'; |
| + |
| +import 'sample_library.dart'; |
| + |
| +import 'dart:mirrors'; |
| + |
| +/** |
| + * Wrapper around the [dart:html] [_Utils] class that lets us invoke its |
| + * methods from a different library. |
|
vsm
2014/04/25 00:01:14
Is this just for testing purposes?
|
| + */ |
| +class UtilsWrapper { |
| + ClassMirror _receiver; |
| + |
| + UtilsWrapper() { |
| + var html = currentMirrorSystem().libraries[Uri.parse('dart:html')]; |
| + _receiver = html.declarations[MirrorSystem.getSymbol('_Utils', html)]; |
| + } |
| + |
| + noSuchMethod(Invocation msg) => |
| + _receiver.invoke(msg.memberName, |
| + msg.positionalArguments, |
| + msg.namedArguments).reflectee; |
| +} |
| + |
| +/** |
| + * Helper class to convert the packed property structure used by Dartium back |
| + * to a more human readable form. |
| + */ |
| +class _Property { |
| + _Property(List input, int offset) : |
| + name = input[offset], |
| + setter = input[offset + 1], |
| + getter = input[offset + 2], |
| + value = input[offset + 3], |
| + hasValue = input[offset + 4], |
| + writable = input[offset + 5], |
| + isMethod = input[offset + 6], |
| + isOwn = input[offset + 7], |
| + wasThrown = input[offset + 8]; |
| + |
| + final String name; |
| + final Function setter; |
| + final Function getter; |
| + final value; |
| + final bool hasValue; |
| + final bool writable; |
| + final bool isMethod; |
| + final bool isOwn; |
| + final bool wasThrown; |
| +} |
| + |
| +var _Utils = new UtilsWrapper(); |
| + |
| +_Property findObjectClassProperty(o, String name) => |
| + findProperty(_Utils.getObjectClassProperties(o, true, false), name); |
| + |
| +_Property findProperty(List properties, String name) { |
| + for (int i = 0; i < properties.length; i += 9) { |
| + if (properties[i] == name) return new _Property(properties, i); |
| + } |
| + return null; |
| +} |
| + |
| +main() { |
| + useHtmlConfiguration(true); |
| + |
| + var sampleLibraryUrl = reflectClass(Foo).owner.uri.toString(); |
| + |
| + group('completions', () { |
| + |
| + test('object', () { |
| + expect(_Utils.getObjectCompletions(new Foo(), sampleLibraryUrl), |
| + orderedEquals(['_fooPrivate1', '_fooPrivate2', 'field1', 'field2', |
| + 'hashCode', 'member1', 'member2', 'noSuchMethod', 'prop1', |
| + 'prop2', 'runtimeType', 'toString'])); |
| + |
| + // Should not display any privates as a different library is specified. |
| + expect(_Utils.getObjectCompletions(new Foo(), 'dart:html'), |
| + orderedEquals(['field1', 'field2', 'hashCode', 'member1', 'member2', |
| + 'noSuchMethod', 'prop1', 'prop2', 'runtimeType', 'toString'])); |
| + |
| + expect(_Utils.getObjectCompletions(new Object(), sampleLibraryUrl), |
| + orderedEquals( |
| + ['hashCode', 'noSuchMethod', 'runtimeType', 'toString'])); |
| + |
| + // We don't want to make the test depend on exactly what private members |
| + // Object has but it is safe to assume that Object will have some private |
| + // members. |
| + expect(_Utils.getObjectCompletions(new Object(), 'dart:core').length, |
| + greaterThan(4)); |
| + }); |
| + |
| + test('class', () { |
| + expect(_Utils.getObjectCompletions(Foo, sampleLibraryUrl), |
|
vsm
2014/04/25 00:01:14
Test privacy here as well?
|
| + orderedEquals(['staticField1', 'staticField2', 'staticMember1'])); |
| + expect(_Utils.getObjectCompletions(Bar, sampleLibraryUrl), |
| + orderedEquals(['barConst1', 'barStaticMethod', 'barStaticProp'])); |
| + }); |
| + |
| + test('library', () { |
| + expect(_Utils.getLibraryCompletions('dart:core'), |
| + contains('Object')); |
| + |
| + var completions = _Utils.getLibraryCompletions(sampleLibraryUrl); |
| + expect(completions, contains('Foo')); |
| + expect(completions, contains('exampleStaticMethod')); |
| + expect(completions, contains('_examplePrivateStaticMethod')); |
| + expect(completions, contains('_exampleProp2')); |
| + expect(completions, contains('exampleProp2')); |
| + // Contained only in the imported dart:html library. |
| + expect(completions, isNot(contains('Node'))); |
| + expect(completions, isNot(contains('js'))); |
| + |
| + completions = _Utils.getLibraryCompletionsIncludingImports(sampleLibraryUrl); |
|
vsm
2014/04/25 00:01:14
length
|
| + expect(completions, contains('Node')); |
| + expect(completions, contains('js')); |
| + }); |
| + }); |
| + |
| + group('getProperties', () { |
| + |
| + test('treat property as field', () { |
| + expect(_Utils.treatPropertyAsField( |
| + reflectClass(Foo).declarations[MirrorSystem.getSymbol('prop1')]), |
| + isFalse); |
| + |
| + // Insure that dart:html properties are treated as fields. |
| + expect(_Utils.treatPropertyAsField( |
| + reflectClass(Node).declarations[MirrorSystem.getSymbol('text')]), |
| + true); |
| + }); |
| + |
| + test('describe function', () { |
| + var b = new Bar(); |
| + expect(_Utils.describeFunction(b.barMember1), equals('barMember1() => 42;')); |
|
vsm
2014/04/25 00:01:14
length
|
| + // Invocation trampoline for function barMember1. |
| + var barMember1 = findObjectClassProperty(b, 'barMember1').value; |
| + expect(_Utils.describeFunction(barMember1), equals('barMember1() => 42;')); |
|
vsm
2014/04/25 00:01:14
length
|
| + |
| + var details = _Utils.getInvocationTrampolineDetails(barMember1); |
| + expect(details[0], equals(57)); // Line number. |
| + expect(details[1], equals(3)); // Column number. |
| + expect(details[2], endsWith('sample_library.dart')); // File name. |
| + expect(details[3], equals('barMember1')); // Method name. |
| + }); |
| + |
| + test('object properties', () { |
| + var o = new Bar(); |
| + var property; |
| + var properties = _Utils.getObjectProperties(o, true, false); |
| + |
| + property = findProperty(properties, 'barField1'); |
| + expect(property.value, equals(9)); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isTrue); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + property = findProperty(properties, 'barFinalField'); |
| + expect(property.value, equals(12)); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + // verify that properties don't show up. |
| + expect(findProperty(properties, "barProp1"), isNull); |
| + |
| + properties = _Utils.getObjectClassProperties(o, true, true); |
| + property = findProperty(properties, 'barProp1'); |
| + expect(property.value, isNull); |
| + expect(property.setter, isNull); |
| + expect(property.getter(), equals('bar1')); |
| + expect(property.hasValue, isFalse); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + property = findProperty(properties, 'barProp2'); |
| + expect(property.value, isNull); |
| + expect(property.writable, isTrue); |
| + expect(property.setter is Function, isTrue); |
| + expect(property.getter(), equals(11)); |
| + property.setter(55); |
| + expect(property.getter(), equals(55)); |
| + expect(property.hasValue, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + }); |
| + |
| + test('class properties', () { |
| + var properties = _Utils.getClassProperties(Bar, true, false); |
| + var property = findProperty(properties, 'barStaticMethod'); |
| + expect(property.value(10), equals(30)); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isTrue); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + property = findProperty(properties, 'barConst1'); |
| + expect(property.value, equals(9)); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + properties = _Utils.getClassProperties(Bar, true, true); |
| + expect(findProperty(properties, 'barStaticMethod'), isNull); |
| + |
| + property = findProperty(properties, 'barStaticProp'); |
| + expect(property.value, isNull); |
| + expect(property.setter, isNull); |
| + expect(property.getter(), equals('staticProp')); |
| + expect(property.hasValue, isFalse); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + }); |
| + |
| + test('safe get property', () { |
| + expect(_Utils.getObjectPropertySafe(new Bar(), "_barProp2"), equals(11)); |
| + |
| + // Warning: this assumes that the VM continues to have a field named |
| + // _cid on Object. This case is good to test as it verifies we walk |
| + // up the class hierarchy attempting to guess the library of the |
| + // private field. |
|
vsm
2014/04/25 00:01:14
It might be better to just import a second sample
|
| + expect(_Utils.getObjectPropertySafe(new Bar(), "_cid") is int, isTrue); |
| + }); |
| + |
| + test('library properties', () { |
| + var properties = _Utils.getLibraryProperties(sampleLibraryUrl, true, false); |
|
vsm
2014/04/25 00:01:14
length
|
| + var property = findProperty(properties, 'exampleStaticMethod'); |
| + expect(property.value(10), equals(20)); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isTrue); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + property = findProperty(properties, 'exampleField'); |
| + expect(property.value, equals(8)); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isTrue); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + property = findProperty(properties, 'exampleFinalField'); |
| + expect(property.value, equals(16)); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + property = findProperty(properties, 'Foo'); |
| + expect(property.value.toString(), "Foo"); |
| + expect(property.value is Type, isTrue); |
| + expect(property.setter, isNull); |
| + expect(property.getter, isNull); |
| + expect(property.hasValue, isTrue); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + properties = _Utils.getLibraryProperties(sampleLibraryUrl, true, true); |
| + property = findProperty(properties, 'exampleProp1'); |
| + expect(property.value, isNull); |
| + expect(property.setter, isNull); |
| + expect(property.getter(), equals(30)); |
| + expect(property.hasValue, isFalse); |
| + expect(property.writable, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + |
| + property = findProperty(properties, 'exampleProp2'); |
| + expect(property.value, isNull); |
| + expect(property.writable, isTrue); |
| + expect(property.setter is Function, isTrue); |
| + expect(property.getter(), equals(10)); |
| + property.setter(20); |
| + expect(property.getter(), equals(20)); |
| + expect(property.hasValue, isFalse); |
| + expect(property.isMethod, isFalse); |
| + expect(property.isOwn, isTrue); |
| + expect(property.wasThrown, isFalse); |
| + }); |
| + }); |
| +} |