material-foundation/flutter-packages

Unable to load fonts when running widget tests

RishabJaiswal opened this issue · 10 comments

Package

google_fonts

Existing issue?

What happened?

Expected vs actual result:

I don't want to include font files in my release bundle, hence only relying in the downloadable nature of the fonts but the test log is getting polluted throwing exceptions that the test failed to load.

image

Steps to reproduce:

  1. Create a widget that uses downloadable google_font
  2. Do not include font files in assets or google_fonts
  3. Create a widget test for the widget in step
  4. Run the widget test. The exception in above screen shot is thrown.

Using google_fonts: 4.0.3

Relevant log output

Error: google_fonts was unable to load font SourceSansPro-SemiBold because the following exception occurred:
Exception: Failed to load font with url: https://fonts.gstatic.com/s/a/918213748bccf07ee3b20ed95cc824bb329b9b048732700e32c241bba63a029b.ttf

There is likely something wrong with your test. Please see https://github.com/material-foundation/flutter-packages/blob/main/packages/google_fonts/example/test for examples of how to test with google_fonts.
If troubleshooting doesn't solve the problem, please file an issue at https://github.com/material-foundation/flutter-packages/issues/new/choose.

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following _Exception was thrown running a test (but after the test had completed):
Exception: Failed to load font with url:
https://fonts.gstatic.com/s/a/918213748bccf07ee3b20ed95cc824bb329b9b048732700e32c241bba63a029b.ttf

When the exception was thrown, this was the stack:
#0      _httpFetchFontAndSaveToDevice (package:google_fonts/src/google_fonts_base.dart:274:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

we have the same error, using version 4.0.4

image

The original issue regards tests (see the README and its links for working test examples).

@BetoMottu if you have end users facing this issue, than your app doesn't seem to have the correct permissions. The error includes a link pointing you to resources to properly set your apps' access to the internet.

@guidezpl, before declaring the problem we check all permissions, as the problem seems to have something to do with connection permission. Our application uses this lib since the beginning of the project a little over 1 year, apparently the problem occurred after the update from version 3.0.1 to 4.0.4, previously we didn't have this problem. Analyzing the issues of this project I found something that is also related to our project, and that could be related to the problem. We used the Poppins font and another report was made with a similar problem: #441

As you can see below, only the source link is available for download, without a link to deal with the problem:

image

We keep getting the error, we are investigating on our side.

Experiencing the same issue, Android specifically

https://fonts.gstatic.com/s/a/9ecfd020e9cc0b676025df8390c0dc8cc2062523540887dd04bec0ef4d5a449c.ttf is a valid link, so please double check permissions, https://docs.flutter.dev/data-and-backend/networking

According to Data and Backend > Networking,

Android apps must declare their use of the internet in the Android manifest (AndroidManifest.xml )

We have this included and have had for a while however issue persists. Based on affected users, it affects a handful possibly indicating network connectivity issues. I'd still like to contribute this issue to the discussion.
image

@cybex-dev thanks for checking, are there multiple AndroidManifest.xml files?

@cybex-dev thanks for checking, are there multiple AndroidManifest.xml files?

None extra other than the 3 standard build variation manifests in src/debug, src/main, src/profile all which contain:

<uses-permission android:name="android.permission.INTERNET"/>

I ended up using the local caching solution by providing desired fonts in the google_fonts/ assets directory, see this solution for details if anyone is interested.

That answer isn't quite right, prefer using https://pub.dev/documentation/google_fonts/latest/google_fonts/GoogleFonts/config.html to disallow internet fetching and rely on local fonts only.

Since we have working unit and widget tests in packages/google_fonts/test, I'm considering this issue unreproducible and will therefore close it. If this changes, or if you have an idea for improving the existing tests to cover your case, please reopen this issue. I've also created #560 as a central place to deal with the font fetching issue.