1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.robolectric.Shadows.shadowOf; 5 6 import android.app.Application; 7 import android.view.Gravity; 8 import android.view.View; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 import androidx.test.core.app.ApplicationProvider; 12 import androidx.test.ext.junit.runners.AndroidJUnit4; 13 import org.junit.Before; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.robolectric.R; 17 18 @RunWith(AndroidJUnit4.class) 19 public class ShadowToastTest { 20 21 private Application context; 22 23 @Before setUp()24 public void setUp() throws Exception { 25 context = ApplicationProvider.getApplicationContext(); 26 } 27 28 @Test shouldHaveShortDuration()29 public void shouldHaveShortDuration() throws Exception { 30 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 31 assertThat(toast).isNotNull(); 32 assertThat(toast.getDuration()).isEqualTo(Toast.LENGTH_SHORT); 33 } 34 35 @Test shouldHaveLongDuration()36 public void shouldHaveLongDuration() throws Exception { 37 Toast toast = Toast.makeText(context, "long toast", Toast.LENGTH_LONG); 38 assertThat(toast).isNotNull(); 39 assertThat(toast.getDuration()).isEqualTo(Toast.LENGTH_LONG); 40 } 41 42 @Test shouldMakeTextCorrectly()43 public void shouldMakeTextCorrectly() throws Exception { 44 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 45 assertThat(toast).isNotNull(); 46 assertThat(toast.getDuration()).isEqualTo(Toast.LENGTH_SHORT); 47 toast.show(); 48 assertThat(ShadowToast.getLatestToast()).isSameAs(toast); 49 assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("short toast"); 50 assertThat(ShadowToast.showedToast("short toast")).isTrue(); 51 } 52 53 @Test shouldSetTextCorrectly()54 public void shouldSetTextCorrectly() throws Exception { 55 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 56 toast.setText("other toast"); 57 toast.show(); 58 assertThat(ShadowToast.getLatestToast()).isSameAs(toast); 59 assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("other toast"); 60 assertThat(ShadowToast.showedToast("other toast")).isTrue(); 61 } 62 63 @Test shouldSetTextWithIdCorrectly()64 public void shouldSetTextWithIdCorrectly() throws Exception { 65 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 66 toast.setText(R.string.hello); 67 toast.show(); 68 assertThat(ShadowToast.getLatestToast()).isSameAs(toast); 69 assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("Hello"); 70 assertThat(ShadowToast.showedToast("Hello")).isTrue(); 71 } 72 73 @Test shouldSetViewCorrectly()74 public void shouldSetViewCorrectly() throws Exception { 75 Toast toast = new Toast(context); 76 toast.setDuration(Toast.LENGTH_SHORT); 77 final View view = new TextView(context); 78 toast.setView(view); 79 assertThat(toast.getView()).isSameAs(view); 80 } 81 82 @Test shouldSetGravityCorrectly()83 public void shouldSetGravityCorrectly() throws Exception { 84 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 85 assertThat(toast).isNotNull(); 86 toast.setGravity(Gravity.CENTER, 0, 0); 87 assertThat(toast.getGravity()).isEqualTo(Gravity.CENTER); 88 } 89 90 @Test shouldSetOffsetsCorrectly()91 public void shouldSetOffsetsCorrectly() throws Exception { 92 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 93 toast.setGravity(0, 12, 34); 94 assertThat(toast.getXOffset()).isEqualTo(12); 95 assertThat(toast.getYOffset()).isEqualTo(34); 96 } 97 98 @Test shouldCountToastsCorrectly()99 public void shouldCountToastsCorrectly() throws Exception { 100 assertThat(ShadowToast.shownToastCount()).isEqualTo(0); 101 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 102 assertThat(toast).isNotNull(); 103 toast.show(); 104 toast.show(); 105 toast.show(); 106 assertThat(ShadowToast.shownToastCount()).isEqualTo(3); 107 ShadowToast.reset(); 108 assertThat(ShadowToast.shownToastCount()).isEqualTo(0); 109 toast.show(); 110 toast.show(); 111 assertThat(ShadowToast.shownToastCount()).isEqualTo(2); 112 } 113 114 @Test shouldBeCancelled()115 public void shouldBeCancelled() throws Exception { 116 Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); 117 toast.cancel(); 118 ShadowToast shadowToast = shadowOf(toast); 119 assertThat(shadowToast.isCancelled()).isTrue(); 120 } 121 } 122