| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Test of the graph segmentation algorithm used by deferred loading | 5 // Test of the graph segmentation algorithm used by deferred loading |
| 6 // to determine which elements can be deferred and which libraries | 6 // to determine which elements can be deferred and which libraries |
| 7 // much be included in the initial download (loaded eagerly). | 7 // much be included in the initial download (loaded eagerly). |
| 8 | 8 |
| 9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
| 10 import 'package:compiler/src/compiler.dart'; | 10 import 'package:compiler/src/compiler.dart'; |
| 11 import 'package:expect/expect.dart'; | 11 import 'package:expect/expect.dart'; |
| 12 import 'memory_compiler.dart'; | 12 import 'memory_compiler.dart'; |
| 13 | 13 |
| 14 void main() { | 14 void main() { |
| 15 deferredTest1(); |
| 16 deferredTest2(); |
| 17 } |
| 18 |
| 19 void deferredTest1() { |
| 15 asyncTest(() async { | 20 asyncTest(() async { |
| 16 CompilationResult result = | 21 CompilationResult result = |
| 17 await runCompiler(memorySourceFiles: MEMORY_SOURCE_FILES); | 22 await runCompiler(memorySourceFiles: TEST1); |
| 18 Compiler compiler = result.compiler; | 23 Compiler compiler = result.compiler; |
| 19 | 24 |
| 20 lookupLibrary(name) { | 25 lookupLibrary(name) { |
| 21 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); | 26 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); |
| 22 } | 27 } |
| 23 | 28 |
| 24 var main = compiler.mainFunction; | 29 var main = compiler.mainFunction; |
| 25 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; | 30 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; |
| 26 | 31 |
| 27 var mainOutputUnit = compiler.deferredLoadTask.mainOutputUnit; | 32 var mainOutputUnit = compiler.deferredLoadTask.mainOutputUnit; |
| 28 var backend = compiler.backend; | 33 var backend = compiler.backend; |
| 29 var classes = backend.emitter.neededClasses; | |
| 30 var lib1 = lookupLibrary("memory:lib1.dart"); | 34 var lib1 = lookupLibrary("memory:lib1.dart"); |
| 31 var lib2 = lookupLibrary("memory:lib2.dart"); | 35 var lib2 = lookupLibrary("memory:lib2.dart"); |
| 32 var foo1 = lib1.find("foo1"); | 36 var foo1 = lib1.find("foo1"); |
| 33 var foo2 = lib2.find("foo2"); | 37 var foo2 = lib2.find("foo2"); |
| 34 | 38 |
| 35 Expect.notEquals(mainOutputUnit, outputUnitForElement(foo2)); | 39 Expect.notEquals(mainOutputUnit, outputUnitForElement(foo2)); |
| 36 }); | 40 }); |
| 37 } | 41 } |
| 38 | 42 |
| 43 void deferredTest2() { |
| 44 asyncTest(() async { |
| 45 CompilationResult result = await runCompiler(memorySourceFiles: TEST2); |
| 46 Compiler compiler = result.compiler; |
| 47 |
| 48 lookupLibrary(name) { |
| 49 return compiler.libraryLoader.lookupLibrary(Uri.parse(name)); |
| 50 } |
| 51 |
| 52 var main = compiler.mainFunction; |
| 53 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; |
| 54 |
| 55 var mainOutputUnit = compiler.deferredLoadTask.mainOutputUnit; |
| 56 var shared = lookupLibrary("memory:shared.dart"); |
| 57 var a = shared.find("A"); |
| 58 |
| 59 Expect.equals(mainOutputUnit, outputUnitForElement(a)); |
| 60 }); |
| 61 } |
| 62 |
| 39 // lib1 imports lib2 deferred. But mainlib never uses DeferredLibrary. | 63 // lib1 imports lib2 deferred. But mainlib never uses DeferredLibrary. |
| 40 // Test that this case works. | 64 // Test that this case works. |
| 41 const Map MEMORY_SOURCE_FILES = const { | 65 const Map TEST1 = const { |
| 42 "main.dart":""" | 66 "main.dart":""" |
| 43 library mainlib; | 67 library mainlib; |
| 44 | 68 |
| 45 import 'lib1.dart' as lib1; | 69 import 'lib1.dart' as lib1; |
| 46 | 70 |
| 47 void main() { | 71 void main() { |
| 48 lib1.foo1(); | 72 lib1.foo1(); |
| 49 } | 73 } |
| 50 """, | 74 """, |
| 51 "lib1.dart":""" | 75 "lib1.dart":""" |
| 52 library lib1; | 76 library lib1; |
| 53 | 77 |
| 54 import 'lib2.dart' deferred as lib2; | 78 import 'lib2.dart' deferred as lib2; |
| 55 | 79 |
| 56 const def = const DeferredLibrary('lib2'); | 80 const def = const DeferredLibrary('lib2'); |
| 57 | 81 |
| 58 void foo1() { | 82 void foo1() { |
| 59 lib1.loadLibrary().then((_) => lib2.foo2()); | 83 lib1.loadLibrary().then((_) => lib2.foo2()); |
| 60 } | 84 } |
| 61 """, | 85 """, |
| 62 "lib2.dart":""" | 86 "lib2.dart":""" |
| 63 library lib2; | 87 library lib2; |
| 64 | 88 |
| 65 void foo2() {} | 89 void foo2() {} |
| 66 """, | 90 """, |
| 67 }; | 91 }; |
| 92 |
| 93 // main indirectly uses class A from shared. A should still be included in the |
| 94 // main fragment. |
| 95 const Map TEST2 = const { |
| 96 "main.dart":""" |
| 97 import 'def.dart' deferred as def; |
| 98 import 'shared.dart'; |
| 99 |
| 100 typedef void F(x); |
| 101 |
| 102 main() { |
| 103 print(foo is F); |
| 104 def.loadLibrary().then((_) { |
| 105 def.toto(); |
| 106 }); |
| 107 } |
| 108 """, |
| 109 "def.dart":""" |
| 110 import 'shared.dart'; |
| 111 |
| 112 toto() { print(new A()); } |
| 113 """, |
| 114 "shared.dart":""" |
| 115 class A {} |
| 116 class B extends A {} |
| 117 foo(B b) => null; |
| 118 """, |
| 119 }; |
| OLD | NEW |