1// Copyright 2013 The Flutter Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5part of ui; 6 7/// Initializes the platform. 8Future<void> webOnlyInitializePlatform({ 9 engine.AssetManager assetManager, 10}) async { 11 if (!debugEmulateFlutterTesterEnvironment) { 12 engine.window.webOnlyLocationStrategy = const engine.HashLocationStrategy(); 13 } 14 15 assetManager ??= const engine.AssetManager(); 16 await webOnlySetAssetManager(assetManager); 17 await _fontCollection.ensureFontsLoaded(); 18 engine.webOnlyInitializeEngine(); 19 20 // This needs to be after `webOnlyInitializeEngine` because that is where the 21 // canvaskit script is added to the page. 22 if (engine.experimentalUseSkia) { 23 await engine.initializeSkia(); 24 } 25 _webOnlyIsInitialized = true; 26} 27 28engine.AssetManager _assetManager; 29engine.FontCollection _fontCollection; 30 31bool _webOnlyIsInitialized = false; 32bool get webOnlyIsInitialized => _webOnlyIsInitialized; 33 34/// Specifies that the platform should use the given [AssetManager] to load 35/// assets. 36/// 37/// The given asset manager is used to initialize the font collection. 38Future<void> webOnlySetAssetManager(engine.AssetManager assetManager) async { 39 assert(assetManager != null, 'Cannot set assetManager to null'); 40 if (assetManager == _assetManager) { 41 return; 42 } 43 44 _assetManager = assetManager; 45 46 _fontCollection ??= engine.FontCollection(); 47 _fontCollection.clear(); 48 if (_assetManager != null) { 49 await _fontCollection.registerFonts(_assetManager); 50 } 51 52 if (debugEmulateFlutterTesterEnvironment) { 53 _fontCollection.debugRegisterTestFonts(); 54 } 55} 56 57/// Flag that shows whether the Flutter Testing Behavior is enabled. 58/// 59/// This flag can be used to decide if the code is running from a Flutter Test 60/// such as a Widget test. 61/// 62/// For example in these tests we use a predictable-size font which makes widget 63/// tests less flaky. 64bool get debugEmulateFlutterTesterEnvironment => 65 _debugEmulateFlutterTesterEnvironment; 66 67set debugEmulateFlutterTesterEnvironment(bool value) { 68 _debugEmulateFlutterTesterEnvironment = value; 69 if (_debugEmulateFlutterTesterEnvironment) { 70 const Size logicalSize = Size(800.0, 600.0); 71 engine.window.webOnlyDebugPhysicalSizeOverride = 72 logicalSize * window.devicePixelRatio; 73 } 74} 75 76bool _debugEmulateFlutterTesterEnvironment = false; 77 78/// This class handles downloading assets over the network. 79engine.AssetManager get webOnlyAssetManager => _assetManager; 80 81/// A collection of fonts that may be used by the platform. 82engine.FontCollection get webOnlyFontCollection => _fontCollection; 83