Skip to content

Commit

Permalink
fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Mar 6, 2024
1 parent 6b32647 commit 4df152c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
39 changes: 24 additions & 15 deletions lib/src/acter_avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ class ActerAvatar extends StatefulWidget {
/// Avatar gesture tap for parent badge of `DisplayMode.Space`.
final void Function()? onParentBadgeTap;

ActerAvatar(
{Key? key,
required this.avatarInfo,
required this.mode,
this.onAvatarTap,
this.onParentBadgeTap,
this.avatarsInfo,
this.tooltip = TooltipStyle.Combined,
this.secondaryToolTip = TooltipStyle.Combined,
this.size,
this.badgeSize})
: super(key: key ?? Key('avatar-${avatarInfo.uniqueId}-$size'));
ActerAvatar({
Key? key,
required this.avatarInfo,
required this.mode,
this.onAvatarTap,
this.onParentBadgeTap,
this.avatarsInfo,
this.tooltip = TooltipStyle.Combined,
this.secondaryToolTip = TooltipStyle.Combined,
this.size,
this.badgeSize = 20,
}) : super(key: key ?? Key('avatar-${avatarInfo.uniqueId}-$size'));

@override
_ActerAvatar createState() => _ActerAvatar();
Expand Down Expand Up @@ -268,12 +268,20 @@ class _ActerAvatar extends State<ActerAvatar> {
}

Widget renderSpaceParent(BuildContext context) {
double badgeOverflow = badgeSize / 5;
if (widget.badgeSize == null) {
// nothing. ignore
return SizedBox.shrink();
}
final badgeSize = widget.badgeSize ?? 20;
if (widget.avatarsInfo == null || widget.avatarsInfo!.isEmpty) {
return SizedBox(height: badgeSize + badgeOverflow);
return SizedBox(
height: badgeSize,
width: badgeSize,
);
}

final parentInfo = widget.avatarsInfo![0];
double badgeOverflow = badgeSize / 5;

return Positioned(
bottom: -badgeOverflow,
Expand All @@ -284,13 +292,14 @@ class _ActerAvatar extends State<ActerAvatar> {
avatarInfo: parentInfo,
mode: DisplayMode.Space,
size: badgeSize,
badgeSize: null,
),
),
);
}

Widget renderFallback(BuildContext context) {
double textFallbackSize = widget.size == null ? 48 : widget.size!;
double textFallbackSize = widget.size ?? 48;
double multiFallbackSize = widget.size == null ? 48 : widget.size! * 2.0;

/// Fallback
Expand Down
2 changes: 1 addition & 1 deletion lib/src/constants/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class TestKeys {
static const multiAvatarKey = Key('Multi-avatar');
static const textAvatarKey = Key('Text-Avatar');
static const circleAvatarKey = Key('Circle-Avatar');
static const rectangleAvatarKey = Key('Rectangle-Avatar');
static const spaceAvatarKey = Key('Space-Avatar');
static const stackedAvatarKey = Key('Stacked-Avatar');
}
57 changes: 29 additions & 28 deletions test/acter_avatar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ void main() {
});
});

group('Rectangular Avatar tests', () {
testWidgets('Rectangular Avatar with specified size',
group('Space Avatar tests', () {
testWidgets('Space Avatar with specified size',
(WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ActerAvatar(
key: TestKeys.rectangleAvatarKey,
key: TestKeys.spaceAvatarKey,
avatarInfo: AvatarInfo(uniqueId: 'test:acter.org'),
mode: DisplayMode.Space,
size: 36,
Expand All @@ -91,60 +91,59 @@ void main() {
));
await tester.pumpAndSettle();
final Size avatarSize =
tester.getSize(find.byKey(TestKeys.rectangleAvatarKey));
tester.getSize(find.byKey(TestKeys.spaceAvatarKey));

// should expect specified fallback size
expect(avatarSize.height, equals(36));
expect(avatarSize.width, equals(36));
});
testWidgets('Rectangular Avatar with fallback size',
(WidgetTester tester) async {
testWidgets('Space Avatar with fallback size', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ActerAvatar(
key: TestKeys.rectangleAvatarKey,
key: TestKeys.spaceAvatarKey,
avatarInfo: AvatarInfo(uniqueId: 'test:acter.org'),
mode: DisplayMode.Space,
),
),
));
await tester.pumpAndSettle();
final Size avatarSize =
tester.getSize(find.byKey(TestKeys.rectangleAvatarKey));
tester.getSize(find.byKey(TestKeys.spaceAvatarKey));
// should expect default fallback avatar size
expect(avatarSize.height, equals(48));
expect(avatarSize.width, equals(48));
});

testWidgets('Rectangular Avatar with NetworkImage render',
testWidgets('Space Avatar with NetworkImage render',
(WidgetTester tester) async {
final String imagePath =
'https://st5.depositphotos.com/38460822/63964/i/600/depositphotos_639649504-stock-photo-mail-sign-sign-alphabet-made.jpg';
final image = NetworkImage(imagePath);
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ActerAvatar(
key: TestKeys.rectangleAvatarKey,
key: TestKeys.spaceAvatarKey,
avatarInfo: AvatarInfo(uniqueId: '@test:acter.org', avatar: image),
mode: DisplayMode.Space,
),
),
));
await tester.pumpAndSettle();
final avatarFinder = find.byKey(TestKeys.rectangleAvatarKey);
final avatarFinder = find.byKey(TestKeys.spaceAvatarKey);
// should expect `ActerAvatar` is present
expect(avatarFinder, findsOneWidget);
final avatar = avatarFinder.evaluate().first.widget as ActerAvatar;
// should expect Asset Image in case of image render.
expect(avatar.avatarInfo.avatar, NetworkImage(imagePath));
});

testWidgets('Rectangular Avatar Parent Badge specified size',
testWidgets('Space Avatar Parent Badge specified size',
(WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ActerAvatar(
key: TestKeys.rectangleAvatarKey,
key: TestKeys.spaceAvatarKey,
avatarInfo: AvatarInfo(uniqueId: '@test:acter.org'),
avatarsInfo: [AvatarInfo(uniqueId: 'Acter-Global')],
mode: DisplayMode.Space,
Expand All @@ -153,52 +152,54 @@ void main() {
),
));
await tester.pumpAndSettle();
final avatarFinder = find.byKey(TestKeys.rectangleAvatarKey);
final avatarFinder = find.byKey(TestKeys.spaceAvatarKey);
// should expect `ActerAvatar` is present
expect(avatarFinder, findsOneWidget);
final sizedBoxSize = tester.getSize(find.byType(SizedBox));
final innerAvatar = tester.getSize(find.descendant(
of: avatarFinder, matching: find.byType(ActerAvatar)));
// expect parent badge specified size.
expect(sizedBoxSize.height, equals(35));
expect(sizedBoxSize.width, equals(35));
expect(innerAvatar.height, equals(35));
expect(innerAvatar.width, equals(35));
});
testWidgets('Rectangular Avatar Parent Badge fallback size',
testWidgets('Space Avatar Parent Badge fallback size',
(WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ActerAvatar(
key: TestKeys.rectangleAvatarKey,
key: TestKeys.spaceAvatarKey,
avatarInfo: AvatarInfo(uniqueId: '@test:acter.org'),
avatarsInfo: [AvatarInfo(uniqueId: 'Acter-Global')],
mode: DisplayMode.Space,
),
),
));
await tester.pumpAndSettle();
final avatarFinder = find.byKey(TestKeys.rectangleAvatarKey);
final avatarFinder = find.byKey(TestKeys.spaceAvatarKey);
// should expect `ActerAvatar` is present
expect(avatarFinder, findsOneWidget);
final sizedBoxSize = tester.getSize(find.byType(SizedBox));
final innerAvatar = tester.getSize(find.descendant(
of: avatarFinder, matching: find.byType(ActerAvatar)));
// expect parent badge fallback size.
expect(sizedBoxSize.height, equals(20));
expect(sizedBoxSize.width, equals(20));
expect(innerAvatar.height, equals(20));
expect(innerAvatar.width, equals(20));
});
testWidgets('Rectangular Avatar Parent badge with NetworkImage render',
testWidgets('Space Avatar Parent badge with NetworkImage render',
(WidgetTester tester) async {
final String imagePath =
'https://st5.depositphotos.com/38460822/63964/i/600/depositphotos_639649504-stock-photo-mail-sign-sign-alphabet-made.jpg';
final image = NetworkImage(imagePath);
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ActerAvatar(
key: TestKeys.rectangleAvatarKey,
key: TestKeys.spaceAvatarKey,
avatarInfo: AvatarInfo(uniqueId: '@test:acter.org'),
avatarsInfo: [AvatarInfo(uniqueId: 'Acter-Global', avatar: image)],
mode: DisplayMode.Space,
),
),
));
await tester.pumpAndSettle();
final avatarFinder = find.byKey(TestKeys.rectangleAvatarKey);
final avatarFinder = find.byKey(TestKeys.spaceAvatarKey);
// should expect `ActerAvatar` is present
expect(avatarFinder, findsOneWidget);
final avatar = avatarFinder.evaluate().first.widget as ActerAvatar;
Expand Down Expand Up @@ -339,7 +340,7 @@ void main() {
onTapped(context, 'Group Chat Avatar tapped'),
),
ActerAvatar(
key: TestKeys.rectangleAvatarKey,
key: TestKeys.spaceAvatarKey,
avatarInfo: AvatarInfo(uniqueId: '@test:acter.org'),
mode: DisplayMode.Space,
onAvatarTap: () => onTapped(context, 'Space Avatar tapped'),
Expand Down Expand Up @@ -368,7 +369,7 @@ void main() {
expect(groupChatGestureFinder, findsOneWidget);

final spaceGestureFinder = find.descendant(
of: find.byKey(TestKeys.rectangleAvatarKey),
of: find.byKey(TestKeys.spaceAvatarKey),
matching: find.byType(GestureDetector));
// we have found the Gesture Detector, proceed with tester operation
expect(spaceGestureFinder, findsOneWidget);
Expand Down

0 comments on commit 4df152c

Please sign in to comment.