Hi everybody! I'm trying to create UI tests so I c...
# android
m
Hi everybody! I'm trying to create UI tests so I can test our app taking a picture with the camera and uploading it to a server. So far I was able to mock and create UI tests for the app going to the gallery and taking a picture, but with the camera, it's impossible. Perhaps it's the way I'm implementing the camera functionality:
Copy code
@Composable
private fun ShowCamera(onPictureTaken: (File, Long) -> Unit) {
    val context = LocalContext.current
    val imageFile = remember { createImageFile(context) }
    val uri = FileProvider.getUriForFile(context, "${context.packageName}.provider", imageFile)
    val cameraLauncher =
        rememberLauncherForActivityResult(contract = ActivityResultContracts.TakePicture()) {
            if (it) {
                val fileSize = FileUtils.fileSize(context, uri)
                onPictureTaken(file, file.length())
            }
        }
    LaunchedEffect(Unit) {
        cameraLauncher.launch(uri)
    }
}
Now in the UI tests:
Copy code
val actionTakePictureWithCameraIntent = KIntent {
                hasAction(MediaStore.ACTION_IMAGE_CAPTURE)
                withResult {
                    withCode(RESULT_OK)
                    withData(createImageCaptureActivityResultStub(applicationContext))
                }
            }
...
private fun createImageCaptureActivityResultStub(context: Context): Intent {
        val file = savePickedImage("pickedImageFromCamera.jpeg", context)
        val resultData = Intent()
        val uri = Uri.fromFile(file)
        resultData.setType("image/*")
        resultData.data = uri
        return resultData
    }
The thing is that
fileSize
is always 0 and I have client side validation, so the users can't upload 0 bytes files size. In
createImage
I'm doing:
Copy code
@Throws(IOException::class)
private fun createImageFile(context: Context): File {
    val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
    val imageFileName = "JPEG_" + timeStamp + "_"
    val storageDir: File? = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    return File.createTempFile(
        imageFileName,
        ".jpeg",
        storageDir
    )
}
Is there any way I can get the URI I'm injecting in the UI tests?
🧵 2
not kotlin but kotlin colored 2
c
Image from iOS.jpg