// Copyright 2014 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_tools/src/web_template.dart'; import '../src/common.dart'; const String htmlSample1 = '''
'''; const String htmlSample2 = '''
'''; const String htmlSampleInlineFlutterJsBootstrap = '''
'''; const String htmlSampleInlineFlutterJsBootstrapOutput = '''
'''; const String htmlSampleFullFlutterBootstrapReplacement = '''
'''; const String htmlSampleFullFlutterBootstrapReplacementOutput = '''
'''; const String htmlSampleLegacyVar = '''
'''; const String htmlSampleLegacyLoadEntrypoint = '''
'''; String htmlSample2Replaced({required String baseHref, required String serviceWorkerVersion}) => '''
'''; const String htmlSample3 = '''
'''; void main() { final MemoryFileSystem fs = MemoryFileSystem(); final File flutterJs = fs.file('flutter.js'); flutterJs.writeAsStringSync('(flutter.js content)'); test('can parse baseHref', () { expect(WebTemplate.baseHref(''), 'foo/111'); expect(WebTemplate.baseHref(htmlSample1), 'foo/222'); expect(WebTemplate.baseHref(htmlSample2), ''); // Placeholder base href. }); test('handles missing baseHref', () { expect(WebTemplate.baseHref(''), ''); expect(WebTemplate.baseHref(''), ''); expect(WebTemplate.baseHref(htmlSample3), ''); }); test('throws on invalid baseHref', () { expect(() => WebTemplate.baseHref(''), throwsToolExit()); expect(() => WebTemplate.baseHref(''), throwsToolExit()); expect(() => WebTemplate.baseHref(''), throwsToolExit()); expect(() => WebTemplate.baseHref(''), throwsToolExit()); expect(() => WebTemplate.baseHref(''), throwsToolExit()); }); test('applies substitutions', () { const WebTemplate indexHtml = WebTemplate(htmlSample2); expect( indexHtml.withSubstitutions( baseHref: '/foo/333/', serviceWorkerVersion: 'v123xyz', flutterJsFile: flutterJs, ), htmlSample2Replaced(baseHref: '/foo/333/', serviceWorkerVersion: 'v123xyz'), ); }); test('applies substitutions with legacy var version syntax', () { const WebTemplate indexHtml = WebTemplate(htmlSampleLegacyVar); expect( indexHtml.withSubstitutions( baseHref: '/foo/333/', serviceWorkerVersion: 'v123xyz', flutterJsFile: flutterJs, ), htmlSample2Replaced(baseHref: '/foo/333/', serviceWorkerVersion: 'v123xyz'), ); }); test('applies substitutions to inline flutter.js bootstrap script', () { const WebTemplate indexHtml = WebTemplate(htmlSampleInlineFlutterJsBootstrap); expect(indexHtml.getWarnings(), isEmpty); expect( indexHtml.withSubstitutions( baseHref: '/', serviceWorkerVersion: '(service worker version)', flutterJsFile: flutterJs, buildConfig: '(build config)', ), htmlSampleInlineFlutterJsBootstrapOutput, ); }); test('applies substitutions to full flutter_bootstrap.js replacement', () { const WebTemplate indexHtml = WebTemplate(htmlSampleFullFlutterBootstrapReplacement); expect(indexHtml.getWarnings(), isEmpty); expect( indexHtml.withSubstitutions( baseHref: '/', serviceWorkerVersion: '(service worker version)', flutterJsFile: flutterJs, buildConfig: '(build config)', flutterBootstrapJs: '(flutter bootstrap script)', ), htmlSampleFullFlutterBootstrapReplacementOutput, ); }); test('re-parses after substitutions', () { const WebTemplate indexHtml = WebTemplate(htmlSample2); expect(WebTemplate.baseHref(htmlSample2), ''); // Placeholder base href. final String substituted = indexHtml.withSubstitutions( baseHref: '/foo/333/', serviceWorkerVersion: 'v123xyz', flutterJsFile: flutterJs, ); // The parsed base href should be updated after substitutions. expect(WebTemplate.baseHref(substituted), 'foo/333'); }); test('warns on legacy service worker patterns', () { const WebTemplate indexHtml = WebTemplate(htmlSampleLegacyVar); final List warnings = indexHtml.getWarnings(); expect(warnings.length, 2); expect(warnings.where((WebTemplateWarning warning) => warning.lineNumber == 13), isNotEmpty); expect(warnings.where((WebTemplateWarning warning) => warning.lineNumber == 16), isNotEmpty); }); test('warns on legacy FlutterLoader.loadEntrypoint', () { const WebTemplate indexHtml = WebTemplate(htmlSampleLegacyLoadEntrypoint); final List warnings = indexHtml.getWarnings(); expect(warnings.length, 1); expect(warnings.single.lineNumber, 14); }); }