WifiCapturer implements SensorCollector { public void makeFingerprint(final ProgressDialog progressDialog, final HistogramConsumer consumer) { this.progressDialog = progressDialog; this.fingerprintHistogramConsumer = consumer; startSensors(); } WifiCapturer(final Activity activity);  WifiCapturer(final Activity activity, final HistogramConsumer consumer, final int maxAge); void makeFingerprint(final  ProgressDialog progressDialog, final HistogramConsumer consumer); @Override void startSensors(); @Override void stopSensors(); static final String TAG; static final int NUMBER_SCANS; }
@Test public void testFirstScan() { wifiCapturer.makeFingerprint(progressDialog, consumer); receiver = getReceiver(); when(progressDialog.getProgress()).thenReturn(0); receiver.onReceive(null, null); ArgumentCaptor<Runnable> uiRunnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class); verify(activity).runOnUiThread(uiRunnableArgumentCaptor.capture()); Runnable uiRunnable = uiRunnableArgumentCaptor.getValue(); uiRunnable.run(); verify(progressDialog).setProgress(100 / WifiCapturer.NUMBER_SCANS); verify(wifiManager, times(2)).startScan(); verify(consumer, never()).addHistogram(any(Histogram.class)); }
@Test public void testLastScan() { wifiCapturer.makeFingerprint(progressDialog, consumer); receiver = getReceiver(); when(progressDialog.getProgress()).thenReturn(100); receiver.onReceive(null, null); ArgumentCaptor<Runnable> uiRunnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class); verify(activity).runOnUiThread(uiRunnableArgumentCaptor.capture()); Runnable uiRunnable = uiRunnableArgumentCaptor.getValue(); uiRunnable.run(); verify(progressDialog).dismiss(); verify(wifiManager, times(1)).startScan(); verify(consumer).addHistogram(any(Histogram.class)); }
@Test public void testFingerprintHistogram() { wifiCapturer.makeFingerprint(progressDialog, consumer); receiver = getReceiver(); when(progressDialog.getProgress()).thenReturn(1, 2, 100); 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); receiver.onReceive(null, null); receiver.onReceive(null, null); receiver.onReceive(null, null); ArgumentCaptor<Runnable> uiRunnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class); Runnable uiRunnable; verify(activity, times(3)).runOnUiThread(uiRunnableArgumentCaptor.capture()); uiRunnable = uiRunnableArgumentCaptor.getAllValues().get(0); uiRunnable.run(); uiRunnable = uiRunnableArgumentCaptor.getAllValues().get(1); uiRunnable.run(); uiRunnable = uiRunnableArgumentCaptor.getAllValues().get(2); uiRunnable.run(); ArgumentCaptor<Histogram> consumerArgumentCaptor = ArgumentCaptor.forClass(Histogram.class); verify(consumer).addHistogram(consumerArgumentCaptor.capture()); Histogram histogram = consumerArgumentCaptor.getValue(); assertEquals(1, histogram.size()); assertTrue(histogram.keySet().contains("00:01:02:03:04:05")); Map<Integer, Float> levels = histogram.get("00:01:02:03:04:05"); assertEquals(2, levels.size()); assertEquals(1.0 / 3.0, levels.get(-40), 0.00001); assertEquals(2.0 / 3.0, levels.get(-50), 0.00001); }