From 175a443fda09eb80de11fb753495bd87de76168c Mon Sep 17 00:00:00 2001 From: Nick Dowell Date: Wed, 25 Nov 2020 14:05:50 +0000 Subject: [PATCH] Improve reliability of OOM test Capturing the battery and orientation values in the initial creation of the BugsnagSystemState object ensures they will be there when the app is terminated to simulate an OOM. The test was sometimes failing because BugsnagSystemState queues up asynchronous writes in the background and these do not always have time to complete when running the test scenario. --- Bugsnag/BugsnagSystemState.m | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Bugsnag/BugsnagSystemState.m b/Bugsnag/BugsnagSystemState.m index a546ecf17..3088b3833 100644 --- a/Bugsnag/BugsnagSystemState.m +++ b/Bugsnag/BugsnagSystemState.m @@ -27,6 +27,10 @@ #define STATE_DIR @"bugsnag/state" #define STATE_FILE @"system_state.json" +#if BSG_PLATFORM_IOS +NSString *BSGOrientationNameFromEnum(UIDeviceOrientation deviceOrientation) +#endif + static NSDictionary* loadPreviousState(BugsnagKVStore *kvstore, NSString *jsonPath) { NSData *data = [NSData dataWithContentsOfFile:jsonPath]; if(data == nil) { @@ -127,6 +131,12 @@ id blankIfNil(id value) { device[@"simulator"] = @NO; #endif device[@"totalMemory"] = systemInfo[@BSG_KSSystemField_Memory][@"usable"]; +#if BSG_PLATFORM_IOS + device[BSGKeyBatteryLevel] = @([UIDevice currentDevice].batteryLevel); + UIDeviceBatteryState batteryState = [UIDevice currentDevice].batteryState; + device[BSGKeyCharging] = @(batteryState == UIDeviceBatteryStateCharging || batteryState == UIDeviceBatteryStateFull); + device[BSGKeyOrientation] = BSGOrientationNameFromEnum([UIDevice currentDevice].orientation); +#endif NSMutableDictionary *state = [NSMutableDictionary new]; state[BSGKeyApp] = app;