This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59d2a99
commit 04b84f5
Showing
6 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ry/src/androidTest/java/io/element/android/wysiwyg/test/utils/ScreenshotFailureHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.element.android.wysiwyg.test.utils | ||
|
||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.os.Environment | ||
import android.provider.MediaStore | ||
import android.view.View | ||
import androidx.test.espresso.FailureHandler | ||
import androidx.test.espresso.base.DefaultFailureHandler | ||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation | ||
import org.hamcrest.Matcher | ||
import java.io.IOException | ||
import java.text.SimpleDateFormat | ||
import java.util.Calendar | ||
|
||
class ScreenshotFailureHandler(appContext: Context) : FailureHandler { | ||
private val defaultHandler: FailureHandler = DefaultFailureHandler(appContext) | ||
|
||
override fun handle(error: Throwable, viewMatcher: Matcher<View>) { | ||
getInstrumentation() | ||
.uiAutomation | ||
.takeScreenshot() | ||
.save() | ||
|
||
defaultHandler.handle(error, viewMatcher) | ||
} | ||
} | ||
|
||
private fun Bitmap.save() { | ||
val timestamp = timestamp() | ||
val contentResolver = getInstrumentation().targetContext.applicationContext.contentResolver | ||
try { | ||
val contentValues = ContentValues().apply { | ||
put(MediaStore.MediaColumns.DISPLAY_NAME, "$timestamp.jpeg") | ||
put(MediaStore.Images.Media.RELATIVE_PATH, "${Environment.DIRECTORY_PICTURES}/UiTest") | ||
} | ||
|
||
val uri = contentResolver | ||
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) | ||
?: return | ||
|
||
contentResolver.openOutputStream(uri)?.use { outputStream -> | ||
compress(Bitmap.CompressFormat.PNG, 20, outputStream) | ||
} | ||
|
||
contentResolver.update(uri, contentValues, null, null) | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
private fun timestamp(): String = | ||
SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
|
||
./gradlew unitTestsWithCoverage $CI_GRADLE_ARG_PROPERTIES | ||
./gradlew generateUnitTestCoverageReport $CI_GRADLE_ARG_PROPERTIES | ||
|
||
# Don't exit immediately from UI test failure to collect screenshots | ||
set +e | ||
|
||
./gradlew instrumentationTestsWithCoverage $CI_GRADLE_ARG_PROPERTIES | ||
|
||
UI_TEST_EXIT_CODE=$? | ||
if [ $UI_TEST_EXIT_CODE -ne 0 ]; then | ||
echo "UI tests failed." | ||
echo "Pulling screenshots from device..." | ||
adb shell ls /sdcard/Pictures/UiTest/ | ||
mkdir build/reports/screenshots | ||
adb pull /sdcard/Pictures/UiTest/ build/reports/screenshots/ | ||
exit $UI_TEST_EXIT_CODE | ||
fi | ||
set -e | ||
|
||
./gradlew generateInstrumentationTestCoverageReport $CI_GRADLE_ARG_PROPERTIES | ||
|