Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ueman committed Jun 29, 2024
1 parent e8e7088 commit 4484638
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 115 deletions.
2 changes: 1 addition & 1 deletion passkit/lib/src/passkit/pk_pass_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PkPassImage {
}

Uint8List fromMultiplier(int multiplier) {
assert(multiplier == 1 || multiplier == 2 || multiplier == 3);
multiplier = multiplier.clamp(1, 3);
return switch (multiplier) {
1 => (image1 ?? image2 ?? image3)!,
2 => (image2 ?? image3 ?? image1)!,
Expand Down
16 changes: 16 additions & 0 deletions passkit_ui/test/helpers/load_sample_passkit_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:io';

import 'package:passkit/passkit.dart';

PkPass loadSample(PassType type) {
final fileName = switch (type) {
PassType.boardingPass => 'BoardingPass_unsigned',
PassType.coupon => 'Coupon',
PassType.eventTicket => 'Event',
PassType.storeCard => 'StoreCard',
PassType.generic => 'Generic',
PassType.unknown => 'Generic',
};
final bytes = File('test/sample_passes/$fileName.pkpass').readAsBytesSync();
return PkPass.fromBytes(bytes);
}
Binary file added passkit_ui/test/sample_passes/BoardingPass.pkpass
Binary file not shown.
Binary file not shown.
Binary file added passkit_ui/test/sample_passes/Coupon.pkpass
Binary file not shown.
Binary file added passkit_ui/test/sample_passes/Event.pkpass
Binary file not shown.
Binary file added passkit_ui/test/sample_passes/Generic.pkpass
Binary file not shown.
Binary file added passkit_ui/test/sample_passes/StoreCard.pkpass
Binary file not shown.
152 changes: 38 additions & 114 deletions passkit_ui/test/src/pk_pass_widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,132 +1,56 @@
import 'package:collection/collection.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:passkit/passkit.dart';
import 'package:passkit_ui/passkit_ui.dart';
import 'package:passkit_ui/src/boarding_pass.dart';
import 'package:passkit_ui/src/coupon.dart';
import 'package:passkit_ui/src/event_ticket.dart';
import 'package:passkit_ui/src/generic.dart';
import 'package:passkit_ui/src/store_card.dart';

import '../helpers/helpers.dart';

class _FakePkPass extends Fake implements PkPass {
_FakePkPass({required this.type});

@override
final PassType type;

@override
PkPassImage? get icon => _passImage;

@override
PkPassImage? get logo => _passImage;

@override
PkPassImage? get footer => _passImage;

@override
PkPassImage? get strip => _passImage;

@override
PkPassImage? get background => null;

@override
PkPassImage? get thumbnail => null;

@override
PassData get pass {
return PassData(
description: 'description',
formatVersion: 0,
organizationName: 'organizationName',
passTypeIdentifier: 'passTypeIdentifier',
serialNumber: 'serialNumber',
teamIdentifier: 'teamIdentifier',
boardingPass: _passStructure,
coupon: _passStructure,
eventTicket: _passStructure,
generic: _passStructure,
storeCard: _passStructure,
logoText: 'logoText',
);
}

static final PkPassImage? _passImage = PkPassImage.fromImages(
image1: transparentPixelPng,
image2: transparentPixelPng,
image3: transparentPixelPng,
);

static final PassStructure _passStructure = PassStructure(
headerFields: [
FieldDict(key: 'key', label: 'label', value: 'value'),
FieldDict(key: 'key', label: 'label', value: 'value'),
],
primaryFields: [
FieldDict(key: 'key', label: 'label', value: 'value'),
FieldDict(key: 'key', label: 'label', value: 'value'),
],
secondaryFields: [
FieldDict(key: 'key', label: 'label', value: 'value'),
FieldDict(key: 'key', label: 'label', value: 'value'),
],
auxiliaryFields: [
FieldDict(key: 'key', label: 'label', value: 'value'),
FieldDict(key: 'key', label: 'label', value: 'value'),
],
backFields: [
FieldDict(key: 'key', label: 'label', value: 'value'),
FieldDict(key: 'key', label: 'label', value: 'value'),
],
);
}
import '../helpers/load_sample_passkit_file.dart';

void main() {
group('PkPassWidget', () {
final passes = PassType.values.map((type) => _FakePkPass(type: type));

final widgets = {
PassType.boardingPass: BoardingPass(
pass: passes.firstWhere((pass) => pass.type == PassType.boardingPass),
),
PassType.coupon: Coupon(
pass: passes.firstWhere((pass) => pass.type == PassType.coupon),
),
PassType.eventTicket: EventTicket(
pass: passes.firstWhere((pass) => pass.type == PassType.eventTicket),
),
PassType.storeCard: StoreCard(
pass: passes.firstWhere((pass) => pass.type == PassType.storeCard),
),
PassType.generic: Generic(
pass: passes.firstWhere((pass) => pass.type == PassType.generic),
),
PassType.unknown: Generic(
pass: passes.firstWhere((pass) => pass.type == PassType.unknown),
),
};

final zip = IterableZip([passes, widgets.entries]);
testWidgets(
'renders BoardingPass when type is PassType.boardingPass',
(tester) async {
await tester
.pumpApp(PkPassWidget(pass: loadSample(PassType.boardingPass)));
expect(find.byType(BoardingPass), findsOneWidget);
},
);

for (final pair in zip) {
final pass = pair.first as PkPass;
final pkPassWidget = pair.last as MapEntry<PassType, Widget>;
final passType = pass.type;
final widgetType = pkPassWidget.value.runtimeType;
testWidgets(
'renders Coupon when type is PassType.coupon',
(tester) async {
await tester.pumpApp(PkPassWidget(pass: loadSample(PassType.coupon)));
expect(find.byType(Coupon), findsOneWidget);
},
);

testWidgets(
'renders $widgetType when type is $passType',
(tester) async {
tester.view.devicePixelRatio = 1.0;
testWidgets(
'renders EventTicket when type is PassType.eventTicket',
(tester) async {
await tester
.pumpApp(PkPassWidget(pass: loadSample(PassType.eventTicket)));
expect(find.byType(EventTicket), findsOneWidget);
},
);

await tester.pumpApp(PkPassWidget(pass: pass));
expect(find.byType(widgetType), findsOneWidget);
testWidgets(
'renders Generic when type is PassType.generic',
(tester) async {
await tester.pumpApp(PkPassWidget(pass: loadSample(PassType.generic)));
expect(find.byType(Generic), findsOneWidget);
},
);

tester.view.resetDevicePixelRatio();
},
);
}
testWidgets(
'renders Generic when type is PassType.unknown',
(tester) async {
await tester.pumpApp(PkPassWidget(pass: loadSample(PassType.unknown)));
expect(find.byType(Generic), findsOneWidget);
},
);
});
}

0 comments on commit 4484638

Please sign in to comment.