Skip to content

Commit ce6817b

Browse files
Paulik8bleroux
andauthored
Added equals and hashCode for TextInputConfiguration and AutofillConfiguration (#162238)
- Added equals and hashCode for `TextInputConfiguration` and `AutofillConfiguration` (fixed flutter/flutter#139033) - Fixed copyWith method in `TextInputConfiguration` (fixed flutter/flutter#162236) - Added some tests <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Bruno Leroux <[email protected]>
1 parent 211d83d commit ce6817b

File tree

4 files changed

+403
-0
lines changed

4 files changed

+403
-0
lines changed

packages/flutter/lib/src/services/autofill.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,45 @@ class AutofillConfiguration {
737737
}
738738
: null;
739739
}
740+
741+
@override
742+
bool operator ==(Object other) {
743+
if (identical(this, other)) {
744+
return true;
745+
}
746+
if (other.runtimeType != runtimeType) {
747+
return false;
748+
}
749+
return other is AutofillConfiguration &&
750+
other.enabled == enabled &&
751+
other.uniqueIdentifier == uniqueIdentifier &&
752+
listEquals(other.autofillHints, autofillHints) &&
753+
other.currentEditingValue == currentEditingValue &&
754+
other.hintText == hintText;
755+
}
756+
757+
@override
758+
int get hashCode {
759+
return Object.hash(
760+
enabled,
761+
uniqueIdentifier,
762+
Object.hashAll(autofillHints),
763+
currentEditingValue,
764+
hintText,
765+
);
766+
}
767+
768+
@override
769+
String toString() {
770+
final List<String> description = <String>[
771+
'enabled: $enabled',
772+
'uniqueIdentifier: $uniqueIdentifier',
773+
'autofillHints: $autofillHints',
774+
'currentEditingValue: $currentEditingValue',
775+
if (hintText != null) 'hintText: $hintText',
776+
];
777+
return 'AutofillConfiguration(${description.join(', ')})';
778+
}
740779
}
741780

742781
/// An object that represents an autofillable input field in the autofill workflow.

packages/flutter/lib/src/services/text_input.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export 'package:vector_math/vector_math_64.dart' show Matrix4;
4444

4545
export 'autofill.dart' show AutofillConfiguration, AutofillScope;
4646
export 'text_editing.dart' show TextSelection;
47+
4748
// TODO(a14n): the following export leads to Segmentation fault, see https://github.com/flutter/flutter/issues/106332
4849
// export 'text_editing_delta.dart' show TextEditingDelta;
4950

@@ -713,6 +714,7 @@ class TextInputConfiguration {
713714
smartQuotesType: smartQuotesType ?? this.smartQuotesType,
714715
enableSuggestions: enableSuggestions ?? this.enableSuggestions,
715716
enableInteractiveSelection: enableInteractiveSelection ?? this.enableInteractiveSelection,
717+
actionLabel: actionLabel ?? this.actionLabel,
716718
inputAction: inputAction ?? this.inputAction,
717719
textCapitalization: textCapitalization ?? this.textCapitalization,
718720
keyboardAppearance: keyboardAppearance ?? this.keyboardAppearance,
@@ -772,6 +774,81 @@ class TextInputConfiguration {
772774
'enableDeltaModel': enableDeltaModel,
773775
};
774776
}
777+
778+
@override
779+
bool operator ==(Object other) {
780+
if (identical(this, other)) {
781+
return true;
782+
}
783+
if (other.runtimeType != runtimeType) {
784+
return false;
785+
}
786+
return other is TextInputConfiguration &&
787+
other.viewId == viewId &&
788+
other.inputType == inputType &&
789+
other.readOnly == readOnly &&
790+
other.obscureText == obscureText &&
791+
other.autocorrect == autocorrect &&
792+
other.smartDashesType == smartDashesType &&
793+
other.smartQuotesType == smartQuotesType &&
794+
other.enableSuggestions == enableSuggestions &&
795+
other.enableInteractiveSelection == enableInteractiveSelection &&
796+
other.actionLabel == actionLabel &&
797+
other.inputAction == inputAction &&
798+
other.keyboardAppearance == keyboardAppearance &&
799+
other.textCapitalization == textCapitalization &&
800+
other.autofillConfiguration == autofillConfiguration &&
801+
other.enableIMEPersonalizedLearning == enableIMEPersonalizedLearning &&
802+
listEquals(other.allowedMimeTypes, allowedMimeTypes) &&
803+
other.enableDeltaModel == enableDeltaModel;
804+
}
805+
806+
@override
807+
int get hashCode {
808+
return Object.hash(
809+
viewId,
810+
inputType,
811+
readOnly,
812+
obscureText,
813+
autocorrect,
814+
smartDashesType,
815+
smartQuotesType,
816+
enableSuggestions,
817+
enableInteractiveSelection,
818+
actionLabel,
819+
inputAction,
820+
keyboardAppearance,
821+
textCapitalization,
822+
autofillConfiguration,
823+
enableIMEPersonalizedLearning,
824+
Object.hashAll(allowedMimeTypes),
825+
enableDeltaModel,
826+
);
827+
}
828+
829+
@override
830+
String toString() {
831+
final List<String> description = <String>[
832+
if (viewId != null) 'viewId: $viewId',
833+
'inputType: $inputType',
834+
'readOnly: $readOnly',
835+
'obscureText: $obscureText',
836+
'autocorrect: $autocorrect',
837+
'smartDashesType: $smartDashesType',
838+
'smartQuotesType: $smartQuotesType',
839+
'enableSuggestions: $enableSuggestions',
840+
'enableInteractiveSelection: $enableInteractiveSelection',
841+
if (actionLabel != null) 'actionLabel: $actionLabel',
842+
'inputAction: $inputAction',
843+
'keyboardAppearance: $keyboardAppearance',
844+
'textCapitalization: $textCapitalization',
845+
'autofillConfiguration: $autofillConfiguration',
846+
'enableIMEPersonalizedLearning: $enableIMEPersonalizedLearning',
847+
'allowedMimeTypes: $allowedMimeTypes',
848+
'enableDeltaModel: $enableDeltaModel',
849+
];
850+
return 'TextInputConfiguration(${description.join(', ')})';
851+
}
775852
}
776853

