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

[0.5.x] Backport device screenshot testing #2141

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
**/docs/**/*.png filter=lfs diff=lfs merge=lfs -text
*.mp4 filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
**/test/screenshots/**.png filter=lfs diff=lfs merge=lfs -text
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ plugins {
alias(libs.plugins.protobuf) apply false
alias(libs.plugins.gradleMavenPublishPlugin)
alias(libs.plugins.dependencyAnalysis)
alias(libs.plugins.roborazzi) apply false
}

apply(plugin = "org.jetbrains.dokka")
Expand Down
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ okio = "3.8.0"
org-robolectric = "4.11.1"
playServicesAuth = "21.0.0"
protobuf = "3.25.2"
roborazzi = "1.11.0"
room = "2.6.1"
runtimeTracing = "1.0.0-beta01"
snapshot-android = "1.0.4"
Expand Down Expand Up @@ -199,6 +200,9 @@ retrofit2-convertermoshi = { module = "com.squareup.retrofit2:converter-moshi",
retrofit2-retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "com-squareup-retrofit2" }
robolectric = { module = "org.robolectric:robolectric", version.ref = "org-robolectric" }
robolectric-shadows = { module = "org.robolectric:shadows-framework", version.ref = "org-robolectric" }
roborazzi = { group = "io.github.takahirom.roborazzi", name = "roborazzi", version.ref = "roborazzi" }
roborazzi-compose = { group = "io.github.takahirom.roborazzi", name = "roborazzi-compose", version.ref = "roborazzi" }
roborazzi-rule = { group = "io.github.takahirom.roborazzi", name = "roborazzi-junit-rule", version.ref = "roborazzi" }
room-common = { module = "androidx.room:room-common", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
Expand All @@ -219,3 +223,4 @@ kotlinGradle = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
protobuf = "com.google.protobuf:0.9.4"
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
roborazzi = { id = "io.github.takahirom.roborazzi", version.ref = "roborazzi" }
4 changes: 4 additions & 0 deletions roboscreenshots/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ dependencies {
implementation(libs.wearcompose.material)
implementation(libs.wearcompose.foundation)

implementation(libs.roborazzi)
implementation(libs.roborazzi.compose)
implementation(libs.roborazzi.rule)

testImplementation(libs.robolectric)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.screenshots.rng

import kotlin.math.roundToInt

public enum class WearDevice(
public val id: String,
public val modelName: String,
public val screenSizePx: Int,
public val density: Float,
public val fontScale: Float = 1f,
) {
MobvoiTicWatchPro5(
id = "ticwatch_pro_5",
modelName = "Mobvoi TicWatch Pro 5",
screenSizePx = 466,
density = 2.0f,
),
SamsungGalaxyWatch5(
id = "galaxy_watch_5",
modelName = "Samsung Galaxy Watch 5",
screenSizePx = 396,
density = 2.0f,
),
SamsungGalaxyWatch6(
id = "galaxy_watch_6",
modelName = "Samsung Galaxy Watch 6 Large",
screenSizePx = 480,
density = 2.125f,
),
GooglePixelWatch(
id = "pixel_watch",
modelName = "Google Pixel Watch",
screenSizePx = 384,
density = 2.0f,
),
GenericSmallRound(
id = "small_round",
modelName = "Generic Small Round",
screenSizePx = 384,
density = 2.0f,
),
GenericLargeRound(
id = "large_round",
modelName = "Generic Large Round",
screenSizePx = 454,
density = 2.0f,
),
SamsungGalaxyWatch6SmallFont(
id = "galaxy_watch_6_small_font",
modelName = "Samsung Galaxy Watch 6 Large",
screenSizePx = 480,
density = 2.125f,
fontScale = 0.94f,
),
GooglePixelWatchLargeFont(
id = "pixel_watch_large_font",
modelName = "Google Pixel Watch",
screenSizePx = 384,
density = 2.0f,
fontScale = 1.24f,
),
;

public val dp: Int = (screenSizePx / density).roundToInt()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.screenshots.rng

import androidx.compose.runtime.Composable
import org.junit.runner.RunWith
import org.robolectric.ParameterizedRobolectricTestRunner

@RunWith(ParameterizedRobolectricTestRunner::class)
public abstract class WearDeviceScreenshotTest(override val device: WearDevice) : WearScreenshotTest() {
public override val tolerance: Float = 0.02f

public fun runTest(content: @Composable () -> Unit) {
runTest(suffix = null, content)
}

public companion object {
@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters
public fun devices(): List<WearDevice> = WearDevice.entries
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:OptIn(ExperimentalRoborazziApi::class)

package com.google.android.horologist.screenshots.rng

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.wear.compose.material.MaterialTheme
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
import com.github.takahirom.roborazzi.RoborazziOptions
import com.github.takahirom.roborazzi.ThresholdValidator
import com.github.takahirom.roborazzi.captureRoboImage
import org.junit.Rule
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode

@Config(
sdk = [33],
qualifiers = "w227dp-h227dp-small-notlong-round-watch-xhdpi-keyshidden-nonav",
)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
public abstract class WearScreenshotTest {
@get:Rule
public val composeRule: ComposeContentTestRule = createComposeRule()

public abstract val device: WearDevice

// Allow for individual tolerances to be set on each test, should be between 0.0 and 1.0
public open val tolerance: Float = 0.0f

public fun runTest(suffix: String? = null, content: @Composable () -> Unit) {
RuntimeEnvironment.setQualifiers("+w${device.dp}dp-h${device.dp}dp")
RuntimeEnvironment.setFontScale(device.fontScale)

composeRule.setContent {
Box(modifier = Modifier.fillMaxSize().background(MaterialTheme.colors.background)) {
content()
}
}

captureScreenshot(suffix.orEmpty())
}

public fun captureScreenshot(suffix: String) {
composeRule.onRoot().captureRoboImage(
filePath = "src/test/screenshots/${this.javaClass.simpleName}_${device.id}$suffix.png",
roborazziOptions = RoborazziOptions(
recordOptions = RoborazziOptions.RecordOptions(
applyDeviceCrop = true,
),
compareOptions = RoborazziOptions.CompareOptions(
resultValidator = ThresholdValidator(tolerance),
),
),
)
}
}
1 change: 1 addition & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
plugins {
id("com.android.application")
kotlin("android")
alias(libs.plugins.roborazzi)
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.screensizes

import com.google.android.horologist.auth.composables.screens.CheckYourPhoneScreen
import com.google.android.horologist.screenshots.rng.WearDevice
import com.google.android.horologist.screenshots.rng.WearDeviceScreenshotTest
import org.junit.Test

class CheckYourPhoneTest(device: WearDevice) : WearDeviceScreenshotTest(device = device) {
public override val tolerance: Float = 0.01f

@Test
fun initial() = runTest {
CheckYourPhoneScreen()
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading