Skip to content

Commit

Permalink
fixup! WIP-Adding unit test for message class
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim committed Dec 15, 2024
1 parent d6df6f1 commit 17430f6
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 163 deletions.
52 changes: 9 additions & 43 deletions lib/pages/chat/events/message/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import 'package:fluffychat/utils/date_time_extension.dart';
import 'package:fluffychat/utils/extension/event_status_custom_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/filtered_timeline_extension.dart';
import 'package:fluffychat/presentation/mixins/message_avatar_mixin.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:fluffychat/widgets/avatar/avatar.dart';
import 'package:fluffychat/widgets/context_menu/context_menu_action.dart';
import 'package:fluffychat/widgets/swipeable.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -99,7 +99,7 @@ class Message extends StatefulWidget {
State<Message> createState() => _MessageState();
}

class _MessageState extends State<Message> {
class _MessageState extends State<Message> with MessageAvatarMixin {
InViewState? inViewState;

final inviewNotifier = ValueNotifier(false);
Expand Down Expand Up @@ -187,10 +187,13 @@ class _MessageState extends State<Message> {
: MainAxisAlignment.start;

final rowChildren = <Widget>[
_placeHolderWidget(
widget.event.isSameSenderWith(widget.previousEvent),
widget.event.isOwnMessage,
widget.event,
placeHolderWidget(
widget.onAvatarTap,
event: widget.event,
sameSender: widget.event.isSameSenderWith(widget.previousEvent),
ownMessage: widget.event.isOwnMessage,
context: context,
selectMode: widget.selectMode,
),
Expanded(
child: MessageContentWithTimestampBuilder(
Expand Down Expand Up @@ -311,43 +314,6 @@ class _MessageState extends State<Message> {
);
}

bool shouldDisplayAvatar(bool sameSender, bool ownMessage) {
return sameSender &&
(!ownMessage || !Message.responsiveUtils.isMobile(context));
}

Widget _placeHolderWidget(bool sameSender, bool ownMessage, Event event) {
if (widget.selectMode ||
(event.room.isDirectChat &&
Message.responsiveUtils.isMobile(context))) {
return const SizedBox();
}

if (shouldDisplayAvatar(sameSender, ownMessage)) {
return Padding(
padding: MessageStyle.paddingAvatar,
child: FutureBuilder<User?>(
future: event.fetchSenderUser(),
builder: (context, snapshot) {
final user = snapshot.data ?? event.senderFromMemoryOrFallback;
return Avatar(
size: MessageStyle.avatarSize,
fontSize: MessageStyle.fontSize,
mxContent: user.avatarUrl,
name: user.calcDisplayname(),
onTap: () => widget.onAvatarTap!(event),
);
},
),
);
}

return const Padding(
padding: MessageStyle.paddingAvatar,
child: SizedBox(width: MessageStyle.avatarSize),
);
}

Widget _messageSelectedWidget(
BuildContext context,
Widget child,
Expand Down
56 changes: 56 additions & 0 deletions lib/presentation/mixins/message_avatar_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/pages/chat/events/message/message_style.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:fluffychat/widgets/avatar/avatar.dart';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';

mixin MessageAvatarMixin {
ResponsiveUtils responsive = getIt.get<ResponsiveUtils>();

bool _shouldDisplayAvatar(
bool sameSender,
bool ownMessage,
BuildContext context,
) {
return sameSender && (!ownMessage || !responsive.isMobile(context));
}

Widget placeHolderWidget(
Function(Event)? onAvatarTap, {
required bool sameSender,
required bool ownMessage,
required Event event,
required BuildContext context,
required bool selectMode,
}) {
if (selectMode ||
(event.room.isDirectChat && responsive.isMobile(context))) {
return const SizedBox();
}

if (_shouldDisplayAvatar(sameSender, ownMessage, context)) {
return Padding(
padding: MessageStyle.paddingAvatar,
child: FutureBuilder<User?>(
future: event.fetchSenderUser(),
builder: (context, snapshot) {
final user = snapshot.data ?? event.senderFromMemoryOrFallback;
return Avatar(
size: MessageStyle.avatarSize,
fontSize: MessageStyle.fontSize,
mxContent: user.avatarUrl,
name: user.calcDisplayname(),
onTap: () => onAvatarTap!(event),
);
},
),
);
}

return const Padding(
padding: MessageStyle.paddingAvatar,
child: SizedBox(width: MessageStyle.avatarSize),
);
}
}
145 changes: 145 additions & 0 deletions test/mixin/message_avatar_mixin_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// ignore_for_file: depend_on_referenced_packages

import 'package:fluffychat/config/localizations/localization_service.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/presentation/mixins/message_avatar_mixin.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:fluffychat/widgets/theme_builder.dart';
import 'package:fluffychat/widgets/avatar/avatar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:matrix/matrix.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

import '../utils/test_client.dart';

class MockMessageAvatarUtils with MessageAvatarMixin {}

Future<void> main() async {
TestWidgetsFlutterBinding.ensureInitialized();
late MockMessageAvatarUtils mockMessageAvatarUtils;

setUpAll(() {
final getIt = GetIt.instance;
getIt.registerSingleton(ResponsiveUtils());
mockMessageAvatarUtils = MockMessageAvatarUtils();
});

final client = await prepareTestClient(loggedIn: true);
final room = Room(id: '!726s6s6f:example.com', client: client);
final event = Event(
content: {
'body': 'Test message',
'msgtype': 'm.text',
},
type: 'm.room.message',
eventId: '7365636s6r64300:example.com',
senderId: '@bob:example.com',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
room: room,
);

group('[MessageAvatarUtils] TEST\n', () {
Future<void> runTest(
WidgetTester tester, {
required Event event,
required bool selectMode,
required bool sameSender,
required bool ownMessage,
}) async {
Widget? widget;
await tester.pumpWidget(
ThemeBuilder(
builder: (context, themeMode, primaryColor) => MaterialApp(
locale: const Locale('en'),
localizationsDelegates: const [
LocaleNamesLocalizationsDelegate(),
L10n.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: LocalizationService.supportedLocales,
theme: TwakeThemes.buildTheme(
context,
Brightness.light,
primaryColor,
),
builder: (context, child) => MediaQuery(
data: const MediaQueryData(
size: Size(1920, 1080),
),
child: child!,
),
home: Scaffold(
body: Builder(
builder: (context) {
widget = mockMessageAvatarUtils.placeHolderWidget(
(_) {},
sameSender: sameSender,
ownMessage: ownMessage,
event: event,
context: context,
selectMode: selectMode,
);
return const SizedBox();
},
),
),
),
),
);

await tester.pumpAndSettle();

if (selectMode || ownMessage) {
expect(find.byType(SizedBox), findsOneWidget);
} else {
expect(widget, isNotNull);
expect(find.byType(Avatar), findsOneWidget);
}
}

testWidgets(
'GIVEN selectMode is true THEN display SizedBox',
(WidgetTester tester) async {
await runTest(
tester,
event: event,
selectMode: true,
sameSender: true,
ownMessage: false,
);
},
);

testWidgets(
'GIVEN ownMessage is true THEN display SizedBox',
(WidgetTester tester) async {
await runTest(
tester,
event: event,
selectMode: false,
sameSender: false,
ownMessage: true,
);
},
);

testWidgets(
'GIVEN not ownMessage AND not selectMode THEN display Avatar',
(WidgetTester tester) async {
await runTest(
tester,
event: event,
selectMode: false,
sameSender: true,
ownMessage: false,
);
},
);
});
}
Loading

0 comments on commit 17430f6

Please sign in to comment.