test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/TestHelpers.java in calabash-android-0.2.11 vs test-server/instrumentation-backend/src/sh/calaba/instrumentationbackend/TestHelpers.java in calabash-android-0.2.12

- old
+ new

@@ -10,10 +10,12 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; import android.content.Context; +import android.content.res.Resources.NotFoundException; +import android.graphics.drawable.Drawable; import android.view.View; import android.widget.TextView; public class TestHelpers { private static Map<String, Integer> resourceNamesToIds = new HashMap<String, Integer>(); @@ -67,26 +69,74 @@ } } return null; } + @SuppressWarnings("unchecked") + public static <ViewType extends View> ViewType getViewById(String resName, Class<? extends View> expectedViewType) { + final View theView = getViewById(resName); + + if (null == theView) { + return null; + } + + if (!expectedViewType.isInstance(theView)) { + throw new RuntimeException(String.format("getViewById: Expected to find a View of type %s but found one of type %s", expectedViewType.getClass().getName(), theView.getClass().getName())); + } + + return (ViewType) theView; + } + public static View getViewById(String resName) { Integer intID = resourceNamesToIds.get(resName); System.out.println("getViewById: Looking for view " + resName + " as id " + intID); if (intID == null) { throw new RuntimeException("getViewById: Looking for view " + resName + " which does not have an id"); } int id = intID.intValue(); - View view = InstrumentationBackend.solo.getCurrentActivity().findViewById(id); + View view = InstrumentationBackend.solo.getView(id); if (view != null) { if (id == view.getId()) { System.out.println("Did find view " + resName + "."); return view; } System.out.println("Did find view " + resName + " but getId returned: " + view.getId()); } System.out.println("Did not find view " + resName); return view; + } + + public static Drawable getDrawableById(String resName) { + int id; +// This would probably work too... +if( true ) { + try { +// if( resName.startsWith("drawable/") == false ) { +// resName = "drawable/" + resName; +// } +// TypedValue outValue = new TypedValue(); +// InstrumentationBackend.solo.getCurrentActivity().getResources().getValue( resName, outValue, false ); +// id = outValue.resourceId; + id = InstrumentationBackend.solo.getCurrentActivity().getResources().getIdentifier( resName, "drawable", InstrumentationBackend.solo.getCurrentActivity().getPackageName() ); + } catch( NotFoundException e ) { + throw new RuntimeException("getDrawableById: Looking for drawable " + resName + " but was not found"); + } +} else { + Integer intID = resourceNamesToIds.get(resName); + System.out.println("getDrawableById: Looking for drawable " + resName + " as id " + intID); + if (intID == null) { + throw new RuntimeException("getDrawableById: Looking for drawable " + resName + " which does not have an id"); + } + id = intID.intValue(); +} + Drawable drawable = InstrumentationBackend.solo.getCurrentActivity().getResources().getDrawable(id); + if (drawable != null) { + System.out.println("Did find drawable " + resName + ": " + drawable); + return drawable; + } else { + System.out.println("Did not find drawable " + resName); + return drawable; + } } public static int[] parseTime(String timeString) { String[] splitTimeString = timeString.split(":"); int hour = Integer.parseInt(splitTimeString[0]);