unit testing - Android instrumentation.callActivityOnPause causes "System services not available to Activities before onCreate()" -
i'm setting unit test test camera app release camera handle when app goes background via onpause callback. gets part call onpause method of activity i'd get:
java.lang.illegalstateexception: system services not available activities before oncreate() @ android.app.activity.getsystemservice(activity.java:4738) @ android.app.activity.getmultiwindowstyle(activity.java:4563) @ android.app.activity.getmultiwindowstyle(activity.java:4568) @ android.app.activity.onpause(activity.java:1366) @ com.ricky.chow.cameraopencv.activity.maincameraactivity.onpause(maincameraactivity.java:57) @ android.app.activity.performpause(activity.java:5555) @ android.app.instrumentation.callactivityonpause(instrumentation.java:1239) @ com.ricky.chow.cameraopencv.activity.maincameraactivitytest.teardownthisactivity(maincameraactivitytest.java:58) @ com.ricky.chow.cameraopencv.activity.maincameraactivitytest.testcamerapreviewexists(maincameraactivitytest.java:78) @ java.lang.reflect.method.invokenative(native method) @ android.test.instrumentationtestcase.runmethod(instrumentationtestcase.java:214) @ android.test.instrumentationtestcase.runtest(instrumentationtestcase.java:199) @ android.test.androidtestrunner.runtest(androidtestrunner.java:191) @ android.test.androidtestrunner.runtest(androidtestrunner.java:176) @ android.test.instrumentationtestrunner.onstart(instrumentationtestrunner.java:554) @ android.app.instrumentation$instrumentationthread.run(instrumentation.java:1729)
unit test:
public class maincameraactivitytest extends activityunittestcase<maincameraactivity> { private intent mlaunchintent; private imagebutton capturebutton; private framelayout camerapreviewview; private instrumentation minstrumentation; private maincameraactivity mactivity; public maincameraactivitytest(class<maincameraactivity> activityclass) { super(activityclass); } public maincameraactivitytest() { super(maincameraactivity.class); } private void setupthisactivity() { mlaunchintent = new intent(getinstrumentation().gettargetcontext(), maincameraactivity.class); minstrumentation = getinstrumentation(); mactivity = startactivity(mlaunchintent, null, null); minstrumentation.callactivityonstart(mactivity); minstrumentation.callactivityonresume(mactivity); capturebutton = (imagebutton)mactivity.findviewbyid(r.id.capturebutton); camerapreviewview = (framelayout)mactivity.findviewbyid(r.id.camerapreview); } private void teardownthisactivity() { minstrumentation.callactivityonpause(mactivity); minstrumentation.callactivityonstop(mactivity); } public void testcameraprieviewworks() { setupthisactivity(); mediancamerapost mmediancam = (mediancamerapost) camerapreviewview.getchildat(0); assert.assertnotnull(mmediancam); assert.assertequals(mmediancam.getvisibility(), view.visible); teardownthisactivity(); }
}
Comments
Post a Comment