Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/widgets/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Copy link
Collaborator

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

if (replaceIfMuted && store.isUserMuted(userId)) {
Expand All @@ -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);
Expand All @@ -89,6 +89,7 @@ class AvatarImage extends StatelessWidget {
avatarUrl.get(physicalSize),
filterQuality: FilterQuality.medium,
fit: BoxFit.cover,
errorBuilder: (_, _, _) => _AvatarPlaceholder(size: size),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also change the resolvedUrl == null case to return _AvatarPlaceholder, too, for this part of the issue description in #1558:

because the URL is invalid

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and add a test for that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the user == null case above that, for this part of the issue description:

Or, pre-#1556, for a message from an unknown sender

);
}
}
Expand Down
15 changes: 11 additions & 4 deletions test/test_images.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -12,12 +13,18 @@ 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;
}

Expand Down
24 changes: 12 additions & 12 deletions test/widgets/new_dm_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,19 @@ void main() {
});

group('user selection', () {
void checkUserSelected(WidgetTester tester, User user, bool expected) {
final icon = tester.widget<Icon>(find.descendant(
of: findUserTile(user),
matching: find.byType(Icon)));

if (expected) {
check(findUserChip(user)).findsOne();
check(icon).icon.equals(ZulipIcons.check_circle_checked);
} else {
check(findUserChip(user)).findsNothing();
check(icon).icon.equals(ZulipIcons.check_circle_unchecked);
Finder findInUserTile(User user, Finder finder) => find.descendant(of: findUserTile(user), matching: finder);

void checkUserSelected(WidgetTester tester, User user, bool expected) {
if (expected) {
check(findUserChip(user)).findsOne();
check(findInUserTile(user,find.byIcon(ZulipIcons.check_circle_checked))).findsOne();
check(findInUserTile(user,find.byIcon(ZulipIcons.check_circle_unchecked))).findsNothing();
} else {
check(findUserChip(user)).findsNothing();
check(findInUserTile(user,find.byIcon(ZulipIcons.check_circle_unchecked))).findsOne();
check(findInUserTile(user,find.byIcon(ZulipIcons.check_circle_checked))).findsNothing();
}
}
}

testWidgets('tapping user chip deselects the user', (tester) async {
await setupSheet(tester, users: [eg.otherUser, eg.thirdUser]);
Expand Down
81 changes: 75 additions & 6 deletions test/widgets/user_test.dart
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';

Copy link
Collaborator

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, 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();
Expand All @@ -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));
Expand Down Expand Up @@ -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);
});
});
}