provide an overridable method to create the barcode scanner in tests
Showing
1 changed file
with
12 additions
and
6 deletions
| @@ -13,6 +13,7 @@ import android.os.Looper | @@ -13,6 +13,7 @@ import android.os.Looper | ||
| 13 | import android.util.Size | 13 | import android.util.Size |
| 14 | import android.view.Surface | 14 | import android.view.Surface |
| 15 | import android.view.WindowManager | 15 | import android.view.WindowManager |
| 16 | +import androidx.annotation.VisibleForTesting | ||
| 16 | import androidx.camera.core.Camera | 17 | import androidx.camera.core.Camera |
| 17 | import androidx.camera.core.CameraSelector | 18 | import androidx.camera.core.CameraSelector |
| 18 | import androidx.camera.core.ExperimentalGetImage | 19 | import androidx.camera.core.ExperimentalGetImage |
| @@ -41,7 +42,7 @@ class MobileScanner( | @@ -41,7 +42,7 @@ class MobileScanner( | ||
| 41 | private val activity: Activity, | 42 | private val activity: Activity, |
| 42 | private val textureRegistry: TextureRegistry, | 43 | private val textureRegistry: TextureRegistry, |
| 43 | private val mobileScannerCallback: MobileScannerCallback, | 44 | private val mobileScannerCallback: MobileScannerCallback, |
| 44 | - private val mobileScannerErrorCallback: MobileScannerErrorCallback | 45 | + private val mobileScannerErrorCallback: MobileScannerErrorCallback, |
| 45 | ) { | 46 | ) { |
| 46 | 47 | ||
| 47 | /// Internal variables | 48 | /// Internal variables |
| @@ -159,6 +160,15 @@ class MobileScanner( | @@ -159,6 +160,15 @@ class MobileScanner( | ||
| 159 | return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) | 160 | return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) |
| 160 | } | 161 | } |
| 161 | 162 | ||
| 163 | + /** | ||
| 164 | + * Create a barcode scanner from the given options. | ||
| 165 | + * | ||
| 166 | + * Can be overridden in tests. | ||
| 167 | + */ | ||
| 168 | + @VisibleForTesting | ||
| 169 | + fun createBarcodeScanner(options: BarcodeScannerOptions?) : BarcodeScanner { | ||
| 170 | + return if (options == null) BarcodeScanning.getClient() else BarcodeScanning.getClient(options) | ||
| 171 | + } | ||
| 162 | 172 | ||
| 163 | // scales the scanWindow to the provided inputImage and checks if that scaled | 173 | // scales the scanWindow to the provided inputImage and checks if that scaled |
| 164 | // scanWindow contains the barcode | 174 | // scanWindow contains the barcode |
| @@ -238,11 +248,7 @@ class MobileScanner( | @@ -238,11 +248,7 @@ class MobileScanner( | ||
| 238 | } | 248 | } |
| 239 | 249 | ||
| 240 | lastScanned = null | 250 | lastScanned = null |
| 241 | - scanner = if (barcodeScannerOptions != null) { | ||
| 242 | - BarcodeScanning.getClient(barcodeScannerOptions) | ||
| 243 | - } else { | ||
| 244 | - BarcodeScanning.getClient() | ||
| 245 | - } | 251 | + scanner = createBarcodeScanner(barcodeScannerOptions) |
| 246 | 252 | ||
| 247 | val cameraProviderFuture = ProcessCameraProvider.getInstance(activity) | 253 | val cameraProviderFuture = ProcessCameraProvider.getInstance(activity) |
| 248 | val executor = ContextCompat.getMainExecutor(activity) | 254 | val executor = ContextCompat.getMainExecutor(activity) |
-
Please register or login to post a comment