@UsesMocks({SocializeEntityLoader.class, EntityLoaderUtils.class}) public void testShowEntityById() { final Entity entity = new Entity(); SocializeEntityUtils utils = new SocializeEntityUtils() { @Override public void getEntity(Activity context, long id, EntityGetListener listener) { listener.onGet(entity); } }; SocializeEntityLoader entityLoader = AndroidMock.createMock(SocializeEntityLoader.class); EntityLoaderUtils entityLoaderUtils = AndroidMock.createMock(EntityLoaderUtils.class); AndroidMock.expect(entityLoaderUtils.initEntityLoader()).andReturn(entityLoader); AndroidMock.expect(entityLoader.canLoad(TestUtils.getActivity(this), entity)).andReturn(true); entityLoader.loadEntity(TestUtils.getActivity(this), entity); AndroidMock.replay(entityLoaderUtils, entityLoader); utils.setEntityLoaderUtils(entityLoaderUtils); utils.showEntity(TestUtils.getActivity(this), 123, null); AndroidMock.verify(entityLoaderUtils, entityLoader); }
@UsesMocks({ AnnouncementPeriodicTask.class, StringUtils.class, }) @Override protected void setUp() throws Exception { super.setUp(); oldDefaultLocale = Locale.getDefault(); Locale.setDefault(DEFAULT_LOCALE); // Eww, the effort required just to mock TextToSpeech is insane final AtomicBoolean listenerCalled = new AtomicBoolean(); OnInitListener blockingListener = new OnInitListener() { @Override public void onInit(int status) { synchronized (this) { listenerCalled.set(true); notify(); } } }; ttsDelegate = new TextToSpeechDelegate(getContext(), blockingListener); // Wait for all async operations done in the constructor to finish. synchronized (blockingListener) { while (!listenerCalled.get()) { // Releases the synchronized lock until we're woken up. blockingListener.wait(); } } // Phew, done, now we can start forwarding calls tts = AndroidMock.createMock(TextToSpeechInterface.class); initListenerCapture = new Capture<OnInitListener>(); phoneListenerCapture = new Capture<PhoneStateListener>(); // Create a partial forwarding mock mockTask = AndroidMock.createMock(AnnouncementPeriodicTask.class, getContext()); task = new AnnouncementPeriodicTask(getContext()) { @Override protected TextToSpeech newTextToSpeech(Context ctx, OnInitListener onInitListener) { return mockTask.newTextToSpeech(ctx, onInitListener); } @Override protected String getAnnouncement(TripStatistics stats) { return mockTask.getAnnouncement(stats); } @Override protected void listenToPhoneState(PhoneStateListener listener, int events) { mockTask.listenToPhoneState(listener, events); } }; }
@UsesMocks({SocializeConfig.class, MockContext.class, Intent.class}) public void test_registerC2DM() { final String senderId = "foobar"; NotificationRegistrationState notificationRegistrationState = AndroidMock.createMock(NotificationRegistrationState.class); SocializeConfig config = AndroidMock.createMock(SocializeConfig.class); MockContext context = AndroidMock.createNiceMock(MockContext.class); final Intent registrationIntent = AndroidMock.createMock(Intent.class); AndroidMock.expect(notificationRegistrationState.isC2dmPending()).andReturn(false); notificationRegistrationState.setC2dmPendingRequestTime(AndroidMock.anyLong()); notificationRegistrationState.save(context); AndroidMock.expect(config.getProperty(SocializeConfig.SOCIALIZE_C2DM_SENDER_ID)) .andReturn(senderId); AndroidMock.expect( registrationIntent.putExtra( SocializeNotificationRegistrationSystem.EXTRA_APPLICATION_PENDING_INTENT, (PendingIntent) null)) .andReturn(registrationIntent); AndroidMock.expect( registrationIntent.putExtra( SocializeNotificationRegistrationSystem.EXTRA_SENDER, senderId)) .andReturn(registrationIntent); AndroidMock.expect(context.startService(registrationIntent)).andReturn(null); AndroidMock.replay(notificationRegistrationState, config, context, registrationIntent); SocializeNotificationRegistrationSystem system = new SocializeNotificationRegistrationSystem() { @Override public boolean isRegisteredC2DM() { return false; } @Override protected Intent newIntent(String action) { return registrationIntent; } @Override protected PendingIntent newPendingIntent(Context context) { return null; } }; system.setConfig(config); system.setNotificationRegistrationState(notificationRegistrationState); system.registerC2DM(context); AndroidMock.verify(notificationRegistrationState, config, context, registrationIntent); }
@SuppressWarnings("unchecked") @Override protected void setUp() throws Exception { super.setUp(); appUtils = AndroidMock.createMock(AppUtils.class); context = AndroidMock.createMock(Activity.class); locationManager = AndroidMock.createMock(SocializeLocationManager.class, appUtils); locationListenerFactory = AndroidMock.createMock(IBeanFactory.class); listener = AndroidMock.createMock(SocializeLocationListener.class); }
@SuppressWarnings("unchecked") public void test_doRegistrationSocialize() throws SocializeException { final String registrationId = "foobar"; final User user = new User(); DeviceRegistration registration = AndroidMock.createMock(DeviceRegistration.class); NotificationRegistrationState notificationRegistrationState = AndroidMock.createMock(NotificationRegistrationState.class); DeviceRegistrationSystem deviceRegistrationSystem = new DeviceRegistrationSystem() { @Override public void registerDevice( SocializeSession session, DeviceRegistration registration, DeviceRegistrationListener listener) { listener.onSuccess(); } }; IBeanFactory<DeviceRegistration> deviceRegistrationFactory = AndroidMock.createMock(IBeanFactory.class); SocializeSession session = AndroidMock.createMock(SocializeSession.class); AndroidMock.expect(deviceRegistrationFactory.getBean()).andReturn(registration); AndroidMock.expect(session.getUser()).andReturn(user); registration.setRegistrationId(registrationId); notificationRegistrationState.setC2DMRegistrationId(registrationId); notificationRegistrationState.setRegisteredSocialize(user); notificationRegistrationState.save(getContext()); AndroidMock.replay( deviceRegistrationFactory, registration, notificationRegistrationState, session); PublicSocializeNotificationRegistrationSystem system = new PublicSocializeNotificationRegistrationSystem(); system.setDeviceRegistrationFactory(deviceRegistrationFactory); system.setDeviceRegistrationSystem(deviceRegistrationSystem); system.setNotificationRegistrationState(notificationRegistrationState); system.doRegistrationSocialize(getContext(), session, registrationId); AndroidMock.verify( deviceRegistrationFactory, registration, notificationRegistrationState, session); }
public void test_registerSocialize() { NotificationRegistrationState notificationRegistrationState = AndroidMock.createMock(NotificationRegistrationState.class); notificationRegistrationState.setPendingSocializeRequestTime(AndroidMock.anyLong()); notificationRegistrationState.save(getContext()); final SocializeSession session = AndroidMock.createMock(SocializeSession.class); final String registrationId = "foobar"; SocializeUserSystem userSystem = new SocializeUserSystem(null) { @Override public void authenticate( Context context, SocializeAuthListener listener, SocializeSessionConsumer sessionConsumer) { listener.onAuthSuccess(session); } }; SocializeNotificationRegistrationSystem system = new SocializeNotificationRegistrationSystem() { @Override protected void doRegistrationSocialize( Context context, SocializeSession session, String registrationId) { addResult(0, session); } }; system.setUserSystem(userSystem); system.setNotificationRegistrationState(notificationRegistrationState); AndroidMock.replay(notificationRegistrationState, session); system.registerSocialize(getContext(), registrationId); AndroidMock.verify(notificationRegistrationState, session); SocializeSession result = getResult(0); assertSame(session, result); }
@SuppressWarnings("unchecked") @UsesMocks({ SocializeProvider.class, SocializeSession.class, SocializeResponseFactory.class, SocializeEntityResponse.class, SocializeConfig.class, Properties.class, SocializeSessionConsumer.class }) @Override protected void setUp() throws Exception { super.setUp(); signal = new CountDownLatch(1); provider = AndroidMock.createMock(SocializeProvider.class); responseFactory = AndroidMock.createMock(SocializeResponseFactory.class); mockEntityResponse = AndroidMock.createMock(SocializeEntityResponse.class); mockSessionConsumer = AndroidMock.createMock(SocializeSessionConsumer.class); api = new SocializeApi<SocializeObject, SocializeProvider<SocializeObject>>(provider); mockSession = AndroidMock.createMock(SocializeSession.class); AndroidMock.replay(mockSession); listener = new SocializeActionListener() { @Override public void onResult(RequestType type, SocializeResponse response) { System.out.println("Api listener onResult fired"); signal.countDown(); } @Override public void onError(SocializeException error) { System.out.println("Api listener onError fired"); signal.countDown(); } }; }
public void test_isRegistrationPending() { NotificationRegistrationState notificationRegistrationState = AndroidMock.createMock(NotificationRegistrationState.class); AndroidMock.expect(notificationRegistrationState.isC2dmPending()).andReturn(true); AndroidMock.replay(notificationRegistrationState); SocializeNotificationRegistrationSystem system = new SocializeNotificationRegistrationSystem(); system.setNotificationRegistrationState(notificationRegistrationState); assertTrue(system.isRegistrationPending()); AndroidMock.verify(notificationRegistrationState); }
public void test_isRegisteredSocialize() { NotificationRegistrationState notificationRegistrationState = AndroidMock.createMock(NotificationRegistrationState.class); final User user = new User(); AndroidMock.expect(notificationRegistrationState.isRegisteredSocialize(user)).andReturn(true); AndroidMock.replay(notificationRegistrationState); SocializeNotificationRegistrationSystem system = new SocializeNotificationRegistrationSystem(); system.setNotificationRegistrationState(notificationRegistrationState); assertTrue(system.isRegisteredSocialize(user)); AndroidMock.verify(notificationRegistrationState); }
public void test_registerC2DMFailed() { NotificationRegistrationState notificationRegistrationState = AndroidMock.createMock(NotificationRegistrationState.class); notificationRegistrationState.setC2dmPendingRequestTime(0); notificationRegistrationState.save(getContext()); AndroidMock.replay(notificationRegistrationState); SocializeNotificationRegistrationSystem system = new SocializeNotificationRegistrationSystem(); system.setNotificationRegistrationState(notificationRegistrationState); system.registerC2DMFailed(getContext(), null); AndroidMock.verify(notificationRegistrationState); }
@UsesMocks({ NotificationAuthenticator.class, ActivitySystem.class, SocializeSession.class, SocializeAction.class, User.class, NotificationMessage.class }) public void test_translate() throws SocializeException { NotificationAuthenticator notificationAuthenticator = AndroidMock.createMock(NotificationAuthenticator.class); ActivitySystem activitySystem = AndroidMock.createMock(ActivitySystem.class); SocializeSession session = AndroidMock.createMock(SocializeSession.class); SocializeAction action = AndroidMock.createMock(SocializeAction.class); User user = AndroidMock.createMock(User.class); NotificationMessage message = AndroidMock.createMock(NotificationMessage.class); final long actionId = 69L; final long userId = 1001L; final ActionType type = ActionType.COMMENT; final Context context = getContext(); AndroidMock.expect(message.getActionId()).andReturn(actionId).anyTimes(); AndroidMock.expect(message.getActionType()).andReturn(type).anyTimes(); AndroidMock.expect(notificationAuthenticator.authenticate(context)).andReturn(session); AndroidMock.expect(activitySystem.getAction(session, actionId, type)).andReturn(action); AndroidMock.expect(action.getUser()).andReturn(user); AndroidMock.expect(user.getId()).andReturn(userId).anyTimes(); AndroidMock.replay(notificationAuthenticator, activitySystem, session, action, user, message); SocializeActionMessageTranslator translator = new SocializeActionMessageTranslator(); Bundle data = new Bundle(); translator.setActivitySystem(activitySystem); translator.setNotificationAuthenticator(notificationAuthenticator); SocializeAction translated = translator.translate(context, data, message); AndroidMock.verify(notificationAuthenticator, activitySystem, session, action, user, message); assertNotNull(translated); assertSame(action, translated); assertNotNull(data.getString(Socialize.ACTION_ID)); assertNotNull(data.getString(Socialize.USER_ID)); assertEquals(String.valueOf(actionId), data.getString(Socialize.ACTION_ID)); assertEquals(String.valueOf(userId), data.getString(Socialize.USER_ID)); }
public void testLastKnownLocationAvailable() { final String strProvider = "foobar"; Location location = AndroidMock.createMock(Location.class, strProvider); AndroidMock.expect(appUtils.hasPermission(context, "android.permission.ACCESS_FINE_LOCATION")) .andReturn(true); AndroidMock.expect( locationManager.getBestProvider( (Criteria) AndroidMock.anyObject(), AndroidMock.eq(true))) .andReturn(strProvider); AndroidMock.expect(locationManager.getLastKnownLocation(strProvider)).andReturn(location); AndroidMock.expect(config.getBooleanProperty(SocializeConfig.SOCIALIZE_LOCATION_ENABLED, true)) .andReturn(true) .anyTimes(); locationManager.removeUpdates(listener); AndroidMock.expect(locationListenerFactory.getBean()).andReturn(listener); AndroidMock.replay(appUtils, locationManager, locationListenerFactory, config); DefaultLocationProvider provider = new DefaultLocationProvider(); provider.setLocationManager(locationManager); provider.setAppUtils(appUtils); provider.setConfig(config); provider.setLocationListenerFactory(locationListenerFactory); provider.init(context); Location loc = provider.getLocation(getContext()); AndroidMock.verify(appUtils, locationManager, locationListenerFactory, config); assertNotNull(loc); assertSame(location, loc); }
@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()); }