-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! WIP-Adding unit test for message class
- Loading branch information
1 parent
d6df6f1
commit 17430f6
Showing
4 changed files
with
210 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}, | ||
); | ||
}); | ||
} |
Oops, something went wrong.