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
6 changes: 3 additions & 3 deletions lib/widgets/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ 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)) {
return _AvatarPlaceholder(size: size);
}
Expand All @@ -80,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 @@ -90,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
16 changes: 11 additions & 5 deletions test/test_images.dart
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';

Expand All @@ -12,12 +12,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;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Expand Down
101 changes: 95 additions & 6 deletions test/widgets/user_test.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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/content.dart';
import 'package:zulip/widgets/icons.dart';
import 'package:zulip/widgets/store.dart';
import 'package:zulip/widgets/theme.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();
Expand All @@ -28,9 +28,17 @@ 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(
MaterialApp(
theme: ThemeData(extensions: [
DesignVariables.light,
]),
home: GlobalStoreWidget(
child: PerAccountStoreWidget(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 +86,86 @@ 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(
MaterialApp(
theme: ThemeData(extensions: [
DesignVariables.light,
]),
home: 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(
MaterialApp(
theme: ThemeData(extensions: [
DesignVariables.light,
]),
home: 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(
MaterialApp(
theme: ThemeData(extensions: [
DesignVariables.light,
]),
home: 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);
});
});
}
Loading