Table of Contents Package explorer Creating a test case Special methods Test annotations Running the tests Running all tests from Eclipse Running a single test case from Eclipse ulato ing tests from the command line peessyae 36373788394222344 na tests Other command-line options Ch: apter 3:Building Blocks on the Android SDK 49 The demonstratior application sertions in depth m me ages Static imports as ns en mo chutils class Altemnate route to file and database operations The Mockco Iver class The TestCase base class The no-argument constructor name constructor The se 66222 The AndroidTestCase base class The assertActivityRequiresPermission()method e assertReadingContentUriRequiresPermission method on
Table of Contents [ ii ] 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
Table of Contents The assertWritingContentUriRequiresPermission(method on Instru entation The ActivityMonitor inner class Example The InstrumentationTestCase class The launchActivity and launchActivityWithIntent method The sendKeys and sendRepeatedKeys methods The runTestOnUiThread helper method The Activity TestCase class rumentationTestCase2 class The constructor ne set The own me test on 1234556667 The erT class or iceTestcase<Ta The constructor 。 The TestSuiteBuilder.FailedToCreateTests class Using external libraries in test pr Chapter 4:Test Driven Development 85 Getting started with TDD Writing a test case Running all tests oring the co 9e? ding the re ements heing a sam ro -the Temperature Converte JsenereCceoe cept design 5686758688889960 Creating the TemperatureConverterActivity Tests project Creating the fixture % Test preconditions Creating the user interface Testing the existence of the user interface components 【
Table of Contents [ iii ] The assertWritingContentUriRequiresPermission() method 65 Description 65 Example 66 Instrumentation 66 The ActivityMonitor inner class 66 Example 67 The InstrumentationTestCase class 68 The launchActivity and launchActivityWithIntent method 69 The sendKeys and sendRepeatedKeys methods 69 The runTestOnUiThread helper method 71 The ActivityTestCase class 72 The scrubClass method 73 The ActivityInstrumentationTestCase2 class 74 The constructor 75 The setUp method 75 The tearDown method 76 The testPreconditions method 76 The ProviderTestCase2<T> class 76 The constructor 77 Example 78 The ServiceTestCase<T> 78 The constructor 79 The TestSuiteBuilder.FailedToCreateTests class 80 Using external libraries in test projects 80 Summary 84 Chapter 4: Test Driven Development 85 Getting started with TDD 85 Writing a test case 86 Running all tests 87 Refactoring the code 87 What is the advantage? 88 Understanding the requirements 88 Creating a sample project—the Temperature Converter 88 The list of requirements 89 User interface concept design 89 Creating the projects 90 Creating the TemperatureConverterActivityTests project 92 Creating the fixture 96 Test preconditions 97 Creating the user interface 97 Testing the existence of the user interface components 98
Table of Contents Getting the IDs defined Translating requirements to tests Screen layout Adding functionality Temperature conversion The EditNumber class 18 TemperatureConverter unit tests The EditNumber tests The TemperatureChangeWatcher class More TemperatureConverter tests The InputFilter tests 01191315 Viewing our final application Summary 128 Chapter 5:Android Testing Environment 129 Creating Android Virtual Devices Running AVDs from the command line Cleaning up 412131415 Terminating the emulator Additional emulator configurations Simulating network conditions Additional gemu options Running monkey Client-server monkey Test scripting with monkeyrunner Getting test screenshots Record and playback Summary Chapter 6:Behavior Driven Development 149 Brief history Given,when,then FitNesse Running FitNesse from the command line Creating a TemperatureConverterTests subwiki Adding child pages to the su Adding the supporting test classes [N]
Table of Contents [ iv ] Getting the IDs defined 98 Translating requirements to tests 99 Empty fields 100 View properties 100 Screen layout 104 Adding functionality 104 Temperature conversion 104 The EditNumber class 105 TemperatureConverter unit tests 110 The EditNumber tests 114 The TemperatureChangeWatcher class 119 More TemperatureConverter tests 123 The InputFilter tests 125 Viewing our final application 126 Summary 128 Chapter 5: Android Testing Environment 129 Creating Android Virtual Devices 129 Running AVDs from the command line 132 Headless emulator 133 Disabling the keyguard 134 Cleaning up 135 Terminating the emulator 136 Additional emulator configurations 136 Simulating network conditions 137 Additional qemu options 140 Running monkey 142 Client-server monkey 143 Test scripting with monkeyrunner 144 Getting test screenshots 145 Record and playback 147 Summary 148 Chapter 6: Behavior Driven Development 149 Brief history 149 Given, when, then 150 FitNesse 151 Running FitNesse from the command line 151 Creating a TemperatureConverterTests subwiki 152 Adding child pages to the subwiki 153 Adding the acceptance test fixture 155 Adding the supporting test classes 156
Table of Contents GivWenzen 158 Creating the test scenaric 159 Chapter 7:Testing Recipes Android Unit to and applications The Ren The Tempera ConverterApplicationTests class Testing activities Testing files,databases,and ContentProviders The BrowserProvider tests ns ren serv ce e us o of objects ane ed tes The hasToString matcher ng Views n isolation ng parsers d ne pars tivity ne parse sing for memory leaks mmar Chapter 8:Continuous Integration Building Android applications manually using Ant 218 Git-the fast version control system Creating a local git repository Continuous Integration with Hudson Installing and configuring Hudson Creating the jobs Obtaining Android test results Summary Chapter 9:Performance Testing and Profiling 243 Ye Olde Loage method Performance tests in Android SDK 8 Launching the performance test Creating the LaunchPerformanceBase instrumentation v
Table of Contents [ v ] GivWenZen 158 Creating the test scenario 159 Summary 165 Chapter 7: Testing Recipes 167 Android Unit tests 167 Testing activities and applications 170 Applications and preferences 170 The RenamingMockContext class 170 The TemperatureConverterApplicationTests class 171 Testing activities 175 Testing files, databases, and ContentProviders 181 The BrowserProvider tests 185 Testing exceptions 191 Testing local and remote services 192 Extensive use of mock objects 196 Importing libraries 198 The testTextChanged test 198 Introducing Hamcrest 202 Hamcrest matchers 203 The hasToString matcher 204 Testing Views in isolation 205 Testing parsers 209 Android assets 209 The parser activity 210 The parser test 211 Testing for memory leaks 212 Summary 215 Chapter 8: Continuous Integration 217 Building Android applications manually using Ant 218 Git—the fast version control system 224 Creating a local git repository 224 Continuous Integration with Hudson 225 Installing and configuring Hudson 226 Creating the jobs 227 Obtaining Android test results 231 Summary 240 Chapter 9: Performance Testing and Profiling 243 Ye Olde Logge method 244 Performance tests in Android SDK 246 Launching the performance test 246 Creating the LaunchPerformanceBase instrumentation 246
Table of Contents Creating the TemperatureConverterActivityLaunchPerformance class ceview and dmtracedump platform tools crobenc marks Caliper mi verterBenchmark project Running caliper 4455455万源 Summary Chapter 10:Alternative Testing Tactics 263 Building Android from source 264 Co coverage 264 EMMA features System requirements h ndroid sou cod Re0eonerco lding Steps e cove cover ge ana e resto ng the ins 279 overing th ex ept riction 283 erage target 284 ng Roboti Configuring the r 286 Crea st's JVM the Te nverterJVMTest project ain 297 Introducing Robolect ric 299 Installing Robolectric 299 Creating a new Java project 299 Writing some tests 299 Summary 303 Index 305
Table of Contents [ vi ] Creating the TemperatureConverterActivityLaunchPerformance class 248 Running the tests 249 Using the Traceview and dmtracedump platform tools 251 Microbenchmarks 255 Caliper microbenchmarks 256 Creating the TemperatureConverterBenchmark project 257 Running caliper 258 Summary 261 Chapter 10: Alternative Testing Tactics 263 Building Android from source 264 Code coverage 264 EMMA features 265 System requirements 266 Downloading the Android source code 266 Installing repo 267 Creating the working copy 267 The Building Steps 268 TemperatureConverter code coverage 270 Generating code coverage analysis report 274 Covering the restoring the instance state 279 Covering the exceptions 281 Bypassing access restrictions 282 Covering the options menu 283 The undocumented Ant coverage target 284 Introducing Robotium 285 Downloading Robotium 286 Configuring the project 286 Creating the test cases 286 The testFahrenheitToCelsiusConversion() test 286 The testOnCreateOptionsMenu() revisited 289 Testing on host's JVM 291 Creating the TemperatureConverterJVMTest project 291 Comparing the performance gain 296 Adding Android to the picture 297 Introducing Robolectric 299 Installing Robolectric 299 Creating a new Java project 299 Writing some tests 299 Summary 303 Index 305