我观察到一种奇怪的行为,即Espresso中创建的活动即使在重新创建后也会保留其引用。尝试旋转活动然后检查适配器的计数时,我遇到了这个问题。
以下是我使用的代码:
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new Adapter();
Log.e("MAIN", "Adapter: " + adapter);
}
测试:
@Test
public void itemCountSavedOnRotate() {
Log.e("TEST", "Adapter: " + mActivity.adapter);
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
sleep(1000);
Log.e("TEST", "Adapter: " + mActivity.adapter);
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
sleep(1000);
Log.e("TEST", "Adapter: " + mActivity.adapter);
}
确认问题的logcat:
06-28 21:12:30.835 25871-25871/com.simas E/MAIN﹕ Adapter: com.simas.versatileviewpager.sample.MainActivity$1@25f54f7d
06-28 21:12:31.344 25871-25892/com.simas E/TEST﹕ Adapter: com.simas.versatileviewpager.sample.MainActivity$1@25f54f7d
06-28 21:12:31.485 25871-25871/com.simas E/MAIN﹕ Adapter: com.simas.versatileviewpager.sample.MainActivity$1@2969e070
06-28 21:12:32.429 25871-25892/com.simas E/TEST﹕ Adapter: com.simas.versatileviewpager.sample.MainActivity$1@25f54f7d
06-28 21:12:32.571 25871-25871/com.simas E/MAIN﹕ Adapter: com.simas.versatileviewpager.sample.MainActivity$1@2fae45a0
06-28 21:12:33.518 25871-25892/com.simas E/TEST﹕ Adapter: com.simas.versatileviewpager.sample.MainActivity$1@25f54f7d
这可能是Espresso的默认行为吗?
我不应该直接检查引用,而是测试UI吗?如果是这样,那么这种测试的可能替代方案(工具)是什么?