[dart2js] Adding dump info library tests

Change-Id: I1d9a30838dc08612032051f60344588f84b1d436
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/242163
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Mark Zhou <markzipan@google.com>
diff --git a/pkg/compiler/test/dump_info/data/deferred/lib.dart b/pkg/compiler/test/dump_info/data/deferred/lib.dart
new file mode 100644
index 0000000..6339eb2
--- /dev/null
+++ b/pkg/compiler/test/dump_info/data/deferred/lib.dart
@@ -0,0 +1,73 @@
+// Copyright (c) 2017, 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.
+
+/*library: library=[{
+  "id": "library/memory:sdk/tests/web/native/lib.dart::",
+  "kind": "library",
+  "name": "<unnamed>",
+  "size": 289,
+  "children": [
+    "function/memory:sdk/tests/web/native/lib.dart::defaultArg",
+    "function/memory:sdk/tests/web/native/lib.dart::funky"
+  ],
+  "canonicalUri": "memory:sdk/tests/web/native/lib.dart"
+}]*/
+
+// @dart = 2.7
+
+/*member: defaultArg:function=[{
+  "id": "function/memory:sdk/tests/web/native/lib.dart::defaultArg",
+  "kind": "function",
+  "name": "defaultArg",
+  "size": 95,
+  "outputUnit": "outputUnit/1",
+  "parent": "library/memory:sdk/tests/web/native/lib.dart::",
+  "children": [],
+  "modifiers": {
+    "static": false,
+    "const": false,
+    "factory": false,
+    "external": false
+  },
+  "returnType": "dynamic",
+  "inferredReturnType": "Value([exact=JSString], value: \"\")",
+  "parameters": [],
+  "sideEffects": "SideEffects(reads nothing; writes nothing)",
+  "inlinedCount": 0,
+  "code": "_static_0(A, \"lib__defaultArg$closure\", \"defaultArg\", 0);\n\ndefaultArg() {\n      return \"\";\n    }",
+  "type": "dynamic Function()"
+}]*/
+defaultArg() => "";
+
+/*member: funky:function=[{
+  "id": "function/memory:sdk/tests/web/native/lib.dart::funky",
+  "kind": "function",
+  "name": "funky",
+  "size": 194,
+  "outputUnit": "outputUnit/1",
+  "parent": "library/memory:sdk/tests/web/native/lib.dart::",
+  "children": [],
+  "modifiers": {
+    "static": false,
+    "const": false,
+    "factory": false,
+    "external": false
+  },
+  "returnType": "dynamic",
+  "inferredReturnType": "[null|subclass=Object]",
+  "parameters": [
+    {
+      "name": "x",
+      "type": "[subclass=Closure]",
+      "declaredType": "dynamic"
+    }
+  ],
+  "sideEffects": "SideEffects(reads anything; writes anything)",
+  "inlinedCount": 0,
+  "code": "_static(A, \"lib__funky$closure\", 0, null, [\"call$1\", \"call$0\"], [\"funky\", function() {\n      return A.funky(A.lib__defaultArg$closure());\n    }], 1, 0);\n\nfunky(x) {\n      return x.call$0();\n    }",
+  "type": "dynamic Function([dynamic])"
+}]*/
+funky([x = defaultArg]) => x();
+
+final int notUsed = 3;
diff --git a/pkg/compiler/test/dump_info/data/deferred/main.dart b/pkg/compiler/test/dump_info/data/deferred/main.dart
new file mode 100644
index 0000000..7e1cee2
--- /dev/null
+++ b/pkg/compiler/test/dump_info/data/deferred/main.dart
@@ -0,0 +1,55 @@
+// Copyright (c) 2017, 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.
+
+// @dart = 2.7
+
+/*library: library=[{
+  "id": "library/memory:sdk/tests/web/native/main.dart::",
+  "kind": "library",
+  "name": "<unnamed>",
+  "size": 301,
+  "children": [
+    "function/memory:sdk/tests/web/native/main.dart::main"
+  ],
+  "canonicalUri": "memory:sdk/tests/web/native/main.dart"
+}]*/
+
+import 'lib.dart' deferred as lib;
+
+/*member: main:
+ function=[{
+  "id": "function/memory:sdk/tests/web/native/main.dart::main",
+  "kind": "function",
+  "name": "main",
+  "size": 301,
+  "outputUnit": "outputUnit/main",
+  "parent": "library/memory:sdk/tests/web/native/main.dart::",
+  "children": [
+    "closure/memory:sdk/tests/web/native/main.dart::main.main_closure"
+  ],
+  "modifiers": {
+    "static": false,
+    "const": false,
+    "factory": false,
+    "external": false
+  },
+  "returnType": "dynamic",
+  "inferredReturnType": "[exact=_Future]",
+  "parameters": [],
+  "sideEffects": "SideEffects(reads anything; writes anything)",
+  "inlinedCount": 0,
+  "code": "main() {\n      return A.loadDeferredLibrary(\"lib\").then$1$1(new A.main_closure(), type$.Null);\n    }",
+  "type": "dynamic Function()"
+}],
+ holding=[
+  {"id":"function/dart:_js_helper::loadDeferredLibrary","mask":null},
+  {"id":"function/dart:_rti::_setArrayType","mask":null},
+  {"id":"function/dart:_rti::findType","mask":null},
+  {"id":"function/dart:async::_Future.then","mask":"[exact=_Future]"},
+  {"id":"function/memory:sdk/tests/web/native/main.dart::main.main_closure.call","mask":null},
+  {"id":"function/memory:sdk/tests/web/native/main.dart::main.main_closure.call","mask":null}]
+*/
+main() => lib.loadLibrary().then((_) {
+      (lib.funky)();
+    });
diff --git a/pkg/compiler/test/dump_info/data/deferred_future/lib1.dart b/pkg/compiler/test/dump_info/data/deferred_future/lib1.dart
new file mode 100644
index 0000000..4c3cfcd
--- /dev/null
+++ b/pkg/compiler/test/dump_info/data/deferred_future/lib1.dart
@@ -0,0 +1,18 @@
+// Copyright (c) 2019, 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.
+
+/*library: library=[{
+  "id": "library/memory:sdk/tests/web/native/lib1.dart::",
+  "kind": "library",
+  "name": "<unnamed>",
+  "size": 0,
+  "children": [],
+  "canonicalUri": "memory:sdk/tests/web/native/lib1.dart"
+}]*/
+
+// @dart = 2.7
+
+import 'lib2.dart';
+
+const dynamic field = const A();
diff --git a/pkg/compiler/test/dump_info/data/deferred_future/lib2.dart b/pkg/compiler/test/dump_info/data/deferred_future/lib2.dart
new file mode 100644
index 0000000..81cc3a1
--- /dev/null
+++ b/pkg/compiler/test/dump_info/data/deferred_future/lib2.dart
@@ -0,0 +1,44 @@
+// Copyright (c) 2019, 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.
+
+/*library: library=[{
+  "id": "library/memory:sdk/tests/web/native/lib2.dart::",
+  "kind": "library",
+  "name": "<unnamed>",
+  "size": 68,
+  "children": [
+    "class/memory:sdk/tests/web/native/lib2.dart::A"
+  ],
+  "canonicalUri": "memory:sdk/tests/web/native/lib2.dart"
+}]*/
+
+// @dart = 2.7
+
+class A {
+  const A();
+
+  /*member: A.method:function=[{
+  "id": "function/memory:sdk/tests/web/native/lib2.dart::A.method",
+  "kind": "function",
+  "name": "method",
+  "size": 0,
+  "outputUnit": "outputUnit/1",
+  "parent": "class/memory:sdk/tests/web/native/lib2.dart::A",
+  "children": [],
+  "modifiers": {
+    "static": false,
+    "const": false,
+    "factory": false,
+    "external": false
+  },
+  "returnType": "dynamic",
+  "inferredReturnType": "[null]",
+  "parameters": [],
+  "sideEffects": "SideEffects(reads nothing; writes nothing)",
+  "inlinedCount": 1,
+  "code": "",
+  "type": "dynamic Function()"
+}]*/
+  method() {}
+}
diff --git a/pkg/compiler/test/dump_info/data/deferred_future/main.dart b/pkg/compiler/test/dump_info/data/deferred_future/main.dart
new file mode 100644
index 0000000..10e2e59
--- /dev/null
+++ b/pkg/compiler/test/dump_info/data/deferred_future/main.dart
@@ -0,0 +1,63 @@
+// Copyright (c) 2019, 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.
+
+/*library: library=[{
+  "id": "library/memory:sdk/tests/web/native/main.dart::",
+  "kind": "library",
+  "name": "<unnamed>",
+  "size": 857,
+  "children": [
+    "function/memory:sdk/tests/web/native/main.dart::main"
+  ],
+  "canonicalUri": "memory:sdk/tests/web/native/main.dart"
+}]*/
+
+// @dart = 2.7
+
+import 'dart:async';
+import 'lib1.dart' deferred as lib1;
+import 'lib2.dart' as lib2;
+
+/*member: main:
+ function=[{
+  "id": "function/memory:sdk/tests/web/native/main.dart::main",
+  "kind": "function",
+  "name": "main",
+  "size": 857,
+  "outputUnit": "outputUnit/main",
+  "parent": "library/memory:sdk/tests/web/native/main.dart::",
+  "children": [],
+  "modifiers": {
+    "static": false,
+    "const": false,
+    "factory": false,
+    "external": false
+  },
+  "returnType": "dynamic",
+  "inferredReturnType": "[exact=_Future]",
+  "parameters": [],
+  "sideEffects": "SideEffects(reads nothing; writes nothing)",
+  "inlinedCount": 0,
+  "code": "main() {\n      var $async$goto = 0,\n        $async$completer = A._makeAsyncAwaitCompleter(type$.dynamic);\n      var $async$main = A._wrapJsFunctionForAsync(function($async$errorCode, $async$result) {\n        if ($async$errorCode === 1)\n          return A._asyncRethrow($async$result, $async$completer);\n        while (true)\n          switch ($async$goto) {\n            case 0:\n              // Function start\n              $async$goto = 2;\n              return A._asyncAwait(A.loadDeferredLibrary(\"lib1\"), $async$main);\n            case 2:\n              // returning from await.\n              A.checkDeferredIsLoaded(\"lib1\");\n              A.checkDeferredIsLoaded(\"lib1\");\n              // implicit return\n              return A._asyncReturn(null, $async$completer);\n          }\n      });\n      return A._asyncStartSync($async$main, $async$completer);\n    }",
+  "type": "dynamic Function()"
+}],
+ holding=[
+  {"id":"function/dart:_js_helper::checkDeferredIsLoaded","mask":null},
+  {"id":"function/dart:_js_helper::loadDeferredLibrary","mask":null},
+  {"id":"function/dart:_rti::findType","mask":null},
+  {"id":"function/dart:async::StreamIterator.StreamIterator","mask":null},
+  {"id":"function/dart:async::_asyncAwait","mask":null},
+  {"id":"function/dart:async::_asyncRethrow","mask":null},
+  {"id":"function/dart:async::_asyncReturn","mask":null},
+  {"id":"function/dart:async::_asyncStartSync","mask":null},
+  {"id":"function/dart:async::_makeAsyncAwaitCompleter","mask":null},
+  {"id":"function/dart:async::_wrapJsFunctionForAsync","mask":null},
+  {"id":"function/memory:sdk/tests/web/native/lib2.dart::A.method","mask":"inlined"},
+  {"id":"function/memory:sdk/tests/web/native/lib2.dart::A.method","mask":null}]
+*/
+main() async {
+  await lib1.loadLibrary();
+  lib1.field is FutureOr<lib2.A>;
+  lib1.field.method();
+}
diff --git a/pkg/compiler/test/dump_info/data/js_members.dart b/pkg/compiler/test/dump_info/data/js_members.dart
index 0d2e390..db9ca6b 100644
--- a/pkg/compiler/test/dump_info/data/js_members.dart
+++ b/pkg/compiler/test/dump_info/data/js_members.dart
@@ -1,3 +1,15 @@
+/*library: library=[{
+  "id": "library/memory:sdk/tests/web/native/main.dart::",
+  "kind": "library",
+  "name": "js_parameters_test",
+  "size": 1891,
+  "children": [
+    "class/memory:sdk/tests/web/native/main.dart::Bar",
+    "class/memory:sdk/tests/web/native/main.dart::Foo",
+    "function/memory:sdk/tests/web/native/main.dart::main"
+  ],
+  "canonicalUri": "memory:sdk/tests/web/native/main.dart"
+}]*/
 @JS()
 library js_parameters_test;
 
