From bf4aed726e9adcfe8b364aef9d0870dd2c4ad5fe Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> Date: Wed, 10 May 2023 17:10:02 +0200 Subject: [PATCH] Fix battery level convertion for iOS 16.4 (#1433) --- CHANGELOG.md | 1 + dart/lib/src/protocol/sentry_device.dart | 4 ++- dart/test/protocol/sentry_device_test.dart | 33 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1940618480..b954afe357 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- Fix battery level convertion for iOS 16.4 ([#1433](https://github.com/getsentry/sentry-dart/pull/1433)) - Adds a namespace for compatibility with AGP 8.0. ([#1427](https://github.com/getsentry/sentry-dart/pull/1427)) ## 7.5.2 diff --git a/dart/lib/src/protocol/sentry_device.dart b/dart/lib/src/protocol/sentry_device.dart index e85e599aac..188794010b 100644 --- a/dart/lib/src/protocol/sentry_device.dart +++ b/dart/lib/src/protocol/sentry_device.dart @@ -178,7 +178,9 @@ class SentryDevice { model: data['model'], modelId: data['model_id'], arch: data['arch'], - batteryLevel: data['battery_level'], + batteryLevel: + (data['battery_level'] is num ? data['battery_level'] as num : null) + ?.toDouble(), orientation: data['orientation'] == 'portrait' ? SentryOrientation.portrait : data['orientation'] == 'landscape' diff --git a/dart/test/protocol/sentry_device_test.dart b/dart/test/protocol/sentry_device_test.dart index 77d41655e0..d7ed3e518f 100644 --- a/dart/test/protocol/sentry_device_test.dart +++ b/dart/test/protocol/sentry_device_test.dart @@ -115,6 +115,39 @@ void main() { true, ); }); + + test('batery level converts int to double', () { + final map = {'battery_level': 1}; + + final sentryDevice = SentryDevice.fromJson(map); + + expect( + sentryDevice.batteryLevel, + 1.0, + ); + }); + + test('batery level maps double', () { + final map = {'battery_level': 1.0}; + + final sentryDevice = SentryDevice.fromJson(map); + + expect( + sentryDevice.batteryLevel, + 1.0, + ); + }); + + test('batery level ignores if not a num', () { + final map = {'battery_level': 'abc'}; + + final sentryDevice = SentryDevice.fromJson(map); + + expect( + sentryDevice.batteryLevel, + null, + ); + }); }); group('copyWith', () {