package org.robolectric.shadows;

import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadow.api.Shadow;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(Toast.class)
public class ShadowToast {
  private String text;
  private int duration;
  private int gravity;
  private int xOffset;
  private int yOffset;
  private View view;
  private boolean cancelled;

  @RealObject Toast toast;

  @Implementation
  public void __constructor__(Context context) {}

  @Implementation
  public static Toast makeText(Context context, int resId, int duration) {
    return makeText(context, context.getResources().getString(resId), duration);
  }

  @Implementation
  public static Toast makeText(Context context, CharSequence text, int duration) {
    Toast toast = new Toast(context);
    toast.setDuration(duration);
    ShadowToast shadowToast = Shadow.extract(toast);
    shadowToast.text = text.toString();
    return toast;
  }

  @Implementation
  public void show() {
    ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application);
    shadowApplication.getShownToasts().add(toast);
  }

  @Implementation
  public void setText(int resId) {
    this.text = RuntimeEnvironment.application.getString(resId);
  }

  @Implementation
  public void setText(CharSequence text) {
    this.text = text.toString();
  }

  @Implementation
  public void setView(View view) {
    this.view = view;
  }

  @Implementation
  public View getView() {
    return view;
  }

  @Implementation
  public void setGravity(int gravity, int xOffset, int yOffset) {
    this.gravity = gravity;
    this.xOffset = xOffset;
    this.yOffset = yOffset;
  }

  @Implementation
  public int getGravity() {
    return gravity;
  }

  @Implementation
  public int getXOffset() {
    return xOffset;
  }

  @Implementation
  public int getYOffset() {
    return yOffset;
  }

  @Implementation
  public void setDuration(int duration) {
    this.duration = duration;
  }

  @Implementation
  public int getDuration() {
    return duration;
  }

  @Implementation
  public void cancel() {
    cancelled = true;
  }

  public boolean isCancelled() {
    return cancelled;
  }

  /**
   * Discards the recorded {@code Toast}s. Shown toasts are automatically cleared between
   * tests. This method allows the user to discard recorded toasts during the test in order to make assertions clearer
   * e.g:
   *
   * <pre>
   *
   *   // Show a single toast
   *   myClass.showToast();
   *
   *   assertThat(ShadowToast.shownToastCount()).isEqualTo(1);
   *   ShadowToast.reset();
   *
   *    // Show another toast
   *   myClass.showToast();
   *
   *   assertThat(ShadowToast.shownToastCount()).isEqualTo(1);
   *
   * </pre>
   */
  public static void reset() {
    ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application);
    shadowApplication.getShownToasts().clear();
  }

  /**
   * Returns the number of {@code Toast} requests that have been made during this test run
   * or since {@link #reset()} has been called.
   *
   * @return the number of {@code Toast} requests that have been made during this test run
   *         or since {@link #reset()} has been called.
   */
  public static int shownToastCount() {
    ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application);
    return shadowApplication.getShownToasts().size();
  }

  /**
   * Returns whether or not a particular custom {@code Toast} has been shown.
   *
   * @param message the message to search for
   * @param layoutResourceIdToCheckForMessage
   *                the id of the resource that contains the toast messages
   * @return whether the {@code Toast} was requested
   */
  public static boolean showedCustomToast(CharSequence message, int layoutResourceIdToCheckForMessage) {
    ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application);
    for (Toast toast : shadowApplication.getShownToasts()) {
      String text = ((TextView) toast.getView().findViewById(layoutResourceIdToCheckForMessage)).getText().toString();
      if (text.equals(message.toString())) {
        return true;
      }
    }
    return false;
  }

  /**
   * query method that returns whether or not a particular {@code Toast} has been shown.
   *
   * @param message the message to search for
   * @return whether the {@code Toast} was requested
   */
  public static boolean showedToast(CharSequence message) {
    ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application);
    for (Toast toast : shadowApplication.getShownToasts()) {
      ShadowToast shadowToast = Shadow.extract(toast);
      String text = shadowToast.text;
      if (text != null && text.equals(message.toString())) {
        return true;
      }
    }
    return false;
  }

  /**
   * Returns the text of the most recently shown {@code Toast}.
   *
   * @return the text of the most recently shown {@code Toast}
   */
  public static String getTextOfLatestToast() {
    ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application);
    List<Toast> shownToasts = shadowApplication.getShownToasts();
    if (shownToasts.isEmpty()) {
      return null;
    } else {
      Toast latestToast = shownToasts.get(shownToasts.size() - 1);
      ShadowToast shadowToast = Shadow.extract(latestToast);
      return shadowToast.text;
    }
  }

  /**
   * Returns the most recently shown {@code Toast}.
   *
   * @return the most recently shown {@code Toast}
   */
  public static Toast getLatestToast() {
    ShadowApplication shadowApplication = Shadow.extract(RuntimeEnvironment.application);
    List<Toast> shownToasts = shadowApplication.getShownToasts();
    return (shownToasts.size() == 0) ? null : shownToasts.get(shownToasts.size() - 1);
  }
}

@@ -702,29 +702,32 @@
 entation%0A  p
-ublic
+rotected
  void __cons
@@ -770,37 +770,40 @@
 plementation%0A  p
-ublic
+rotected
  static Toast ma
@@ -956,29 +956,32 @@
 entation%0A  p
-ublic
+rotected
  static Toas
@@ -1247,29 +1247,32 @@
 entation%0A  p
-ublic
+rotected
  void show()
@@ -1433,37 +1433,40 @@
 plementation%0A  p
-ublic
+rotected
  void setText(in
@@ -1559,37 +1559,40 @@
 plementation%0A  p
-ublic
+rotected
  void setText(Ch
@@ -1661,37 +1661,40 @@
 plementation%0A  p
-ublic
+rotected
  void setView(Vi
@@ -1752,21 +1752,24 @@
 tion%0A  p
-ublic
+rotected
  View ge
@@ -1813,37 +1813,40 @@
 plementation%0A  p
-ublic
+rotected
  void setGravity
@@ -1989,37 +1989,40 @@
 plementation%0A  p
-ublic
+rotected
  int getGravity(
@@ -2063,37 +2063,40 @@
 plementation%0A  p
-ublic
+rotected
  int getXOffset(
@@ -2137,37 +2137,40 @@
 plementation%0A  p
-ublic
+rotected
  int getYOffset(
@@ -2215,29 +2215,32 @@
 entation%0A  p
-ublic
+rotected
  void setDur
@@ -2317,21 +2317,24 @@
 tion%0A  p
-ublic
+rotected
  int get
@@ -2393,21 +2393,24 @@
 tion%0A  p
-ublic
+rotected
  void ca

Make ShadowToast @Implementation methods protected