Created
July 23, 2012 20:03
-
-
Save colabug/3165888 to your computer and use it in GitHub Desktop.
Chariot Blog Android Unit Testing With Robolectric
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void buttonClickShouldStartNewActivity() throws Exception | |
{ | |
Button button = activity.findViewById( R.id.next_screen_button ); | |
button.performClick(); | |
Intent intent = NewActivity.createIntent( activity ); | |
assertThat( activity, new StartedMatcher( intent ) ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void imageShouldEqualResourceDrawable() throws Exception | |
{ | |
ImageView image = (ImageView) activity.findViewById( R.id.sun_earth_image ); | |
assertThat( image.getDrawable(), | |
equalTo( getResourceDrawable( R.drawable.sun_earth ) ) ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void shouldHaveNewFragment() throws Exception | |
{ | |
NewActivity newActivity = new NewActivity(); | |
assertNotNull( newActivity.getSupportFragmentManager().findFragmentById( R.id.new_fragment ) ); | |
} | |
<!--Fragment XML declaration in activity's XML--> | |
<fragment | |
android:id="@+id/new_fragment" | |
android:name="com.colabug.NewFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test that certain functionality was called | |
@Test | |
public void buttonShouldDoSomethingCrazy() throws Exception | |
{ | |
button.performClick(); | |
assertTrue( activity.conditionWasMet ); | |
} | |
// Mock class | |
class TestNewActivity extends NewActivity | |
{ | |
protected boolean conditionWasMet = false; | |
@Override | |
protected void doSomethingCrazy() | |
{ | |
conditionWasMet = true; | |
} | |
} | |
// Original class | |
public class NewActivity extends Activity | |
{ | |
@Override | |
protected void onCreate( Bundle savedInstanceState ) | |
{ | |
super.onCreate( savedInstanceState ); | |
setContentView( R.layout.new_activity ); | |
View button = findViewById( R.id.crazy_button ); | |
button.setOnClickListener( new View.OnClickListener() | |
{ | |
public void onClick( View view ) | |
{ | |
doSomethingCrazy(); | |
} | |
} ); | |
} | |
protected void doSomethingCrazy() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void shouldNotBeNull() throws Exception | |
{ | |
MyActivity activity = new MyActivity(); | |
assertNotNull( activity ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void shouldHaveWelcomeText() throws Exception | |
{ | |
TextView welcomeText = (TextView) activity.findViewById( R.id.welcome_text_view ); | |
assertViewIsVisible( welcomeText ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProjectTestRunner extends RobolectricTestRunner | |
{ | |
public AnimationTestRunner( Class<?> testClass ) throws InitializationError | |
{ | |
super( testClass ); | |
} | |
public static void startFragment( Fragment fragment ) | |
{ | |
FragmentManager fragmentManager = new FragmentActivity().getSupportFragmentManager(); | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.add( fragment, null ); | |
fragmentTransaction.commit(); | |
} | |
public static String getResourceString( int resourceId ) | |
{ | |
return Robolectric.application.getApplicationContext().getString( resourceId ); | |
} | |
public static Drawable getResourceDrawable( int resourceId ) | |
{ | |
return Robolectric.application.getApplicationContext().getResources().getDrawable( resourceId ); | |
} | |
public static void assertViewIsVisible( View view ) | |
{ | |
assertNotNull( view ); | |
assertThat( view.getVisibility(), equalTo( View.VISIBLE ) ); | |
} | |
public static void assertViewIsHidden( View view ) | |
{ | |
assertNotNull( view ); | |
assertThat( view.getVisibility(), equalTo( View.GONE ) ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Before | |
public void setUp() throws Exception | |
{ | |
MyActivity activity = new MyActivity(); | |
activity.onCreate( null ); | |
ImageView image = (ImageView) activity.findViewById( R.id.sun_earth_image ); | |
Button button = (Button) activity.findViewById( R.id.next_screen_button ); | |
} | |
@Test | |
public void shouldHaveHiddenButton() throws Exception | |
{ | |
assertViewIsHidden( button ); | |
} | |
@Test | |
public void shouldShowButtonAfterImageClick() throws Exception | |
{ | |
image.performClick(); | |
assertViewIsVisible( button ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void welcomeTextShouldEqualResource() throws Exception | |
{ | |
TextView welcomeText = (TextView) activity.findViewById( R.id.welcome_text_view ); | |
assertThat( welcomeText.getText().toString(), | |
equalTo( getResourceString( R.string.WELCOME_STRING ) ) ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void welcomeTextClickShouldToastUser() throws Exception | |
{ | |
TextView welcomeText = (TextView) activity.findViewById( R.id.welcome_text_view ); | |
welcomeText.performClick(); | |
assertThat( getTextOfLatestToast(), | |
equalTo( getResourceString( R.string.WELCOME_TOAST ) ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment