public void testRun() throws Exception { // Expect service data calls TripStatistics stats = new TripStatistics(); // Expect announcement building call expect(mockTask.getAnnouncement(same(stats))).andStubReturn(ANNOUNCEMENT); // Put task in "ready" state startTask(TextToSpeech.SUCCESS); expect(tts.isLanguageAvailable(DEFAULT_LOCALE)).andStubReturn(TextToSpeech.LANG_AVAILABLE); expect(tts.setLanguage(DEFAULT_LOCALE)).andReturn(TextToSpeech.LANG_AVAILABLE); expect(tts.setSpeechRate(AnnouncementPeriodicTask.TTS_SPEECH_RATE)) .andReturn(TextToSpeech.SUCCESS); expect(tts.setOnUtteranceCompletedListener((OnUtteranceCompletedListener) EasyMock.anyObject())) .andReturn(0); // Expect actual announcement call expect( tts.speak( eq(ANNOUNCEMENT), eq(TextToSpeech.QUEUE_FLUSH), eq(AnnouncementPeriodicTask.SPEECH_PARAMS))) .andReturn(0); // Run the announcement AndroidMock.replay(tts); task.announce(stats); AndroidMock.verify(mockTask, tts); }
public void testApiAsyncCallsListOnProviderPaginated() throws Throwable { final String endpoint = "foobar"; final String key = "foobar_key"; final String ids[] = null; final int start = 0, end = 10; final ListResult<SocializeObject> returned = new ListResult<SocializeObject>(new LinkedList<SocializeObject>()); AndroidMock.expect(provider.list(mockSession, endpoint, key, ids, "id", start, end)) .andReturn(returned); AndroidMock.replay(provider); runTestOnUiThread( new Runnable() { @Override public void run() { api.listAsync(mockSession, endpoint, key, ids, start, end, listener); } }); assertTrue("Timeout waiting for countdown latch", signal.await(timeout, TimeUnit.SECONDS)); AndroidMock.verify(provider); }
@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); } }; }
/** * In addition to {@link AbstractTestCaseForClientMessageHandler#setUp()} is set up also a mock * for {@link ILocationService}. */ @Override protected void setUp() { super.setUp(); locationServiceMock = AndroidMock.createStrictMock(ILocationService.class); AndroidMock.expect(cdcMock.getLocationService()).andReturn(locationServiceMock).anyTimes(); }
public void testRun_noService() throws Exception { startTask(TextToSpeech.SUCCESS); // Run the announcement AndroidMock.replay(tts); task.run(null); AndroidMock.verify(mockTask, tts); }
private void startTask(int state) { AndroidMock.resetToNice(tts); AndroidMock.replay(tts); doStart(); OnInitListener ttsInitListener = initListenerCapture.getValue(); ttsInitListener.onInit(state); AndroidMock.resetToDefault(tts); }
public void testDialogDismissWorksOnError() throws Throwable { AndroidMock.expect( provider.authenticate("/authenticate/", "test_key", "test_secret", "test_uuid")) .andThrow(new SocializeException("TEST ERROR IGNORE ME!")); AndroidMock.replay(provider); final ProgressDialog authProgress = ProgressDialog.show(getActivity(), "Authenticating", "Please wait..."); final SocializeAuthListener alistener = new SocializeAuthListener() { @Override public void onAuthSuccess(SocializeSession session) { authProgress.dismiss(); signal.countDown(); fail(); } @Override public void onAuthFail(SocializeException error) { authProgress.dismiss(); signal.countDown(); fail(); } @Override public void onError(SocializeException error) { authProgress.dismiss(); signal.countDown(); } @Override public void onCancel() { authProgress.dismiss(); signal.countDown(); fail(); } }; runTestOnUiThread( new Runnable() { @Override public void run() { api.authenticateAsync( getContext(), "test_key", "test_secret", "test_uuid", null, alistener, mockSessionConsumer, false); } }); assertTrue("Timeout waiting for countdown latch", signal.await(timeout, TimeUnit.SECONDS)); }
public void testRun_notReady() throws Exception { // Put task in "not ready" state startTask(TextToSpeech.ERROR); // Run the announcement AndroidMock.replay(tts); task.run(null); AndroidMock.verify(mockTask, tts); }
public void testStart_notReady() { doStart(); OnInitListener ttsInitListener = initListenerCapture.getValue(); assertNotNull(ttsInitListener); AndroidMock.replay(tts); ttsInitListener.onInit(TextToSpeech.ERROR); AndroidMock.verify(mockTask, tts); }
@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); }
public void testRun_whileRinging() throws Exception { startTask(TextToSpeech.SUCCESS); expect(tts.isSpeaking()).andStubReturn(false); // Run the announcement AndroidMock.replay(tts); PhoneStateListener phoneListener = phoneListenerCapture.getValue(); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null); task.run(null); AndroidMock.verify(mockTask, tts); }
public void testApiAsyncGetCallsAddOnResponse() throws Throwable { final String endpoint = "foobar"; final String id = null; SocializeObject obj = new SocializeObject(); api.setResponseFactory(responseFactory); AndroidMock.expect(responseFactory.newEntityResponse()).andReturn(mockEntityResponse); AndroidMock.expect(provider.get(mockSession, endpoint, id)).andReturn(obj); mockEntityResponse.addResult(obj); AndroidMock.replay(responseFactory); AndroidMock.replay(provider); AndroidMock.replay(mockEntityResponse); runTestOnUiThread( new Runnable() { @Override public void run() { api.getAsync(mockSession, endpoint, id, listener); } }); assertTrue("Timeout waiting for countdown latch", signal.await(timeout, TimeUnit.SECONDS)); AndroidMock.verify(responseFactory); AndroidMock.verify(provider); AndroidMock.verify(mockEntityResponse); }
public void testShutdown() { // First, start doStart(); AndroidMock.verify(mockTask); AndroidMock.reset(mockTask); // Then, shut down PhoneStateListener phoneListener = phoneListenerCapture.getValue(); mockTask.listenToPhoneState(same(phoneListener), eq(PhoneStateListener.LISTEN_NONE)); tts.shutdown(); AndroidMock.replay(mockTask, tts); task.shutdown(); AndroidMock.verify(mockTask, tts); }
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 testRun_ringWhileSpeaking() throws Exception { startTask(TextToSpeech.SUCCESS); expect(tts.isSpeaking()).andStubReturn(true); expect(tts.stop()).andReturn(TextToSpeech.SUCCESS); AndroidMock.replay(tts); // Update the state to ringing - this should stop the current announcement. PhoneStateListener phoneListener = phoneListenerCapture.getValue(); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null); // Run the announcement - this should do nothing. task.run(null); AndroidMock.verify(mockTask, tts); }
private void doStart() { mockTask.listenToPhoneState( capture(phoneListenerCapture), eq(PhoneStateListener.LISTEN_CALL_STATE)); expect(mockTask.newTextToSpeech(same(getContext()), capture(initListenerCapture))) .andStubReturn(ttsDelegate); AndroidMock.replay(mockTask); task.start(); }
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); }
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); }
public void testApiAsyncCallsPutOnProviderWithList() throws Throwable { final String endpoint = "foobar"; final List<SocializeObject> objects = new ArrayList<SocializeObject>(); AndroidMock.expect(provider.put(mockSession, endpoint, objects)).andReturn(null); AndroidMock.replay(provider); runTestOnUiThread( new Runnable() { @Override public void run() { api.putAsync(mockSession, endpoint, objects, listener); } }); assertTrue("Timeout waiting for countdown latch", signal.await(timeout, TimeUnit.SECONDS)); AndroidMock.verify(provider); }
public void testApiAsyncCallsGetOnProvider() throws Throwable { final String endpoint = "foobar"; final String id = null; AndroidMock.expect(provider.get(mockSession, endpoint, id)).andReturn(new SocializeObject()); AndroidMock.replay(provider); runTestOnUiThread( new Runnable() { @Override public void run() { api.getAsync(mockSession, endpoint, id, listener); } }); assertTrue("Timeout waiting for countdown latch", signal.await(timeout, TimeUnit.SECONDS)); AndroidMock.verify(provider); }
/** Tests {@link LocationMessageHandler#handleMessage}: Only location is transmitted. */ public void testHandleMessage_Location() { // Configure Test location = true; bearing = true; speed = true; initFeatures(); initMyLocation(); // Looking from myLocation to remoteLocation: // 42,45 +-0,1 km distance, -2° +-1 bearing logicListenerMock.setDistance(AndroidMock.eq(42.45f * 1000, 100), AndroidMock.eq(-2f, 1f)); // Run Test Location remoteLocation = new Location((String) null); setLatLong(remoteLocation); runTest(remoteLocation); // Verify Test verifyFieldMocks(); }
@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); }
@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); }
/** * Tests {@link AdjustSpeedMessageHandler#handleMessage}. {@link IAccessoryCommunication} throws * {@link RangeException}. */ public void testHandleMessage_rangeException() { // Configure Test accessoryCommunicationMock.rotateCamera(10f, -200f); RangeException ex = new RangeException("Oh no!"); AndroidMock.expectLastCall().andThrow(ex); logicMock.handleError(ex); // Run Test RotateCameraMessageHandler handler = aHandlerForTest(); handler.handleMessage(new RotateCameraMessage(10f, -200f)); // Verify Test verifyFieldMocks(); }
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); }
public void testApiAsyncListCallsSetResultsOnResponse() throws Throwable { final String endpoint = "foobar"; final String key = "foobar_key"; final String[] ids = null; final ListResult<SocializeObject> returned = new ListResult<SocializeObject>(new LinkedList<SocializeObject>()); api.setResponseFactory(responseFactory); AndroidMock.expect(responseFactory.newEntityResponse()).andReturn(mockEntityResponse); AndroidMock.expect( provider.list( mockSession, endpoint, key, ids, "id", 0, SocializeConfig.MAX_LIST_RESULTS)) .andReturn(returned); mockEntityResponse.setResults(returned); AndroidMock.replay(responseFactory); AndroidMock.replay(provider); AndroidMock.replay(mockEntityResponse); runTestOnUiThread( new Runnable() { @Override public void run() { api.listAsync(mockSession, endpoint, key, ids, listener); } }); assertTrue("Timeout waiting for countdown latch", signal.await(timeout, TimeUnit.SECONDS)); AndroidMock.verify(responseFactory); AndroidMock.verify(provider); AndroidMock.verify(mockEntityResponse); }
public void testDoesNotHavePermission() { AndroidMock.expect(appUtils.hasPermission(context, "android.permission.ACCESS_FINE_LOCATION")) .andReturn(false) .anyTimes(); AndroidMock.expect(appUtils.hasPermission(context, "android.permission.ACCESS_COARSE_LOCATION")) .andReturn(false) .anyTimes(); AndroidMock.expect(locationListenerFactory.getBean()).andReturn(listener); AndroidMock.replay(appUtils); AndroidMock.replay(locationListenerFactory); DefaultLocationProvider provider = new DefaultLocationProvider(); provider.setAppUtils(appUtils); provider.setLocationListenerFactory(locationListenerFactory); provider.init(context); assertNull(provider.getLastKnownLocation()); AndroidMock.verify(appUtils); AndroidMock.verify(locationListenerFactory); }
@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(); } }; }
@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); }