InstrumentationRegistry location depends on runner implementation and dependencies, look for args at known locations

This commit is contained in:
Simon Søndergaard 2019-02-12 12:14:28 +01:00
Родитель f27fab6424
Коммит a613051894
1 изменённых файлов: 34 добавлений и 2 удалений

Просмотреть файл

@ -1,16 +1,25 @@
package com.microsoft.appcenter.espresso;
import android.os.Bundle;
import android.support.test.InstrumentationRegistry;
import android.util.Log;
import com.microsoft.appcenter.event.EventReporter;
import com.microsoft.appcenter.event.StdOutEventReporter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Factory {
private static String TAG = Factory.class.getSimpleName();
private static String locations[] = {"androidx.test.platform.app.InstrumentationRegistry",
"androidx.test.InstrumentationRegistry",
"android.support.test.InstrumentationRegistry"};
private static EventReporter eventReporter;
static {
Bundle arguments = InstrumentationRegistry.getArguments();
Bundle arguments = getArguments();
String label = arguments.getString("label");
if ("true".equals(label)) {
int timeoutInSec = 1;
@ -24,6 +33,29 @@ public class Factory {
}
}
private static Bundle getArguments() {
for (String location: locations) {
try {
Class<?> aClass = Class.forName(location);
Method getArguments = aClass.getMethod("getArguments", (Class[]) null);
return (Bundle) getArguments.invoke(null, (Object[]) null);
// We need to support api level 8 and up, so we cannot combine catches into on
} catch (IllegalStateException e) {
String msg = String.format("Unable to find arguments in {0}, trying next \"global\" Registry", location);
Log.d(TAG, msg);
} catch (ClassNotFoundException e) {
// Ignore
} catch (NoSuchMethodException e) {
// Ignore
} catch (IllegalAccessException e) {
// Ignore
} catch (InvocationTargetException e) {
// Ignore
}
}
return new Bundle();
}
public static ReportHelper getReportHelper() {
return new ReportHelper(eventReporter);
}