protected void doOnClickTest(final AUTH_REPSONSE response) throws Exception { final CountDownLatch latch = new CountDownLatch(1); SocializeAuthListener listener = null; // Can't mock the "getContext" method View view = Mockito.spy(new View(getContext())); FacebookUtilsProxy facebookUtils = new FacebookUtilsProxy() { @Override public void onResume(Activity context, SocializeAuthListener listener) {} @Override public void getCurrentPermissions( Activity context, String token, OnPermissionResult callback) {} @Override public void unlink(Context context, SocializeDeAuthListener listener) {} @Override public void setAppId(Context context, String appId) {} @Override public void postEntity( Activity context, Entity entity, String text, SocialNetworkShareListener listener) {} @Override public void link( Activity context, String token, boolean verifyPermissions, SocializeAuthListener listener) {} @Override public void link( Activity context, SocializeAuthListener listener, String... permissions) {} @Override public void link(Activity context, SocializeAuthListener listener) {} @Override public boolean isLinked(Context context) { return false; } @Override public void linkForRead( Activity context, SocializeAuthListener listener, String... permissions) { addResult(0, listener); latch.countDown(); } @Override public void linkForRead( Activity context, String token, boolean verifyPermissions, SocializeAuthListener listener, String... permissions) {} @Override public void linkForWrite( Activity context, SocializeAuthListener listener, String... permissions) {} @Override public void linkForWrite( Activity context, String token, boolean verifyPermissions, SocializeAuthListener listener, String... permissions) {} @Override public boolean isLinkedForRead(Context context, String... permissions) { return false; } @Override public boolean isLinkedForWrite(Context context, String... permissions) { return false; } @Override public boolean isAvailable(Context context) { return true; } @Override public String getAccessToken(Context context) { return null; } @Override public byte[] getImageForPost(Activity context, Uri imagePath) throws IOException { return null; } @Override public byte[] getImageForPost(Activity context, Bitmap image, CompressFormat format) throws IOException { return null; } @Override public void post( Activity context, String graphPath, Map<String, Object> postData, SocialNetworkPostListener listener) {} @Override public void get( Activity context, String graphPath, Map<String, Object> postData, SocialNetworkPostListener listener) {} @Override public void delete( Activity context, String graphPath, Map<String, Object> postData, SocialNetworkPostListener listener) {} @Override public void extendAccessToken(Activity context, SocializeAuthListener listener) {} @Override public void getCurrentPermissions( Activity parent, String token, FacebookPermissionCallback callback) {} @Override public Facebook getFacebook(Context context) { return null; } }; // Stub in facebook utils SocializeIOC.registerStub("facebookUtils", facebookUtils); final SocializeSession session = Mockito.mock(SocializeSession.class); final SocializeException error = new SocializeException("DUMMY - IGNORE ME"); FacebookAuthClickListener facebookAuthClickListener = new FacebookAuthClickListener(); facebookAuthClickListener.setListener(listener); facebookAuthClickListener.onClick(view); boolean ok = latch.await(20, TimeUnit.SECONDS); assertTrue(ok); listener = getResult(0); assertNotNull(listener); switch (response) { case SUCCESS: listener.onAuthSuccess(session); break; case CANCEL: listener.onCancel(); break; case ERROR: listener.onError(error); break; case FAIL: listener.onAuthFail(error); break; } Mockito.verify(view).setEnabled(false); Mockito.verify(view).setEnabled(true); }
@SuppressWarnings("unchecked") protected void doOnClickTest(final AUTH_REPSONSE response) { final String consumerKey = "foo"; final String consumerSecret = "bar"; final String fbId = "foobar_FB"; SocializeConfig config = AndroidMock.createMock(SocializeConfig.class); SocializeAuthListener listener = AndroidMock.createMock(SocializeAuthListener.class); DialogFactory<ProgressDialog> dialogFactory = AndroidMock.createMock(DialogFactory.class); ProgressDialog dialog = AndroidMock.createMock(ProgressDialog.class, getContext()); View view = AndroidMock.createMock(View.class, getContext()); final SocializeSession session = AndroidMock.createMock(SocializeSession.class); final SocializeException error = new SocializeException("DUMMY - IGNORE ME"); final PublicSocialize socialize = new PublicSocialize() { @Override public void authenticate( Context context, String consumerKey, String consumerSecret, AuthProviderInfo authProviderInfo, SocializeAuthListener listener) { addResult(0, consumerKey); addResult(1, consumerSecret); addResult(2, authProviderInfo); // Call each method of the listener for the test switch (response) { case SUCCESS: listener.onAuthSuccess(session); break; case CANCEL: listener.onCancel(); break; case ERROR: listener.onError(error); break; case FAIL: listener.onAuthFail(error); break; } } }; view.setEnabled(false); AndroidMock.expect(config.getProperty(SocializeConfig.SOCIALIZE_CONSUMER_KEY)) .andReturn(consumerKey); AndroidMock.expect(config.getProperty(SocializeConfig.SOCIALIZE_CONSUMER_SECRET)) .andReturn(consumerSecret); AndroidMock.expect(config.getProperty(SocializeConfig.FACEBOOK_APP_ID)).andReturn(fbId); AndroidMock.expect(dialogFactory.show(getContext(), "Authentication", "Authenticating...")) .andReturn(dialog); dialog.dismiss(); view.setEnabled(true); switch (response) { case SUCCESS: listener.onAuthSuccess(session); break; case CANCEL: listener.onCancel(); break; case ERROR: listener.onError(error); break; case FAIL: listener.onAuthFail(error); break; } AndroidMock.replay(config); AndroidMock.replay(dialogFactory); AndroidMock.replay(dialog); AndroidMock.replay(view); AndroidMock.replay(listener); FacebookAuthClickListener facebookAuthClickListener = new FacebookAuthClickListener() { @Override protected SocializeService getSocialize() { return socialize; } }; facebookAuthClickListener.setConfig(config); facebookAuthClickListener.setDialogFactory(dialogFactory); facebookAuthClickListener.setListener(listener); facebookAuthClickListener.onClick(view); AndroidMock.verify(config); AndroidMock.verify(dialogFactory); AndroidMock.verify(dialog); AndroidMock.verify(view); AndroidMock.verify(listener); final String consumerKeyAfter = getResult(0); final String consumerSecretAfter = getResult(1); final AuthProviderInfo authProviderInfo = getResult(2); assertEquals(consumerKey, consumerKeyAfter); assertEquals(consumerSecret, consumerSecretAfter); assertTrue((authProviderInfo instanceof FacebookAuthProviderInfo)); FacebookAuthProviderInfo fba = (FacebookAuthProviderInfo) authProviderInfo; assertEquals(AuthProviderType.FACEBOOK, fba.getType()); assertEquals(fbId, fba.getAppId()); }