From 998e063d1db3943e4f7048bb67ee54fc6d74bcef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boros=20Ma=CC=81te=CC=81?= <33902750+qwadrox@users.noreply.github.com> Date: Fri, 15 Mar 2024 19:32:24 +0100 Subject: [PATCH] test: :test_tube: cover all lines with tests --- lib/home_widget_info.dart | 21 ++++++++++++++ test/home_widget_info_test.dart | 31 ++++++++++++++++++++ test/home_widget_test.dart | 50 +++++++++++++++++++++++++++++---- 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/lib/home_widget_info.dart b/lib/home_widget_info.dart index 60f16d6d..1ac3310b 100644 --- a/lib/home_widget_info.dart +++ b/lib/home_widget_info.dart @@ -41,4 +41,25 @@ class HomeWidgetInfo { String toString() { return 'HomeWidgetInfo{family: $family, kind: $kind, widgetId: $widgetId, androidClassName: $androidClassName, label: $label}'; } + + @override + bool operator ==(Object other) { + if (identical(this, other)) return true; + + return other is HomeWidgetInfo && + other.family == family && + other.kind == kind && + other.widgetId == widgetId && + other.androidClassName == androidClassName && + other.label == label; + } + + @override + int get hashCode { + return family.hashCode ^ + kind.hashCode ^ + widgetId.hashCode ^ + androidClassName.hashCode ^ + label.hashCode; + } } diff --git a/test/home_widget_info_test.dart b/test/home_widget_info_test.dart index a73e2d06..1edf4ac6 100644 --- a/test/home_widget_info_test.dart +++ b/test/home_widget_info_test.dart @@ -34,5 +34,36 @@ void main() { 'HomeWidgetInfo{family: systemSmall, kind: ParkingWidget, widgetId: 1, androidClassName: com.example.MyWidget, label: My Widget}', ); }); + + test('HomeWidgetInfo equality', () { + final info1 = HomeWidgetInfo( + family: 'medium', + kind: 'anotherKind', + widgetId: 1, + androidClassName: 'com.example.AnotherWidget', + label: 'Another Widget', + ); + + final info2 = HomeWidgetInfo( + family: 'medium', + kind: 'anotherKind', + widgetId: 1, + androidClassName: 'com.example.AnotherWidget', + label: 'Another Widget', + ); + + final info3 = HomeWidgetInfo( + family: 'systemSmall', + kind: 'ParkingWidget', + widgetId: 1, + androidClassName: 'com.example.MyWidget', + label: 'My Widget', + ); + + expect(info1 == info2, true); + expect(info1.hashCode, equals(info2.hashCode)); + expect(info1 == info3, false); + expect(info1.hashCode, isNot(equals(info3.hashCode))); + }); }); } diff --git a/test/home_widget_test.dart b/test/home_widget_test.dart index 25c917b4..79f47177 100644 --- a/test/home_widget_test.dart +++ b/test/home_widget_test.dart @@ -9,6 +9,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:golden_toolkit/golden_toolkit.dart'; import 'package:home_widget/home_widget.dart'; import 'package:home_widget/home_widget_callback_dispatcher.dart'; +import 'package:home_widget/home_widget_info.dart'; import 'package:mocktail/mocktail.dart'; // ignore: depend_on_referenced_packages @@ -51,8 +52,6 @@ void main() { return null; case 'isRequestPinWidgetSupported': return true; - case 'getInstalledWidgets': - return null; } }); }); @@ -133,10 +132,6 @@ void main() { expect(arguments['qualifiedAndroidName'], 'com.example.androidName'); }); - test('getInstalledWidgets', () async { - expect(await HomeWidget.getInstalledWidgets(), []); - }); - group('initiallyLaunchedFromHomeWidget', () { test('Valid Uri String gets parsed', () async { launchUri = 'homeWidget://homeWidgetTest'; @@ -366,6 +361,49 @@ void main() { ); }); }); + + group('getInstalledWidgets', () { + test( + 'returns a list of HomeWidgetInfo objects when method channel provides data', + () async { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(channel, (MethodCall methodCall) async { + switch (methodCall.method) { + case 'getInstalledWidgets': + return [ + {"id": "widget1", "name": "Widget One"}, + {"id": "widget2", "name": "Widget Two"}, + ]; + default: + return null; + } + }); + + final expectedWidgets = [ + HomeWidgetInfo.fromMap({"id": "widget1", "name": "Widget One"}), + HomeWidgetInfo.fromMap({"id": "widget2", "name": "Widget Two"}), + ]; + + final widgets = await HomeWidget.getInstalledWidgets(); + + expect(widgets, equals(expectedWidgets)); + }); + + test('returns an empty list when method channel returns null', () async { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + // ignore: body_might_complete_normally_nullable + .setMockMethodCallHandler(channel, (MethodCall methodCall) async { + switch (methodCall.method) { + case 'getInstalledWidgets': + return null; + } + }); + + final widgets = await HomeWidget.getInstalledWidgets(); + + expect(widgets, isEmpty); + }); + }); } void emitEvent(ByteData? event) {