|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 New Vector Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package im.vector.app.espresso.tools |
| 18 | + |
| 19 | +import android.content.ContentResolver |
| 20 | +import android.content.ContentValues |
| 21 | +import android.graphics.Bitmap |
| 22 | +import android.net.Uri |
| 23 | +import android.os.Environment |
| 24 | +import android.provider.MediaStore |
| 25 | +import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation |
| 26 | +import org.junit.rules.TestWatcher |
| 27 | +import org.junit.runner.Description |
| 28 | +import timber.log.Timber |
| 29 | +import java.io.File |
| 30 | +import java.io.FileOutputStream |
| 31 | +import java.io.IOException |
| 32 | +import java.io.OutputStream |
| 33 | +import java.text.SimpleDateFormat |
| 34 | +import java.util.Date |
| 35 | +import java.util.Locale |
| 36 | + |
| 37 | +private val SCREENSHOT_FOLDER_LOCATION = "${Environment.DIRECTORY_PICTURES}/failure_screenshots" |
| 38 | +private val deviceLanguage = Locale.getDefault().language |
| 39 | + |
| 40 | +class ScreenshotFailureRule : TestWatcher() { |
| 41 | + override fun failed(e: Throwable?, description: Description) { |
| 42 | + val screenShotName = "$deviceLanguage-${description.methodName}-${SimpleDateFormat("EEE-MMMM-dd-HH:mm:ss").format(Date())}" |
| 43 | + val bitmap = getInstrumentation().uiAutomation.takeScreenshot() |
| 44 | + storeFailureScreenshot(bitmap, screenShotName) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Stores screenshots in sdcard/Pictures/failure_screenshots |
| 50 | + */ |
| 51 | +private fun storeFailureScreenshot(bitmap: Bitmap, screenshotName: String) { |
| 52 | + val contentResolver = getInstrumentation().targetContext.applicationContext.contentResolver |
| 53 | + |
| 54 | + val contentValues = ContentValues().apply { |
| 55 | + put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg") |
| 56 | + put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()) |
| 57 | + } |
| 58 | + if (android.os.Build.VERSION.SDK_INT >= 29) { |
| 59 | + useMediaStoreScreenshotStorage( |
| 60 | + contentValues, |
| 61 | + contentResolver, |
| 62 | + screenshotName, |
| 63 | + SCREENSHOT_FOLDER_LOCATION, |
| 64 | + bitmap |
| 65 | + ) |
| 66 | + } else { |
| 67 | + usePublicExternalScreenshotStorage( |
| 68 | + contentValues, |
| 69 | + contentResolver, |
| 70 | + screenshotName, |
| 71 | + SCREENSHOT_FOLDER_LOCATION, |
| 72 | + bitmap |
| 73 | + ) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +private fun useMediaStoreScreenshotStorage( |
| 78 | + contentValues: ContentValues, |
| 79 | + contentResolver: ContentResolver, |
| 80 | + screenshotName: String, |
| 81 | + screenshotLocation: String, |
| 82 | + bitmap: Bitmap |
| 83 | +) { |
| 84 | + contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "$screenshotName.jpeg") |
| 85 | + contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, screenshotLocation) |
| 86 | + val uri: Uri? = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) |
| 87 | + if (uri != null) { |
| 88 | + contentResolver.openOutputStream(uri)?.let { saveScreenshotToStream(bitmap, it) } |
| 89 | + contentResolver.update(uri, contentValues, null, null) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +private fun usePublicExternalScreenshotStorage( |
| 94 | + contentValues: ContentValues, |
| 95 | + contentResolver: ContentResolver, |
| 96 | + screenshotName: String, |
| 97 | + screenshotLocation: String, |
| 98 | + bitmap: Bitmap |
| 99 | +) { |
| 100 | + val directory = File(Environment.getExternalStoragePublicDirectory(screenshotLocation).toString()) |
| 101 | + if (!directory.exists()) { |
| 102 | + directory.mkdirs() |
| 103 | + } |
| 104 | + val file = File(directory, "$screenshotName.jpeg") |
| 105 | + saveScreenshotToStream(bitmap, FileOutputStream(file)) |
| 106 | + contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) |
| 107 | +} |
| 108 | + |
| 109 | +private fun saveScreenshotToStream(bitmap: Bitmap, outputStream: OutputStream) { |
| 110 | + outputStream.use { |
| 111 | + try { |
| 112 | + bitmap.compress(Bitmap.CompressFormat.JPEG, 50, it) |
| 113 | + } catch (e: IOException) { |
| 114 | + Timber.e("Screenshot was not stored at this time") |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments