@Override public Resources getResources() { Resources res = new MockResources() { @Override // The actual selects singular vs plural as well and in the given language public String getQuantityString(int id, int quantity) { if (id == R.plurals.Nmins) { return quantity + " mins"; } if (id == R.plurals.Nminutes) { return quantity + " minutes"; } if (id == R.plurals.Nhours) { return quantity + " hours"; } if (id == R.plurals.Ndays) { return quantity + " days"; } return id + " " + quantity; } }; return res; }
/** * Used to get a test calculator with a mocked resource file * * @return test calculator */ private Calculator getTestCalc() { //TODO make this into something cleaner--use Mockito to completely fake the resource MockResources mockResources = new MockResources() { @Override public String[] getStringArray(int id) { if (id == R.array.unit_type_array_combined){ return new String[]{"key_currency|Currency|Currency", "key_temp|Temperature|Temp", "key_weight|Weight|Weight", "key_len|Length|Length", "key_area|Area|Area", "key_vol|Volume|Volume", "key_speed|Speed|Speed", "key_time|Time|Time", "key_fuel|FuelEconomy|FuelEco", "key_power|Power|Power", "key_energy|Energy|Energy", "key_force|Force|Force", "key_torque|Torque|Torque", "key_pressure|Pressure|Pressure", "key_digital|DigitalStorage|Digital"}; } return null; } }; return Calculator.getTestCalculator(mockResources); }
public void testGetStringResource() { MockResources mockResources = new MyMockResources(); String actual = mainActivity.getStringResource(mockResources); assertEquals("string", actual); }