android 6
DESCRIPTION
android 6TRANSCRIPT
![Page 1: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/1.jpg)
Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
CS 696 Mobile Phone Application DevelopmentFall Semester, 2009
Doc 6 TestingSept 16, 2009
![Page 2: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/2.jpg)
Testing & Subversion References
2
JUnit Cookbook http://junit.sourceforge.net/doc/cookbook/cookbook.htm
JUnit Test Infected: Programmers Love Writing Tests http://junit.sourceforge.net/doc/testinfected/testing.htm
JUnit Javadoc: http://www.junit.org/junit/javadoc/3.8/index.htm, http://junit.org/junit/javadoc/4.5/
JUnit FAQ, http://junit.sourceforge.net/doc/faq/faq.htm
Brian Marick’s Testing Web Site: http://www.exampler.com/testing-com/
Testing for Programmers, Brian Marick, Available at: http://www.exampler.com/testing-com/writings.html
Android Examples supplied with Android 1.6
Android Documentation, http://developer.android.com/reference/android/test/ActivityTestCase.html
![Page 3: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/3.jpg)
Johnson's Law
If it is not tested it does not work
The more time between coding and testing
More effort is needed to write tests More effort is needed to find bugs Fewer bugs are found Time is wasted working with buggy code Development time increases Quality decreases
Testing
3
![Page 4: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/4.jpg)
Unit Testing
4
Tests individual code segments
Automated tests
![Page 5: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/5.jpg)
Using print statements
Writing driver program in main
Writing small sample programs to run code
Running program and testing it be using it
What wrong with:
5
![Page 6: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/6.jpg)
First write the tests
Then write the code to be tested
Writing tests first saves time
Makes you clear of the interface & functionality of the code
Removes temptation to skip tests
When to Write Tests
6
![Page 7: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/7.jpg)
Everything that could possibly break
Test values Inside valid range Outside valid range On the boundary between valid/invalid
GUIs are very hard to test Keep GUI layer very thin Unit test program behind the GUI, not the GUI
What to Test
7
![Page 8: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/8.jpg)
Adapted with permission from “A Short Catalog of Test Ideas” by Brian Marick, http://www.testing.com/writings.html
StringsEmpty String
CollectionsEmpty CollectionCollection with one elementCollection with duplicate elementsCollections with maximum possible size
NumbersZeroThe smallest numberJust below the smallest numberThe largest numberJust above the largest number
Common Things Programs Handle Incorrectly
8
![Page 9: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/9.jpg)
XUnit
9
Free frameworks for Unit testing
SUnit originally written by Kent Beck 1994
JUnit written by Kent Beck & Erich Gamma
Available at: http://www.junit.org/
Ports to many languages at: http://www.xprogramming.com/software.htm
![Page 10: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/10.jpg)
Sample JUnit 4.x Example
10
import static org.junit.Assert.*;import java.util.ArrayList;import org.junit.Before;import org.junit.Test;
public class HelloWorldTest { int testValue; @Test public void testMe() { assertEquals(1, testValue); } @Test public void foo() { assertTrue(2 == testValue); }
@Test(expected=IndexOutOfBoundsException.class) public void testIndexOutOfBoundsException() { ArrayList emptyList = new ArrayList(); Object notValid = emptyList.get(0); } @Before public void initialize(){ testValue = 1; }}
![Page 11: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/11.jpg)
Goal: Implement a Stack containing integers.
Tests: Subclass junit.framework.TestCase Methods starting with 'test" are run by TestRunner
import junit.framework.*;public class TestStack extends TestCase {
public void testDefaultConstructor() { Stack test = new Stack(); assertTrue("Default constructor", test.isEmpty() ); } public void testSizeConstructor() { Stack test = new Stack(5); assertTrue( test.isEmpty() ); }}
JUnit Example - JUnit 3.x
11
![Page 12: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/12.jpg)
public class Stack { int[] elements; int topElement = -1; public Stack() { this(10); }
public Stack(int size) { elements = new int[size]; } public boolean isEmpty() { return topElement == -1; }}
Start of Stack Class
12
![Page 13: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/13.jpg)
assertTrue()assertFalse()assertEquals()assertNotEquals()assertSame()assertNotSame()assertNull()assertNotNull()fail()
For a complete list see
http://www.junit.org/junit/javadoc/3.8/index.htm/allclasses-frame.html/junit/junit/framework/Assert.html/Assert.html
Assert Methods
13
![Page 14: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/14.jpg)
If can be useful to modify the code to break the tests
package example;
public class Stack { int[] elements; int topElement = -1; etc. public boolean isEmpty() { return topElement == 1; }}
Testing the Tests
14
![Page 15: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/15.jpg)
Before each test setUp() is runAfter each test tearDown() is run
package example;import junit.framework.TestCase;
public class StackTest extends TestCase { Stack test;
public void setUp() { test = new Stack(5); for (int k = 1; k <=5;k++) test.push( k); }
public void testPushPop() { for (int k = 5; k >= 1; k--) assertEquals( "Pop fail on element " + k, test.pop() , k); }}
Test Fixtures - JUnit 3.x
15
![Page 16: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/16.jpg)
public void testIndexOutOfBoundsException() { ArrayList list = new ArrayList(10); try { Object o = list.get(11); fail("Should raise an IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException success) {}}
Example is from the JUnit FAQ
Testing Exceptions - JUnit 3.x
16
![Page 17: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/17.jpg)
17
Android Testing
![Page 18: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/18.jpg)
18
Android JUnit
![Page 19: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/19.jpg)
Main Classes
19
ActivityUnitTestCaseUnit testing of your Activity
ActivityInstrumentationTestCase2Functional Testing of activities
![Page 20: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/20.jpg)
DatabaseExampleTest
20
public class DatabaseExampleTest extends ActivityInstrumentationTestCase2<DatabaseExample> {
public DatabaseExampleTest() { super("edu.sdsu.cs696", DatabaseExample.class); }
protected void setUp() throws Exception { super.setUp(); }
public void testTest() { assertTrue(2 == 2); }
testTest is just to show a very simple test. It was the first test I ran to make sure I could run the framework
![Page 21: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/21.jpg)
Simple Test of View Existance
21
public void testPreconditions() { Button readButton = getReadButton(); assertNotNull(getActivity()); assertNotNull(readButton); }
private Button getReadButton() { return (Button) getActivity().findViewById(edu.sdsu.cs696.R.id.read); }
getActivity() is an existing method that returns the activity you are testing.
![Page 22: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/22.jpg)
Testing insert
22
public void testInsert() { try { Cursor result = getRowsWithName("Cat"); int rowCount = result.getCount(); assertTrue(rowCount == 0); insert("10", "Cat"); result = getRowsWithName("Cat"); rowCount = result.getCount(); assertTrue(rowCount == 1); } finally { deleteCatRows(); } }
![Page 23: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/23.jpg)
getRowsWithName()
23
private Cursor getRowsWithName(String name) { Cursor result; String[] columns = new String[] { DatabaseHelper.ID, DatabaseHelper.NAME, }; String[] sqlArguments = new String[] { name }; result = getActivity().managedQuery(NameProvider.CONTENT_URI, columns, " NAME = ?", sqlArguments, null); return result; }
![Page 24: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/24.jpg)
insert
24
private void insert(final String id, final String name) { getActivity().runOnUiThread(new Runnable() { public void run() { EditText idField = getIDEditText(); EditText nameField = getNameEditText(); idField.setText((CharSequence) id); nameField.setText((CharSequence) name); Button insertButton = getInsertButton(); insertButton.performClick(); } }); // wait for the request to go through getInstrumentation().waitForIdleSync(); }}
![Page 25: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/25.jpg)
Some Gets
25
private Button getInsertButton() { return (Button) getActivity().findViewById(edu.sdsu.cs696.R.id.insert); }
private EditText getIDEditText() { return (EditText) getActivity().findViewById( edu.sdsu.cs696.R.id.databaseId); }
private EditText getNameEditText() { return (EditText) getActivity().findViewById(edu.sdsu.cs696.R.id.name); }
![Page 26: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/26.jpg)
delete
26
private void deleteCatRows() { int rowsDeleted = getActivity().getContentResolver().delete( NameProvider.CONTENT_URI, "NAME = 'Cat'", null); Log.i("test", "deleted rows after insert " + rowsDeleted); }
The Log.i method is not needed, but logging is one way to help find out what is going on when things don't work
![Page 27: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/27.jpg)
27
Monkey - Random Testing
![Page 28: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/28.jpg)
28
Monkey
Generates random events for your activity
Enters text
Click buttons
Selects menus
Rotates screen
etc.
![Page 29: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/29.jpg)
Terse output
29
Al pro 21->adb shell monkey -p edu.sdsu.cs696 500 // activityResuming(com.android.launcher)
![Page 30: Android 6](https://reader033.vdocuments.us/reader033/viewer/2022051621/55cf8def550346703b8cd1f6/html5/thumbnails/30.jpg)
First Verbose Level
30
Al pro 22->adb shell monkey -p edu.sdsu.cs696 -v 500:Monkey: seed=0 count=500:AllowPackage: edu.sdsu.cs696:IncludeCategory: android.intent.category.LAUNCHER:IncludeCategory: android.intent.category.MONKEY// Event percentages:// 0: 15.0%// 1: 10.0%// 2: 15.0%// 3: 25.0%// 4: 15.0%etc...:Sending Pointer ACTION_UP x=304.0 y=6.0:Sending Pointer ACTION_MOVE x=1.0 y=3.0:Sending Pointer ACTION_MOVE x=-2.0 y=-3.0:Dropped: keys=0 pointers=0 trackballs=0 flips=0// Monkey finished