diff --git a/pkg/compiler/test/dump_info/data/members.dart b/pkg/compiler/test/dump_info/data/members.dart
index 326e97c..8538881 100644
--- a/pkg/compiler/test/dump_info/data/members.dart
+++ b/pkg/compiler/test/dump_info/data/members.dart
@@ -1,3 +1,18 @@
+/*library: library=[{
+  "id": "library/memory:sdk/tests/web/native/main.dart::",
+  "kind": "library",
+  "name": "<unnamed>",
+  "size": 475,
+  "children": [
+    "class/memory:sdk/tests/web/native/main.dart::A",
+    "class/memory:sdk/tests/web/native/main.dart::C",
+    "classType/memory:sdk/tests/web/native/main.dart::A",
+    "field/memory:sdk/tests/web/native/main.dart::constList",
+    "function/memory:sdk/tests/web/native/main.dart::F",
+    "function/memory:sdk/tests/web/native/main.dart::main"
+  ],
+  "canonicalUri": "memory:sdk/tests/web/native/main.dart"
+}]*/
 class C {
   /*member: C.value:
    function=[{
diff --git a/pkg/compiler/test/dump_info/dump_info_test.dart b/pkg/compiler/test/dump_info/dump_info_test.dart
index 9a36426..b27abdf 100644
--- a/pkg/compiler/test/dump_info/dump_info_test.dart
+++ b/pkg/compiler/test/dump_info/dump_info_test.dart
@@ -52,21 +52,41 @@
   static const String wildcard = '%';
 
   @override
-  void computeMemberData(Compiler compiler, MemberEntity member,
+  void computeLibraryData(Compiler compiler, LibraryEntity library,
       Map<Id, ActualData<Features>> actualMap,
-      {bool verbose: false}) {
-    var converter = info.AllInfoToJsonConverter(
+      {bool verbose}) {
+    final converter = info.AllInfoToJsonConverter(
         isBackwardCompatible: true, filterTreeshaken: false);
     DumpInfoStateData dumpInfoState = compiler.dumpInfoStateForTesting;
 
     Features features = new Features();
-    var functionInfo = dumpInfoState.entityToInfo[member];
+    final libraryInfo = dumpInfoState.entityToInfo[library];
+    if (libraryInfo == null) return;
+
+    features.addElement(
+        Tags.library, indentedEncoder.convert(libraryInfo.accept(converter)));
+
+    final id = LibraryId(library.canonicalUri);
+    actualMap[id] = new ActualData<Features>(
+        id, features, library.canonicalUri, -1, library);
+  }
+
+  @override
+  void computeMemberData(Compiler compiler, MemberEntity member,
+      Map<Id, ActualData<Features>> actualMap,
+      {bool verbose: false}) {
+    final converter = info.AllInfoToJsonConverter(
+        isBackwardCompatible: true, filterTreeshaken: false);
+    DumpInfoStateData dumpInfoState = compiler.dumpInfoStateForTesting;
+
+    Features features = new Features();
+    final functionInfo = dumpInfoState.entityToInfo[member];
     if (functionInfo == null) return;
 
     if (functionInfo is info.FunctionInfo) {
       features.addElement(Tags.function,
           indentedEncoder.convert(functionInfo.accept(converter)));
-      for (var use in functionInfo.uses) {
+      for (final use in functionInfo.uses) {
         features.addElement(
             Tags.holding, encoder.convert(converter.visitDependencyInfo(use)));
       }
@@ -75,7 +95,7 @@
     if (functionInfo is info.FieldInfo) {
       features.addElement(Tags.function,
           indentedEncoder.convert(functionInfo.accept(converter)));
-      for (var use in functionInfo.uses) {
+      for (final use in functionInfo.uses) {
         features.addElement(
             Tags.holding, encoder.convert(converter.visitDependencyInfo(use)));
       }
@@ -138,7 +158,7 @@
                     expectedText.substring(0, expectedText.indexOf(wildcard));
                 List matches = [];
                 for (Object actualObject in actualList) {
-                  var formattedActualObject =
+                  final formattedActualObject =
                       encoder.convert(jsonDecode(actualObject));
                   if (formattedActualObject.startsWith(prefix)) {
                     matches.add(actualObject);
@@ -150,7 +170,7 @@
                 }
               } else {
                 for (Object actualObject in actualList) {
-                  var formattedActualObject =
+                  final formattedActualObject =
                       encoder.convert(jsonDecode(actualObject));
                   if (expectedText == formattedActualObject) {
                     actualList.remove(actualObject);