Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Background App Launch in case of NSURLSession(Background Session) event?

How can one test the scenario of Application Launch in background for handling Background NSURLSession's event?

Flow:

  1. Application starts a upload/download task using Background URL session.
  2. User hits home button. App is in suspended or in background state.
  3. OS decides to Exit the application. I know, one can exit the app by double-tapping home button and swipe-up the particular app. But in that case OS will never re-launch the app in background for event handling.
  4. Upload/download task needs some event handling. OS re-lauches App in background.

So the question is how do I make OS exit the app like it may normally do after some-time. The purpose is to test the code for this scenario. I tried using UIApplicationExitsOnSuspend but it does not work since then App can not be launched in Background.

like image 522
Taha Samad Avatar asked Feb 13 '15 14:02

Taha Samad


2 Answers

You could write an app that has a button that allocates and intentionally leaks chunks of memory. If you get this thing to allocate enough RAM, the OS will start killing other apps to get their RAM back.

Hopefully this would exhibit the behavior you need.

like image 89
Almo Avatar answered Nov 10 '22 15:11

Almo


It's not a perfect solution, but I was able to manually test the launch of an app due to a Background URLSession on a physical device as follows:

  1. Connect device for debugging via USB
  2. Disable Internet connectivity on device (i.e. disable WiFi/Cellular)
  3. Start the app via Xcode
  4. Issue a request using background URLSession. The request shouldn't fail, it will just be waiting around for an Internet connection until it times out, so use a reasonably long timeout to make testing easier.
  5. Kill the app via Xcode (press stop button)
  6. View the device logs via Windows > Devices and Simulators
  7. Enable Internet connectivity on the device again without starting the app
  8. The requests from the background URLSession should then complete and in the device logs (from step 6.) you should see any NSLog statements issued as a result of the app being launched via the application(_:handleEventsForBackgroundURLSession:completionHandler:) app delegate method.

They key point is that killing the app via Xcode, as opposed to killing it using device itself, does not prevent the app from being relaunched for background event handling.

A possible alternative to killing the app manually via Xcode might be to intentionally crash the app in code - this might be more suitable for automated testing.

like image 36
James Avatar answered Nov 10 '22 15:11

James