Into Open Source


Let the monkey go wild…

Geplaatst in Android,Artikel door Dirk op 04/01/2011
Tags: , , , ,

Imagine the monkey on the left working with your mobile Android application… Yeah that’s where I’m going, making your application monkey proof. It’s something that should be done for every application, but for christ sake, how do you test if your application is monkey proof?

ADB
Before I explain you the solution that comes with the Android SDK I should first get into ADB. ADB is the abbreviation for Android Device Bridge. It’s a utility in the SDK that allows developers to connect to their Android devices (Real devices or emulator devices (AVD’s)) and execute some actions on it. For example with ADB you can reboot a device, push files to it, get files from it, look into the SQLite database instance (only available for dev phones or emulators),… The tool can only be used from command line.

ADB Monkey
So now you what you can do with ADB, there’s even more! Google provides a monkey tester in the ADB. What it does:
It starts the application you want to monkey test and then starts firing all kind of simulated events. An simulated event can be a finger touch, the usage of the back button, usage of the menu button, a press on the camera or multimedia button, start playing music while your application is on top,… Every possible event a user can fire on any device when using your application can be simulated using the ADB Monkey tool. If any of the simulated events causes the application to crash, an entire stacktrace is available at the command line.

It’s quite easy to use. There’s only one thing you need to know, the package of the application you want to test which is defined in your application’s AndroidManifest.xml file. Next open a command line and use it to navigate to the SDK directory. In the SDK directory navigate to the tools directory in the root of the SDK or in the platforms/android-X/ directory. Once you’re there just execute this command (with your package declaration off course):

adb shell monkey -p your.package.declaration -v 500

This command will fire 500 random events to your application and will, if you’re application is properly designed, only output the launch of all the simulated events. An excerpt of the output:

:Sending Pointer ACTION_MOVE x=-3.0 y=4.0
:Sending Pointer ACTION_DOWN x=244.0 y=371.0
:Sending Pointer ACTION_UP x=244.0 y=371.0
:Sending Pointer ACTION_DOWN x=163.0 y=98.0
:Sending Pointer ACTION_UP x=163.0 y=98.0
:Sending Pointer ACTION_MOVE x=-2.0 y=3.0
:Sending Pointer ACTION_MOVE x=2.0 y=-1.0
:Sending Flip keyboardOpen=false
:Sending Pointer ACTION_DOWN x=193.0 y=137.0
:Sending Pointer ACTION_UP x=178.0 y=139.0
:Sending Pointer ACTION_DOWN x=295.0 y=451.0
:Sending Pointer ACTION_UP x=295.0 y=451.0
:Sending Pointer ACTION_MOVE x=2.0 y=-3.0
:Sending Pointer ACTION_DOWN x=249.0 y=465.0
:Sending Pointer ACTION_UP x=249.0 y=465.0
:Sending Pointer ACTION_DOWN x=153.0 y=336.0
:Sending Pointer ACTION_UP x=152.0 y=345.0
:Sending Pointer ACTION_DOWN x=65.0 y=316.0
:Sending Pointer ACTION_UP x=65.0 y=316.0
// Sending event #400
:Sending Pointer ACTION_MOVE x=4.0 y=3.0
:Sending Pointer ACTION_DOWN x=92.0 y=470.0
:Sending Pointer ACTION_UP x=92.0 y=470.0
:Sending Pointer ACTION_MOVE x=2.0 y=-1.0
:Sending Pointer ACTION_DOWN x=289.0 y=234.0
:Sending Pointer ACTION_UP x=289.0 y=234.0
:Sending Pointer ACTION_DOWN x=45.0 y=452.0
:Sending Pointer ACTION_UP x=45.0 y=452.0
:Sending Pointer ACTION_MOVE x=2.0 y=2.0
:Sending Pointer ACTION_DOWN x=59.0 y=459.0
:Sending Pointer ACTION_UP x=59.0 y=459.0
:Sending Pointer ACTION_MOVE x=-4.0 y=1.0
:Sending Pointer ACTION_DOWN x=57.0 y=384.0
:Sending Pointer ACTION_UP x=57.0 y=384.0
:Sending Pointer ACTION_DOWN x=304.0 y=165.0
:Sending Pointer ACTION_UP x=304.0 y=166.0
:Sending Pointer ACTION_MOVE x=1.0 y=3.0
:Sending Pointer ACTION_MOVE x=-2.0 y=-3.0
Events injected: 500
: Dropped: keys=0 pointers=0 trackballs=0 flips=0
## Network stats: elapsed time=7178ms (0ms mobile, 7178ms wifi, 0ms not connected)
// Monkey finished

For more information on monkey and it’s configuration options you can visit the Android SDK webiste: http://developer.android.com/guide/developing/tools/monkey.html

Caution
Before you get to ambitious and want to fire like a 30.000 events to your application make sure that you read the next sentence well! Once the monkey has been started, he can’t be stopped! You can however quit the command line and hope that it sends some kind of kill command to the monkey running on the device or emulator but it won’t stop on your device or emulator. So if you want to fire a really large number of events do it on an emulator (you can always close the emulator when you want) or do it on a device that you don’t need the next couple of hours!

Geef een reactie

Fill in your details below or click an icon to log in:

WordPress.com logo

Je reageert onder je WordPress.com account. Log Out / Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log Out / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log Out / Bijwerken )

Verbinden met %s


Follow

Get every new post delivered to your Inbox.