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 RoboMenuItem()34 public RoboMenuItem() { 35 this(RuntimeEnvironment.application); 36 } 37 RoboMenuItem(Context context)38 public RoboMenuItem(Context context) { 39 this.context = context; 40 } 41 RoboMenuItem(int itemId)42 public RoboMenuItem(int itemId) { 43 this.itemId = itemId; 44 } 45 setItemId(int itemId)46 public void setItemId(int itemId) { 47 this.itemId = itemId; 48 } 49 setGroupId(int groupId)50 public void setGroupId(int groupId) { 51 this.groupId = groupId; 52 } 53 54 @Override getItemId()55 public int getItemId() { 56 return itemId; 57 } 58 59 @Override getGroupId()60 public int getGroupId() { 61 return groupId; 62 } 63 64 @Override getOrder()65 public int getOrder() { 66 return order; 67 } 68 setOrder(int order)69 public void setOrder(int order) { 70 this.order = order; 71 } 72 73 @Override setTitle(CharSequence title)74 public MenuItem setTitle(CharSequence title) { 75 this.title = title; 76 return this; 77 } 78 79 @Override setTitle(int title)80 public MenuItem setTitle(int title) { 81 return this; 82 } 83 84 @Override getTitle()85 public CharSequence getTitle() { 86 return title; 87 } 88 89 @Override setTitleCondensed(CharSequence title)90 public MenuItem setTitleCondensed(CharSequence title) { 91 return this; 92 } 93 94 @Override getTitleCondensed()95 public CharSequence getTitleCondensed() { 96 return null; 97 } 98 99 @Override setIcon(Drawable icon)100 public MenuItem setIcon(Drawable icon) { 101 this.icon = icon; 102 return this; 103 } 104 105 @Override setIcon(int iconRes)106 public MenuItem setIcon(int iconRes) { 107 this.icon = iconRes == 0 ? null : context.getResources().getDrawable(iconRes); 108 return this; 109 } 110 111 @Override getIcon()112 public Drawable getIcon() { 113 return this.icon; 114 } 115 116 @Override setIntent(Intent intent)117 public MenuItem setIntent(Intent intent) { 118 this.intent = intent; 119 return this; 120 } 121 122 @Override getIntent()123 public Intent getIntent() { 124 return this.intent; 125 } 126 127 @Override setShortcut(char numericChar, char alphaChar)128 public MenuItem setShortcut(char numericChar, char alphaChar) { 129 return this; 130 } 131 132 @Override setNumericShortcut(char numericChar)133 public MenuItem setNumericShortcut(char numericChar) { 134 return this; 135 } 136 137 @Override getNumericShortcut()138 public char getNumericShortcut() { 139 return 0; 140 } 141 142 @Override setAlphabeticShortcut(char alphaChar)143 public MenuItem setAlphabeticShortcut(char alphaChar) { 144 return this; 145 } 146 147 @Override getAlphabeticShortcut()148 public char getAlphabeticShortcut() { 149 return 0; 150 } 151 152 @Override setCheckable(boolean checkable)153 public MenuItem setCheckable(boolean checkable) { 154 this.checkable = checkable; 155 return this; 156 } 157 158 @Override isCheckable()159 public boolean isCheckable() { 160 return checkable; 161 } 162 163 @Override setChecked(boolean checked)164 public MenuItem setChecked(boolean checked) { 165 this.checked = checked; 166 return this; 167 } 168 169 @Override isChecked()170 public boolean isChecked() { 171 return checked; 172 } 173 174 @Override setVisible(boolean visible)175 public MenuItem setVisible(boolean visible) { 176 this.visible = visible; 177 return this; 178 } 179 180 @Override isVisible()181 public boolean isVisible() { 182 return visible; 183 } 184 185 @Override setEnabled(boolean enabled)186 public MenuItem setEnabled(boolean enabled) { 187 this.enabled = enabled; 188 return this; 189 } 190 191 @Override isEnabled()192 public boolean isEnabled() { 193 return enabled; 194 } 195 196 @Override hasSubMenu()197 public boolean hasSubMenu() { 198 return subMenu != null; 199 } 200 201 @Override getSubMenu()202 public SubMenu getSubMenu() { 203 return subMenu; 204 } 205 setSubMenu(SubMenu subMenu)206 public void setSubMenu(SubMenu subMenu) { 207 this.subMenu = subMenu; 208 } 209 210 @Override setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener)211 public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) { 212 this.menuItemClickListener = menuItemClickListener; 213 return this; 214 } 215 216 @Override getMenuInfo()217 public ContextMenu.ContextMenuInfo getMenuInfo() { 218 return null; 219 } 220 click()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 setShowAsAction(int actionEnum)230 public void setShowAsAction(int actionEnum) { 231 } 232 233 @Override setShowAsActionFlags(int actionEnum)234 public MenuItem setShowAsActionFlags(int actionEnum) { 235 return this; 236 } 237 238 @Override setActionView(View view)239 public MenuItem setActionView(View view) { 240 actionView = view; 241 return this; 242 } 243 244 @Override setActionView(int resId)245 public MenuItem setActionView(int resId) { 246 return this; 247 } 248 249 @Override getActionView()250 public View getActionView() { 251 return actionView; 252 } 253 254 @Override setActionProvider(ActionProvider actionProvider)255 public MenuItem setActionProvider(ActionProvider actionProvider) { 256 return this; 257 } 258 259 @Override getActionProvider()260 public ActionProvider getActionProvider() { 261 return null; 262 } 263 264 @Override expandActionView()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 collapseActionView()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 isActionViewExpanded()293 public boolean isActionViewExpanded() { 294 return expanded; 295 } 296 297 @Override setOnActionExpandListener(OnActionExpandListener listener)298 public MenuItem setOnActionExpandListener(OnActionExpandListener listener) { 299 actionExpandListener = listener; 300 return this; 301 } 302 } 303 304