fakeNfcTag function
A tag that answers to the technologies given, and to nothing else.
The tag is inert -- its handle addresses nothing, because nothing is in the field -- so what this covers is the code that inspects a tag and decides what it is. Put the fake host APIs in place as well when the code under test goes on to talk to the tag.
final tag = fakeNfcTag(
id: Uint8List.fromList([0x04, 0xA2, 0x1B]),
techs: [FakeTech.nfcA(), FakeTech.mifareClassic()],
);
expect(MifareClassic.from(tag)?.sectorCount, 16);
expect(FeliCa.from(tag), isNull);
techList is derived from the Android technologies in techs by default, because a tag
carrying a technology the platform left off its list is a tag no device would deliver, and
code that branches on the list would quietly take the wrong branch. Pass it to say
something the derivation cannot. An iOS tag has no such list at all, which is what a tag
built from CoreNFC protocols alone reports.
handle is arbitrary: the fake host APIs ignore it, and a real one refuses it.
Implementation
NfcTag fakeNfcTag({
String handle = 'fake-tag',
Uint8List? id,
List<FakeTech> techs = const [],
List<String>? techList,
int? otherTagCount,
}) {
final data = TagPigeon(handle: handle, id: id, otherTagCount: otherTagCount);
for (final tech in techs) {
tech._apply(data);
}
final names = techs.map((tech) => tech._techName).whereType<String>().toList();
data.techList = techList ?? (names.isEmpty ? null : names);
return NfcTag(data);
}