protected void unbindService(final ServiceConnection serviceConnection) { if (unbindServiceShouldThrowIllegalArgument) { throw new IllegalArgumentException(); } unboundServiceConnections.add(serviceConnection); boundServiceConnections.remove(serviceConnection); ShadowLooper shadowLooper = Shadow.extract(Looper.getMainLooper()); shadowLooper.post( () -> { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; if (serviceConnectionDataForServiceConnection.containsKey(serviceConnection)) { serviceConnectionDataWrapper = serviceConnectionDataForServiceConnection.get(serviceConnection); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnection.onServiceDisconnected( serviceConnectionDataWrapper.componentNameForBindService); }, 0); }
@Test public void shouldNotQueueMessagesIfLooperIsQuit() throws Exception { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); looper.quit(); assertThat(shadowOf(looper).hasQuit()).named("hasQuit").isTrue(); assertThat(shadowOf(looper).post(new Runnable() { @Override public void run() { } }, 0)).named("post").isFalse(); assertThat(shadowOf(looper).postAtFrontOfQueue(new Runnable() { @Override public void run() { } })).named("postAtFrontOfQueue").isFalse(); assertThat(shadowOf(looper).getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); }
protected boolean bindService( final Intent intent, final ServiceConnection serviceConnection, int i) { boundServiceConnections.add(serviceConnection); unboundServiceConnections.remove(serviceConnection); if (unbindableActions.contains(intent.getAction())) { return false; } startedServices.add(new Intent.FilterComparison(intent)); ShadowLooper shadowLooper = Shadow.extract(Looper.getMainLooper()); shadowLooper.post( () -> { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; final Intent.FilterComparison filterComparison = new Intent.FilterComparison(intent); if (serviceConnectionDataForIntent.containsKey(filterComparison)) { serviceConnectionDataWrapper = serviceConnectionDataForIntent.get(filterComparison); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnectionDataForServiceConnection.put( serviceConnection, serviceConnectionDataWrapper); serviceConnection.onServiceConnected( serviceConnectionDataWrapper.componentNameForBindService, serviceConnectionDataWrapper.binderForBindService); }, 0); return true; }
@Test public void shouldThrowawayRunnableQueueIfLooperQuits() throws Exception { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); shadowOf(looper).pause(); shadowOf(looper).post(new Runnable() { @Override public void run() { } }, 0); looper.quit(); assertThat(shadowOf(looper).hasQuit()).named("hasQuit").isTrue(); assertThat(shadowOf(looper).getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNull(); }
protected void unbindService(final ServiceConnection serviceConnection) { if (unbindServiceShouldThrowIllegalArgument) { throw new IllegalArgumentException(); } unboundServiceConnections.add(serviceConnection); boundServiceConnections.remove(serviceConnection); ShadowLooper shadowLooper = Shadow.extract(Looper.getMainLooper()); shadowLooper.post( () -> { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; if (serviceConnectionDataForServiceConnection.containsKey(serviceConnection)) { serviceConnectionDataWrapper = serviceConnectionDataForServiceConnection.get(serviceConnection); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnection.onServiceDisconnected( serviceConnectionDataWrapper.componentNameForBindService); }, 0); }
@Implementation public void unbindService(final ServiceConnection serviceConnection) { if (unbindServiceShouldThrowIllegalArgument) { throw new IllegalArgumentException(); } unboundServiceConnections.add(serviceConnection); boundServiceConnections.remove(serviceConnection); shadowOf(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; if (serviceConnectionDataForServiceConnection.containsKey(serviceConnection)) { serviceConnectionDataWrapper = serviceConnectionDataForServiceConnection.get(serviceConnection); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnection.onServiceDisconnected(serviceConnectionDataWrapper.componentNameForBindService); } }, 0); }
@Implementation public void unbindService(final ServiceConnection serviceConnection) { if (unbindServiceShouldThrowIllegalArgument) { throw new IllegalArgumentException(); } unboundServiceConnections.add(serviceConnection); boundServiceConnections.remove(serviceConnection); shadowOf(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; if (serviceConnectionDataForServiceConnection.containsKey(serviceConnection)) { serviceConnectionDataWrapper = serviceConnectionDataForServiceConnection.get(serviceConnection); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnection.onServiceDisconnected(serviceConnectionDataWrapper.componentNameForBindService); } }, 0); }
@Implementation public void unbindService(final ServiceConnection serviceConnection) { if (unbindServiceShouldThrowIllegalArgument) { throw new IllegalArgumentException(); } unboundServiceConnections.add(serviceConnection); boundServiceConnections.remove(serviceConnection); shadowOf(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; if (serviceConnectionDataForServiceConnection.containsKey(serviceConnection)) { serviceConnectionDataWrapper = serviceConnectionDataForServiceConnection.get(serviceConnection); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnection.onServiceDisconnected(serviceConnectionDataWrapper.componentNameForBindService); } }, 0); }
@Implementation public boolean bindService(final Intent intent, final ServiceConnection serviceConnection, int i) { boundServiceConnections.add(serviceConnection); unboundServiceConnections.remove(serviceConnection); if (unbindableActions.contains(intent.getAction())) { return false; } startedServices.add(new Intent.FilterComparison(intent)); shadowOf(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; final Intent.FilterComparison filterComparison = new Intent.FilterComparison(intent); if (serviceConnectionDataForIntent.containsKey(filterComparison)) { serviceConnectionDataWrapper = serviceConnectionDataForIntent.get(filterComparison); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnectionDataForServiceConnection.put(serviceConnection, serviceConnectionDataWrapper); serviceConnection.onServiceConnected(serviceConnectionDataWrapper.componentNameForBindService, serviceConnectionDataWrapper.binderForBindService); } }, 0); return true; }
protected boolean bindService( final Intent intent, final ServiceConnection serviceConnection, int i) { boundServiceConnections.add(serviceConnection); unboundServiceConnections.remove(serviceConnection); if (unbindableActions.contains(intent.getAction())) { return false; } startedServices.add(new Intent.FilterComparison(intent)); ShadowLooper shadowLooper = Shadow.extract(Looper.getMainLooper()); shadowLooper.post( () -> { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; final Intent.FilterComparison filterComparison = new Intent.FilterComparison(intent); if (serviceConnectionDataForIntent.containsKey(filterComparison)) { serviceConnectionDataWrapper = serviceConnectionDataForIntent.get(filterComparison); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnectionDataForServiceConnection.put( serviceConnection, serviceConnectionDataWrapper); serviceConnection.onServiceConnected( serviceConnectionDataWrapper.componentNameForBindService, serviceConnectionDataWrapper.binderForBindService); }, 0); return true; }
@Implementation public boolean bindService(final Intent intent, final ServiceConnection serviceConnection, int i) { boundServiceConnections.add(serviceConnection); unboundServiceConnections.remove(serviceConnection); if (unbindableActions.contains(intent.getAction())) { return false; } startedServices.add(new Intent.FilterComparison(intent)); shadowOf(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; final Intent.FilterComparison filterComparison = new Intent.FilterComparison(intent); if (serviceConnectionDataForIntent.containsKey(filterComparison)) { serviceConnectionDataWrapper = serviceConnectionDataForIntent.get(filterComparison); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnectionDataForServiceConnection.put(serviceConnection, serviceConnectionDataWrapper); serviceConnection.onServiceConnected(serviceConnectionDataWrapper.componentNameForBindService, serviceConnectionDataWrapper.binderForBindService); } }, 0); return true; }
@Implementation public boolean bindService(final Intent intent, final ServiceConnection serviceConnection, int i) { boundServiceConnections.add(serviceConnection); unboundServiceConnections.remove(serviceConnection); if (unbindableActions.contains(intent.getAction())) { return false; } startedServices.add(new Intent.FilterComparison(intent)); shadowOf(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { final ServiceConnectionDataWrapper serviceConnectionDataWrapper; final Intent.FilterComparison filterComparison = new Intent.FilterComparison(intent); if (serviceConnectionDataForIntent.containsKey(filterComparison)) { serviceConnectionDataWrapper = serviceConnectionDataForIntent.get(filterComparison); } else { serviceConnectionDataWrapper = defaultServiceConnectionData; } serviceConnectionDataForServiceConnection.put(serviceConnection, serviceConnectionDataWrapper); serviceConnection.onServiceConnected(serviceConnectionDataWrapper.componentNameForBindService, serviceConnectionDataWrapper.binderForBindService); } }, 0); return true; }