Skip to content

Instantly share code, notes, and snippets.

@jccarbonfive
Created July 17, 2012 05:08
Show Gist options
  • Save jccarbonfive/3127294 to your computer and use it in GitHub Desktop.
Save jccarbonfive/3127294 to your computer and use it in GitHub Desktop.
$ cd sample
$ ant debug install
$ cd ../sampletest
$ ant test
test:
[echo] Running tests ...
[exec]
[exec] com.sample.GreeterTest:
[exec] Failure in testExpectAGreetingAfterSubmittingAName:
[exec] junit.framework.AssertionFailedError: Greeting not found
...
[exec] FAILURES!!!
[exec] Tests run: 1, Failures: 1, Errors: 0
$ cd sampletest
$ ant debug install
$ adb shell am instrument -w -e class com.sample.MainTest com.sample.tests/android.test.InstrumentationTestRunner com.sample.MainTest:
Failure in testExpectToIntendToStartANewActivityAfterSubmittingAName:
junit.framework.AssertionFailedError: Intent not started
...
Test results for InstrumentationTestRunner=.F
Time: 0.284
FAILURES!!!
$ cd sample
$ ant debug install
$ cd ../sampletest
$ adb shell am instrument -w -e class com.sample.MainTest com.sample.tests/android.test.InstrumentationTestRunner ests/android.test.InstrumentationTestRunner
com.sample.MainTest:.
Test results for InstrumentationTestRunner=.
Time: 0.268
OK (1 test)
$ ant test
test:
[echo] Running tests ...
[exec]
[exec] com.sample.GreeterTest:.
[exec] com.sample.MainTest:.
[exec] Test results for InstrumentationTestRunner=..
[exec] Time: 4.99
[exec]
[exec] OK (2 tests)
[exec]
[exec]
BUILD SUCCESSFUL
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Greeting" />
</application>
</manifest>
package com.sample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Greeting extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String greeting = String.format("Hello %s", name);
TextView textView = new TextView(this);
textView.setText(greeting);
setContentView(textView);
}
}
package com.sample;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit"
/>
</LinearLayout>
package com.sample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText) findViewById(R.id.name);
String name = editText.getText().toString();
Intent greeting = new Intent(Main.this, Greeting.class);
greeting.putExtra("name", name);
startActivity(greeting);
}
});
}
}
package com.sample;
import android.test.ActivityUnitTestCase;
import android.app.Activity;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Button;
public class MainTest extends ActivityUnitTestCase<Main> {
public MainTest() {
super(Main.class);
}
public void testExpectToIntendToStartANewActivityAfterSubmittingAName() {
Intent intent = new Intent(getInstrumentation().getTargetContext(),
Main.class);
startActivity(intent, null, null);
Activity activity = getActivity();
String name = "Test User";
EditText editText = (EditText) activity.findViewById(R.id.name);
editText.setText(name);
Button submit = (Button) activity.findViewById(R.id.submit);
submit.performClick();
Intent startedIntent = getStartedActivityIntent();
assertNotNull("Intent not started", startedIntent);
assertEquals(name, startedIntent.getStringExtra("name"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment