-
Notifications
You must be signed in to change notification settings - Fork 386
feat: add screenshot testing to compose snippets #958
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
base: adaptive-layouts
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2026 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.example.compose.snippets.adaptivelayouts | ||
|
|
||
| import androidx.compose.ui.test.assertIsDisplayed | ||
| import androidx.compose.ui.test.junit4.v2.createComposeRule | ||
| import androidx.compose.ui.test.onNodeWithText | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
|
|
||
| class MyFeedTest { | ||
|
|
||
| @get:Rule | ||
| val composeTestRule = createComposeRule() | ||
|
|
||
| @Test | ||
|
kkuan2011 marked this conversation as resolved.
|
||
| fun myFeed_displaysItems() { | ||
| val names = listOf("Alice", "Bob", "Charlie") | ||
|
|
||
| composeTestRule.setContent { | ||
| MyFeed(names = names) | ||
| } | ||
|
|
||
| // Verify that all names in the feed are displayed | ||
| for (name in names) { | ||
| composeTestRule.onNodeWithText(name).assertIsDisplayed() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2026 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.example.compose.snippets.adaptivelayouts | ||
|
|
||
| import android.content.res.Configuration | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.tooling.preview.PreviewParameter | ||
| import androidx.compose.ui.tooling.preview.PreviewParameterProvider | ||
| import androidx.compose.ui.tooling.preview.Wallpapers | ||
| import com.android.tools.screenshot.PreviewTest | ||
| import com.example.compose.snippets.adaptivelayouts.MyFeed | ||
|
|
||
| class SampleNamesProvider : PreviewParameterProvider<List<String>> { | ||
| override val values = sequenceOf( | ||
| listOf("User 1", "User 2", "User 3", "User 4", "User 5") | ||
| ) | ||
| } | ||
|
|
||
| @PreviewTest | ||
| @Preview | ||
| @Composable | ||
| fun MyFeedPreview( | ||
| @PreviewParameter(SampleNamesProvider::class) names: List<String>, | ||
| ) { | ||
| MyFeed(names) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2026 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.example.compose.snippets.layouts.grid | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import com.android.tools.screenshot.PreviewTest | ||
|
|
||
|
|
||
| @Preview | ||
| @PreviewTest | ||
| @Composable | ||
| fun GridExamplePreviewTest() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to provide any input on what type of device to take the screenshots on?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does make sense. Ideally we would take screenshots on:
|
||
| GridExample() | ||
| } | ||
|
|
||
| @Preview | ||
| @PreviewTest | ||
| @Composable | ||
| fun SixCardsPreviewTest() { | ||
| SixCards() | ||
| } | ||
|
|
||
| @Preview | ||
| @PreviewTest | ||
| @Composable | ||
| fun DefineGridPreviewTest() { | ||
| DefineGrid() | ||
| } | ||
|
|
||
| @Preview | ||
| @PreviewTest | ||
| @Composable | ||
| fun DefineGridAndPlaceItemsPreviewTest() { | ||
| DefineGridAndPlaceItems() | ||
| } | ||
|
|
||
| @Preview | ||
| @PreviewTest | ||
| @Composable | ||
| fun DefineExplicitAndImplicitGridTracksPreviewTest() { | ||
| DefineExplicitAndImplicitGridTracks() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,6 +123,7 @@ compose-remote = "1.0.0-alpha11" | |
| glance-wear = "1.0.0-alpha10" | ||
| remote-material3 = "1.0.0-alpha04" | ||
| screenshotValidationApi = "0.0.1-alpha14" | ||
| screenshot = "0.0.1-alpha15" | ||
|
|
||
| [libraries] | ||
| accompanist-adaptive = "com.google.accompanist:accompanist-adaptive:0.37.3" | ||
|
|
@@ -309,6 +310,7 @@ androidx-compose-remote-tooling-preview = { module = "androidx.compose.remote:re | |
| glance-preview = { group = "androidx.glance", name = "glance-preview", version.ref = "androidx-glance-appwidget" } | ||
| glance-appwidget-preview = { group = "androidx.glance", name = "glance-appwidget-preview", version.ref = "androidx-glance-appwidget" } | ||
| screenshot-validation-api = { group = "com.android.tools.screenshot", name = "screenshot-validation-api", version.ref = "screenshotValidationApi" } | ||
| androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling"} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can you put in alphabetical order? |
||
|
|
||
| [plugins] | ||
| android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } | ||
|
|
@@ -324,5 +326,6 @@ kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = | |
| kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } | ||
| ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } | ||
| roborazzi = { id = "io.github.takahirom.roborazzi", version.ref = "roborazzi" } | ||
| screenshot = { id = "com.android.compose.screenshot", version.ref = "screenshot"} | ||
| spotless = { id = "com.diffplug.spotless", version.ref = "spotless" } | ||
| version-catalog-update = { id = "nl.littlerobots.version-catalog-update", version.ref = "version-catalog-update" } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to change the github action so that the tests are run for every PR? otherwise people may not realize that a code change breaks the test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me. @kkuan2011 should we add a workflow file to this PR? That would allow us to check the workflow along with this test.