OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
11 import 'memory_source_file_helper.dart'; | 11 import 'memory_source_file_helper.dart'; |
12 import "dart:async"; | 12 import "dart:async"; |
13 | 13 |
14 import 'package:compiler/implementation/dart2jslib.dart' | |
15 as dart2js; | |
16 | |
17 class FakeOutputStream<T> extends EventSink<T> { | 14 class FakeOutputStream<T> extends EventSink<T> { |
18 void add(T event) {} | 15 void add(T event) {} |
19 void addError(T event, [StackTrace stackTrace]) {} | 16 void addError(T event, [StackTrace stackTrace]) {} |
20 void close() {} | 17 void close() {} |
21 } | 18 } |
22 | 19 |
23 void main() { | 20 void main() { |
24 Uri script = currentDirectory.resolveUri(Platform.script); | 21 Uri script = currentDirectory.resolveUri(Platform.script); |
25 Uri libraryRoot = script.resolve('../../../sdk/'); | 22 Uri libraryRoot = script.resolve('../../../sdk/'); |
26 Uri packageRoot = script.resolve('./packages/'); | 23 Uri packageRoot = script.resolve('./packages/'); |
27 | 24 |
28 var provider = new MemorySourceFileProvider(MEMORY_SOURCE_FILES); | 25 var provider = new MemorySourceFileProvider(MEMORY_SOURCE_FILES); |
29 var handler = new FormattingDiagnosticHandler(provider); | 26 var handler = new FormattingDiagnosticHandler(provider); |
30 | 27 |
31 Compiler compiler = new Compiler(provider.readStringFromUri, | 28 Compiler compiler = new Compiler(provider.readStringFromUri, |
32 (name, extension) => new FakeOutputStream(), | 29 (name, extension) => new FakeOutputStream(), |
33 handler.diagnosticHandler, | 30 handler.diagnosticHandler, |
34 libraryRoot, | 31 libraryRoot, |
35 packageRoot, | 32 packageRoot, |
36 [], | 33 [], |
37 {}); | 34 {}); |
38 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) { | 35 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) { |
39 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; | 36 var outputUnitForElement = compiler.deferredLoadTask.outputUnitForElement; |
40 var mainOutputUnit = compiler.deferredLoadTask.mainOutputUnit; | 37 var mainOutputUnit = compiler.deferredLoadTask.mainOutputUnit; |
41 var lib = | 38 var lib = |
42 compiler.libraryLoader.lookupLibrary(Uri.parse("memory:lib.dart")); | 39 compiler.libraryLoader.lookupLibrary(Uri.parse("memory:lib.dart")); |
43 var f1 = lib.find("f1"); | 40 var customType = lib.find("CustomType"); |
44 var f2 = lib.find("f2"); | 41 var foo = lib.find("foo"); |
45 Expect.notEquals(mainOutputUnit, outputUnitForElement(f1)); | 42 Expect.notEquals(mainOutputUnit, outputUnitForElement(foo)); |
46 Expect.equals(mainOutputUnit, outputUnitForElement(f2)); | 43 // Native elements are not deferred |
| 44 Expect.equals(mainOutputUnit, outputUnitForElement(customType)); |
47 })); | 45 })); |
48 } | 46 } |
49 | 47 |
50 // The main library imports lib1 and lib2 deferred and use lib1.foo1 and | 48 // The main library imports a file defining a custom element. |
51 // lib2.foo2. This should trigger seperate outputunits for main, lib1 and lib2. | 49 // Registering this class implicitly causes the constructors to be |
52 // | 50 // live. Check that this is handled. |
53 // Both lib1 and lib2 import lib3 directly and | |
54 // both use lib3.foo3. Therefore a shared output unit for lib1 and lib2 should | |
55 // be created. | |
56 // | |
57 // lib1 and lib2 also import lib4 deferred, but lib1 uses lib4.bar1 and lib2 | |
58 // uses lib4.bar2. So two output units should be created for lib4, one for each | |
59 // import. | |
60 const Map MEMORY_SOURCE_FILES = const {"main.dart": """ | 51 const Map MEMORY_SOURCE_FILES = const {"main.dart": """ |
61 import "dart:async"; | 52 import "lib.dart" deferred as a; |
| 53 import 'dart:html'; |
62 | 54 |
63 @def import 'lib.dart' as lib show f1; | 55 main() { |
64 import 'lib.dart' show f2; | 56 document.registerElement("foo-tag", a.a); |
65 | 57 a.foo(); |
66 const def = const DeferredLibrary("deferred"); | |
67 | |
68 void main() { | |
69 print(f2()); | |
70 def.load().then((_) { | |
71 print(lib.f1()); | |
72 }); | |
73 } | 58 } |
74 """, "lib.dart": """ | 59 """, "lib.dart": """ |
75 int f1 () { | 60 import 'dart:html'; |
76 return 1; | 61 var a = CustomType; |
| 62 |
| 63 class CustomType extends HtmlElement { |
| 64 factory CustomType() => null; |
| 65 CustomType.created() : super.created() ; |
77 } | 66 } |
78 | 67 |
79 int f2 () { | 68 foo() {} |
80 return 2; | |
81 } | |
82 """,}; | 69 """,}; |
OLD | NEW |