-
Notifications
You must be signed in to change notification settings - Fork 350
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
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 |
|---|---|---|
|
|
@@ -66,7 +66,7 @@ class AvatarImage extends StatelessWidget { | |
| final user = store.getUser(userId); | ||
|
|
||
| if (user == null) { // TODO(log) | ||
| return const SizedBox.shrink(); | ||
| return _AvatarPlaceholder(size: size); | ||
| } | ||
|
|
||
| if (replaceIfMuted && store.isUserMuted(userId)) { | ||
|
|
@@ -79,7 +79,7 @@ class AvatarImage extends StatelessWidget { | |
| }; | ||
|
|
||
| if (resolvedUrl == null) { | ||
| return const SizedBox.shrink(); | ||
| return _AvatarPlaceholder(size: size); | ||
| } | ||
|
|
||
| final avatarUrl = AvatarUrl.fromUserData(resolvedUrl: resolvedUrl); | ||
|
|
@@ -89,6 +89,7 @@ class AvatarImage extends StatelessWidget { | |
| avatarUrl.get(physicalSize), | ||
| filterQuality: FilterQuality.medium, | ||
| fit: BoxFit.cover, | ||
| errorBuilder: (_, _, _) => _AvatarPlaceholder(size: size), | ||
|
Collaborator
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
Collaborator
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.
Collaborator
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,18 +1,17 @@ | ||
| import 'package:checks/checks.dart'; | ||
| import 'package:flutter/cupertino.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/rendering.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:zulip/model/store.dart'; | ||
| import 'package:zulip/widgets/image.dart'; | ||
| import 'package:zulip/widgets/store.dart'; | ||
| import 'package:zulip/widgets/icons.dart'; | ||
| import 'package:zulip/widgets/user.dart'; | ||
|
|
||
|
Collaborator
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. nit: keep this blank line, which separates third-party imports from first-party imports |
||
| 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(); | ||
|
|
@@ -28,9 +27,12 @@ void main() { | |
| await store.addUser(user); | ||
|
|
||
| prepareBoringImageHttpClient(); | ||
| await tester.pumpWidget(GlobalStoreWidget( | ||
| child: PerAccountStoreWidget(accountId: eg.selfAccount.id, | ||
| child: AvatarImage(userId: user.userId, size: size ?? 30)))); | ||
| await tester.pumpWidget( | ||
| TestZulipApp( | ||
| accountId: eg.selfAccount.id, | ||
| child: AvatarImage(userId: user.userId, size: size ?? 30), | ||
| ) | ||
| ); | ||
| await tester.pump(); | ||
| await tester.pump(); | ||
| tester.widget(find.byType(AvatarImage)); | ||
|
|
@@ -78,5 +80,72 @@ 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( | ||
| accountId: eg.selfAccount.id, | ||
| child: AvatarImage(userId: badUser.userId, size: 30))); | ||
| await tester.pumpAndSettle(); | ||
| check( | ||
| find.descendant( | ||
| of: find.byType(AvatarImage), | ||
| matching: find.byIcon(ZulipIcons.person), | ||
| ).evaluate().length | ||
| ).equals(1); | ||
| debugNetworkImageHttpClientProvider = null; | ||
| }); | ||
|
|
||
| testWidgets('shows placeholder when user avatarUrl is null', (WidgetTester tester) async { | ||
| addTearDown(testBinding.reset); | ||
| await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot()); | ||
| final store = await testBinding.globalStore.perAccount(eg.selfAccount.id); | ||
|
|
||
| final userWithNoUrl = eg.user(avatarUrl: null); | ||
| await store.addUser(userWithNoUrl); | ||
|
|
||
| await tester.pumpWidget( | ||
| TestZulipApp( | ||
| accountId: eg.selfAccount.id, | ||
| child: AvatarImage(userId: userWithNoUrl.userId, size: 30), | ||
| ), | ||
| ); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| check( | ||
| find.descendant( | ||
| of: find.byType(AvatarImage), | ||
| matching: find.byIcon(ZulipIcons.person), | ||
| ).evaluate().length | ||
| ).equals(1); | ||
| }); | ||
|
|
||
| testWidgets('shows placeholder when user is not found', (WidgetTester tester) async { | ||
| addTearDown(testBinding.reset); | ||
| await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot()); | ||
|
|
||
| const nonExistentUserId = 9999999; | ||
|
|
||
| await tester.pumpWidget( | ||
| TestZulipApp( | ||
| accountId: eg.selfAccount.id, | ||
| child: AvatarImage(userId: nonExistentUserId, size: 30), | ||
| ), | ||
| ); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| check( | ||
| find.descendant( | ||
| of: find.byType(AvatarImage), | ||
| matching: find.byIcon(ZulipIcons.person), | ||
| ).evaluate().length | ||
| ).equals(1); | ||
| }); | ||
| }); | ||
| } | ||
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.
nit: keep this blank line, separating unrelated stanzas of code