From 4e2e69797ac412f3cbee9ba7e48478713f272bc1 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 6 Nov 2017 09:52:57 -0800 Subject: [PATCH] Eliminate MethodCall hashCode and equals (#12277) Since MethodCall equality checks are limited to test scenarios, this patch replaces them with an equivalent test matcher. At present MethodCalls are always used in scenarios where indentity-based equality/hashing is appropriate. This change avoids an assertion failure when MethodCall args are Iterable (possible since args are of type dyanmic), and hashValue() from dart:ui asserts that its input is not an Iterable. The alternative of implementing support for deep equality in dart:ui was rejected on the basis that if we're to encourage performant code, expensive checks should be obviously-expensive to the author. --- .../lib/src/services/message_codec.dart | 43 ------------ .../test/services/haptic_feedback_test.dart | 5 +- .../test/services/message_codecs_utils.dart | 68 +++++++++++++++++++ .../test/services/system_chrome_test.dart | 23 ++++--- .../test/services/system_navigator_test.dart | 5 +- .../test/services/system_sound_test.dart | 5 +- .../test/widgets/editable_text_test.dart | 25 ++++--- packages/flutter/test/widgets/title_test.dart | 11 +-- 8 files changed, 115 insertions(+), 70 deletions(-) create mode 100644 packages/flutter/test/services/message_codecs_utils.dart diff --git a/packages/flutter/lib/src/services/message_codec.dart b/packages/flutter/lib/src/services/message_codec.dart index 7f2aebf379..09b41e82c5 100644 --- a/packages/flutter/lib/src/services/message_codec.dart +++ b/packages/flutter/lib/src/services/message_codec.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'dart:typed_data'; -import 'dart:ui' show hashValues; import 'package:flutter/foundation.dart'; @@ -46,48 +45,6 @@ class MethodCall { /// Must be a valid value for the [MethodCodec] used. final dynamic arguments; - @override - bool operator == (dynamic other) { - if (identical(this, other)) - return true; - if (runtimeType != other.runtimeType) - return false; - return method == other.method && _deepEquals(arguments, other.arguments); - } - - @override - int get hashCode => hashValues(method, arguments); - - bool _deepEquals(dynamic a, dynamic b) { - if (a == b) - return true; - if (a is List) - return b is List && _deepEqualsList(a, b); - if (a is Map) - return b is Map && _deepEqualsMap(a, b); - return false; - } - - bool _deepEqualsList(List a, List b) { - if (a.length != b.length) - return false; - for (int i = 0; i < a.length; i++) { - if (!_deepEquals(a[i], b[i])) - return false; - } - return true; - } - - bool _deepEqualsMap(Map a, Map b) { - if (a.length != b.length) - return false; - for (dynamic key in a.keys) { - if (!b.containsKey(key) || !_deepEquals(a[key], b[key])) - return false; - } - return true; - } - @override String toString() => '$runtimeType($method, $arguments)'; } diff --git a/packages/flutter/test/services/haptic_feedback_test.dart b/packages/flutter/test/services/haptic_feedback_test.dart index 3f029fdc89..71c8d8b2b3 100644 --- a/packages/flutter/test/services/haptic_feedback_test.dart +++ b/packages/flutter/test/services/haptic_feedback_test.dart @@ -5,6 +5,8 @@ import 'package:flutter/services.dart'; import 'package:test/test.dart'; +import 'message_codecs_utils.dart'; + void main() { test('Haptic feedback control test', () async { final List log = []; @@ -15,6 +17,7 @@ void main() { await HapticFeedback.vibrate(); - expect(log, equals([const MethodCall('HapticFeedback.vibrate')])); + expect(log, hasLength(1)); + expect(log.single, isMethodCall('HapticFeedback.vibrate', arguments: null)); }); } diff --git a/packages/flutter/test/services/message_codecs_utils.dart b/packages/flutter/test/services/message_codecs_utils.dart new file mode 100644 index 0000000000..0687b54623 --- /dev/null +++ b/packages/flutter/test/services/message_codecs_utils.dart @@ -0,0 +1,68 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/services.dart' show MethodCall; +import 'package:meta/meta.dart'; +import 'package:test/test.dart'; + +class _IsMethodCall extends Matcher { + const _IsMethodCall(this.name, this.arguments); + + final String name; + final dynamic arguments; + + @override + bool matches(dynamic item, Map matchState) { + if (item is! MethodCall) + return false; + if (item.method != name) + return false; + return _deepEquals(item.arguments, arguments); + } + + bool _deepEquals(dynamic a, dynamic b) { + if (a == b) + return true; + if (a is List) + return b is List && _deepEqualsList(a, b); + if (a is Map) + return b is Map && _deepEqualsMap(a, b); + return false; + } + + bool _deepEqualsList(List a, List b) { + if (a.length != b.length) + return false; + for (int i = 0; i < a.length; i++) { + if (!_deepEquals(a[i], b[i])) + return false; + } + return true; + } + + bool _deepEqualsMap(Map a, Map b) { + if (a.length != b.length) + return false; + for (dynamic key in a.keys) { + if (!b.containsKey(key) || !_deepEquals(a[key], b[key])) + return false; + } + return true; + } + + @override + Description describe(Description description) { + return description + .add('has method name: ').addDescriptionOf(name) + .add(' with arguments: ').addDescriptionOf(arguments); + } +} + +/// Returns a matcher that matches [MethodCall] instances with the specified +/// method name and arguments. +/// +/// Arguments checking implements deep equality for [List] and [Map] types. +Matcher isMethodCall(String name, {@required dynamic arguments}) { + return new _IsMethodCall(name, arguments); +} diff --git a/packages/flutter/test/services/system_chrome_test.dart b/packages/flutter/test/services/system_chrome_test.dart index 7ee15924d2..65b0bb67be 100644 --- a/packages/flutter/test/services/system_chrome_test.dart +++ b/packages/flutter/test/services/system_chrome_test.dart @@ -7,6 +7,8 @@ import 'dart:typed_data'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'message_codecs_utils.dart'; + void main() { testWidgets('SystemChrome overlay style test', (WidgetTester tester) async { // The first call is a cache miss and will queue a microtask @@ -33,10 +35,11 @@ void main() { DeviceOrientation.portraitUp, ]); - expect(log, equals([new MethodCall( + expect(log, hasLength(1)); + expect(log.single, isMethodCall( 'SystemChrome.setPreferredOrientations', - ['DeviceOrientation.portraitUp'], - )])); + arguments: ['DeviceOrientation.portraitUp'], + )); }); test('setApplicationSwitcherDescription control test', () async { @@ -50,10 +53,11 @@ void main() { const ApplicationSwitcherDescription(label: 'Example label', primaryColor: 0xFF00FF00) ); - expect(log, equals([new MethodCall( + expect(log, hasLength(1)); + expect(log.single, isMethodCall( 'SystemChrome.setApplicationSwitcherDescription', - {'label':'Example label','primaryColor':4278255360} - )])); + arguments: {'label': 'Example label', 'primaryColor': 4278255360}, + )); }); test('setApplicationSwitcherDescription missing plugin', () async { @@ -80,9 +84,10 @@ void main() { await SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]); - expect(log, equals([new MethodCall( + expect(log, hasLength(1)); + expect(log.single, isMethodCall( 'SystemChrome.setEnabledSystemUIOverlays', - ['SystemUiOverlay.top'], - )])); + arguments: ['SystemUiOverlay.top'], + )); }); } diff --git a/packages/flutter/test/services/system_navigator_test.dart b/packages/flutter/test/services/system_navigator_test.dart index a693761716..753d37dd39 100644 --- a/packages/flutter/test/services/system_navigator_test.dart +++ b/packages/flutter/test/services/system_navigator_test.dart @@ -5,6 +5,8 @@ import 'package:flutter/services.dart'; import 'package:test/test.dart'; +import 'message_codecs_utils.dart'; + void main() { test('System navigator control test', () async { final List log = []; @@ -15,6 +17,7 @@ void main() { await SystemNavigator.pop(); - expect(log, equals([const MethodCall('SystemNavigator.pop')])); + expect(log, hasLength(1)); + expect(log.single, isMethodCall('SystemNavigator.pop', arguments: null)); }); } diff --git a/packages/flutter/test/services/system_sound_test.dart b/packages/flutter/test/services/system_sound_test.dart index 2f3f1e7960..9abb15ad9d 100644 --- a/packages/flutter/test/services/system_sound_test.dart +++ b/packages/flutter/test/services/system_sound_test.dart @@ -5,6 +5,8 @@ import 'package:flutter/services.dart'; import 'package:test/test.dart'; +import 'message_codecs_utils.dart'; + void main() { test('System sound control test', () async { final List log = []; @@ -15,6 +17,7 @@ void main() { await SystemSound.play(SystemSoundType.click); - expect(log, equals([const MethodCall('SystemSound.play', 'SystemSoundType.click')])); + expect(log, hasLength(1)); + expect(log.single, isMethodCall('SystemSound.play', arguments: 'SystemSoundType.click')); }); } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 3e4cf5231e..c535ecb2d7 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -10,6 +10,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter/services.dart'; +import '../services/message_codecs_utils.dart'; import 'semantics_tester.dart'; void main() { @@ -243,17 +244,19 @@ void main() { }); await tester.pump(); - expect(log, [ - const MethodCall('TextInput.setEditingState', const { - 'text': 'Wobble', - 'selectionBase': -1, - 'selectionExtent': -1, - 'selectionAffinity': 'TextAffinity.downstream', - 'selectionIsDirectional': false, - 'composingBase': -1, - 'composingExtent': -1, - }), - ]); + expect(log, hasLength(1)); + expect(log.single, isMethodCall( + 'TextInput.setEditingState', + arguments: const { + 'text': 'Wobble', + 'selectionBase': -1, + 'selectionExtent': -1, + 'selectionAffinity': 'TextAffinity.downstream', + 'selectionIsDirectional': false, + 'composingBase': -1, + 'composingExtent': -1, + }, + )); }); testWidgets('EditableText identifies as text field (w/ focus) in semantics', (WidgetTester tester) async { diff --git a/packages/flutter/test/widgets/title_test.dart b/packages/flutter/test/widgets/title_test.dart index e9ca5c1fb2..bc17354417 100644 --- a/packages/flutter/test/widgets/title_test.dart +++ b/packages/flutter/test/widgets/title_test.dart @@ -6,6 +6,8 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter/services.dart'; +import '../services/message_codecs_utils.dart'; + void main() { testWidgets('toString control test', (WidgetTester tester) async { final Widget widget = new Title( @@ -58,9 +60,10 @@ void main() { color: const Color(0xFF00FF00), )); - expect(log, equals([new MethodCall( - 'SystemChrome.setApplicationSwitcherDescription', - {'label': '', 'primaryColor': 4278255360}, - )])); + expect(log, hasLength(1)); + expect(log.single, isMethodCall( + 'SystemChrome.setApplicationSwitcherDescription', + arguments: {'label': '', 'primaryColor': 4278255360}, + )); }); }