777854
TextAffinity? _toTextAffinity(String? affinity) {

packages/flutter/test/services/autofill_test.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,74 @@ void main() {
9595
},
9696
);
9797
});
98+
99+
group('AutoFillConfiguration', () {
100+
late AutofillConfiguration fakeAutoFillConfiguration;
101+
late AutofillConfiguration fakeAutoFillConfiguration2;
102+
103+
setUp(() {
104+
// If you create two objects with `const` with the same values, the second object will be equal to the first one by reference.
105+
// This means that even without overriding the `equals` method, the test will pass.
106+
// ignore: prefer_const_constructors
107+
fakeAutoFillConfiguration = AutofillConfiguration(
108+
uniqueIdentifier: 'id1',
109+
// ignore: prefer_const_literals_to_create_immutables
110+
autofillHints: <String>['client1'],
111+
currentEditingValue: TextEditingValue.empty,
112+
hintText: 'hint',
113+
);
114+
// ignore: prefer_const_constructors
115+
fakeAutoFillConfiguration2 = AutofillConfiguration(
116+
uniqueIdentifier: 'id1',
117+
// ignore: prefer_const_literals_to_create_immutables
118+
autofillHints: <String>['client1'],
119+
currentEditingValue: TextEditingValue.empty,
120+
hintText: 'hint',
121+
);
122+
});
123+
124+
test('equality operator works correctly', () {
125+
expect(fakeAutoFillConfiguration, equals(fakeAutoFillConfiguration2));
126+
expect(fakeAutoFillConfiguration.enabled, equals(fakeAutoFillConfiguration2.enabled));
127+
expect(
128+
fakeAutoFillConfiguration.uniqueIdentifier,
129+
equals(fakeAutoFillConfiguration2.uniqueIdentifier),
130+
);
131+
expect(
132+
fakeAutoFillConfiguration.autofillHints,
133+
equals(fakeAutoFillConfiguration2.autofillHints),
134+
);
135+
expect(
136+
fakeAutoFillConfiguration.currentEditingValue,
137+
equals(fakeAutoFillConfiguration2.currentEditingValue),
138+
);
139+
expect(fakeAutoFillConfiguration.hintText, equals(fakeAutoFillConfiguration2.hintText));
140+
});
141+
142+
test('hashCode works correctly', () {
143+
expect(fakeAutoFillConfiguration.hashCode, equals(fakeAutoFillConfiguration2.hashCode));
144+
expect(
145+
fakeAutoFillConfiguration.enabled.hashCode,
146+
equals(fakeAutoFillConfiguration2.enabled.hashCode),
147+
);
148+
expect(
149+
fakeAutoFillConfiguration.uniqueIdentifier.hashCode,
150+
equals(fakeAutoFillConfiguration2.uniqueIdentifier.hashCode),
151+
);
152+
expect(
153+
Object.hashAll(fakeAutoFillConfiguration.autofillHints),
154+
equals(Object.hashAll(fakeAutoFillConfiguration2.autofillHints)),
155+
);
156+
expect(
157+
fakeAutoFillConfiguration.currentEditingValue.hashCode,
158+
equals(fakeAutoFillConfiguration2.currentEditingValue.hashCode),
159+
);
160+
expect(
161+
fakeAutoFillConfiguration.hintText.hashCode,
162+
equals(fakeAutoFillConfiguration2.hintText.hashCode),
163+
);
164+
});
165+
});
98166
}
99167

100168
class FakeAutofillClient implements TextInputClient, AutofillClient {

0 commit comments

Comments
 (0)