Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added OnRatingBarChangeListener support to ShadowRatingBar

  • Loading branch information...
commit 575bc4a6673262cbff1bd63f6fc3e9957bc83fa8 1 parent a1869aa
Robert Taylor authored
13 src/main/java/com/xtremelabs/robolectric/shadows/ShadowProgressBar.java
View
@@ -7,10 +7,19 @@
@Implements(ProgressBar.class)
public class ShadowProgressBar extends ShadowView {
- private int progress = 0;
- private int secondaryProgress = 0;
+ private int progress;
+ private int secondaryProgress;
private int max = 100;
+ @Override public void applyAttributes() {
+ super.applyAttributes();
+
+ final int max = attributeSet.getAttributeIntValue("android", "max", this.max);
+
+ if (max >= 0)
+ setMax(max);
+ }
+
@Implementation
public void setMax(int max) {
progress = 0;
25 src/main/java/com/xtremelabs/robolectric/shadows/ShadowRatingBar.java
View
@@ -1,15 +1,20 @@
package com.xtremelabs.robolectric.shadows;
import android.widget.RatingBar;
+import android.widget.RatingBar.OnRatingBarChangeListener;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
+import com.xtremelabs.robolectric.internal.RealObject;
@Implements(RatingBar.class)
public class ShadowRatingBar extends ShadowAbsSeekBar {
+ @RealObject
+ private RatingBar realRatingBar;
private int mNumStars = 5;
-
+ private OnRatingBarChangeListener listener;
+
@Override public void applyAttributes() {
super.applyAttributes();
@@ -92,4 +97,22 @@ private float getProgressPerStar() {
return 1;
}
}
+
+ @Implementation
+ @Override
+ public void setProgress(int progress) {
+ super.setProgress(progress);
+ if (listener != null)
+ listener.onRatingChanged(realRatingBar, getRating(), true);
+ }
+
+ @Implementation
+ public void setOnRatingBarChangeListener(OnRatingBarChangeListener listener) {
+ this.listener = listener;
+ }
+
+ @Implementation
+ public OnRatingBarChangeListener getOnRatingBarChangeListener() {
+ return listener;
+ }
}
51 src/test/java/com/xtremelabs/robolectric/shadows/RatingBarTest.java
View
@@ -1,19 +1,60 @@
package com.xtremelabs.robolectric.shadows;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
import android.app.Activity;
import android.content.Context;
import android.widget.RatingBar;
+
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertThat;
+import com.xtremelabs.robolectric.util.Transcript;
@RunWith(WithTestDefaultsRunner.class)
public class RatingBarTest {
+ private RatingBar ratingBar;
+ private ShadowRatingBar shadow;
+ private RatingBar.OnRatingBarChangeListener listener;
+ private Transcript transcript;
+
+ @Before
+ public void setup() {
+ ratingBar = new RatingBar(new Activity());
+ shadow = Robolectric.shadowOf(ratingBar);
+ listener = new TestRatingBarChangedListener();
+ transcript = new Transcript();
+ ratingBar.setOnRatingBarChangeListener(listener);
+ }
+
+ @Test
+ public void testOnSeekBarChangedListener() {
+ assertThat(shadow.getOnRatingBarChangeListener(), sameInstance(listener));
+ ratingBar.setOnRatingBarChangeListener(null);
+ assertThat(shadow.getOnRatingBarChangeListener(), nullValue());
+ }
+
+ @Test
+ public void testOnChangeNotification() {
+ ratingBar.setRating(5.0f);
+ transcript.assertEventsSoFar("onRatingChanged() - 5.0");
+ }
+
+ private class TestRatingBarChangedListener implements RatingBar.OnRatingBarChangeListener {
+
+ @Override
+ public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
+ transcript.add("onRatingChanged() - " + rating);
+ }
+ }
+
@Test
public void testInheritance() {
TestRatingBar ratingBar = new TestRatingBar(new Activity());
Please sign in to comment.
Something went wrong with that request. Please try again.