@Test
public void testScanBuffer() {

    wifiCapturer.startSensors();
    // Get us the BroadcastReceiver that the Captor has created for the wifi-notifications:
    receiver = getReceiver();
    // The WifiManager-mock will return three different scanresults, one on each call to getScanResults:
    List<ScanResult> scanResult1 = makeScanResult(scan1, -50);
    List<ScanResult> scanResult2 = makeScanResult(scan2, -40);
    List<ScanResult> scanResult3 = makeScanResult(scan3, -50);
    when(wifiManager.getScanResults()).thenReturn(scanResult1, scanResult2, scanResult3);
    // Send 3 fake-notifications to the receiver:
    receiver.onReceive(null, null);
    receiver.onReceive(null, null);
    receiver.onReceive(null, null);
    // The activity should not have been bothered with any ui-stuff, because in this mode the scan is supposed to
    // happen completely in the background.
    verify(activity, never()).runOnUiThread(any(Runnable.class));
    // Collect the three Histograms, that our consumer hopefully received, one for each scan.
    // The ArgumentCaptor will automagically capture the arguments for each invocation:
    ArgumentCaptor<Histogram> consumerArgumentCaptor = ArgumentCaptor.forClass(Histogram.class);
    verify(consumer, times(3)).addHistogram(consumerArgumentCaptor.capture());
    Histogram histogram1 =  consumerArgumentCaptor.getAllValues().get(0);
    Histogram histogram2 =  consumerArgumentCaptor.getAllValues().get(1);
    Histogram histogram3 =  consumerArgumentCaptor.getAllValues().get(2);
    // All histograms should have the same size (there was always only one accesspoint visible):
    assertEquals(1, histogram1.size());
    assertEquals(1, histogram2.size());
    assertEquals(1, histogram3.size());
    // And it was really the same accesspoint each time:
    assertTrue(histogram1.keySet().contains("00:01:02:03:04:05"));
    assertTrue(histogram2.keySet().contains("00:01:02:03:04:05"));
    assertTrue(histogram3.keySet().contains("00:01:02:03:04:05"));
    // What's different is the levels that were collected for the different histograms, because the first one
    // had only one scanresult, the second had two, and the third histogram had all three scans.
    Map<Integer, Float> levels1 = histogram1.get("00:01:02:03:04:05");
    assertEquals(1, levels1.size());
    assertEquals(1.0f, levels1.get(-50), 0.00001);
    Map<Integer, Float> levels2 = histogram2.get("00:01:02:03:04:05");
    assertEquals(2, levels2.size());
    // Stupid floats need not be exactly equal:
    assertEquals(0.5f, levels2.get(-40), 0.00001);
    assertEquals(0.5f, levels2.get(-50), 0.00001);
    Map<Integer, Float> levels3 = histogram3.get("00:01:02:03:04:05");
    assertEquals(2, levels3.size());
    assertEquals(1.0 / 3.0, levels3.get(-40), 0.00001);
    assertEquals(2.0 / 3.0, levels3.get(-50), 0.00001);
}