Sunday, July 29, 2012

Testing iPhone location app with Automation

Even if we have now ability to add GPX files to the project and iPhone will even "move" between its points, still what if we want to add speed, altitude etc to our location simulation?

It is all easy and possible! Welcome to Automation tool in Instruments. Let me use my speedometer app as a showcase for it. I'll cram in as much as possible in this single session: find memory leaks while my app is continuously processing (simulated) location changes and gather screenshots for the AppStore (you don't really expect me to do it while I drive?! I may not be here then for the next article :)).

Start up automation from xcode (e.g. do alt-run) with target of your physical iphone:

I'll start with leaks profile:

The app starts on the phone and starts recording automatically. No leaks, even if I've waited for a year. As it does nothing... So lets add some location changes that app have to handle:

Actually, you may stop the recording at any time. We are setting things up right now :). Lets even clean up and delete that idle "run" we produced:

Then, open the library of Instrument tools, and then drag the Automation below the Leaks tool:

Now its time to add an automation script, click on Add and then Create:

Here is my script to simulate a few points:

 var target = UIATarget.localTarget();  
 // speed is in meters/sec  
 var points = [  
                 {location:{latitude:48.8899,longitude:14.2}, options:{speed:8, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:48.8899,longitude:14.9}, options:{speed:11, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:48.8899,longitude:14.6}, options:{speed:12, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:48.8899,longitude:14.7}, options:{speed:13, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:49.2,longitude:14.10}, options:{speed:15, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:49.4,longitude:14.8}, options:{speed:15, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:48.8899,longitude:14.9}, options:{speed:9, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:48.8899,longitude:15.1}, options:{speed:8, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
                 {location:{latitude:48.8899,longitude:16.1}, options:{speed:3, altitude:200, horizontalAccuracy:10, verticalAccuracy:15}},  
 for (var i = 0; i < points.length; i++)  

Note, that in addition to location it includes speed, altitude, and accuracy. Speed and accuracy is what is indispensable to simulate the speedometer behaviour correctly per algorithms implemented.

Keep the other options intact for a moment (script starts with recording) and for leaks investigations we don't need to log the results yet.

Let's start a recording now and see if plain old Stan is going to be ashamed. As script runs enjoy the screen of your app as it reacts on location changes, ooh that power!

Yep, plain old Stan should be ashamed at least a bit. There is a leak! It actually leaks on every location change... I'll let you go through your backtrace etc, Intruments give you enough to find where leak is coming from. In my case I forgot to release the CLLocation retained as a property when I was deallocing the holding object. After the fix, I just redeployed the app to the phone and kicked off recording again. No leaks whatsoever!

So now the bonus part. If you want to automate screenshots gathering for you app just pay attention to this line in the above for loop:


For every location/speed point I take a screenshot. To keep them on the disk, setup the logging directory and switch on that logging (below the Add script button). This is enough to get this all safely written to the target directory:

This is it I guess! Make sure you explore everything around as I provided only the essence. See the Editor log, Trace log for automation, it all gives some more insight.

Happy testing and simulating!

More on location testing from me: GPX files in xcode.

No comments: