1 package com.xtremelabs.robolectric.shadows; 2 3 import static org.hamcrest.CoreMatchers.instanceOf; 4 import static org.hamcrest.CoreMatchers.nullValue; 5 import static org.hamcrest.CoreMatchers.sameInstance; 6 import static org.junit.Assert.assertThat; 7 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 12 import android.app.Activity; 13 import android.content.Context; 14 import android.widget.RatingBar; 15 16 import com.xtremelabs.robolectric.Robolectric; 17 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 18 import com.xtremelabs.robolectric.util.Transcript; 19 20 @RunWith(WithTestDefaultsRunner.class) 21 public class RatingBarTest { 22 23 private RatingBar ratingBar; 24 private ShadowRatingBar shadow; 25 private RatingBar.OnRatingBarChangeListener listener; 26 private Transcript transcript; 27 28 @Before setup()29 public void setup() { 30 ratingBar = new RatingBar(new Activity()); 31 shadow = Robolectric.shadowOf(ratingBar); 32 listener = new TestRatingBarChangedListener(); 33 transcript = new Transcript(); 34 ratingBar.setOnRatingBarChangeListener(listener); 35 } 36 37 @Test testOnSeekBarChangedListener()38 public void testOnSeekBarChangedListener() { 39 assertThat(shadow.getOnRatingBarChangeListener(), sameInstance(listener)); 40 ratingBar.setOnRatingBarChangeListener(null); 41 assertThat(shadow.getOnRatingBarChangeListener(), nullValue()); 42 } 43 44 @Test testOnChangeNotification()45 public void testOnChangeNotification() { 46 ratingBar.setRating(5.0f); 47 transcript.assertEventsSoFar("onRatingChanged() - 5.0"); 48 } 49 50 private class TestRatingBarChangedListener implements RatingBar.OnRatingBarChangeListener { 51 52 @Override onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)53 public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 54 transcript.add("onRatingChanged() - " + rating); 55 } 56 } 57 58 @Test testInheritance()59 public void testInheritance() { 60 TestRatingBar ratingBar = new TestRatingBar(new Activity()); 61 ShadowRatingBar shadow = Robolectric.shadowOf(ratingBar); 62 assertThat(shadow, instanceOf(ShadowAbsSeekBar.class)); 63 } 64 65 private static class TestRatingBar extends RatingBar { 66 TestRatingBar(Context context)67 public TestRatingBar(Context context) { 68 super(context); 69 } 70 } 71 } 72