Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pszklarska committed Nov 21, 2021
1 parent dd7d89c commit 2b8aeec
Showing 1 changed file with 118 additions and 63 deletions.
181 changes: 118 additions & 63 deletions test/beacon_broadcast_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,64 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

late BeaconBroadcast beaconBroadcast;
const MethodChannel methodChannel = MethodChannel(
'pl.pszklarska.beaconbroadcast/beacon_state',
);

setUp(() {
beaconBroadcast = BeaconBroadcast();
void onStartMethodReturn(Future value) {
methodChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'start') {
return value;
}
});
}

const MethodChannel methodChannel = MethodChannel(
'pl.pszklarska.beaconbroadcast/beacon_state',
);
void onStopMethodReturn(Future value) {
methodChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'stop') {
return value;
}
});
}

void onIsAdvertisingMethodReturn(Future value) {
methodChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'start' || methodCall.method == 'stop') {
return Future.value();
} else if (methodCall.method == 'isAdvertising') {
return Future.value(true);
if (methodCall.method == 'isAdvertising') {
return value;
}
return null;
});
});
}

void onIsTransmissionSupportedMethodReturn(Future value) {
methodChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'isTransmissionSupported') {
return value;
}
});
}

group('starting beacon advertising', () {
test('passing all the data starts normally', () async {
test('when all data is set returns normally', () async {
onStartMethodReturn(Future.value());

expect(
() => beaconBroadcast
() => BeaconBroadcast()
.setUUID("uuid")
.setMajorId(1)
.setMinorId(1)
.setTransmissionPower(-59)
.setAdvertiseMode(AdvertiseMode.lowLatency)
.setManufacturerId(0)
.setExtraData([100])
.setIdentifier("identifier")
.start(),
returnsNormally);
});

test('not passing uuid throws exception', () async {
test('when uuid is not set throws exception', () async {
onStartMethodReturn(Future.value());
expect(
() => beaconBroadcast
() => BeaconBroadcast()
.setMajorId(1)
.setMinorId(1)
.setTransmissionPower(-59)
Expand All @@ -50,9 +73,10 @@ void main() {
throwsA(isA<IllegalArgumentException>()));
});

test('not passing major id throws exception', () async {
test('when major id is not set throws exception', () async {
onStartMethodReturn(Future.value());
expect(
() => beaconBroadcast
() => BeaconBroadcast()
.setUUID("uuid")
.setMinorId(1)
.setTransmissionPower(-59)
Expand All @@ -61,9 +85,10 @@ void main() {
throwsA(isA<IllegalArgumentException>()));
});

test('not passing minor id throws exception', () async {
test('when minor id is not set throws exception', () async {
onStartMethodReturn(Future.value());
expect(
() => beaconBroadcast
() => BeaconBroadcast()
.setUUID("uuid")
.setMajorId(1)
.setTransmissionPower(-59)
Expand All @@ -72,57 +97,87 @@ void main() {
throwsA(isA<IllegalArgumentException>()));
});

test('not passing minor id when different layout is set returns normally',
() async {
expect(
() => beaconBroadcast
.setUUID("uuid")
.setMajorId(1)
.setTransmissionPower(-59)
.setIdentifier("identifier")
.setLayout("layout")
.start(),
returnsNormally);
test('when identifier and transmission power are not set starts normally', () async {
onStartMethodReturn(Future.value());
expect(() => BeaconBroadcast().setUUID("uuid").setMajorId(1).setMinorId(1).start(), returnsNormally);
});

test('not passing major id when different layout is set returns normally',
() async {
expect(
() => beaconBroadcast
.setUUID("uuid")
.setMinorId(1)
.setTransmissionPower(-59)
.setIdentifier("identifier")
.setLayout("layout")
.start(),
returnsNormally);
test('when extra data contains integer out of range throws exception', () async {
onStartMethodReturn(Future.value());
expect(() => BeaconBroadcast().setUUID("uuid").setExtraData([270]).start(),
throwsA(isA<IllegalArgumentException>()));
});

test('not passing UUID when different layout is set throws exception',
() async {
expect(
() => beaconBroadcast
.setMinorId(1)
.setTransmissionPower(-59)
.setIdentifier("identifier")
.setLayout("layout")
.start(),
throwsA(isA<IllegalArgumentException>()));
group('when custom layout is set', () {
test('and minor id is not set returns normally', () async {
onStartMethodReturn(Future.value());
expect(
() => BeaconBroadcast()
.setUUID("uuid")
.setMajorId(1)
.setTransmissionPower(-59)
.setIdentifier("identifier")
.setLayout("layout")
.start(),
returnsNormally);
});

test('and major id is not set returns normally', () async {
onStartMethodReturn(Future.value());
expect(
() => BeaconBroadcast()
.setUUID("uuid")
.setMinorId(1)
.setTransmissionPower(-59)
.setIdentifier("identifier")
.setLayout("layout")
.start(),
returnsNormally);
});

test('and UUID is not set throws exception', () async {
onStartMethodReturn(Future.value());
expect(
() => BeaconBroadcast()
.setMinorId(1)
.setTransmissionPower(-59)
.setIdentifier("identifier")
.setLayout("layout")
.start(),
throwsA(isA<IllegalArgumentException>()));
});
});
});

test('not passing identifier and tansmission power starts normally',
() async {
expect(
() => beaconBroadcast
.setUUID("uuid")
.setMajorId(1)
.setMinorId(1)
.start(),
returnsNormally);
group('checking if transmission is supported', () {
test('when device returns 0 return BeaconStatus.supported', () async {
onIsTransmissionSupportedMethodReturn(Future.value(0));
expect(await BeaconBroadcast().checkTransmissionSupported(), BeaconStatus.supported);
});

test('when device returns 1 return BeaconStatus.notSupportedMinSdk', () async {
onIsTransmissionSupportedMethodReturn(Future.value(1));
expect(await BeaconBroadcast().checkTransmissionSupported(), BeaconStatus.notSupportedMinSdk);
});

test('when device returns 2 return BeaconStatus.notSupportedBle', () async {
onIsTransmissionSupportedMethodReturn(Future.value(2));
expect(await BeaconBroadcast().checkTransmissionSupported(), BeaconStatus.notSupportedBle);
});

test('when device returns other value return BeaconStatus.notSupportedCannotGetAdvertiser', () async {
onIsTransmissionSupportedMethodReturn(Future.value(3));
expect(await BeaconBroadcast().checkTransmissionSupported(), BeaconStatus.notSupportedCannotGetAdvertiser);
});
});

test('beacon is advertising returns true', () async {
onIsAdvertisingMethodReturn(Future.value(true));
expect(await BeaconBroadcast().isAdvertising(), isTrue);
});

test('checking if beacon is advertising returns true', () async {
expect(await beaconBroadcast.isAdvertising(), isTrue);
test('beacon stops returns normally', () async {
onStopMethodReturn(Future.value());
expect(() => BeaconBroadcast().stop(), returnsNormally);
});
}

0 comments on commit 2b8aeec

Please sign in to comment.