| 1 | // Copyright 2014 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 | |
| 5 | import 'dart:async'; |
| 6 | |
| 7 | import '../base/file_system.dart'; |
| 8 | import '../globals.dart' as globals; |
| 9 | |
| 10 | /// Manages a Font configuration that can be shared across multiple tests. |
| 11 | class FontConfigManager { |
| 12 | Directory? _fontsDirectory; |
| 13 | |
| 14 | /// Returns a Font configuration that limits font fallback to the artifact |
| 15 | /// cache directory. |
| 16 | late final File fontConfigFile = () { |
| 17 | final sb = StringBuffer(); |
| 18 | sb.writeln('<fontconfig>' ); |
| 19 | sb.writeln(' <dir> ${globals.cache.getCacheArtifacts().path}</dir>' ); |
| 20 | sb.writeln(' <cachedir>/var/cache/fontconfig</cachedir>' ); |
| 21 | sb.writeln('</fontconfig>' ); |
| 22 | |
| 23 | if (_fontsDirectory == null) { |
| 24 | _fontsDirectory = globals.fs.systemTempDirectory.createTempSync('flutter_test_fonts.' ); |
| 25 | globals.printTrace('Using this directory for fonts configuration: ${_fontsDirectory!.path}' ); |
| 26 | } |
| 27 | |
| 28 | final File cachedFontConfig = globals.fs.file(' ${_fontsDirectory!.path}/fonts.conf' ); |
| 29 | cachedFontConfig.createSync(); |
| 30 | cachedFontConfig.writeAsStringSync(sb.toString()); |
| 31 | return cachedFontConfig; |
| 32 | }(); |
| 33 | |
| 34 | Future<void> dispose() async { |
| 35 | if (_fontsDirectory != null) { |
| 36 | globals.printTrace('Deleting ${_fontsDirectory!.path}...' ); |
| 37 | try { |
| 38 | await _fontsDirectory!.delete(recursive: true); |
| 39 | } on FileSystemException { |
| 40 | // Silently exit |
| 41 | } |
| 42 | _fontsDirectory = null; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |