Permalink
Browse files

Store Toast duration in ShadowToast

Previously the duration the Toast was created with was
ignored and always reported as zero when calling getDuration
even when the Toast was created with a non-zero value such as
the Toast.LENGTH_LONG constant.
  • Loading branch information...
kevinsawicki committed Aug 28, 2012
1 parent e10916e commit af192a2568d2c463726c92303fc16cb1b3bae8e6
@@ -20,6 +20,7 @@
@Implements(Toast.class)
public class ShadowToast {
private String text;
+ private int duration;
private int gravity;
private View view;
@@ -33,6 +34,7 @@ public static Toast makeText(Context context, int resId, int duration) {
@Implementation(i18nSafe=false)
public static Toast makeText(Context context, CharSequence text, int duration) {
Toast toast = new Toast(null);
+ toast.setDuration(duration);
shadowOf(toast).text = text.toString();
return toast;
}
@@ -62,6 +64,16 @@ public int getGravity() {
return gravity;
}
+ @Implementation
+ public void setDuration(int duration) {
+ this.duration = duration;
+ }
+
+ @Implementation
+ public int getDuration() {
+ return duration;
+ }
+
/**
* Non-Android accessor that discards the recorded {@code Toast}s
*/
@@ -0,0 +1,31 @@
+package com.xtremelabs.robolectric.shadows;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import android.app.Activity;
+import android.widget.Toast;
+
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(WithTestDefaultsRunner.class)
+public class ToastTest {
+
+ @Test
+ public void shouldHaveShortDuration() throws Exception {
+ Toast toast = Toast.makeText(new Activity(), "short toast",
+ Toast.LENGTH_SHORT);
+ assertNotNull(toast);
+ assertEquals(Toast.LENGTH_SHORT, toast.getDuration());
+ }
+
+ @Test
+ public void shouldHaveLongDuration() throws Exception {
+ Toast toast = Toast.makeText(new Activity(), "long toast",
+ Toast.LENGTH_LONG);
+ assertNotNull(toast);
+ assertEquals(Toast.LENGTH_LONG, toast.getDuration());
+ }
+}

0 comments on commit af192a2

Please sign in to comment.