Home | History | Annotate | Download | only in fakes
      1 package org.robolectric.fakes;
      2 
      3 import android.content.Context;
      4 import android.content.Intent;
      5 import android.graphics.drawable.Drawable;
      6 import android.view.ActionProvider;
      7 import android.view.ContextMenu;
      8 import android.view.MenuItem;
      9 import android.view.SubMenu;
     10 import android.view.View;
     11 import org.robolectric.RuntimeEnvironment;
     12 
     13 /**
     14  * Robolectric implementation of {@link android.view.MenuItem}.
     15  */
     16 public class RoboMenuItem implements MenuItem {
     17   private int itemId;
     18   private int groupId;
     19   private CharSequence title;
     20   private boolean enabled = true;
     21   private boolean checked = false;
     22   private boolean checkable = false;
     23   private boolean visible = true;
     24   private boolean expanded = false;
     25   private OnMenuItemClickListener menuItemClickListener;
     26   public Drawable icon;
     27   private Intent intent;
     28   private SubMenu subMenu;
     29   private View actionView;
     30   private OnActionExpandListener actionExpandListener;
     31   private int order;
     32   private Context context;
     33 
     34   public RoboMenuItem() {
     35     this(RuntimeEnvironment.application);
     36   }
     37 
     38   public RoboMenuItem(Context context) {
     39     this.context = context;
     40   }
     41 
     42   public RoboMenuItem(int itemId) {
     43     this.itemId = itemId;
     44   }
     45 
     46   public void setItemId(int itemId) {
     47     this.itemId = itemId;
     48   }
     49 
     50   public void setGroupId(int groupId) {
     51     this.groupId = groupId;
     52   }
     53 
     54   @Override
     55   public int getItemId() {
     56     return itemId;
     57   }
     58 
     59   @Override
     60   public int getGroupId() {
     61     return groupId;
     62   }
     63 
     64   @Override
     65   public int getOrder() {
     66     return order;
     67   }
     68 
     69   public void setOrder(int order) {
     70     this.order = order;
     71   }
     72 
     73   @Override
     74   public MenuItem setTitle(CharSequence title) {
     75     this.title = title;
     76     return this;
     77   }
     78 
     79   @Override
     80   public MenuItem setTitle(int title) {
     81     return this;
     82   }
     83 
     84   @Override
     85   public CharSequence getTitle() {
     86     return title;
     87   }
     88 
     89   @Override
     90   public MenuItem setTitleCondensed(CharSequence title) {
     91     return this;
     92   }
     93 
     94   @Override
     95   public CharSequence getTitleCondensed() {
     96     return null;
     97   }
     98 
     99   @Override
    100   public MenuItem setIcon(Drawable icon) {
    101     this.icon = icon;
    102     return this;
    103   }
    104 
    105   @Override
    106   public MenuItem setIcon(int iconRes) {
    107     this.icon = iconRes == 0 ? null : context.getResources().getDrawable(iconRes);
    108     return this;
    109   }
    110 
    111   @Override
    112   public Drawable getIcon() {
    113     return this.icon;
    114   }
    115 
    116   @Override
    117   public MenuItem setIntent(Intent intent) {
    118     this.intent = intent;
    119     return this;
    120   }
    121 
    122   @Override
    123   public Intent getIntent() {
    124     return this.intent;
    125   }
    126 
    127   @Override
    128   public MenuItem setShortcut(char numericChar, char alphaChar) {
    129     return this;
    130   }
    131 
    132   @Override
    133   public MenuItem setNumericShortcut(char numericChar) {
    134     return this;
    135   }
    136 
    137   @Override
    138   public char getNumericShortcut() {
    139     return 0;
    140   }
    141 
    142   @Override
    143   public MenuItem setAlphabeticShortcut(char alphaChar) {
    144     return this;
    145   }
    146 
    147   @Override
    148   public char getAlphabeticShortcut() {
    149     return 0;
    150   }
    151 
    152   @Override
    153   public MenuItem setCheckable(boolean checkable) {
    154     this.checkable = checkable;
    155     return this;
    156   }
    157 
    158   @Override
    159   public boolean isCheckable() {
    160     return checkable;
    161   }
    162 
    163   @Override
    164   public MenuItem setChecked(boolean checked) {
    165     this.checked = checked;
    166     return this;
    167   }
    168 
    169   @Override
    170   public boolean isChecked() {
    171     return checked;
    172   }
    173 
    174   @Override
    175   public MenuItem setVisible(boolean visible) {
    176     this.visible = visible;
    177     return this;
    178   }
    179 
    180   @Override
    181   public boolean isVisible() {
    182     return visible;
    183   }
    184 
    185   @Override
    186   public MenuItem setEnabled(boolean enabled) {
    187     this.enabled = enabled;
    188     return this;
    189   }
    190 
    191   @Override
    192   public boolean isEnabled() {
    193     return enabled;
    194   }
    195 
    196   @Override
    197   public boolean hasSubMenu() {
    198     return subMenu != null;
    199   }
    200 
    201   @Override
    202   public SubMenu getSubMenu() {
    203     return subMenu;
    204   }
    205 
    206   public void setSubMenu(SubMenu subMenu) {
    207     this.subMenu = subMenu;
    208   }
    209 
    210   @Override
    211   public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
    212     this.menuItemClickListener = menuItemClickListener;
    213     return this;
    214   }
    215 
    216   @Override
    217   public ContextMenu.ContextMenuInfo getMenuInfo() {
    218     return null;
    219   }
    220 
    221   public void click() {
    222     if (enabled && menuItemClickListener != null) {
    223       menuItemClickListener.onMenuItemClick(this);
    224     } else if (enabled && intent != null) {
    225       context.startActivity(intent);
    226     }
    227   }
    228 
    229   @Override
    230   public void setShowAsAction(int actionEnum) {
    231   }
    232 
    233   @Override
    234   public MenuItem setShowAsActionFlags(int actionEnum) {
    235     return this;
    236   }
    237 
    238   @Override
    239   public MenuItem setActionView(View view) {
    240     actionView = view;
    241     return this;
    242   }
    243 
    244   @Override
    245   public MenuItem setActionView(int resId) {
    246     return this;
    247   }
    248 
    249   @Override
    250   public View getActionView() {
    251     return actionView;
    252   }
    253 
    254   @Override
    255   public MenuItem setActionProvider(ActionProvider actionProvider) {
    256     return this;
    257   }
    258 
    259   @Override
    260   public ActionProvider getActionProvider() {
    261     return null;
    262   }
    263 
    264   @Override
    265   public boolean expandActionView() {
    266     if (actionView != null) {
    267       if (actionExpandListener != null) {
    268         actionExpandListener.onMenuItemActionExpand(this);
    269       }
    270 
    271       expanded = true;
    272       return true;
    273     }
    274 
    275     return false;
    276   }
    277 
    278   @Override
    279   public boolean collapseActionView() {
    280     if (actionView != null) {
    281       if (actionExpandListener != null) {
    282         actionExpandListener.onMenuItemActionCollapse(this);
    283       }
    284 
    285       expanded = false;
    286       return true;
    287     }
    288 
    289     return false;
    290   }
    291 
    292   @Override
    293   public boolean isActionViewExpanded() {
    294     return expanded;
    295   }
    296 
    297   @Override
    298   public MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
    299     actionExpandListener = listener;
    300     return this;
    301   }
    302 }
    303 
    304