Table Of ContentAndroid Application Testing
Guide
Build intensively tested and bug free Android
applications
Diego Torres Milano
BIRMINGHAM - MUMBAI
Android Application Testing Guide
Copyright © 2011 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2011
Production Reference: 1170611
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849513-50-0
www.packtpub.com
Cover Image by Asher Wishkerman (a.wishkerman@mpic.de )
Credits
Author Project Coordinator
Diego Torres Milano Joel Goveya
Reviewers Proofreaders
Paul Bourdeaux Aaron Nash
Noah Eltzroth Stephen Silk
Tomas Malmsten
Indexer
Gabor Paller
Hemangini Bari
Abhinav Tyagi
Graphics
Acquisition Editor Nilesh Mohite
Tarun Singh
Production Coordinator
Development Editor Kruthika Bangera
Chris Rodrigues
Cover Work
Technical Editor Kruthika Bangera
Conrad Sardinha
About the Author
Diego Torres Milano has been involved with the Android platform since its
inception, at the end of 2007, when he started exploring and researching the platform
possibilities, mainly in the areas of User Interfaces, Unit and Acceptance Tests, and
Test Driven Development.
This is reflected by a number of articles mainly published in his personal blog
(http://dtmilano.blogspot.com) and his participation as a lecturer in some
conferences and courses like Mobile Dev Camp 2008 in Amsterdam (Netherlands)
and Japan Linux Symposium 2009 (Tokyo), Droidcon London 2009, Skillsmatter
2009 (London, UK), and he has also authored Android training courses delivered to
various companies in Europe.
Previously, he was the founder and developer of several Open Source projects,
mainly CULT Universal Linux Thin Project (cult-thinclient.sf.net) and the
very successful PXES Universal Linux Thin Client project (that was later acquired
by 2X Software, www.2x.com). PXES is a Linux-based Operating System specialized
for thin clients used by hundreds of thousands of thin clients all over the world. This
project has a popularity peak of 35M hits and 400K downloads from SourceForge
in 2005. This project had a dual impact: big companies in Europe decided to use it
because of improved security and efficiency; organizations, institutions, and schools
in some developing countries in South America, Africa, and Asia decided to use
it because of the minimal hardware requirements to have a huge social impact
providing computers, sometimes recycled ones, to everyone.
Among the other Open Source projects that he has founded we can mention
Autoglade, Gnome-tla, JGlade, and he has been contributing to various Linux
distributions such as RedHat, Fedora, and Ubuntu.
He also has been giving presentations in Linux World, LinuxTag, GUADEC ES,
University of Buenos Aires, and so on.
He has been developing software, participating in Open Source projects, and
advising companies worldwide for more than 15 years.
He can be contacted at dtmilano@gmail.com.
Firstly, I would like to thank my family: Laura, Augusto and Octavio
for their patience and consideration. The time I borrowed to achieve
this goal was mostly theirs.
Secondly I would like to thank my personal friend and IN3
Integracion Informatica co-founder, Caludio Palonsky, with whom
we started this amazing adventure more than 15 years ago when we
pioneered the provision of Linux services and support to enterprises
in South America. He certainly taught me to be a bit more consultant
and a bit less hacker (but I'm a very bad student :-)). And special
thanks to Ricston's Peter Delia with whom we started providing
Android training services throughout Europe as early as mid 2008
when Android was just a beautiful dream of having a mainstream
Open Source operating system in the mobile arena. This is now a
reality dictated by the market.
And lastly I would like to thank all the reviewers and the Packt
Publishing team who gave me their opinion, suggestions, and
corrections on early manuscripts; without them the book would
never have had the quality it endowed.
About the Reviewers
Paul Bourdeaux is the Senior Software Engineer and Application Development
Team Lead at Sundog, a marketing and technology company based in the Midwest.
He has a strong background in traditional software engineering, has authored
several white papers relating to mobile marketing and software engineering, and has
presented at both national and regional software engineering conferences. Paul is the
mobile marketing expert at Sundog, and his passion lies in mobile and cloud based
software engineering.
Noah Eltzroth teaches dynamic web development at the Sullivan College of
Technology and Design in Louisville, Kentucky. He enjoys working on a variety of
different software projects including business-oriented portals, data processing, and
Android applications. In his free time, Noah enjoys programming in both Java and
PHP.
Tomas Malmsten has been working with software development for over a
decade. During this time he has had the opportunity to work with a vast variety of
technologies in various different business settings. His main focus has been the Java
ecosystem where he has worked with everything from large enterprise systems to
Android application development.
Tomas is a passionate software craftsman who strives for excellence in all aspects of
programming. From customer service and interaction to well crafted maintainable
programs.
You can get in touch with Tomas through any of the following means:
• Blog: http://www.tomasmalmsten.com
• Twitter: http://twitter.com/tomasmalmsten
• E-mail: public@tomasmalmsten.com
• LinkedIn: http://se.linkedin.com/in/tomasmalmsten
Gábor Paller received his MSc. and PhD. degrees from the Technical University
of Budapest in 1992 and 1996, respectively. Dr. Paller joined Nokia in 1998 and held
positions in Nokia R&D and Nokia Research Center. His interests included wireless
protocol development, mobile device management, mobile Java and middleware.
He was also involved in standardization and joint research program activities.
After having left Nokia, he worked at OnRelay Ltd. on fixed-mobile convergence
technologies, and then in 2010 he joined Ericsson where he works on IMS. Gabor
Paller runs the popular My Life with Android blog and reviewed a number of
Android books.
Abhinav Tyagi is a Computer Science graduate from Pune. He also holds a post
graduate diploma in Advanced Computing from CDAC, Pune. He developed several
Android applications while working as a Software Engineer at Antarix Networks,
Mumbai.
He is currently working on telecom protocols as a Research & Development Engineer
at Nokia Siemens Networks.
I would like to thank Joel Goveya and Tarun Singh for giving me this
wonderful opportunity.
Table of Contents
Preface 1
Chapter 1: Getting Started with Testing 7
Brief history 7
Software bugs 8
How bugs severely affect your projects 9
Why, what, how, and when to test 9
What to test 11
Activity lifecycle events 12
Database and filesystem operations 12
Physical characteristics of the device 12
Types of tests 13
Unit tests 13
The test fixture 15
The setUp() method 15
The tearDown() method 15
Test preconditions 16
The actual tests 16
Integration tests 20
Functional or acceptance tests 20
Test case scenario 22
Performance tests 22
System tests 23
Android testing framework 23
Instrumentation 23
Test targets 25
Summary 26
Chapter 2: Testing on Android 27
JUnit 28
Creating the Android main project 28
Creating the Android test project 29
Table of Contents
Package explorer 31
Creating a test case 32
Special methods 36
Test annotations 36
Running the tests 37
Running all tests from Eclipse 37
Running a single test case from Eclipse 38
Running from the emulator 39
Running tests from the command line 41
Running all tests 42
Running tests from a specific test case 42
Running a specific test by name 42
Running specific tests by category 43
Running performance tests 44
Dry run 44
Debugging tests 45
Other command-line options 47
Summary 47
Chapter 3: Building Blocks on the Android SDK 49
The demonstration application 50
Assertions in depth 50
Custom messages 52
Static imports 52
View assertions 53
Even more assertions 55
The TouchUtils class 57
Mock Objects 58
MockContext overview 59
The IsolatedContext class 59
Alternate route to file and database operations 60
The MockContentResolver class 60
The TestCase base class 61
The no-argument constructor 61
The given name constructor 62
The setName() method 62
The AndroidTestCase base class 62
The assertActivityRequiresPermission() method 63
Description 64
Example 64
The assertReadingContentUriRequiresPermission method 64
Description 64
Example 65
[ ii ]