ShakeDetector implements SensorEventListener,IPermissionHandler { public void start(@Nullable SensorManager manager) { if (manager == null || (mConfig != null && !isPermissionGranted(mConfig))) { return; } Sensor accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); if (accelerometer != null) { this.mSensorManager = manager; mLastTimestamp = -1; mCurrentIndex = 0; mMagnitudes = new double[MAX_SAMPLES]; mTimestamps = new long[MAX_SAMPLES]; mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); } } ShakeDetector(@Nullable ShakeListener listener,@Nullable Config config); void start(@Nullable SensorManager manager); void stop(); @Override void onSensorChanged(SensorEvent event); @Override void onAccuracyChanged(Sensor sensor, int accuracy); @Override boolean isPermissionGranted(@NonNull Config config);  }
@Test public void start() throws Exception { ShakeDetector.ShakeListener mockListener = mock(ShakeDetector.ShakeListener.class); Sensor sensor = mock(Sensor.class); ShakeDetector detector = new ShakeDetector(mockListener,null); when(mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)).thenReturn(sensor); detector.start(mSensorManager); verify(mSensorManager).getDefaultSensor(Sensor.TYPE_ACCELEROMETER); verify(mSensorManager).registerListener(detector,sensor,SensorManager.SENSOR_DELAY_UI); }