1 package org.robolectric.fakes; 2 3 import android.content.ComponentName; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.view.KeyEvent; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.SubMenu; 10 import java.util.ArrayList; 11 import java.util.Collections; 12 import java.util.Comparator; 13 import java.util.List; 14 import org.robolectric.RuntimeEnvironment; 15 16 /** 17 * Robolectric implementation of {@link android.view.Menu}. 18 */ 19 public class RoboMenu implements Menu { 20 private List<MenuItem> menuItems = new ArrayList<>(); 21 private Context context; 22 RoboMenu()23 public RoboMenu() { 24 this(RuntimeEnvironment.application); 25 } 26 RoboMenu(Context context)27 public RoboMenu(Context context) { 28 this.context = context; 29 } 30 31 @Override add(CharSequence title)32 public MenuItem add(CharSequence title) { 33 return add(0, 0, 0, title); 34 } 35 36 @Override add(int titleRes)37 public MenuItem add(int titleRes) { 38 return add(0, 0, 0, titleRes); 39 } 40 41 @Override add(int groupId, int itemId, int order, CharSequence title)42 public MenuItem add(int groupId, int itemId, int order, CharSequence title) { 43 RoboMenuItem menuItem = new RoboMenuItem(context); 44 menuItem.setOrder(order); 45 menuItems.add(menuItem); 46 menuItem.setGroupId(groupId); 47 Collections.sort(menuItems, new CustomMenuItemComparator()); 48 menuItem.setItemId(itemId); 49 menuItem.setTitle(title); 50 return menuItem; 51 } 52 53 @Override add(int groupId, int itemId, int order, int titleRes)54 public MenuItem add(int groupId, int itemId, int order, int titleRes) { 55 return add(groupId, itemId, order, context.getResources().getString(titleRes)); 56 } 57 58 @Override addSubMenu(CharSequence title)59 public SubMenu addSubMenu(CharSequence title) { 60 RoboSubMenu tsm = new RoboSubMenu(context); 61 RoboMenuItem menuItem = new RoboMenuItem(context); 62 menuItems.add(menuItem); 63 menuItem.setTitle(title); 64 menuItem.setSubMenu(tsm); 65 return tsm; 66 } 67 68 @Override addSubMenu(int titleRes)69 public SubMenu addSubMenu(int titleRes) { 70 RoboSubMenu tsm = new RoboSubMenu(context); 71 RoboMenuItem menuItem = new RoboMenuItem(context); 72 menuItems.add(menuItem); 73 menuItem.setTitle(titleRes); 74 menuItem.setSubMenu(tsm); 75 return tsm; 76 } 77 78 @Override addSubMenu(int groupId, int itemId, int order, CharSequence title)79 public SubMenu addSubMenu(int groupId, int itemId, int order, CharSequence title) { 80 RoboSubMenu tsm = new RoboSubMenu(context); 81 RoboMenuItem menuItem = new RoboMenuItem(context); 82 menuItems.add(menuItem); 83 menuItem.setGroupId(groupId); 84 menuItem.setItemId(itemId); 85 menuItem.setTitle(title); 86 menuItem.setSubMenu(tsm); 87 return tsm; 88 } 89 90 @Override addSubMenu(int groupId, int itemId, int order, int titleRes)91 public SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes) { 92 RoboSubMenu tsm = new RoboSubMenu(context); 93 RoboMenuItem menuItem = new RoboMenuItem(context); 94 menuItems.add(menuItem); 95 menuItem.setGroupId(groupId); 96 menuItem.setItemId(itemId); 97 menuItem.setTitle(titleRes); 98 menuItem.setSubMenu(tsm); 99 return tsm; 100 } 101 102 @Override addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems)103 public int addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, 104 Intent intent, int flags, MenuItem[] outSpecificItems) { 105 return 0; 106 } 107 108 @Override removeItem(int id)109 public void removeItem(int id) { 110 MenuItem menuItem = findItem(id); 111 menuItems.remove(menuItem); 112 } 113 114 @Override removeGroup(int groupId)115 public void removeGroup(int groupId) { 116 } 117 118 @Override clear()119 public void clear() { 120 menuItems.clear(); 121 } 122 123 @Override setGroupCheckable(int group, boolean checkable, boolean exclusive)124 public void setGroupCheckable(int group, boolean checkable, boolean exclusive) { 125 } 126 127 @Override setGroupVisible(int group, boolean visible)128 public void setGroupVisible(int group, boolean visible) { 129 } 130 131 @Override setGroupEnabled(int group, boolean enabled)132 public void setGroupEnabled(int group, boolean enabled) { 133 } 134 135 @Override hasVisibleItems()136 public boolean hasVisibleItems() { 137 return false; 138 } 139 140 @Override findItem(int id)141 public MenuItem findItem(int id) { 142 for (MenuItem item : menuItems) { 143 if (item.getItemId() == id) { 144 return item; 145 } 146 } 147 return null; 148 } 149 150 @Override size()151 public int size() { 152 return menuItems.size(); 153 } 154 155 @Override getItem(int index)156 public MenuItem getItem(int index) { 157 return menuItems.get(index); 158 } 159 160 @Override close()161 public void close() { 162 } 163 164 @Override performShortcut(int keyCode, KeyEvent event, int flags)165 public boolean performShortcut(int keyCode, KeyEvent event, int flags) { 166 return false; 167 } 168 169 @Override isShortcutKey(int keyCode, KeyEvent event)170 public boolean isShortcutKey(int keyCode, KeyEvent event) { 171 return false; 172 } 173 174 @Override performIdentifierAction(int id, int flags)175 public boolean performIdentifierAction(int id, int flags) { 176 return false; 177 } 178 179 @Override setQwertyMode(boolean isQwerty)180 public void setQwertyMode(boolean isQwerty) { 181 } 182 findMenuItem(CharSequence title)183 public RoboMenuItem findMenuItem(CharSequence title) { 184 for (int i = 0; i < size(); i++) { 185 RoboMenuItem menuItem = (RoboMenuItem) getItem(i); 186 if (menuItem.getTitle().equals(title)) { 187 return menuItem; 188 } 189 } 190 return null; 191 } 192 findMenuItemContaining(CharSequence desiredText)193 public RoboMenuItem findMenuItemContaining(CharSequence desiredText) { 194 for (int i = 0; i < size(); i++) { 195 RoboMenuItem menuItem = (RoboMenuItem) getItem(i); 196 if (menuItem.getTitle().toString().contains(desiredText)) { 197 return menuItem; 198 } 199 } 200 return null; 201 } 202 203 private static class CustomMenuItemComparator implements Comparator<MenuItem> { 204 205 @Override compare(MenuItem a, MenuItem b)206 public int compare(MenuItem a, MenuItem b) { 207 if (a.getOrder() == b.getOrder()) { 208 return 0; 209 } else if (a.getOrder() > b.getOrder()) { 210 return 1; 211 } else { 212 return -1; 213 } 214 } 215 } 216 } 217 218