lundi 4 novembre 2019

Testing Espresso: service is created correctly

I'm trying to build a test with espresso that checks if the service is created when the button is clicked.

The MainActivity code:

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();
                }
        }
}

How can i check in the Test class? I know how to check if another activity i created, but i don't know how to control if the service is created.

Test class:

@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
    }
}

Aucun commentaire:

Enregistrer un commentaire