Skip to content

Commit 437a0ff

Browse files
authored
[google_fonts] Make the pendingFonts test deterministic (flutter#13262)
`pendingFonts waits for fonts to be loaded` fails on roughly 3 out of 10 runs of `Windows_x64 dart_unit_tests_shard_1`, and only for PRs that make the tool run every package (`.ci/`, `script/`, Flutter rolls, ...), so it never shows up in postsubmit and blocks exactly the PRs that can't avoid it. The history and the analysis are in flutter/flutter#194407; the short version: - The three `expect`s in the test pass on every platform. The failure is a late error that flips an already-passed test to failed (the CI log shows the pass counter dropping from `+92` to `+91 -1` with no `[E]` block). - After the mock HTTP fetch, `_httpFetchFontAndSaveToDevice` writes the font to the application support directory without awaiting the result. The test does not fake `PathProviderPlatform`, so under `flutter test` that write fails with `MissingPluginException` inside the dropped future, after the test body has returned. This file has a single test, so whether the runner is still attached when the error lands is a timing question; Linux consistently loses the error, Windows regularly does not. - Keeping the test alive for two seconds after its last `expect` reproduces the error on Linux every time. This change installs the same `FakePathProviderPlatform` pointing at a temp directory that `load_font_if_necessary_test.dart` already uses, so the background write succeeds and there is no straggler error. With the change, the "wait two seconds" variant passes. Test-only change, so no version or CHANGELOG update (exempt category: PRs that only affect tests). Fixes flutter/flutter#194407 Unblocks flutter#13223 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 293b18c commit 437a0ff

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎packages/google_fonts/test/google_fonts_text_style_test.dart‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
@TestOn('vm') // Uses dart:io
6+
library;
7+
8+
import 'dart:io';
9+
510
import 'package:cupertino_ui/cupertino_ui.dart';
611
import 'package:flutter/services.dart';
712
import 'package:flutter_test/flutter_test.dart';
@@ -12,6 +17,8 @@ import 'package:google_fonts/src/google_fonts_variant.dart';
1217
import 'package:http/http.dart' as http;
1318
import 'package:material_ui/material_ui.dart';
1419
import 'package:mockito/mockito.dart';
20+
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
21+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
1522

1623
class MockHttpClient extends Mock implements http.Client {
1724
Future<http.Response> gets(dynamic uri, {dynamic headers}) {
@@ -27,6 +34,19 @@ class MockAssetManifest extends Mock implements AssetManifest {
2734
List<String> listAssets() => <String>[];
2835
}
2936

37+
class FakePathProviderPlatform extends Fake
38+
with MockPlatformInterfaceMixin
39+
implements PathProviderPlatform {
40+
FakePathProviderPlatform(this._applicationSupportPath);
41+
42+
final String _applicationSupportPath;
43+
44+
@override
45+
Future<String?> getApplicationSupportPath() async {
46+
return _applicationSupportPath;
47+
}
48+
}
49+
3050
const String _fakeResponse = 'fake response body - success';
3151
// The number of bytes in _fakeResponse.
3252
const int _fakeResponseLengthInBytes = 28;
@@ -56,6 +76,16 @@ void main() {
5676
when(mockHttpClient.gets(any)).thenAnswer((_) async {
5777
return http.Response(_fakeResponse, 200);
5878
});
79+
80+
// After a font is fetched over HTTP, the package writes it to the
81+
// application support directory without awaiting the result. In a
82+
// `flutter test` environment there is no path_provider implementation, so
83+
// that write fails with a MissingPluginException after the test body has
84+
// already finished. Whether that late error is still attributed to this
85+
// test depends on timing (it regularly is on Windows CI), so point the
86+
// package at a real temporary directory instead.
87+
final Directory directory = await Directory.systemTemp.createTemp();
88+
PathProviderPlatform.instance = FakePathProviderPlatform(directory.path);
5989
});
6090

6191
tearDown(() {

0 commit comments

Comments
 (0)