From 0df36d11a94aafcf16fa3104f812fb55be503c63 Mon Sep 17 00:00:00 2001 From: dab246 Date: Tue, 10 Nov 2026 08:17:18 +0700 Subject: [PATCH] TF-5022 T-07 Add account switcher helpers toast sections and Viewing chrome capability --- lib/features/base/model/ui_keys.dart | 6 + .../session_delegation_extension.dart | 7 + .../model/shown_account_capabilities.dart | 6 + .../account_switch_view_factory_provider.dart | 19 +++ .../utils/account_switch_failure_toast.dart | 46 +++++++ .../utils/account_switcher_sections.dart | 42 ++++++ .../utils/account_switcher_visibility.dart | 9 ++ .../widgets/account_switcher_avatar.dart | 17 +++ lib/l10n/intl_en.arb | 78 +++++++++++ lib/l10n/intl_messages.arb | 100 +++++++++++++- lib/main/localizations/app_localizations.dart | 72 ++++++++++ .../shown_account_capabilities_test.dart | 10 +- .../account_switch_failure_toast_test.dart | 126 ++++++++++++++++++ .../utils/account_switcher_sections_test.dart | 51 +++++++ .../account_switcher_visibility_test.dart | 45 +++++++ .../widgets/account_switcher_avatar_test.dart | 26 ++++ test/fixtures/recording_app_toast.dart | 19 +++ 17 files changed, 675 insertions(+), 4 deletions(-) create mode 100644 lib/features/delegation/presentation/providers/account_switch_view_factory_provider.dart create mode 100644 lib/features/delegation/presentation/utils/account_switch_failure_toast.dart create mode 100644 lib/features/delegation/presentation/utils/account_switcher_sections.dart create mode 100644 lib/features/delegation/presentation/utils/account_switcher_visibility.dart create mode 100644 lib/features/delegation/presentation/widgets/account_switcher_avatar.dart create mode 100644 test/features/delegation/presentation/utils/account_switch_failure_toast_test.dart create mode 100644 test/features/delegation/presentation/utils/account_switcher_sections_test.dart create mode 100644 test/features/delegation/presentation/utils/account_switcher_visibility_test.dart create mode 100644 test/features/delegation/presentation/widgets/account_switcher_avatar_test.dart create mode 100644 test/fixtures/recording_app_toast.dart diff --git a/lib/features/base/model/ui_keys.dart b/lib/features/base/model/ui_keys.dart index 7165523c3..7bac332a5 100644 --- a/lib/features/base/model/ui_keys.dart +++ b/lib/features/base/model/ui_keys.dart @@ -41,6 +41,12 @@ class UiKeys { static const String userAvatar = 'userAvatar'; // Settings button that replaces the avatar inside Twake Workplace. static const String workplaceSettingsButton = 'workplace_settings_button'; + static const String viewingOwnerBanner = 'viewingOwnerBanner'; + static const String viewingOwnerBannerDismiss = 'viewingOwnerBannerDismiss'; + static const String accountSwitcherMenu = 'accountSwitcherMenu'; + static const String accountSwitchLoadingDialog = 'accountSwitchLoadingDialog'; + static const String accountSwitchLoadingCancel = + 'accountSwitchLoadingCancel'; // Advanced search static const String openAdvancedSearchButton = 'open_advanced_search_button'; diff --git a/lib/features/delegation/domain/extensions/session_delegation_extension.dart b/lib/features/delegation/domain/extensions/session_delegation_extension.dart index 25c10a5cd..e2f8a89f2 100644 --- a/lib/features/delegation/domain/extensions/session_delegation_extension.dart +++ b/lib/features/delegation/domain/extensions/session_delegation_extension.dart @@ -1,4 +1,5 @@ import 'package:jmap_dart_client/jmap/account_id.dart'; +import 'package:jmap_dart_client/jmap/core/account/account.dart'; import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart'; import 'package:jmap_dart_client/jmap/core/session/session.dart'; @@ -10,4 +11,10 @@ extension SessionDelegationExtension on Session { accounts[accountId]?.isPersonal == false; bool get supportsDelegation => capabilities.containsKey(delegationCapability); + + /// Owner mailboxes listed on this session (`isPersonal: false`). + Iterable> get delegatedAccounts => + accounts.entries.where((entry) => entry.value.isPersonal == false); + + bool get hasDelegatedAccounts => delegatedAccounts.isNotEmpty; } diff --git a/lib/features/delegation/presentation/model/shown_account_capabilities.dart b/lib/features/delegation/presentation/model/shown_account_capabilities.dart index b1acf57fe..bd8a72ca7 100644 --- a/lib/features/delegation/presentation/model/shown_account_capabilities.dart +++ b/lib/features/delegation/presentation/model/shown_account_capabilities.dart @@ -23,12 +23,16 @@ class ShownAccountCapabilities with EquatableMixin { /// account is shown, so mail sent from it never picks up my setting. final bool canApplyMyComposerSettings; + /// Viewing chrome (banner, stacked avatar, Delegated badge) while a delegated account is shown. + final bool showsViewingOwnerChrome; + const ShownAccountCapabilities._({ required this.canOpenSettings, required this.canCreateFilter, required this.canCreateDefaultFolders, required this.canUseNotificationKeychain, required this.canApplyMyComposerSettings, + required this.showsViewingOwnerChrome, }); factory ShownAccountCapabilities.of(ActiveAccount? shownAccount) { @@ -40,6 +44,7 @@ class ShownAccountCapabilities with EquatableMixin { canCreateDefaultFolders: personalShown, canUseNotificationKeychain: personalShown, canApplyMyComposerSettings: !delegatedShown, + showsViewingOwnerChrome: delegatedShown, ); } @@ -50,5 +55,6 @@ class ShownAccountCapabilities with EquatableMixin { canCreateDefaultFolders, canUseNotificationKeychain, canApplyMyComposerSettings, + showsViewingOwnerChrome, ]; } diff --git a/lib/features/delegation/presentation/providers/account_switch_view_factory_provider.dart b/lib/features/delegation/presentation/providers/account_switch_view_factory_provider.dart new file mode 100644 index 000000000..e822637af --- /dev/null +++ b/lib/features/delegation/presentation/providers/account_switch_view_factory_provider.dart @@ -0,0 +1,19 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:get/get.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/model/account_switch_view.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/view/dashboard_account_switch_view.dart'; +import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart'; +import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/delegates/getx_account_switch_dashboard.dart'; + +typedef AccountSwitchViewFactory = AccountSwitchView Function(BuildContext context); + +/// Builds the dashboard switch view for avatar menu / Viewing banner entry points. +final accountSwitchViewFactoryProvider = + Provider((ref) { + return (context) => DashboardAccountSwitchView( + ProviderScope.containerOf(context, listen: false), + GetXAccountSwitchDashboard(Get.find()), + Get.key, + ); +}); diff --git a/lib/features/delegation/presentation/utils/account_switch_failure_toast.dart b/lib/features/delegation/presentation/utils/account_switch_failure_toast.dart new file mode 100644 index 000000000..ce7f87984 --- /dev/null +++ b/lib/features/delegation/presentation/utils/account_switch_failure_toast.dart @@ -0,0 +1,46 @@ +import 'package:core/presentation/utils/app_toast.dart'; +import 'package:flutter/widgets.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/model/account_switch_result.dart'; +import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; + +/// Inputs for [showAccountSwitchFailureToast] (keeps the helper arity low). +class AccountSwitchFailureToastArgs { + final AccountSwitchResult result; + final String ownerDisplayName; + final bool returningToMine; + + const AccountSwitchFailureToastArgs({ + required this.result, + required this.ownerDisplayName, + this.returningToMine = false, + }); +} + +/// Error toast after a refused, failed, cancelled, or timed-out switch. +void showAccountSwitchFailureToast({ + required BuildContext context, + required AppToast appToast, + required AccountSwitchFailureToastArgs args, +}) { + final l10n = AppLocalizations.of(context); + final result = args.result; + + if (result is AccountSwitchRefused && + result.reason == AccountSwitchRefusal.composerOpen) { + appToast.showToastErrorMessage( + context, + l10n.closeComposerBeforeSwitchingAccounts, + ); + return; + } + + final shouldToast = result is AccountSwitchFailed || + result is AccountSwitchCancelled || + result is AccountSwitchTimedOut; + if (!shouldToast) return; + + final message = args.returningToMine + ? l10n.couldNotReturnToYourMailbox + : l10n.couldNotOpenOwnerMailbox(args.ownerDisplayName); + appToast.showToastErrorMessage(context, message); +} diff --git a/lib/features/delegation/presentation/utils/account_switcher_sections.dart b/lib/features/delegation/presentation/utils/account_switcher_sections.dart new file mode 100644 index 000000000..4efa46640 --- /dev/null +++ b/lib/features/delegation/presentation/utils/account_switcher_sections.dart @@ -0,0 +1,42 @@ +import 'package:flutter/widgets.dart'; +import 'package:jmap_dart_client/jmap/account_id.dart'; +import 'package:jmap_dart_client/jmap/core/session/session.dart'; +import 'package:linagora_design_flutter/linagora_design_flutter.dart'; +import 'package:model/extensions/session_extension.dart'; +import 'package:tmail_ui_user/features/delegation/domain/extensions/session_delegation_extension.dart'; +import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; + +typedef AccountSwitcherAvatarBuilder = Widget Function(String emailAddress); + +/// Account menu sections from the session (address-only in v1). +List> buildAccountSwitcherSections({ + required Session session, + required AppLocalizations l10n, + required AccountSwitcherAvatarBuilder avatarFor, +}) { + final ownAddress = session.getOwnEmailAddressOrEmpty(); + return [ + LinagoraAccountSection( + title: l10n.yourMailbox, + items: [ + LinagoraAccountItem( + id: session.accountId, + name: ownAddress, + // v1 is address-only. Omit email to avoid a duplicate line. + avatar: avatarFor(ownAddress), + ), + ], + ), + LinagoraAccountSection( + title: l10n.delegatedToYou, + items: [ + for (final entry in session.delegatedAccounts) + LinagoraAccountItem( + id: entry.key, + name: entry.value.name.value, + avatar: avatarFor(entry.value.name.value), + ), + ], + ), + ]; +} diff --git a/lib/features/delegation/presentation/utils/account_switcher_visibility.dart b/lib/features/delegation/presentation/utils/account_switcher_visibility.dart new file mode 100644 index 000000000..a256398f0 --- /dev/null +++ b/lib/features/delegation/presentation/utils/account_switcher_visibility.dart @@ -0,0 +1,9 @@ +import 'package:jmap_dart_client/jmap/core/session/session.dart'; +import 'package:tmail_ui_user/features/delegation/domain/extensions/session_delegation_extension.dart'; + +/// True when delegation is on and the session lists a delegated mailbox. +bool canShowAccountSwitcher({ + required bool delegationEnabled, + required Session? session, +}) => + delegationEnabled && session != null && session.hasDelegatedAccounts; diff --git a/lib/features/delegation/presentation/widgets/account_switcher_avatar.dart b/lib/features/delegation/presentation/widgets/account_switcher_avatar.dart new file mode 100644 index 000000000..255e5a978 --- /dev/null +++ b/lib/features/delegation/presentation/widgets/account_switcher_avatar.dart @@ -0,0 +1,17 @@ +import 'package:core/presentation/extensions/string_extension.dart'; +import 'package:core/presentation/views/image/avatar_builder.dart'; +import 'package:flutter/material.dart'; + +/// Round initials avatar sized for the account switcher chip or menu tile. +Widget accountSwitcherAvatar(String emailAddress, double size) { + return (AvatarBuilder() + ..text(emailAddress.firstCharacterToUpperCase) + ..size(size) + ..avatarColor(emailAddress.gradientColors) + ..addTextStyle(TextStyle( + color: Colors.white, + fontSize: size * 0.44, + fontWeight: FontWeight.w600, + ))) + .build(); +} diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb index 355935bc1..56c568bc7 100644 --- a/lib/l10n/intl_en.arb +++ b/lib/l10n/intl_en.arb @@ -3378,5 +3378,83 @@ "type": "text", "placeholders_order": [], "placeholders": {} + }, + "openingOwnerMailbox": "Opening {name}'s mailbox…", + "@openingOwnerMailbox": { + "type": "text", + "placeholders_order": [ + "name" + ], + "placeholders": { + "name": {} + } + }, + "openingYourMailbox": "Opening your mailbox…", + "@openingYourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "viewingOwnerMailbox": "Viewing {name}", + "@viewingOwnerMailbox": { + "type": "text", + "placeholders_order": [ + "name" + ], + "placeholders": { + "name": {} + } + }, + "yourMailbox": "Your mailbox", + "@yourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "delegatedToYou": "Delegated to you", + "@delegatedToYou": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "accountSwitcherMenu": "Account switcher", + "@accountSwitcherMenu": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "couldNotOpenOwnerMailbox": "Couldn't open {name}'s mailbox", + "@couldNotOpenOwnerMailbox": { + "type": "text", + "placeholders_order": [ + "name" + ], + "placeholders": { + "name": {} + } + }, + "couldNotReturnToYourMailbox": "Couldn't return to your mailbox", + "@couldNotReturnToYourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "closeComposerBeforeSwitchingAccounts": "Close the composer before switching accounts", + "@closeComposerBeforeSwitchingAccounts": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "delegatedMailbox": "Delegated mailbox", + "@delegatedMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "backToYourMailbox": "Back to your mailbox", + "@backToYourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} } } diff --git a/lib/l10n/intl_messages.arb b/lib/l10n/intl_messages.arb index 56ea81cfc..7434fcae9 100644 --- a/lib/l10n/intl_messages.arb +++ b/lib/l10n/intl_messages.arb @@ -1,5 +1,5 @@ { - "@@last_modified": "2026-11-04T10:09:40.401067", + "@@last_modified": "2026-11-10T01:26:08.509231", "initializing_data": "Initializing data...", "@initializing_data": { "type": "text", @@ -5876,6 +5876,34 @@ "placeholders_order": [], "placeholders": {} }, + "endedDelegationLostItems": "Access ended. {count} unsent or draft messages were removed.", + "@endedDelegationLostItems": { + "type": "text", + "placeholders_order": [ + "count" + ], + "placeholders": { + "count": {} + } + }, + "delegation": "Delegation", + "@delegation": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "delegationSettingExplanation": "Open the mailboxes other users delegated to you", + "@delegationSettingExplanation": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "delegationToggleDescription": "Enable delegation", + "@delegationToggleDescription": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, "openingOwnerMailbox": "Opening {name}'s mailbox…", "@openingOwnerMailbox": { "type": "text", @@ -5885,5 +5913,73 @@ "placeholders": { "name": {} } + }, + "openingYourMailbox": "Opening your mailbox…", + "@openingYourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "viewingOwnerMailbox": "Viewing {name}", + "@viewingOwnerMailbox": { + "type": "text", + "placeholders_order": [ + "name" + ], + "placeholders": { + "name": {} + } + }, + "yourMailbox": "Your mailbox", + "@yourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "delegatedToYou": "Delegated to you", + "@delegatedToYou": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "accountSwitcherMenu": "Account switcher", + "@accountSwitcherMenu": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "couldNotOpenOwnerMailbox": "Couldn't open {name}'s mailbox", + "@couldNotOpenOwnerMailbox": { + "type": "text", + "placeholders_order": [ + "name" + ], + "placeholders": { + "name": {} + } + }, + "couldNotReturnToYourMailbox": "Couldn't return to your mailbox", + "@couldNotReturnToYourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "closeComposerBeforeSwitchingAccounts": "Close the composer before switching accounts", + "@closeComposerBeforeSwitchingAccounts": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "delegatedMailbox": "Delegated mailbox", + "@delegatedMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} + }, + "backToYourMailbox": "Back to your mailbox", + "@backToYourMailbox": { + "type": "text", + "placeholders_order": [], + "placeholders": {} } -} +} \ No newline at end of file diff --git a/lib/main/localizations/app_localizations.dart b/lib/main/localizations/app_localizations.dart index 374c69866..073f0c7c0 100644 --- a/lib/main/localizations/app_localizations.dart +++ b/lib/main/localizations/app_localizations.dart @@ -6253,4 +6253,76 @@ class AppLocalizations { args: [name], ); } + + String get openingYourMailbox { + return Intl.message( + 'Opening your mailbox…', + name: 'openingYourMailbox', + ); + } + + String viewingOwnerMailbox(String name) { + return Intl.message( + 'Viewing $name', + name: 'viewingOwnerMailbox', + args: [name], + ); + } + + String get yourMailbox { + return Intl.message( + 'Your mailbox', + name: 'yourMailbox', + ); + } + + String get delegatedToYou { + return Intl.message( + 'Delegated to you', + name: 'delegatedToYou', + ); + } + + String get accountSwitcherMenu { + return Intl.message( + 'Account switcher', + name: 'accountSwitcherMenu', + ); + } + + String couldNotOpenOwnerMailbox(String name) { + return Intl.message( + "Couldn't open $name's mailbox", + name: 'couldNotOpenOwnerMailbox', + args: [name], + ); + } + + String get couldNotReturnToYourMailbox { + return Intl.message( + "Couldn't return to your mailbox", + name: 'couldNotReturnToYourMailbox', + ); + } + + String get closeComposerBeforeSwitchingAccounts { + return Intl.message( + 'Close the composer before switching accounts', + name: 'closeComposerBeforeSwitchingAccounts', + ); + } + + String get delegatedMailbox { + return Intl.message( + 'Delegated mailbox', + name: 'delegatedMailbox', + ); + } + + String get backToYourMailbox { + return Intl.message( + 'Back to your mailbox', + name: 'backToYourMailbox', + ); + } } diff --git a/test/features/delegation/presentation/model/shown_account_capabilities_test.dart b/test/features/delegation/presentation/model/shown_account_capabilities_test.dart index 705b020aa..ce88d87b8 100644 --- a/test/features/delegation/presentation/model/shown_account_capabilities_test.dart +++ b/test/features/delegation/presentation/model/shown_account_capabilities_test.dart @@ -16,6 +16,7 @@ void main() { canCreateDefaultFolders, canUseNotificationKeychain, canApplyMyComposerSettings, + showsViewingOwnerChrome, ) in [ ( 'my own account', @@ -25,6 +26,7 @@ void main() { true, true, true, + false, ), ( 'a delegated account', @@ -34,6 +36,7 @@ void main() { false, false, false, + true, ), ( 'no account yet', @@ -43,14 +46,16 @@ void main() { false, false, true, + false, ), ]) { test( '${canOpenSettings ? 'opens' : 'does not open'} Settings, ' '${canCreateFilter ? 'allows' : 'does not allow'} creating a filter, ' '${canCreateDefaultFolders ? 'creates' : 'does not create'} default folders, ' - '${canUseNotificationKeychain ? 'writes' : 'does not write'} the notification keychain and ' - '${canApplyMyComposerSettings ? 'applies' : 'does not apply'} my composer settings ' + '${canUseNotificationKeychain ? 'writes' : 'does not write'} the notification keychain, ' + '${canApplyMyComposerSettings ? 'applies' : 'does not apply'} my composer settings and ' + '${showsViewingOwnerChrome ? 'shows' : 'does not show'} Viewing owner chrome ' 'while $shown is shown', () { final capabilities = ShownAccountCapabilities.of(shownAccount); @@ -60,6 +65,7 @@ void main() { expect(capabilities.canCreateDefaultFolders, canCreateDefaultFolders); expect(capabilities.canUseNotificationKeychain, canUseNotificationKeychain); expect(capabilities.canApplyMyComposerSettings, canApplyMyComposerSettings); + expect(capabilities.showsViewingOwnerChrome, showsViewingOwnerChrome); }, ); } diff --git a/test/features/delegation/presentation/utils/account_switch_failure_toast_test.dart b/test/features/delegation/presentation/utils/account_switch_failure_toast_test.dart new file mode 100644 index 000000000..adf37f612 --- /dev/null +++ b/test/features/delegation/presentation/utils/account_switch_failure_toast_test.dart @@ -0,0 +1,126 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/model/account_switch_result.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/utils/account_switch_failure_toast.dart'; +import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; +import 'package:tmail_ui_user/main/localizations/app_localizations_delegate.dart'; +import 'package:tmail_ui_user/main/localizations/localization_service.dart'; + +import '../../../../fixtures/recording_app_toast.dart'; + +void main() { + late RecordingAppToast appToast; + + setUp(() { + appToast = RecordingAppToast(); + }); + + Future pumpWithContext(WidgetTester tester) async { + late BuildContext captured; + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: const [AppLocalizationsDelegate()], + supportedLocales: LocalizationService.supportedLocales, + locale: LocalizationService.defaultLocale, + home: Builder(builder: (context) { + captured = context; + return const SizedBox.shrink(); + }), + ), + ); + await tester.pump(); + return captured; + } + + group('showAccountSwitchFailureToast', () { + for (final result in [ + AccountSwitchFailed(Exception('load'), StackTrace.current), + const AccountSwitchCancelled(), + const AccountSwitchTimedOut(), + ]) { + testWidgets('toasts Could not open for ${result.runtimeType}', + (tester) async { + final context = await pumpWithContext(tester); + showAccountSwitchFailureToast( + context: context, + appToast: appToast.toast, + args: AccountSwitchFailureToastArgs( + result: result, + ownerDisplayName: 'bob@domain.tld', + ), + ); + + expect( + appToast.errorMessages, + [ + AppLocalizations.of(context) + .couldNotOpenOwnerMailbox('bob@domain.tld'), + ], + ); + }); + + testWidgets( + 'toasts return-to-mine copy for ${result.runtimeType}', + (tester) async { + final context = await pumpWithContext(tester); + showAccountSwitchFailureToast( + context: context, + appToast: appToast.toast, + args: AccountSwitchFailureToastArgs( + result: result, + ownerDisplayName: 'alice@domain.tld', + returningToMine: true, + ), + ); + + expect( + appToast.errorMessages, + [AppLocalizations.of(context).couldNotReturnToYourMailbox], + ); + expect( + appToast.errorMessages.single, + isNot(contains("alice@domain.tld's mailbox")), + ); + }); + } + + testWidgets('toasts close-composer copy when composer is open', + (tester) async { + final context = await pumpWithContext(tester); + showAccountSwitchFailureToast( + context: context, + appToast: appToast.toast, + args: const AccountSwitchFailureToastArgs( + result: AccountSwitchRefused(AccountSwitchRefusal.composerOpen), + ownerDisplayName: 'bob@domain.tld', + ), + ); + + expect( + appToast.errorMessages, + [AppLocalizations.of(context).closeComposerBeforeSwitchingAccounts], + ); + }); + + for (final result in [ + const AccountSwitched(), + const AccountSwitchRefused(AccountSwitchRefusal.accountAlreadyShown), + const AccountSwitchRefused(AccountSwitchRefusal.switchInProgress), + const AccountSwitchRefused(AccountSwitchRefusal.accountNotListed), + ]) { + testWidgets('stays silent for $result', (tester) async { + final context = await pumpWithContext(tester); + showAccountSwitchFailureToast( + context: context, + appToast: appToast.toast, + args: AccountSwitchFailureToastArgs( + result: result, + ownerDisplayName: 'bob@domain.tld', + ), + ); + + expect(appToast.errorMessages, isEmpty); + }); + } + }); +} diff --git a/test/features/delegation/presentation/utils/account_switcher_sections_test.dart b/test/features/delegation/presentation/utils/account_switcher_sections_test.dart new file mode 100644 index 000000000..d40e5e4a1 --- /dev/null +++ b/test/features/delegation/presentation/utils/account_switcher_sections_test.dart @@ -0,0 +1,51 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/utils/account_switcher_sections.dart'; +import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; + +import '../../../../fixtures/delegation_fixtures.dart'; + +void main() { + late AppLocalizations l10n; + + setUpAll(() { + l10n = AppLocalizations(); + }); + + Widget avatar(String email) => Text(email); + + group('buildAccountSwitcherSections', () { + test('puts my mailbox first and every delegated account under Delegated to you', () { + final sections = buildAccountSwitcherSections( + session: DelegationFixtures.session(), + l10n: l10n, + avatarFor: avatar, + ); + + expect(sections, hasLength(2)); + expect(sections[0].title, l10n.yourMailbox); + expect(sections[0].items.single.id, DelegationFixtures.aliceAccountId); + expect(sections[0].items.single.name, DelegationFixtures.aliceAddress); + + expect(sections[1].title, l10n.delegatedToYou); + expect( + sections[1].items.map((item) => item.id), + [DelegationFixtures.bobAccountId, DelegationFixtures.carolAccountId], + ); + expect( + sections[1].items.map((item) => item.name), + [DelegationFixtures.bobAddress, DelegationFixtures.carolAddress], + ); + }); + + test('omits the delegated section items when the session lists none', () { + final sections = buildAccountSwitcherSections( + session: DelegationFixtures.session(listsBob: false, listsCarol: false), + l10n: l10n, + avatarFor: avatar, + ); + + expect(sections[1].items, isEmpty); + }); + }); +} diff --git a/test/features/delegation/presentation/utils/account_switcher_visibility_test.dart b/test/features/delegation/presentation/utils/account_switcher_visibility_test.dart new file mode 100644 index 000000000..684e488a8 --- /dev/null +++ b/test/features/delegation/presentation/utils/account_switcher_visibility_test.dart @@ -0,0 +1,45 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/utils/account_switcher_visibility.dart'; + +import '../../../../fixtures/delegation_fixtures.dart'; + +void main() { + group('canShowAccountSwitcher', () { + test('is false when delegation is off even with delegated accounts', () { + expect( + canShowAccountSwitcher( + delegationEnabled: false, + session: DelegationFixtures.session(), + ), + isFalse, + ); + }); + + test('is false when the session is null', () { + expect( + canShowAccountSwitcher(delegationEnabled: true, session: null), + isFalse, + ); + }); + + test('is false when delegation is on but no delegated account is listed', () { + expect( + canShowAccountSwitcher( + delegationEnabled: true, + session: DelegationFixtures.session(listsBob: false, listsCarol: false), + ), + isFalse, + ); + }); + + test('is true when delegation is on and a delegated account is listed', () { + expect( + canShowAccountSwitcher( + delegationEnabled: true, + session: DelegationFixtures.session(listsBob: true, listsCarol: false), + ), + isTrue, + ); + }); + }); +} diff --git a/test/features/delegation/presentation/widgets/account_switcher_avatar_test.dart b/test/features/delegation/presentation/widgets/account_switcher_avatar_test.dart new file mode 100644 index 000000000..bafac1ed8 --- /dev/null +++ b/test/features/delegation/presentation/widgets/account_switcher_avatar_test.dart @@ -0,0 +1,26 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:tmail_ui_user/features/delegation/presentation/widgets/account_switcher_avatar.dart'; + +void main() { + testWidgets('sizes the avatar and initials to the given diameter', + (tester) async { + const size = 40.0; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: accountSwitcherAvatar('bob@example.com', size), + ), + ), + ); + + expect(find.text('B'), findsOneWidget); + + final avatarFinder = + find.ancestor(of: find.text('B'), matching: find.byType(Container)).first; + expect(tester.getSize(avatarFinder), const Size(size, size)); + + final initials = tester.widget(find.text('B')); + expect(initials.style?.fontSize, size * 0.44); + }); +} diff --git a/test/fixtures/recording_app_toast.dart b/test/fixtures/recording_app_toast.dart new file mode 100644 index 000000000..7ecfeb0b0 --- /dev/null +++ b/test/fixtures/recording_app_toast.dart @@ -0,0 +1,19 @@ +import 'package:core/presentation/utils/app_toast.dart'; +import 'package:mockito/mockito.dart'; + +import '../features/delegation/presentation/session_from_cache_to_dashboard_test.mocks.dart'; + +/// Captures [AppToast.showToastErrorMessage] text via a generated mock so test +/// files do not hand-write a 5-arg override (CodeScene). +class RecordingAppToast { + final List errorMessages = []; + final MockAppToast _toast = MockAppToast(); + + RecordingAppToast() { + when(_toast.showToastErrorMessage(any, any)).thenAnswer((invocation) { + errorMessages.add(invocation.positionalArguments[1] as String); + }); + } + + AppToast get toast => _toast; +}