我已经编写了在android上截图的代码,我正在尝试为此编写一个测试。运行测试方法时,我收到以下错误消息: Activity never becomes requested state "[STARTED, CREATED, DESTROYED, RESUMED]" (last lifecycle transition = "PRE_ON_CREATE")
. 我很难弄清楚我做错了什么。
生产代码已经过手动测试。
我的测试代码在 src/androidTest/java/org/app
:
@RunWith (AndroidJUnit4.class)
public class ScreenshotsCaptorTest {
@Rule
public ActivityScenarioRule<TestActivity> rule =
new ActivityScenarioRule<>(TestActivity.class);
@Test
public void test() {
// This line is never reached
ActivityScenario<TestActivity> scenario = rule.getScenario();
scenario.onActivity(activity -> {
activity.setCallback(() -> {
try {
Bitmap screenshot = ScreenshotsCaptorBuilder.captor.takeScreenshot();
// Assert screenshot is non-null
} catch (CaptureScreenshotFailedException e) {
throw new RuntimeException(e);
}
});
Intent intent = ScreenshotsCaptorBuilder.intent(activity);
activity.startActivityForResult(intent, 100);
});
}
}
我的助手活动 src/androidTest/java/org/app
:
import org.app.test.R;
public class TestActivity extends Activity {
private Callback callback;
@Override
public void onCreate(Bundle savedInstanceState) {
// This line is never reached
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
public void setCallback(Callback callback) {
// This line is never reached
this.callback = callback;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// This line is never reached
ScreenshotsCaptorBuilder.createCaptor(this, resultCode, intent);
callback.done();
}
public interface Callback {
// This line is never reached
void done();
}
}
这个 src/androidTest/AndroidManifest.xml
文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.app">
<application>
<activity
android:name=".TestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以及 src/androidTest/res/layout/activity_test.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
如果有人能给我一些建议或提示来解决我的问题,那将是非常有帮助的。提前谢谢。
暂无答案!
目前还没有任何答案,快来回答吧!