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.application); 47 shadowApplication.getShownToasts().add(toast); 48 } 49 50 @Implementation setText(int resId)51 protected void setText(int resId) { 52 this.text = RuntimeEnvironment.application.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 113 * tests. This method allows the user to discard recorded toasts during the test in order to make assertions clearer 114 * 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.application); 133 shadowApplication.getShownToasts().clear(); 134 } 135 136 /** 137 * Returns the number of {@code Toast} requests that have been made during this test run 138 * or since {@link #reset()} has been called. 139 * 140 * @return the number of {@code Toast} requests that have been made during this test run 141 * or since {@link #reset()} has been called. 142 */ shownToastCount()143 public static int shownToastCount() { 144 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 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 153 * the id of the resource that contains the toast messages 154 * @return whether the {@code Toast} was requested 155 */ showedCustomToast(CharSequence message, int layoutResourceIdToCheckForMessage)156 public static boolean showedCustomToast(CharSequence message, int layoutResourceIdToCheckForMessage) { 157 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 158 for (Toast toast : shadowApplication.getShownToasts()) { 159 String text = ((TextView) toast.getView().findViewById(layoutResourceIdToCheckForMessage)).getText().toString(); 160 if (text.equals(message.toString())) { 161 return true; 162 } 163 } 164 return false; 165 } 166 167 /** 168 * query method that returns whether or not a particular {@code Toast} has been shown. 169 * 170 * @param message the message to search for 171 * @return whether the {@code Toast} was requested 172 */ showedToast(CharSequence message)173 public static boolean showedToast(CharSequence message) { 174 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 175 for (Toast toast : shadowApplication.getShownToasts()) { 176 ShadowToast shadowToast = Shadow.extract(toast); 177 String text = shadowToast.text; 178 if (text != null && text.equals(message.toString())) { 179 return true; 180 } 181 } 182 return false; 183 } 184 185 /** 186 * Returns the text of the most recently shown {@code Toast}. 187 * 188 * @return the text of the most recently shown {@code Toast} 189 */ getTextOfLatestToast()190 public static String getTextOfLatestToast() { 191 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 192 List<Toast> shownToasts = shadowApplication.getShownToasts(); 193 if (shownToasts.isEmpty()) { 194 return null; 195 } else { 196 Toast latestToast = shownToasts.get(shownToasts.size() - 1); 197 ShadowToast shadowToast = Shadow.extract(latestToast); 198 return shadowToast.text; 199 } 200 } 201 202 /** 203 * Returns the most recently shown {@code Toast}. 204 * 205 * @return the most recently shown {@code Toast} 206 */ getLatestToast()207 public static Toast getLatestToast() { 208 ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application); 209 List<Toast> shownToasts = shadowApplication.getShownToasts(); 210 return (shownToasts.size() == 0) ? null : shownToasts.get(shownToasts.size() - 1); 211 } 212 } 213