bootstrap_test.dart 5.85 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_tools/src/web/bootstrap.dart';
6
import 'package:package_config/package_config.dart';
7 8 9 10 11 12 13 14 15 16

import '../../src/common.dart';

void main() {
  test('generateBootstrapScript embeds urls correctly', () {
    final String result = generateBootstrapScript(
      requireUrl: 'require.js',
      mapperUrl: 'mapper.js',
    );
    // require js source is interpolated correctly.
17 18
    expect(result, contains('"requireJs": "require.js"'));
    expect(result, contains('requireEl.src = getTTScriptUrl("requireJs");'));
19
    // stack trace mapper source is interpolated correctly.
20 21
    expect(result, contains('"mapper": "mapper.js"'));
    expect(result, contains('mapperEl.src = getTTScriptUrl("mapper");'));
22
    // data-main is set to correct bootstrap module.
23
    expect(result, contains('requireEl.setAttribute("data-main", "main_module.bootstrap");'));
24 25
  });

26
  test('generateBootstrapScript includes loading indicator', () {
27 28 29 30 31
    final String result = generateBootstrapScript(
      requireUrl: 'require.js',
      mapperUrl: 'mapper.js',
    );
    expect(result, contains('"flutter-loader"'));
32
    expect(result, contains('"indeterminate"'));
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  });

  // https://github.com/flutter/flutter/issues/107742
  test('generateBootstrapScript loading indicator does not trigger scrollbars', () {
    final String result = generateBootstrapScript(
      requireUrl: 'require.js',
      mapperUrl: 'mapper.js',
    );

    // See: https://regexr.com/6q0ft
    final RegExp regex = RegExp(r'(?:\.flutter-loader\s*\{)[^}]+(?:overflow\:\s*hidden;)[^}]+}');

    expect(result, matches(regex), reason: '.flutter-loader must have overflow: hidden');
  });

  // https://github.com/flutter/flutter/issues/82524
  test('generateMainModule removes timeout from requireJS', () {
    final String result = generateMainModule(
      entrypoint: 'foo/bar/main.js',
52
      nullAssertions: false,
53 54 55 56 57 58 59 60 61 62
      nativeNullAssertions: false,
    );

    // See: https://regexr.com/6q0kp
    final RegExp regex = RegExp(
      r'(?:require\.config\(\{)(?:.|\s(?!\}\);))*'
        r'(?:waitSeconds\:\s*0[,]?)'
      r'(?:(?!\}\);).|\s)*\}\);');

    expect(result, matches(regex), reason: 'require.config must have a waitSeconds: 0 config entry');
63 64 65 66 67 68 69 70 71 72 73 74
  });

  test('generateMainModule hides requireJS injected by DDC', () {
    final String result = generateMainModule(
      entrypoint: 'foo/bar/main.js',
      nullAssertions: false,
      nativeNullAssertions: false,
    );
    expect(result, contains('''define._amd = define.amd;'''),
      reason: 'define.amd must be preserved as _amd so users may restore it if needed.');
    expect(result, contains('''delete define.amd;'''),
      reason: "define.amd must be removed so packages don't attempt to use Dart's instance.");
75 76
  });

77 78 79
  test('generateMainModule embeds urls correctly', () {
    final String result = generateMainModule(
      entrypoint: 'foo/bar/main.js',
80
      nullAssertions: false,
81
      nativeNullAssertions: false,
82 83
    );
    // bootstrap main module has correct defined module.
84
    expect(result, contains('define("main_module.bootstrap", ["foo/bar/main.js", "dart_sdk"], '
85 86
      'function(app, dart_sdk) {'));
  });
87

88 89 90
  test('generateMainModule can set bootstrap name', () {
    final String result = generateMainModule(
      entrypoint: 'foo/bar/main.js',
91
      nullAssertions: false,
92
      nativeNullAssertions: false,
93 94 95 96 97 98 99
      bootstrapModule: 'foo_module.bootstrap',
    );
    // bootstrap main module has correct defined module.
    expect(result, contains('define("foo_module.bootstrap", ["foo/bar/main.js", "dart_sdk"], '
      'function(app, dart_sdk) {'));
  });

100 101 102
  test('generateMainModule includes null safety switches', () {
    final String result = generateMainModule(
      entrypoint: 'foo/bar/main.js',
103
      nullAssertions: true,
104
      nativeNullAssertions: true,
105 106
    );

107
    expect(result, contains('''dart_sdk.dart.nonNullAsserts(true);'''));
108 109 110 111 112 113
    expect(result, contains('''dart_sdk.dart.nativeNonNullAsserts(true);'''));
  });

  test('generateMainModule can disable null safety switches', () {
    final String result = generateMainModule(
      entrypoint: 'foo/bar/main.js',
114
      nullAssertions: false,
115 116 117
      nativeNullAssertions: false,
    );

118
    expect(result, contains('''dart_sdk.dart.nonNullAsserts(false);'''));
119
    expect(result, contains('''dart_sdk.dart.nativeNonNullAsserts(false);'''));
120
  });
121 122 123 124 125 126

  test('generateTestBootstrapFileContents embeds urls correctly', () {
    final String result = generateTestBootstrapFileContents('foo.dart.js', 'require.js', 'mapper.js');

    expect(result, contains('el.setAttribute("data-main", \'foo.dart.js\');'));
  });
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  test('generateTestEntrypoint does not generate test config wrappers when testConfigPath is not passed', () {
    final String result = generateTestEntrypoint(
      relativeTestPath: 'relative_path.dart',
      absolutePath: 'absolute_path.dart',
      testConfigPath: null,
      languageVersion: LanguageVersion(2, 8),
    );

    expect(result, isNot(contains('test_config.testExecutable')));
  });

  test('generateTestEntrypoint generates test config wrappers when testConfigPath is passed', () {
    final String result = generateTestEntrypoint(
      relativeTestPath: 'relative_path.dart',
      absolutePath: 'absolute_path.dart',
      testConfigPath: 'test_config_path.dart',
      languageVersion: LanguageVersion(2, 8),
    );

    expect(result, contains('test_config.testExecutable'));
  });
149 150 151 152 153 154 155 156 157 158 159

  test('generateTestEntrypoint embeds urls correctly', () {
    final String result = generateTestEntrypoint(
      relativeTestPath: 'relative_path.dart',
      absolutePath: '/test/absolute_path.dart',
      testConfigPath: null,
      languageVersion: LanguageVersion(2, 8),
    );

    expect(result, contains("Uri.parse('file:///test/absolute_path.dart')"));
  });
160
}