1 package org.robolectric.shadows; 2 3 import android.content.Context; 4 import android.view.View; 5 import android.widget.TextView; 6 import android.widget.Toast; 7 import java.util.List; 8 import org.robolectric.RuntimeEnvironment; 9 import org.robolectric.annotation.Implementation; 10 import org.robolectric.annotation.Implements; 11 import org.robolectric.annotation.RealObject; 12 import org.robolectric.shadow.api.Shadow; 13 14 @SuppressWarnings({"UnusedDeclaration"}) 15 @Implements(Toast.class) 16 public class ShadowToast { 17 private String text; 18 private int duration; 19 private int gravity; 20 private int xOffset; 21 private int yOffset; 22 private View view; 23 private boolean cancelled; 24 25 @RealObject Toast toast; 26 27 @Implementation __constructor__(Context context)28 protected void __constructor__(Context context) {} 29 30 @Implementation makeText(Context context, int resId, int duration)31 protected static Toast makeText(Context context, int resId, int duration) { 32 return makeText(context, context.getResources().getString(resId), duration); 33 } 34 35 @Implementation makeText(Context context, CharSequence text, int duration)36 protected static Toast makeText(Context context, CharSequence text, int duration) { 37 Toast toast = new Toast(context); 38 toast.setDuration(duration); 39 ShadowToast shadowToast = Shadow.extract(toast); 40 shadowToast.text = text.toString(); 41 return toast; 42 } 43 44 @Implementation show()45 protected void show() { 46 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.getApplication()); 47 shadowApplication.getShownToasts().add(toast); 48 } 49 50 @Implementation setText(int resId)51 protected void setText(int resId) { 52 this.text = RuntimeEnvironment.getApplication().getString(resId); 53 } 54 55 @Implementation setText(CharSequence text)56 protected void setText(CharSequence text) { 57 this.text = text.toString(); 58 } 59 60 @Implementation setView(View view)61 protected void setView(View view) { 62 this.view = view; 63 } 64 65 @Implementation getView()66 protected View getView() { 67 return view; 68 } 69 70 @Implementation setGravity(int gravity, int xOffset, int yOffset)71 protected void setGravity(int gravity, int xOffset, int yOffset) { 72 this.gravity = gravity; 73 this.xOffset = xOffset; 74 this.yOffset = yOffset; 75 } 76 77 @Implementation getGravity()78 protected int getGravity() { 79 return gravity; 80 } 81 82 @Implementation getXOffset()83 protected int getXOffset() { 84 return xOffset; 85 } 86 87 @Implementation getYOffset()88 protected int getYOffset() { 89 return yOffset; 90 } 91 92 @Implementation setDuration(int duration)93 protected void setDuration(int duration) { 94 this.duration = duration; 95 } 96 97 @Implementation getDuration()98 protected int getDuration() { 99 return duration; 100 } 101 102 @Implementation cancel()103 protected void cancel() { 104 cancelled = true; 105 } 106 isCancelled()107 public boolean isCancelled() { 108 return cancelled; 109 } 110 111 /** 112 * Discards the recorded {@code Toast}s. Shown toasts are automatically cleared between tests. 113 * This method allows the user to discard recorded toasts during the test in order to make 114 * assertions clearer e.g: 115 * 116 * <pre> 117 * 118 * // Show a single toast 119 * myClass.showToast(); 120 * 121 * assertThat(ShadowToast.shownToastCount()).isEqualTo(1); 122 * ShadowToast.reset(); 123 * 124 * // Show another toast 125 * myClass.showToast(); 126 * 127 * assertThat(ShadowToast.shownToastCount()).isEqualTo(1); 128 * 129 * </pre> 130 */ reset()131 public static void reset() { 132 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.getApplication()); 133 shadowApplication.getShownToasts().clear(); 134 } 135 136 /** 137 * Returns the number of {@code Toast} requests that have been made during this test run or since 138 * {@link #reset()} has been called. 139 * 140 * @return the number of {@code Toast} requests that have been made during this test run or since 141 * {@link #reset()} has been called. 142 */ shownToastCount()143 public static int shownToastCount() { 144 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.getApplication()); 145 return shadowApplication.getShownToasts().size(); 146 } 147 148 /** 149 * Returns whether or not a particular custom {@code Toast} has been shown. 150 * 151 * @param message the message to search for 152 * @param layoutResourceIdToCheckForMessage the id of the resource that contains the toast 153 * messages 154 * @return whether the {@code Toast} was requested 155 */ showedCustomToast( CharSequence message, int layoutResourceIdToCheckForMessage)156 public static boolean showedCustomToast( 157 CharSequence message, int layoutResourceIdToCheckForMessage) { 158 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.getApplication()); 159 for (Toast toast : shadowApplication.getShownToasts()) { 160 String text = 161 ((TextView) toast.getView().findViewById(layoutResourceIdToCheckForMessage)) 162 .getText() 163 .toString(); 164 if (text.equals(message.toString())) { 165 return true; 166 } 167 } 168 return false; 169 } 170 171 /** 172 * query method that returns whether or not a particular {@code Toast} has been shown. 173 * 174 * @param message the message to search for 175 * @return whether the {@code Toast} was requested 176 */ showedToast(CharSequence message)177 public static boolean showedToast(CharSequence message) { 178 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.getApplication()); 179 for (Toast toast : shadowApplication.getShownToasts()) { 180 ShadowToast shadowToast = Shadow.extract(toast); 181 String text = shadowToast.text; 182 if (text != null && text.equals(message.toString())) { 183 return true; 184 } 185 } 186 return false; 187 } 188 189 /** 190 * Returns the text of the most recently shown {@code Toast}. 191 * 192 * @return the text of the most recently shown {@code Toast} 193 */ getTextOfLatestToast()194 public static String getTextOfLatestToast() { 195 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.getApplication()); 196 List<Toast> shownToasts = shadowApplication.getShownToasts(); 197 if (shownToasts.isEmpty()) { 198 return null; 199 } else { 200 Toast latestToast = shownToasts.get(shownToasts.size() - 1); 201 ShadowToast shadowToast = Shadow.extract(latestToast); 202 return shadowToast.text; 203 } 204 } 205 206 /** 207 * Returns the most recently shown {@code Toast}. 208 * 209 * @return the most recently shown {@code Toast} 210 */ getLatestToast()211 public static Toast getLatestToast() { 212 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.getApplication()); 213 List<Toast> shownToasts = shadowApplication.getShownToasts(); 214 return (shownToasts.size() == 0) ? null : shownToasts.get(shownToasts.size() - 1); 215 } 216 } 217