mercredi 2 mars 2016

How to test unsupported views in Robolectric?

So I'm trying to use Robolectric to verify that clicking a button will start a service is being started correctly. However, since I'm using a custom view from the com.google.android.gms.common package, it appears that all of the onClickListeners don't bind correctly to the SignInButton. The following is my code:

TestLoginActivity.java:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public class TestLoginActivity {

    private LoginActivity mActivity;

    @Before
    public void setup() {
    mActivity = Robolectric.buildActivity(LoginActivity.class)
                .create()
                .visible()
                .get();
    }

    @Test
    public void testRegistrationServiceStarted() {
        SignInButton signIn = (SignInButton) mActivity.findViewById(R.id.signup);
        assertTrue(signIn != null);
        assertTrue(signIn.hasOnClickListeners());
        signIn.performClick();

        Intent expectedGcm = new Intent(mActivity, RegistrationIntentService.class);
        Intent actual = Shadows.shadowOf(mActivity).getNextStartedService();
        assertEquals(expectedGcm, actual);
    }

}

LoginActivity.java    

public class LoginActivity extends Activity implements LoginView {

    private static final String TAG = LoginActivity.class.getSimpleName();

    @Bind(R.id.signup)
    SignInButton mSignUp;

    @Inject
    LoginPresenter mLoginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_container);
        getApplication().getComponent().inject(this);
        ButterKnife.bind(this);
        mLoginPresenter.setLoginView(this);
   }

    @OnClick(R.id.signup)
    protected void onSignIn() {
        mLoginPresenter.registerWithGcm(this);
    }

}

The assertTrue on signIn.hasOnClickListeners() fails. However, if I use a regular Button, the test passes. I've also have tried included testCompile 'org.robolectric:shadows-play-services:3.0' but that didn't work either.

How should I proceed with testing out this component? Furthermore, should I create a new issue on the Robolectric project?

Aucun commentaire:

Enregistrer un commentaire