Skip to content

Commit 998e063

Browse files
committed
test: 🧪 cover all lines with tests
1 parent 66ae37e commit 998e063

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

lib/home_widget_info.dart

+21
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,25 @@ class HomeWidgetInfo {
4141
String toString() {
4242
return 'HomeWidgetInfo{family: $family, kind: $kind, widgetId: $widgetId, androidClassName: $androidClassName, label: $label}';
4343
}
44+
45+
@override
46+
bool operator ==(Object other) {
47+
if (identical(this, other)) return true;
48+
49+
return other is HomeWidgetInfo &&
50+
other.family == family &&
51+
other.kind == kind &&
52+
other.widgetId == widgetId &&
53+
other.androidClassName == androidClassName &&
54+
other.label == label;
55+
}
56+
57+
@override
58+
int get hashCode {
59+
return family.hashCode ^
60+
kind.hashCode ^
61+
widgetId.hashCode ^
62+
androidClassName.hashCode ^
63+
label.hashCode;
64+
}
4465
}

test/home_widget_info_test.dart

+31
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,36 @@ void main() {
3434
'HomeWidgetInfo{family: systemSmall, kind: ParkingWidget, widgetId: 1, androidClassName: com.example.MyWidget, label: My Widget}',
3535
);
3636
});
37+
38+
test('HomeWidgetInfo equality', () {
39+
final info1 = HomeWidgetInfo(
40+
family: 'medium',
41+
kind: 'anotherKind',
42+
widgetId: 1,
43+
androidClassName: 'com.example.AnotherWidget',
44+
label: 'Another Widget',
45+
);
46+
47+
final info2 = HomeWidgetInfo(
48+
family: 'medium',
49+
kind: 'anotherKind',
50+
widgetId: 1,
51+
androidClassName: 'com.example.AnotherWidget',
52+
label: 'Another Widget',
53+
);
54+
55+
final info3 = HomeWidgetInfo(
56+
family: 'systemSmall',
57+
kind: 'ParkingWidget',
58+
widgetId: 1,
59+
androidClassName: 'com.example.MyWidget',
60+
label: 'My Widget',
61+
);
62+
63+
expect(info1 == info2, true);
64+
expect(info1.hashCode, equals(info2.hashCode));
65+
expect(info1 == info3, false);
66+
expect(info1.hashCode, isNot(equals(info3.hashCode)));
67+
});
3768
});
3869
}

test/home_widget_test.dart

+44-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter_test/flutter_test.dart';
99
import 'package:golden_toolkit/golden_toolkit.dart';
1010
import 'package:home_widget/home_widget.dart';
1111
import 'package:home_widget/home_widget_callback_dispatcher.dart';
12+
import 'package:home_widget/home_widget_info.dart';
1213
import 'package:mocktail/mocktail.dart';
1314

1415
// ignore: depend_on_referenced_packages
@@ -51,8 +52,6 @@ void main() {
5152
return null;
5253
case 'isRequestPinWidgetSupported':
5354
return true;
54-
case 'getInstalledWidgets':
55-
return null;
5655
}
5756
});
5857
});
@@ -133,10 +132,6 @@ void main() {
133132
expect(arguments['qualifiedAndroidName'], 'com.example.androidName');
134133
});
135134

136-
test('getInstalledWidgets', () async {
137-
expect(await HomeWidget.getInstalledWidgets(), []);
138-
});
139-
140135
group('initiallyLaunchedFromHomeWidget', () {
141136
test('Valid Uri String gets parsed', () async {
142137
launchUri = 'homeWidget://homeWidgetTest';
@@ -366,6 +361,49 @@ void main() {
366361
);
367362
});
368363
});
364+
365+
group('getInstalledWidgets', () {
366+
test(
367+
'returns a list of HomeWidgetInfo objects when method channel provides data',
368+
() async {
369+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
370+
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
371+
switch (methodCall.method) {
372+
case 'getInstalledWidgets':
373+
return [
374+
{"id": "widget1", "name": "Widget One"},
375+
{"id": "widget2", "name": "Widget Two"},
376+
];
377+
default:
378+
return null;
379+
}
380+
});
381+
382+
final expectedWidgets = [
383+
HomeWidgetInfo.fromMap({"id": "widget1", "name": "Widget One"}),
384+
HomeWidgetInfo.fromMap({"id": "widget2", "name": "Widget Two"}),
385+
];
386+
387+
final widgets = await HomeWidget.getInstalledWidgets();
388+
389+
expect(widgets, equals(expectedWidgets));
390+
});
391+
392+
test('returns an empty list when method channel returns null', () async {
393+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
394+
// ignore: body_might_complete_normally_nullable
395+
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
396+
switch (methodCall.method) {
397+
case 'getInstalledWidgets':
398+
return null;
399+
}
400+
});
401+
402+
final widgets = await HomeWidget.getInstalledWidgets();
403+
404+
expect(widgets, isEmpty);
405+
});
406+
});
369407
}
370408

371409
void emitEvent(ByteData? event) {

0 commit comments

Comments
 (0)