Skip to content

Commit

Permalink
example android unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekpneo committed Feb 3, 2011
1 parent 302be46 commit 325a721
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 0 deletions.
8 changes: 8 additions & 0 deletions android-test/.gitignore
@@ -0,0 +1,8 @@
*~
*#
.#*
local.properties
TAGS
gen
bin
res/values/version.xml
22 changes: 22 additions & 0 deletions android-test/AndroidManifest.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gov.nasa.arc.geocam.geocam.tests"
android:versionCode="1"
android:versionName="1.0">

<!-- We add an application tag here just so that we can indicate that
this package needs to link against the android.test library,
which is needed when building test cases. -->
<application>
<uses-library android:name="android.test.runner" />
</application>
<!--
This declares that this application uses the instrumentation test runner targeting
the package of gov.nasa.arc.geocam.geocam. To run the tests use the command:
"adb shell am instrument -w gov.nasa.arc.geocam.geocam.tests/android.test.InstrumentationTestRunner"
-->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="gov.nasa.arc.geocam.geocam"
android:label="Tests for gov.nasa.arc.geocam.geocam"/>
</manifest>
18 changes: 18 additions & 0 deletions android-test/build.properties
@@ -0,0 +1,18 @@
# This file is used to override default values used by the Ant build system.
#
# This file must be checked in Version Control Systems, as it is
# integral to the build system of your project.

# This file is only used by the Ant script.

# You can use this to override default values such as
# 'source.dir' for the location of your java source folder and
# 'out.dir' for the location of your output folder.

# You can also use it define how the release builds are signed by declaring
# the following properties:
# 'key.store' for the location of your keystore and
# 'key.alias' for the name of the key to use.
# The password will be asked during the build when you use the 'release' target.

tested.project.dir=../android
84 changes: 84 additions & 0 deletions android-test/build.xml
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="geocammobile_test" default="help">

<!-- The local.properties file is created and updated by the 'android'
tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />

<!-- The build.properties file can be created by you and is never touched
by the 'android' tool. This is the place to change some of the
default property values used by the Ant rules.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="build.properties" />

<!-- The default.properties file is created and updated by the 'android'
tool, as well as ADT.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<property file="default.properties" />

<!-- Custom Android task to deal with the project target, and import the
proper rules.
This requires ant 1.6.0 or above. -->
<path id="android.antlibs">
<pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
<pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
<pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
</path>

<taskdef name="setup"
classname="com.android.ant.SetupTask"
classpathref="android.antlibs" />

<!-- extension targets. Uncomment the ones where you want to do custom work
in between standard targets -->
<!--
<target name="-pre-build">
</target>
<target name="-pre-compile">
</target>
[This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir}]
<target name="-post-compile">
</target>
-->


<!-- Execute the Android Setup task that will setup some properties
specific to the target, and import the build rules files.
The rules file is imported from
<SDK>/platforms/<target_platform>/ant/ant_rules_r#.xml
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<setup> task.
- customize it to your needs.
- Customize the whole script.
- copy/paste the content of the rules files (minus the top node)
into this file, *after* the <setup> task
- disable the import of the rules by changing the setup task
below to <setup import="false" />.
- customize to your needs.
-->
<setup />

</project>
11 changes: 11 additions & 0 deletions android-test/default.properties
@@ -0,0 +1,11 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=Google Inc.:Google APIs:8
34 changes: 34 additions & 0 deletions android-test/proguard.cfg
@@ -0,0 +1,34 @@
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
native <methods>;
}

-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
@@ -0,0 +1,148 @@
package gov.nasa.arc.geocam.geocam;

import android.test.ActivityUnitTestCase;
import android.test.mock.MockContext;
import android.test.suitebuilder.annotation.MediumTest;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import java.util.HashMap;

import gov.nasa.arc.geocam.geocam.GeoCamMobile;
import gov.nasa.arc.geocam.geocam.AuthorizeUserActivity;

