Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][locker] Preserve whitespace in info items (#13618)

aman-pilot77244be9af1b
diff --git a/mobile/apps/locker/changes/info-field-whitespace.md b/mobile/apps/locker/changes/info-field-whitespace.md
new file mode 100644
index 0000000000..b1c62fbb39
--- /dev/null
+++ b/mobile/apps/locker/changes/info-field-whitespace.md
@@ -0,0 +1 @@
+- Preserved entered whitespace in info items when saving, editing, viewing and copying, including whitespace-only required fields.
diff --git a/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart b/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart
index 76e3035fc9..054d4f6f47 100644
--- a/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/account_credentials_page.dart
@@ -64,12 +64,12 @@ class _AccountCredentialsPageState
return context.strings.accountCredentials;
}
- final controllerName = _nameController.text.trim();
+ final controllerName = _nameController.text;
if (controllerName.isNotEmpty) {
return controllerName;
}
- final dataName = (currentData?.name ?? '').trim();
+ final dataName = currentData?.name ?? '';
if (dataName.isNotEmpty) {
return dataName;
}
@@ -85,26 +85,24 @@ class _AccountCredentialsPageState
@override
bool validateForm() {
- return _nameController.text.trim().isNotEmpty;
+ return _nameController.text.isNotEmpty;
}
@override
bool get hasUnsavedChanges {
- return _nameController.text.trim() != _initialName.trim() ||
- _usernameController.text.trim() != _initialUsername.trim() ||
+ return _nameController.text != _initialName ||
+ _usernameController.text != _initialUsername ||
_passwordController.text != _initialPassword ||
- _notesController.text.trim() != _initialNotes.trim();
+ _notesController.text != _initialNotes;
}
@override
AccountCredentialData createInfoData() {
return AccountCredentialData(
- name: _nameController.text.trim(),
- username: _usernameController.text.trim(),
+ name: _nameController.text,
+ username: _usernameController.text,
password: _passwordController.text,
- notes: _notesController.text.trim().isEmpty
- ? null
- : _notesController.text.trim(),
+ notes: _notesController.text.isEmpty ? null : _notesController.text,
);
}
@@ -160,7 +158,7 @@ class _AccountCredentialsPageState
final fields = <Widget>[];
- if (usernameText.trim().isNotEmpty) {
+ if (usernameText.isNotEmpty) {
fields.add(
buildViewField(label: context.strings.username, value: usernameText),
);
@@ -177,7 +175,7 @@ class _AccountCredentialsPageState
);
}
- if (notesText.trim().isNotEmpty) {
+ if (notesText.isNotEmpty) {
if (fields.isNotEmpty) fields.add(const SizedBox(height: 24));
fields.add(
buildViewField(
diff --git a/mobile/apps/locker/lib/ui/pages/emergency_contact_page.dart b/mobile/apps/locker/lib/ui/pages/emergency_contact_page.dart
index 64d466851a..f6252aa5ab 100644
--- a/mobile/apps/locker/lib/ui/pages/emergency_contact_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/emergency_contact_page.dart
@@ -62,26 +62,23 @@ class _EmergencyContactPageState
@override
bool validateForm() {
- return _nameController.text.trim().isNotEmpty &&
- _contactDetailsController.text.trim().isNotEmpty;
+ return _nameController.text.isNotEmpty &&
+ _contactDetailsController.text.isNotEmpty;
}
@override
bool get hasUnsavedChanges {
- return _nameController.text.trim() != _initialName.trim() ||
- _contactDetailsController.text.trim() !=
- _initialContactDetails.trim() ||
- _notesController.text.trim() != _initialNotes.trim();
+ return _nameController.text != _initialName ||
+ _contactDetailsController.text != _initialContactDetails ||
+ _notesController.text != _initialNotes;
}
@override
EmergencyContactData createInfoData() {
return EmergencyContactData(
- name: _nameController.text.trim(),
- contactDetails: _contactDetailsController.text.trim(),
- notes: _notesController.text.trim().isEmpty
- ? null
- : _notesController.text.trim(),
+ name: _nameController.text,
+ contactDetails: _contactDetailsController.text,
+ notes: _notesController.text.isEmpty ? null : _notesController.text,
);
}
@@ -132,7 +129,7 @@ class _EmergencyContactPageState
label: context.strings.contactDetails,
value: _contactDetailsController.text,
),
- if (_notesController.text.trim().isNotEmpty) ...[
+ if (_notesController.text.isNotEmpty) ...[
const SizedBox(height: 24),
buildViewField(
label: context.strings.contactNotes,
diff --git a/mobile/apps/locker/lib/ui/pages/personal_note_page.dart b/mobile/apps/locker/lib/ui/pages/personal_note_page.dart
index b8d4b671ed..e6079c2c9f 100644
--- a/mobile/apps/locker/lib/ui/pages/personal_note_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/personal_note_page.dart
@@ -58,12 +58,12 @@ class _PersonalNotePageState
return context.strings.note;
}
- final controllerTitle = _nameController.text.trim();
+ final controllerTitle = _nameController.text;
if (controllerTitle.isNotEmpty) {
return controllerTitle;
}
- final dataTitle = (currentData?.title ?? '').trim();
+ final dataTitle = currentData?.title ?? '';
if (dataTitle.isNotEmpty) {
return dataTitle;
}
@@ -79,14 +79,14 @@ class _PersonalNotePageState
@override
bool validateForm() {
- final content = _contentController.text.trim();
+ final content = _contentController.text;
return content.isNotEmpty;
}
@override
PersonalNoteData createInfoData() {
- final content = _contentController.text.trim();
- var title = _nameController.text.trim();
+ final content = _contentController.text;
+ var title = _nameController.text;
if (title.isEmpty && content.isNotEmpty) {
title = _generateTitleFromContent(content);
@@ -100,7 +100,7 @@ class _PersonalNotePageState
int maxWords = 5,
int maxLength = 40,
}) {
- final firstLine = content.split('\n').first.trim();
+ final firstLine = content.trim().split('\n').first.trim();
if (firstLine.isEmpty) return '';
final words = firstLine.split(RegExp(r'\s+'));
@@ -118,7 +118,7 @@ class _PersonalNotePageState
@override
List<Widget> buildViewFields() {
- if (_contentController.text.trim().isEmpty) {
+ if (_contentController.text.isEmpty) {
return const <Widget>[];
}
return [
@@ -207,8 +207,8 @@ class _PersonalNotePageState
@override
bool get hasUnsavedChanges {
- return _contentController.text.trim() != _initialContent.trim() ||
- _nameController.text.trim() != _initialTitle.trim();
+ return _contentController.text != _initialContent ||
+ _nameController.text != _initialTitle;
}
@override
diff --git a/mobile/apps/locker/lib/ui/pages/physical_records_page.dart b/mobile/apps/locker/lib/ui/pages/physical_records_page.dart
index 33264e8971..ee0f88f271 100644
--- a/mobile/apps/locker/lib/ui/pages/physical_records_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/physical_records_page.dart
@@ -59,12 +59,12 @@ class _PhysicalRecordsPageState
return context.strings.physicalRecords;
}
- final controllerName = _nameController.text.trim();
+ final controllerName = _nameController.text;
if (controllerName.isNotEmpty) {
return controllerName;
}
- final dataName = (currentData?.name ?? '').trim();
+ final dataName = currentData?.name ?? '';
if (dataName.isNotEmpty) {
return dataName;
}
@@ -80,24 +80,22 @@ class _PhysicalRecordsPageState
@override
bool validateForm() {
- return _nameController.text.trim().isNotEmpty;
+ return _nameController.text.isNotEmpty;
}
@override
bool get hasUnsavedChanges {
- return _nameController.text.trim() != _initialName.trim() ||
- _locationController.text.trim() != _initialLocation.trim() ||
- _notesController.text.trim() != _initialNotes.trim();
+ return _nameController.text != _initialName ||
+ _locationController.text != _initialLocation ||
+ _notesController.text != _initialNotes;
}
@override
PhysicalRecordData createInfoData() {
return PhysicalRecordData(
- name: _nameController.text.trim(),
- location: _locationController.text.trim(),
- notes: _notesController.text.trim().isEmpty
- ? null
- : _notesController.text.trim(),
+ name: _nameController.text,
+ location: _locationController.text,
+ notes: _notesController.text.isEmpty ? null : _notesController.text,
);
}
@@ -141,7 +139,7 @@ class _PhysicalRecordsPageState
List<Widget> buildViewFields() {
final fields = <Widget>[];
- if (_locationController.text.trim().isNotEmpty) {
+ if (_locationController.text.isNotEmpty) {
fields.add(
buildViewField(
label: context.strings.recordLocation,
@@ -150,7 +148,7 @@ class _PhysicalRecordsPageState
);
}
- if (_notesController.text.trim().isNotEmpty) {
+ if (_notesController.text.isNotEmpty) {
if (fields.isNotEmpty) fields.add(const SizedBox(height: 24));
fields.add(
buildViewField(
diff --git a/mobile/apps/locker/test/ui/pages/required_info_fields_test.dart b/mobile/apps/locker/test/ui/pages/required_info_fields_test.dart
index 7e3c68a00f..65ef43795f 100644
--- a/mobile/apps/locker/test/ui/pages/required_info_fields_test.dart
+++ b/mobile/apps/locker/test/ui/pages/required_info_fields_test.dart
@@ -42,9 +42,17 @@ void main() {
);
}
+ Finder copyTarget(TextInputComponent field) => find.descendant(
+ of: find.byWidget(field),
+ matching: find.byWidgetPredicate(
+ (widget) =>
+ widget is GestureDetector && widget.onTap == field.onSuffixTap,
+ ),
+ );
+
EnteFile partialFile(String type, Map<String, dynamic> data) => EnteFile()
..fileType = FileType.info
- ..title = data['name'] as String
+ ..title = (data['name'] ?? data['title']) as String
..pubMagicMetadata = PubMagicMetadata(
info: {'type': type, 'data': data},
noThumb: true,
@@ -66,7 +74,7 @@ void main() {
expect(state.createInfoData().username, isEmpty);
expect(state.createInfoData().password, isEmpty);
- await tester.enterText(find.byType(TextField).first, ' ');
+ await tester.enterText(find.byType(TextField).first, '');
expect(state.validateForm(), isFalse);
});
@@ -85,7 +93,7 @@ void main() {
expect(state.validateForm(), isTrue);
expect(state.createInfoData().location, isEmpty);
- await tester.enterText(find.byType(TextField).first, ' ');
+ await tester.enterText(find.byType(TextField).first, '');
expect(state.validateForm(), isFalse);
});
@@ -102,9 +110,9 @@ void main() {
tester.state(find.byType(AccountCredentialsPage)) as dynamic;
final data = state.createInfoData() as AccountCredentialData;
expect(data.password, password);
- expect(data.name, 'Account');
- expect(data.username, 'username');
- expect(data.notes, 'notes');
+ expect(data.name, ' Account ');
+ expect(data.username, ' username ');
+ expect(data.notes, ' notes ');
final file = partialFile(
'accountCredential',
jsonDecode(jsonEncode(data.toJson())) as Map<String, dynamic>,
@@ -257,7 +265,7 @@ void main() {
null,
);
});
- await tester.tap(find.byWidget(field.suffix!));
+ await tester.tap(copyTarget(field));
await tester.pump();
expect(copied, name);
await tester.pumpWidget(const SizedBox.shrink());
@@ -319,7 +327,7 @@ void main() {
expect(state.validateForm(), isTrue);
expect(state.createInfoData().title, 'Remember the spare key');
- await tester.enterText(find.byType(TextField).at(1), ' ');
+ await tester.enterText(find.byType(TextField).at(1), '');
await tester.enterText(find.byType(TextField).first, 'Manual title');
expect(state.validateForm(), isFalse);
});
@@ -355,11 +363,14 @@ void main() {
) async {
await showPage(tester, const PersonalNotePage());
final state = tester.state(find.byType(PersonalNotePage)) as dynamic;
- await tester.enterText(
- find.byType(TextField).at(1),
+ for (final content in [
' one two three four five six\nsecond line',
- );
- expect(state.createInfoData().title, 'one two three four five');
+ '\n\n one two three four five six\nsecond line\n',
+ ]) {
+ await tester.enterText(find.byType(TextField).at(1), content);
+ expect(state.createInfoData().title, 'one two three four five');
+ expect(state.createInfoData().content, content);
+ }
const manualTitle = 'My πŸ‘©πŸ½β€πŸ’» note title';
await tester.enterText(find.byType(TextField).first, manualTitle);
@@ -394,4 +405,175 @@ void main() {
content,
);
});
+
+ final infoPages = [
+ (
+ type: 'accountCredential',
+ page: (EnteFile? file, InfoPageMode mode) =>
+ AccountCredentialsPage(existingFile: file, mode: mode),
+ keys: ['name', 'username', 'password', 'notes'],
+ requiredIndices: [0],
+ visibleKeys: ['username', 'notes'],
+ titleKey: 'name',
+ ),
+ (
+ type: 'physicalRecord',
+ page: (EnteFile? file, InfoPageMode mode) =>
+ PhysicalRecordsPage(existingFile: file, mode: mode),
+ keys: ['name', 'location', 'notes'],
+ requiredIndices: [0],
+ visibleKeys: ['location', 'notes'],
+ titleKey: 'name',
+ ),
+ (
+ type: 'emergencyContact',
+ page: (EnteFile? file, InfoPageMode mode) =>
+ EmergencyContactPage(existingFile: file, mode: mode),
+ keys: ['name', 'contactDetails', 'notes'],
+ requiredIndices: [0, 1],
+ visibleKeys: ['name', 'contactDetails', 'notes'],
+ titleKey: '',
+ ),
+ (
+ type: 'note',
+ page: (EnteFile? file, InfoPageMode mode) =>
+ PersonalNotePage(existingFile: file, mode: mode),
+ keys: ['title', 'content'],
+ requiredIndices: [1],
+ visibleKeys: ['content'],
+ titleKey: 'title',
+ ),
+ ];
+
+ for (final info in infoPages) {
+ testWidgets('${info.type} rejects only exactly empty required fields', (
+ tester,
+ ) async {
+ final page = info.page(null, InfoPageMode.edit);
+ await showPage(tester, page);
+ final state = tester.state(find.byType(page.runtimeType)) as dynamic;
+ for (final index in info.requiredIndices) {
+ await tester.enterText(find.byType(TextField).at(index), ' \t\u00a0 ');
+ }
+ expect(state.validateForm(), isTrue);
+ for (final index in info.requiredIndices) {
+ await tester.enterText(find.byType(TextField).at(index), '');
+ expect(state.validateForm(), isFalse);
+ await tester.enterText(find.byType(TextField).at(index), ' \t\u00a0 ');
+ expect(state.validateForm(), isTrue);
+ }
+ });
+
+ for (final value in [
+ ' entered text ',
+ ' \t\u00a0 ',
+ 'line one\nline two\n',
+ ]) {
+ testWidgets('${info.type} saves and reopens exact text "$value"', (
+ tester,
+ ) async {
+ final page = info.page(null, InfoPageMode.edit);
+ await showPage(tester, page);
+ final expected = {
+ for (final key in info.keys)
+ key: value.contains('\n') && key != 'notes' && key != 'content'
+ ? ' entered text '
+ : value,
+ };
+ for (var index = 0; index < info.keys.length; index++) {
+ await tester.enterText(
+ find.byType(TextField).at(index),
+ expected[info.keys[index]]!,
+ );
+ }
+ final state = tester.state(find.byType(page.runtimeType)) as dynamic;
+ final data = state.createInfoData() as InfoData;
+ expect(data.toJson(), expected);
+ final file = partialFile(
+ info.type,
+ jsonDecode(jsonEncode(data.toJson())) as Map<String, dynamic>,
+ );
+ await tester.pumpWidget(const SizedBox.shrink());
+ final reopenedPage = info.page(file, InfoPageMode.edit);
+ await showPage(tester, reopenedPage);
+ for (var index = 0; index < info.keys.length; index++) {
+ expect(
+ tester
+ .widget<TextField>(find.byType(TextField).at(index))
+ .controller!
+ .text,
+ expected[info.keys[index]],
+ );
+ }
+ final reopened = tester.state(find.byType(page.runtimeType)) as dynamic;
+ expect(reopened.hasUnsavedChanges, isFalse);
+ expect((reopened.createInfoData() as InfoData).toJson(), expected);
+ });
+
+ testWidgets('${info.type} displays and copies exact text "$value"', (
+ tester,
+ ) async {
+ final data = {for (final key in info.keys) key: value};
+ final page = info.page(partialFile(info.type, data), InfoPageMode.view);
+ await showPage(tester, page);
+ final state = tester.state(find.byType(page.runtimeType)) as dynamic;
+ if (info.titleKey.isNotEmpty) {
+ expect(state.pageTitle, value);
+ }
+ final fields = tester
+ .widgetList<TextInputComponent>(find.byType(TextInputComponent))
+ .where((field) => field.readOnly && field.onSuffixTap != null)
+ .toList();
+ expect(fields.length, info.visibleKeys.length);
+ String? copied;
+ tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
+ SystemChannels.platform,
+ (call) async {
+ if (call.method == 'Clipboard.setData') {
+ copied = (call.arguments as Map)['text'] as String;
+ }
+ return null;
+ },
+ );
+ addTearDown(() {
+ tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
+ SystemChannels.platform,
+ null,
+ );
+ });
+ for (final field in fields) {
+ expect(field.initialValue, value);
+ final copyButton = copyTarget(field);
+ await tester.ensureVisible(copyButton);
+ await tester.pumpAndSettle();
+ copied = null;
+ await tester.tap(copyButton);
+ await tester.pump();
+ expect(copied, value);
+ }
+ await tester.pumpWidget(const SizedBox.shrink());
+ await tester.pump(const Duration(seconds: 3));
+ });
+ }
+
+ testWidgets('${info.type} detects whitespace edits to every field', (
+ tester,
+ ) async {
+ final page = info.page(
+ partialFile(info.type, {for (final key in info.keys) key: 'value'}),
+ InfoPageMode.edit,
+ );
+ await showPage(tester, page);
+ final state = tester.state(find.byType(page.runtimeType)) as dynamic;
+ expect(state.hasUnsavedChanges, isFalse);
+ for (var index = 0; index < info.keys.length; index++) {
+ for (final value in [' value', 'value ', '\u00a0value\t']) {
+ await tester.enterText(find.byType(TextField).at(index), value);
+ expect(state.hasUnsavedChanges, isTrue, reason: info.keys[index]);
+ await tester.enterText(find.byType(TextField).at(index), 'value');
+ expect(state.hasUnsavedChanges, isFalse);
+ }
+ }
+ });
+ }
}