Skip to content

Commit

Permalink
fix: default text direction not synced (#5405)
Browse files Browse the repository at this point in the history
* fix: default text direction not synced

* test: annotate visibleForTesting
  • Loading branch information
zoli authored May 24, 2024
1 parent 13a3567 commit 13b3439
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class SettingsWorkspaceView extends StatelessWidget {
title:
LocaleKeys.settings_workspacePage_textDirection_title.tr(),
children: const [
_TextDirectionSelect(),
TextDirectionSelect(),
EnableRTLItemsSwitcher(),
],
),
Expand Down Expand Up @@ -387,19 +387,25 @@ class _WorkspaceIconSetting extends StatelessWidget {
}
}

class _TextDirectionSelect extends StatelessWidget {
const _TextDirectionSelect();
@visibleForTesting
class TextDirectionSelect extends StatelessWidget {
const TextDirectionSelect({super.key});

@override
Widget build(BuildContext context) {
return BlocBuilder<AppearanceSettingsCubit, AppearanceSettingsState>(
builder: (context, state) {
final selectedItem = state.textDirection ?? AppFlowyTextDirection.auto;
final selectedItem = state.textDirection ?? AppFlowyTextDirection.ltr;

return SettingsRadioSelect<AppFlowyTextDirection>(
onChanged: (item) => context
.read<AppearanceSettingsCubit>()
.setTextDirection(item.value),
onChanged: (item) {
context
.read<AppearanceSettingsCubit>()
.setTextDirection(item.value);
context
.read<DocumentAppearanceCubit>()
.syncDefaultTextDirection(item.value.name);
},
items: [
SettingsRadioItem(
value: AppFlowyTextDirection.ltr,
Expand Down
159 changes: 159 additions & 0 deletions frontend/appflowy_flutter/test/widget_test/direction_setting_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import 'package:appflowy/workspace/presentation/settings/pages/settings_workspace_view.dart';
import 'package:appflowy/workspace/presentation/settings/shared/settings_radio_select.dart';
import 'package:flowy_infra/theme.dart';
import 'package:flutter/material.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/application/document_appearance_cubit.dart';
import 'package:appflowy/workspace/application/settings/appearance/appearance_cubit.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:appflowy_backend/protobuf/flowy-user/user_setting.pb.dart';
import 'package:appflowy/user/application/user_settings_service.dart';

import '../util.dart';

class MockAppearanceSettingsBloc
extends MockBloc<AppearanceSettingsCubit, AppearanceSettingsState>
implements AppearanceSettingsCubit {}

class MockDocumentAppearanceCubit extends Mock
implements DocumentAppearanceCubit {}

class MockDocumentAppearance extends Mock implements DocumentAppearance {}

void main() {
late AppearanceSettingsPB appearanceSettings;
late DateTimeSettingsPB dateTimeSettings;

setUp(() async {
await AppFlowyUnitTest.ensureInitialized();
appearanceSettings =
await UserSettingsBackendService().getAppearanceSetting();
dateTimeSettings = await UserSettingsBackendService().getDateTimeSettings();
});

testWidgets('TextDirectionSelect update default text direction setting',
(WidgetTester tester) async {
final appearanceSettingsState = AppearanceSettingsState.initial(
AppTheme.fallback,
appearanceSettings.themeMode,
appearanceSettings.font,
appearanceSettings.monospaceFont,
appearanceSettings.layoutDirection,
appearanceSettings.textDirection,
appearanceSettings.enableRtlToolbarItems,
appearanceSettings.locale,
appearanceSettings.isMenuCollapsed,
appearanceSettings.menuOffset,
dateTimeSettings.dateFormat,
dateTimeSettings.timeFormat,
dateTimeSettings.timezoneId,
appearanceSettings.documentSetting.cursorColor.isEmpty
? null
: Color(
int.parse(appearanceSettings.documentSetting.cursorColor),
),
appearanceSettings.documentSetting.selectionColor.isEmpty
? null
: Color(
int.parse(
appearanceSettings.documentSetting.selectionColor,
),
),
1.0,
);
final mockAppearanceSettingsBloc = MockAppearanceSettingsBloc();
when(() => mockAppearanceSettingsBloc.state).thenReturn(
appearanceSettingsState,
);

final mockDocumentAppearanceCubit = MockDocumentAppearanceCubit();
when(() => mockDocumentAppearanceCubit.stream).thenAnswer(
(_) => Stream.fromIterable([MockDocumentAppearance()]),
);

await tester.pumpWidget(
MultiBlocProvider(
providers: [
BlocProvider<AppearanceSettingsCubit>.value(
value: mockAppearanceSettingsBloc,
),
BlocProvider<DocumentAppearanceCubit>.value(
value: mockDocumentAppearanceCubit,
),
],
child: MaterialApp(
theme: appearanceSettingsState.lightTheme,
home: MultiBlocProvider(
providers: [
BlocProvider<AppearanceSettingsCubit>.value(
value: mockAppearanceSettingsBloc,
),
BlocProvider<DocumentAppearanceCubit>.value(
value: mockDocumentAppearanceCubit,
),
],
child: const Scaffold(
body: TextDirectionSelect(),
),
),
),
),
);
await tester.pumpAndSettle();

expect(
find.text(
LocaleKeys.settings_workspacePage_textDirection_leftToRight.tr(),
),
findsOne,
);
expect(
find.text(
LocaleKeys.settings_workspacePage_textDirection_rightToLeft.tr(),
),
findsOne,
);
expect(
find.text(
LocaleKeys.settings_workspacePage_textDirection_auto.tr(),
),
findsOne,
);

final radioSelectFinder =
find.byType(SettingsRadioSelect<AppFlowyTextDirection>);
expect(radioSelectFinder, findsOne);

when(
() => mockAppearanceSettingsBloc.setTextDirection(
any<AppFlowyTextDirection?>(),
),
).thenAnswer((_) async => {});
when(
() => mockDocumentAppearanceCubit.syncDefaultTextDirection(
any<String?>(),
),
).thenAnswer((_) async {});

final radioSelect = tester.widget(radioSelectFinder)
as SettingsRadioSelect<AppFlowyTextDirection>;
final rtlSelect = radioSelect.items
.firstWhere((select) => select.value == AppFlowyTextDirection.rtl);
radioSelect.onChanged(rtlSelect);

verify(
() => mockAppearanceSettingsBloc.setTextDirection(
any<AppFlowyTextDirection?>(),
),
).called(1);
verify(
() => mockDocumentAppearanceCubit.syncDefaultTextDirection(
any<String?>(),
),
).called(1);
});
}

0 comments on commit 13b3439

Please sign in to comment.