public class AuthorizeUserActivityTest
extends ActivityUnitTestCase<AuthorizeUserActivity>
{
private Intent mStartIntent;
private Context mTargetContext;

public AuthorizeUserActivityTest() {
super(AuthorizeUserActivity.class);
}

@Override
public void setUp() throws Exception {
super.setUp();

mStartIntent = new Intent(Intent.ACTION_MAIN);
mTargetContext = getInstrumentation().getTargetContext();
}

/* Make sure the activity starts and that it doesn't think we're
already authorized to use the activity. This is the precondition
for all of the following tests */
@MediumTest
public void testPreconditions() {
assertFalse(GeoCamMobile.SETTINGS_BETA_TEST_CORRECT.equals(""));
assertNotNull(mTargetContext);

UnsavedContext context = new UnsavedContext(mTargetContext);
setActivityContext(context);
startActivity(mStartIntent, null, null);

assertNotNull(getActivity());
assertFalse(getActivity().userIsAuthorized());
}

/* Make sure that the commitKey function actually commits the key to
the sharedPreferences object. */
@MediumTest
public void testCommit() {
final String testKey = "testing key";

UnsavedContext context = new UnsavedContext(mTargetContext);
setActivityContext(context);
startActivity(mStartIntent, null, null);

AuthorizeUserActivity activity = getActivity();
activity.commitKey(testKey);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String storedKey = prefs.getString(GeoCamMobile.SETTINGS_BETA_TEST_KEY, "");
assertTrue(testKey.equals(storedKey));
}

/* Make sure that when the right key is in the shared preferences, that the
userIsAuthorized function agrees that it's the right key and that we're
allowed to proceed */
@MediumTest
public void testAuthorized() {
UnsavedContext context = new UnsavedContext(mTargetContext);
setActivityContext(context);
startActivity(mStartIntent, null, null);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor
.putString(GeoCamMobile.SETTINGS_BETA_TEST_KEY, GeoCamMobile.SETTINGS_BETA_TEST_CORRECT)
.commit();

AuthorizeUserActivity activity = getActivity();
assertTrue(activity.userIsAuthorized());
}

/* Test that entering the correct key into the edit box of the view and
clicking the button, that the activity successfully stores the key,
thinks the user is authorized and redirects to the next activity. */
@MediumTest
public void testCorrectKey() {
UnsavedContext context = new UnsavedContext(mTargetContext);
setActivityContext(context);
startActivity(mStartIntent, null, null);

AuthorizeUserActivity activity = getActivity();

Button button = (Button) activity.findViewById(R.id.authorize_user_continue_button);
EditText key = (EditText) activity.findViewById(R.id.authorize_user_key_entry);

assertNotNull(button);
assertNotNull(key);

key.setText(GeoCamMobile.SETTINGS_BETA_TEST_CORRECT);
button.performClick();

assertTrue(activity.userIsAuthorized());
assertNotNull(getStartedActivityIntent());
assertTrue(isFinishCalled());
}

/* Make sure that when the activity is started fresh, it does not redirect
and that the the user is not authorized. */
@MediumTest
public void testNotFinished() {
UnsavedContext context = new UnsavedContext(mTargetContext);
setActivityContext(context);
startActivity(mStartIntent, null, null);

assertFalse(getActivity().userIsAuthorized());
assertNull(getStartedActivityIntent());
assertFalse(isFinishCalled());
}

/* Make sure that when the correct key is stored already, that the activity
redirects to the next activity. This will be the most common behavior. */
@MediumTest
public void testFinishedCalled() {
UnsavedContext context = new UnsavedContext(mTargetContext);
setActivityContext(context);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor
.putString(GeoCamMobile.SETTINGS_BETA_TEST_KEY, GeoCamMobile.SETTINGS_BETA_TEST_CORRECT)
.commit();

startActivity(mStartIntent, null, null);

assertTrue(getActivity().userIsAuthorized());
assertNotNull(getStartedActivityIntent());
assertTrue(isFinishCalled());
}
}
41 changes: 41 additions & 0 deletions android-test/src/gov/nasa/arc/geocam/geocam/UnsavedContext.java
@@ -0,0 +1,41 @@
package gov.nasa.arc.geocam.geocam;

import android.content.Context;
import android.content.SharedPreferences;
import android.test.mock.MockContentResolver;
import android.test.IsolatedContext;
import android.util.Log;

import java.util.HashMap;
import junit.framework.Assert;

public class UnsavedContext extends IsolatedContext {
private final HashMap<String,UnsavedSharedPreferences> mSharedPrefs =
new HashMap<String,UnsavedSharedPreferences>();
private final Context mWrapper;

public UnsavedContext(Context context) {
super(context.getContentResolver(), context);
mWrapper = context;
}

@Override
public Object getSystemService(String name) {
// Need LayoutInflater to construct our activity's view
if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) {
return mWrapper.getSystemService(name);
}

return super.getSystemService(name);
}

@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
if (!mSharedPrefs.containsKey(name)) {
UnsavedSharedPreferences prefs = new UnsavedSharedPreferences(null);
mSharedPrefs.put(name, prefs);
}

return mSharedPrefs.get(name);
}
}

0 comments on commit 325a721

Please sign in to comment.