Skip to content

Commit

Permalink
fix: throw PlatformException on unsupported platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag-aofl authored and Boehrsi committed Jul 25, 2024
1 parent 9a35391 commit ae54b71
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/src/device_lookup.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:device_marketing_names/src/types/device.dart';
import 'package:device_marketing_names/src/types/platform.dart';
import 'package:device_marketing_names/src/utils/text.dart';
import 'package:flutter/services.dart';

import 'data/device_identifiers.dart';

Expand All @@ -10,7 +11,7 @@ enum DeviceType {
ios,
}

Future<String?> lookupDevice(
Future<String> lookupDevice(
PlatformInfoBase platform, DeviceInfoBase device) async {
if (platform.isWeb()) {
final webInfo = await device.getWebInfo();
Expand All @@ -23,7 +24,11 @@ Future<String?> lookupDevice(
final iosInfo = await device.getIosInfo();
return lookupName(DeviceType.ios, iosInfo.utsname.machine);
} else {
return null;
throw PlatformException(
code: 'UNSUPPORTED_PLATFORM',
message:
'Device name could not be read on device with unsupported type.',
);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/device_marketing_names_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:device_marketing_names/src/device_lookup.dart';
import 'package:device_marketing_names/src/types/device.dart';
import 'package:device_marketing_names/src/types/platform.dart';
import 'package:device_marketing_names/src/utils/text.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
Expand Down Expand Up @@ -305,6 +306,16 @@ void main() {
expect(resulty, 'yetruepad21');
expect(resultz, 'ZX70');
});

test('Throw on unsupported platform', () async {
when(platform.isWeb()).thenReturn(false);
when(platform.isAndroid()).thenReturn(false);
when(platform.isIOS()).thenReturn(false);

final resultFuture = lookupDevice(platform, device);

expect(resultFuture, throwsA(isA<PlatformException>()));
});
});

group('Utils', () {
Expand Down

0 comments on commit ae54b71

Please sign in to comment.