Skip to content

Commit

Permalink
test: 🧪 cover all lines with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qwadrox committed Mar 15, 2024
1 parent 66ae37e commit 998e063
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
21 changes: 21 additions & 0 deletions lib/home_widget_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
31 changes: 31 additions & 0 deletions test/home_widget_info_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
});
});
}
50 changes: 44 additions & 6 deletions test/home_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,8 +52,6 @@ void main() {
return null;
case 'isRequestPinWidgetSupported':
return true;
case 'getInstalledWidgets':
return null;
}
});
});
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 998e063

Please sign in to comment.