Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for full screen snapshots. #59

Merged
merged 16 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ jobs:
instrumentationTests:
runs-on: macos-latest
needs: [build]

permissions:
contents: write
pull-requests: write

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Gradle Wrapper Validation
uses: gradle/wrapper-validation-action@v1
uses: gradle/wrapper-validation-action@v2

- uses: actions/cache@v3
with:
Expand All @@ -93,7 +98,7 @@ jobs:
${{ runner.os }}-gradle

- name: Install JDK 11
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
Expand Down Expand Up @@ -121,7 +126,7 @@ jobs:
script: echo "Generated AVD snapshot."

- name: Run instrumentation tests
id: instrumentation-tests
id: screenshotsverify
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 31
Expand All @@ -134,6 +139,27 @@ jobs:
# Workaround for https://github.com/ReactiveCircus/android-emulator-runner/issues/319
script: adb uninstall com.dropbox.dropshots.test; ./gradlew connectedCheck --stacktrace

- name: Prevent pushing new screenshots if this is a fork
id: checkfork_screenshots
continue-on-error: false
if: steps.screenshotsverify.outcome == 'failure' && github.event.pull_request.head.repo.full_name != github.repository
run: |
echo "::error::Screenshot tests failed, please create a PR in your fork first." && exit 1

- name: Record screenshot tests
id: screenshotsrecord
if: steps.screenshotsverify.outcome == 'failure' && github.event_name == 'pull_request'
run: |
cp dropshots/build/reports/androidTests/dropshots/*.png dropshots/src/androidTest/assets/

- name: Push new screenshots if available
uses: stefanzweifel/git-auto-commit-action@v5
if: steps.screenshotsrecord.outcome == 'success'
with:
file_pattern: '*/*.png'
disable_globbing: true
commit_message: "🤖 Updates screenshots"

- name: (Fail-only) Bundle test reports
if: failure()
run: find . -type d '(' -name 'reports' -o -name 'androidTest-results' ')' | zip -@ -r instrumentation-test-build-reports.zip
Expand All @@ -148,7 +174,7 @@ jobs:
publish:
runs-on: ubuntu-latest
if: github.repository == 'dropbox/dropshots' && github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
needs: [unitTests]
needs: [unitTests, instrumentationTests]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class DropshotsTest {
}
}

@Test
fun testMatchesFullScreenshot() {
activityScenarioRule.scenario.onActivity {
dropshots.assertSnapshot("MatchesFullScreenshot")
}
}

@Test
fun testMatchesActivityScreenshot() {
activityScenarioRule.scenario.onActivity {
Expand Down
17 changes: 15 additions & 2 deletions dropshots/src/main/java/com/dropbox/dropshots/Dropshots.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Dropshots(
}

/**
* Compares a screenshot of the view to a references screenshot from the test application's assets.
* Compares a screenshot of the view to a reference screenshot from the test application's assets.
*
* If `BuildConfig.IS_RECORD_SCREENSHOTS` is set to `true`, then the screenshot will simply be written
* to disk to be pulled to the host machine to update the reference images.
Expand All @@ -80,7 +80,7 @@ public class Dropshots(
) = assertSnapshot(Screenshot.capture(view).bitmap, name, filePath)

/**
* Compares a screenshot of the activity to a references screenshot from the test application's assets.
* Compares a screenshot of the activity to a reference screenshot from the test application's assets.
*
* If `BuildConfig.IS_RECORD_SCREENSHOTS` is set to `true`, then the screenshot will simply be written
* to disk to be pulled to the host machine to update the reference images.
Expand All @@ -93,6 +93,19 @@ public class Dropshots(
filePath: String? = null,
) = assertSnapshot(Screenshot.capture(activity).bitmap, name, filePath)

/**
* Compares a screenshot of the visible screen content to a reference screenshot from the test application's assets.
*
* If `BuildConfig.IS_RECORD_SCREENSHOTS` is set to `true`, then the screenshot will simply be written
* to disk to be pulled to the host machine to update the reference images.
*
* @param filePath where the screenshots should be store in project eg. "views/colors"
*/
public fun assertSnapshot(
name: String = snapshotName,
filePath: String? = null,
) = assertSnapshot(Screenshot.capture().bitmap, name, filePath)

@Suppress("LongMethod")
public fun assertSnapshot(
bitmap: Bitmap,
Expand Down
10 changes: 4 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
kotlin = "1.7.22"
agp = "7.4.0"
androidx-core = "1.7.0"
androidx-test = "1.4.0"
androidx-test-ext = "1.1.3"

[libraries]
android = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand All @@ -13,10 +11,10 @@ androidx-appcompat = { module = "androidx.appcompat:appcompat", version = "1.4.1
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version = "2.1.4" }
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
androidx-fragment = { module = "androidx.fragment:fragment-ktx", version = "1.3.6" }
androidx-test-core = { module = "androidx.test:core-ktx", version.ref = "androidx-test" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit-ktx", version.ref = "androidx-test-ext" }
androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidx-test" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test" }
androidx-test-core = { module = "androidx.test:core-ktx", version = "1.5.0" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit-ktx", version = "1.1.5" }
androidx-test-rules = { module = "androidx.test:rules", version = "1.5.0" }
androidx-test-runner = { module = "androidx.test:runner", version = "1.5.2" }
differ = "com.dropbox.differ:differ:0.0.1-alpha1"
junit = "junit:junit:4.12"
truth = "com.google.truth:truth:1.1.3"
Expand Down