我正在尝试使用espresso构建测试,以检查单击按钮时是否创建了服务。
Mainactivity代码:
public class Mainactivity ... {
// Monitors the state of the connection to the service.
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name,IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
mBound = false;
}
};
//onCreate() doesn't matter,nothing related with service
@Override
protected void onStart() {
super.onStart();
//...
mRequestLocationUpdatesButton = findViewById(R.id.request_location_updates_button);
mRequestLocationUpdatesButton.setOnClicklistener(this);
bindService(new Intent(this,LocationUpdatesService.class),mServiceConnection,Context.BIND_AUTO_CREATE);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.request_location_updates_button) {
if (!checkPermissions()) {
requestPermissions();
} else {
mService.requestLocationUpdates();
}
}
}
我如何报名参加考试?我知道如何检查是否创建了另一个活动,但是我不知道如何控制是否创建了服务。
测试类:
@RunWith(AndroidJUnit4.class)
public class CheckactivityBehaviour {
@Rule
public activityScenarioRule<Mainactivity> activityMain
= new activityScenarioRule<Mainactivity>(Mainactivity.class);
@Test
public void performClick() {
onView(withId(R.id.request_location_updates_button)).perform(click());
//on click this button service is started
}
}