1
+ package dev.steenbakker.mobile_scanner
2
+
3
+ import android.app.Activity
4
+ import android.graphics.Rect
5
+ import androidx.camera.core.ImageProxy
6
+ import com.google.mlkit.vision.barcode.BarcodeScanner
7
+ import com.google.mlkit.vision.barcode.BarcodeScannerOptions
8
+ import com.google.mlkit.vision.barcode.common.Barcode
9
+ import kotlin.test.Test
10
+ import org.mockito.Mockito
11
+ import io.flutter.view.TextureRegistry
12
+ import kotlin.test.expect
13
+
14
+ /*
15
+ * This demonstrates a simple unit test of the Kotlin portion of this plugin's implementation.
16
+ *
17
+ * Once you have built the plugin's example app, you can run these tests from the command
18
+ * line by running `./gradlew testDebugUnitTest` in the `example/android/` directory, or
19
+ * you can run them directly from IDEs that support JUnit such as Android Studio.
20
+ */
21
+
22
+ internal class MobileScannerTest {
23
+ @Test
24
+ fun isBarcodeInScanWindow_canHandleNaNValues () {
25
+ val barcodeScannerMock = Mockito .mock(BarcodeScanner ::class .java)
26
+
27
+ val mobileScanner = MobileScanner (
28
+ Mockito .mock(Activity ::class .java),
29
+ Mockito .mock(TextureRegistry ::class .java),
30
+ { _: List <Map <String , Any ?>>, _: ByteArray? , _: Int? , _: Int? -> },
31
+ { _: String -> },
32
+ { _: BarcodeScannerOptions ? -> barcodeScannerMock }
33
+ )
34
+
35
+ // Intentional suppression for the mock value in the test,
36
+ // since there is no NaN constant.
37
+ @Suppress(" DIVISION_BY_ZERO" )
38
+ val notANumber = 0.0f / 0.0f
39
+
40
+ val barcodeMock: Barcode = Mockito .mock(Barcode ::class .java)
41
+ val imageMock: ImageProxy = Mockito .mock(ImageProxy ::class .java)
42
+
43
+ // TODO: use corner points instead of bounding box
44
+
45
+ // Bounding box that is 100 pixels offset from the left and top,
46
+ // and is 100 pixels in width and height.
47
+ Mockito .`when `(barcodeMock.boundingBox).thenReturn(
48
+ Rect (100 , 100 , 200 , 300 ))
49
+ Mockito .`when `(imageMock.height).thenReturn(400 )
50
+ Mockito .`when `(imageMock.width).thenReturn(400 )
51
+
52
+ // Use a scan window that has an invalid value, but otherwise uses the entire image.
53
+ val scanWindow: List <Float > = listOf (0f , notANumber, 100f , 100f )
54
+
55
+ expect(false ) {
56
+ mobileScanner.isBarcodeInScanWindow(scanWindow, barcodeMock, imageMock)
57
+ }
58
+ }
59
+ }
0 commit comments