Skip to content

Instantly share code, notes, and snippets.

@colabug
Created July 23, 2012 20:03

Revisions

  1. colabug revised this gist Aug 6, 2012. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions RevealFunctionality.java
    Original file line number Diff line number Diff line change
    @@ -9,12 +9,14 @@ public void setUp() throws Exception
    }

    @Test
    public void shouldHaveHiddenButton() throws Exception {
    public void shouldHaveHiddenButton() throws Exception
    {
    assertViewIsHidden( button );
    }

    @Test
    public void shouldShowButtonAfterImageClick() throws Exception {
    public void shouldShowButtonAfterImageClick() throws Exception
    {
    image.performClick();
    assertViewIsVisible( button );
    }
  2. colabug revised this gist Aug 6, 2012. 10 changed files with 81 additions and 54 deletions.
    9 changes: 5 additions & 4 deletions ActivityStart.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    @Test
    public void buttonClickShouldStartNewActivity() throws Exception {
    Button button = activity.findViewById(R.id.next_screen_button);
    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));
    Intent intent = NewActivity.createIntent( activity );
    assertThat( activity, new StartedMatcher( intent ) );
    }
    9 changes: 5 additions & 4 deletions DrawableResource.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    @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)));
    public void imageShouldEqualResourceDrawable() throws Exception
    {
    ImageView image = (ImageView) activity.findViewById( R.id.sun_earth_image );
    assertThat( image.getDrawable(),
    equalTo( getResourceDrawable( R.drawable.sun_earth ) ) );
    }
    2 changes: 1 addition & 1 deletion FragmentPresence.java
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ public void shouldHaveNewFragment() throws Exception
    assertNotNull( newActivity.getSupportFragmentManager().findFragmentById( R.id.new_fragment ) );
    }

    <!--Fragment XML declaration-->
    <!--Fragment XML declaration in activity's XML-->
    <fragment
    android:id="@+id/new_fragment"
    android:name="com.colabug.NewFragment"
    33 changes: 20 additions & 13 deletions MockFunctionality.java
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,40 @@
    // Test that certain functionality was called
    @Test
    public void buttonShouldDoSomethingCrazy() throws Exception {
    public void buttonShouldDoSomethingCrazy() throws Exception
    {
    button.performClick();
    assertTrue(activity.conditionWasMet);
    assertTrue( activity.conditionWasMet );
    }

    // Mock class
    class TestNewActivity extends NewActivity {
    class TestNewActivity extends NewActivity
    {
    protected boolean conditionWasMet = false;

    @Override
    protected void doSomethingCrazy() {
    @Override
    protected void doSomethingCrazy()
    {
    conditionWasMet = true;
    }
    }

    // Original class
    public class NewActivity extends Activity {
    public class NewActivity extends Activity
    {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_activity);
    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) {
    View button = findViewById( R.id.crazy_button );
    button.setOnClickListener( new View.OnClickListener()
    {
    public void onClick( View view )
    {
    doSomethingCrazy();
    }
    });
    } );
    }

    protected void doSomethingCrazy() {}
    5 changes: 3 additions & 2 deletions Nullness.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    @Test
    public void shouldNotBeNull() throws Exception {
    public void shouldNotBeNull() throws Exception
    {
    MyActivity activity = new MyActivity();
    assertNotNull(activity);
    assertNotNull( activity );
    }
    7 changes: 4 additions & 3 deletions Presence.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    @Test
    public void shouldHaveWelcomeText() throws Exception {
    TextView welcomeText = (TextView) activity.findViewById(R.id.welcome_text_view);
    assertViewIsVisible(welcomeText);
    public void shouldHaveWelcomeText() throws Exception
    {
    TextView welcomeText = (TextView) activity.findViewById( R.id.welcome_text_view );
    assertViewIsVisible( welcomeText );
    }
    39 changes: 26 additions & 13 deletions ProjectTestRunner.java
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,37 @@
    public class ProjectTestRunner extends RobolectricTestRunner {
    public class ProjectTestRunner extends RobolectricTestRunner
    {
    public AnimationTestRunner( Class<?> testClass ) throws InitializationError
    {
    super( testClass );
    }

    public ProjectTestRunner(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 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 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 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));
    public static void assertViewIsHidden( View view )
    {
    assertNotNull( view );
    assertThat( view.getVisibility(), equalTo( View.GONE ) );
    }
    }
    13 changes: 7 additions & 6 deletions RevealFunctionality.java
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,20 @@
    @Before
    public void setUp() throws Exception {
    public void setUp() throws Exception
    {
    MyActivity activity = new MyActivity();
    activity.onCreate(null);
    activity.onCreate( null );

    ImageView image = (ImageView) activity.findViewById(R.id.sun_earth_image);
    Button button = (Button) activity.findViewById(R.id.next_screen_button);
    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);
    assertViewIsHidden( button );
    }

    @Test
    public void shouldShowButtonAfterImageClick() throws Exception {
    image.performClick();
    assertViewIsVisible(button);
    assertViewIsVisible( button );
    }
    9 changes: 5 additions & 4 deletions StringResource.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    @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)));
    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 ) ) );
    }
    9 changes: 5 additions & 4 deletions Toasting.java
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    @Test
    public void welcomeTextClickShouldToastUser() throws Exception {
    TextView welcomeText = (TextView) activity.findViewById(R.id.welcome_text_view);
    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)));
    assertThat( getTextOfLatestToast(),
    equalTo( getResourceString( R.string.WELCOME_TOAST ) ) );
    }
  3. colabug revised this gist Aug 6, 2012. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions FragmentPresence.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    @Test
    public void shouldHaveNewFragment() throws Exception
    {
    NewActivity newActivity = new NewActivity();
    assertNotNull( newActivity.getSupportFragmentManager().findFragmentById( R.id.new_fragment ) );
    }

    <!--Fragment XML declaration-->
    <fragment
    android:id="@+id/new_fragment"
    android:name="com.colabug.NewFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
  4. colabug renamed this gist Jul 24, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. colabug revised this gist Jul 24, 2012. 1 changed file with 34 additions and 0 deletions.
    34 changes: 34 additions & 0 deletions Mock functionality
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // 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() {}
    }
  6. colabug revised this gist Jul 24, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion RevealFunctionality.java
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ public void setUp() throws Exception {

    @Test
    public void shouldHaveHiddenButton() throws Exception {
    Button button = activity.findViewById(R.id.next_screen_button);
    assertViewIsHidden(button);
    }

  7. colabug revised this gist Jul 24, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions RevealFunctionality.java
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    @Before
    public void setUp() throws Exception {
    activity = new MyActivity();
    MyActivity activity = new MyActivity();
    activity.onCreate(null);

    image = (ImageView) activity.findViewById(R.id.sun_earth_image);
    button = activity.findViewById(R.id.next_screen_button);
    ImageView image = (ImageView) activity.findViewById(R.id.sun_earth_image);
    Button button = (Button) activity.findViewById(R.id.next_screen_button);
    }

    @Test
  8. colabug revised this gist Jul 23, 2012. 2 changed files with 0 additions and 12 deletions.
    6 changes: 0 additions & 6 deletions Activity Start,java
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    @Test
    public void buttonClickShouldStartNewActivity() throws Exception {
    button.performClick();
    Intent intent = NewActivity.createIntent(activity);
    assertThat(activity, new StartedMatcher(intent));
    }
    6 changes: 0 additions & 6 deletions Activity Startjava
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    @Test
    public void buttonClickShouldStartNewActivity() throws Exception {
    button.performClick();
    Intent intent = NewActivity.createIntent(activity);
    assertThat(activity, new StartedMatcher(intent));
    }
  9. colabug revised this gist Jul 23, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Activity Startjava
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    @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));
  10. colabug revised this gist Jul 23, 2012. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions ProjectTestRunner.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    public class ProjectTestRunner extends RobolectricTestRunner {

    public ProjectTestRunner(Class<?> testClass) throws InitializationError {
    super(testClass);
    }

    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));
    }
    }
  11. colabug revised this gist Jul 23, 2012. 8 changed files with 23 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Activity Startjava
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    @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));
    }
    1 change: 1 addition & 0 deletions ActivityStart.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    @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));
    1 change: 1 addition & 0 deletions DrawableResource.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    @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)));
    }
    1 change: 1 addition & 0 deletions Nullness.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    @Test
    public void shouldNotBeNull() throws Exception {
    MyActivity activity = new MyActivity();
    assertNotNull(activity);
    }
    1 change: 1 addition & 0 deletions Presence.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    @Test
    public void shouldHaveWelcomeText() throws Exception {
    TextView welcomeText = (TextView) activity.findViewById(R.id.welcome_text_view);
    assertViewIsVisible(welcomeText);
    }
    10 changes: 10 additions & 0 deletions RevealFunctionality.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,15 @@
    @Before
    public void setUp() throws Exception {
    activity = new MyActivity();
    activity.onCreate(null);

    image = (ImageView) activity.findViewById(R.id.sun_earth_image);
    button = activity.findViewById(R.id.next_screen_button);
    }

    @Test
    public void shouldHaveHiddenButton() throws Exception {
    Button button = activity.findViewById(R.id.next_screen_button);
    assertViewIsHidden(button);
    }

    1 change: 1 addition & 0 deletions StringResource.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    @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)));
    }
    1 change: 1 addition & 0 deletions Toasting.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    @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)));
  12. colabug revised this gist Jul 23, 2012. No changes.
  13. colabug revised this gist Jul 23, 2012. 7 changed files with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions ActivityStart.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    @Test
    public void buttonClickShouldStartNewActivity() throws Exception {
    button.performClick();
    Intent intent = NewActivity.createIntent(activity);
    assertThat(activity, new StartedMatcher(intent));
    }
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  14. colabug renamed this gist Jul 23, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  15. @invalid-email-address Anonymous created this gist Jul 23, 2012.
    6 changes: 6 additions & 0 deletions Activity Start
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    @Test
    public void buttonClickShouldStartNewActivity() throws Exception {
    button.performClick();
    Intent intent = NewActivity.createIntent(activity);
    assertThat(activity, new StartedMatcher(intent));
    }
    5 changes: 5 additions & 0 deletions Drawable Resource
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    @Test
    public void imageShouldEqualResourceDrawable() throws Exception {
    assertThat(image.getDrawable(),
    equalTo(getResourceDrawable(R.drawable.sun_earth)));
    }
    4 changes: 4 additions & 0 deletions Presence
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    @Test
    public void shouldHaveWelcomeText() throws Exception {
    assertViewIsVisible(welcomeText);
    }
    10 changes: 10 additions & 0 deletions Reveal Functionality
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    @Test
    public void shouldHaveHiddenButton() throws Exception {
    assertViewIsHidden(button);
    }

    @Test
    public void shouldShowButtonAfterImageClick() throws Exception {
    image.performClick();
    assertViewIsVisible(button);
    }
    5 changes: 5 additions & 0 deletions String Resource
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    @Test
    public void welcomeTextShouldEqualResource() throws Exception {
    assertThat(welcomeText.getText().toString(),
    equalTo(getResourceString(R.string.WELCOME_STRING)));
    }
    4 changes: 4 additions & 0 deletions Tests.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    @Test
    public void shouldNotBeNull() throws Exception {
    assertNotNull(activity);
    }
    6 changes: 6 additions & 0 deletions Toasting
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    @Test
    public void welcomeTextClickShouldToastUser() throws Exception {
    welcomeText.performClick();
    assertThat(getTextOfLatestToast(),
    equalTo(getResourceString(R.string.WELCOME_TOAST)));
    }