1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.LOLLIPOP; 4 import static org.robolectric.shadow.api.Shadow.directlyOn; 5 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.widget.Adapter; 9 import android.widget.ListView; 10 import com.android.internal.app.AlertController; 11 import java.lang.reflect.InvocationTargetException; 12 import org.robolectric.RuntimeEnvironment; 13 import org.robolectric.annotation.Implementation; 14 import org.robolectric.annotation.Implements; 15 import org.robolectric.annotation.RealObject; 16 import org.robolectric.util.ReflectionHelpers; 17 18 @Implements(value = AlertController.class, isInAndroidSdk = false) 19 public class ShadowAlertController { 20 21 @RealObject AlertController realAlertController; 22 23 private CharSequence title; 24 private CharSequence message; 25 private View view; 26 private View customTitleView; 27 private int iconId; 28 29 @Implementation setTitle(CharSequence title)30 public void setTitle(CharSequence title) throws InvocationTargetException, IllegalAccessException { 31 this.title = title; 32 directlyOn(realAlertController, AlertController.class).setTitle(title); 33 } 34 getTitle()35 public CharSequence getTitle() { 36 return title == null ? "" : title; 37 } 38 39 @Implementation setCustomTitle(View customTitleView)40 public void setCustomTitle(View customTitleView) { 41 this.customTitleView = customTitleView; 42 directlyOn(realAlertController, AlertController.class).setCustomTitle(customTitleView); 43 } 44 getCustomTitleView()45 public View getCustomTitleView() { 46 return customTitleView; 47 } 48 49 @Implementation setMessage(CharSequence message)50 public void setMessage(CharSequence message) { 51 this.message = message; 52 directlyOn(realAlertController, AlertController.class).setMessage(message); 53 } 54 getMessage()55 public CharSequence getMessage() { 56 return message == null ? "" : message; 57 } 58 59 @Implementation setView(View view)60 public void setView(View view) { 61 this.view = view; 62 directlyOn(realAlertController, AlertController.class).setView(view); 63 } 64 65 @Implementation(minSdk = LOLLIPOP) setView(int resourceId)66 public void setView(int resourceId) { 67 setView(LayoutInflater.from(RuntimeEnvironment.application).inflate(resourceId, null)); 68 } 69 70 @Implementation setIcon(int iconId)71 public void setIcon(int iconId) { 72 this.iconId = iconId; 73 directlyOn(realAlertController, AlertController.class).setIcon(iconId); 74 } 75 getIconId()76 public int getIconId() { 77 return iconId; 78 } 79 getView()80 public View getView() { 81 return view; 82 } 83 getAdapter()84 public Adapter getAdapter() { 85 return ReflectionHelpers.<ListView>callInstanceMethod(realAlertController, "getListView").getAdapter(); 86 } 87 } 88