-
Notifications
You must be signed in to change notification settings - Fork 344
avatar: Show placeholder on image load error #1882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
aa123be
57ce7f1
97c936d
3ed8e3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import 'package:flutter/material.dart'; | ||
|
|
||
| import '../api/model/model.dart'; | ||
| import '../model/avatar_url.dart'; | ||
| import '../model/binding.dart'; | ||
|
|
@@ -90,6 +89,7 @@ class AvatarImage extends StatelessWidget { | |
| avatarUrl.get(physicalSize), | ||
| filterQuality: FilterQuality.medium, | ||
| fit: BoxFit.cover, | ||
| errorBuilder: (_, _, _) => _AvatarPlaceholder(size: size), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also change the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and add a test for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for the
|
||
| ); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import 'dart:async'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'dart:typed_data'; | ||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
|
|
@@ -12,15 +12,20 @@ import 'package:flutter_test/flutter_test.dart'; | |
| /// before the end of the test. | ||
| // TODO(upstream) simplify callers by using addTearDown: https://github.com/flutter/flutter/issues/123189 | ||
| // See also: https://github.com/flutter/flutter/issues/121917 | ||
| FakeImageHttpClient prepareBoringImageHttpClient() { | ||
| FakeImageHttpClient prepareBoringImageHttpClient({bool success = true}) { | ||
| final httpClient = FakeImageHttpClient(); | ||
| debugNetworkImageHttpClientProvider = () => httpClient; | ||
| httpClient.request.response | ||
| ..statusCode = HttpStatus.ok | ||
| ..content = kSolidBlueAvatar; | ||
| if (success) { | ||
| httpClient.request.response | ||
| ..statusCode = HttpStatus.ok | ||
| ..content = kSolidBlueAvatar; | ||
| } else { | ||
| httpClient.request.response | ||
| ..statusCode = HttpStatus.notFound | ||
| ..content = Uint8List(0); | ||
| } | ||
| return httpClient; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep this blank line, which exists to give the code some breathing room so it's more readable. |
||
| class FakeImageHttpClient extends Fake implements HttpClient { | ||
| final FakeImageHttpClientRequest request = FakeImageHttpClientRequest(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,14 +5,15 @@ import 'package:flutter/rendering.dart'; | |
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:zulip/model/store.dart'; | ||
| import 'package:zulip/widgets/content.dart'; | ||
| import 'package:zulip/widgets/icons.dart'; | ||
| import 'package:zulip/widgets/store.dart'; | ||
| import 'package:zulip/widgets/user.dart'; | ||
|
|
||
| import '../example_data.dart' as eg; | ||
| import '../model/binding.dart'; | ||
| import '../model/test_store.dart'; | ||
| import '../stdlib_checks.dart'; | ||
| import '../test_images.dart'; | ||
| import 'test_app.dart'; | ||
|
|
||
| void main() { | ||
| TestZulipBinding.ensureInitialized(); | ||
|
|
@@ -78,5 +79,26 @@ void main() { | |
| check(await actualUrl(tester, avatarUrl)).isNull(); | ||
| debugNetworkImageHttpClientProvider = null; | ||
| }); | ||
| testWidgets('shows placeholder when image URL gives error', (WidgetTester tester) async { | ||
| addTearDown(testBinding.reset); | ||
| prepareBoringImageHttpClient(success: false); | ||
| await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot()); | ||
| final store = await testBinding.globalStore.perAccount(eg.selfAccount.id); | ||
| final badUser = eg.user(avatarUrl: 'https://zulip.com/avatarinvalid.png'); | ||
| await store.addUser(badUser); | ||
| await tester.pumpWidget(TestZulipApp( | ||
| child: PerAccountStoreWidget( | ||
| accountId: eg.selfAccount.id, | ||
| child: AvatarImage(userId: badUser.userId, size: 30)))); | ||
|
Comment on lines
+89
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be able to pass an |
||
| await tester.pumpAndSettle(); | ||
| expect( | ||
| find.descendant( | ||
| of: find.byType(AvatarImage), | ||
| matching: find.byIcon(ZulipIcons.person), | ||
| ), | ||
| findsOneWidget, | ||
| ); | ||
| debugNetworkImageHttpClientProvider = null; | ||
| }); | ||
|
Comment on lines
+82
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits:
|
||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't delete this blank line; it separates third-party imports from our own imports. (Here and elsewhere in the PR.)