Skip to content
Draft
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
41 changes: 41 additions & 0 deletions _ci/adb_demo_mode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

origin=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) || exit

# shellcheck disable=SC1091
. "${origin}/utils.sh"

if [ $# -lt 1 ]; then
echo "Usage: $0 [on|off] [hhmm]" >&2
exit
fi

cmd=${1}

hhmm=${2:-"1200"}

# see available commands https://android.googlesource.com/platform/frameworks/base/+/master/packages/SystemUI/docs/demo_mode.md
case "${cmd,,}" in
on)
warn "Don't forget to switch phone in English language"
info "Enabling demo mode"
adb shell settings put global sysui_demo_allowed 1
adb shell am broadcast -a com.android.systemui.demo -e command enter || exit
adb shell am broadcast -a com.android.systemui.demo -e command clock -e hhmm "${hhmm}"
adb shell am broadcast -a com.android.systemui.demo -e command battery -e plugged false -e level 100 -e powersave false
adb shell am broadcast -a com.android.systemui.demo -e command network -e wifi show -e level 4
adb shell am broadcast -a com.android.systemui.demo -e command network -e mobile show -e datatype none -e level 4 -e fully true
adb shell am broadcast -a com.android.systemui.demo -e command notifications -e visible false
adb shell cmd overlay enable com.android.internal.systemui.navbar.gestural
;;
off)
info "Disabling demo mode"
adb shell am broadcast -a com.android.systemui.demo -e command exit
adb shell settings put global sysui_demo_allowed 0
adb shell cmd overlay enable com.android.internal.systemui.navbar.threebutton
;;
*)
step_error "Invalid command '${cmd}'"
esac

step_done
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ androidx-test-core = { module = "androidx.test:core", version = "1.6.1" }
androidx-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose" }
androidx-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test-runner" }
androidx-uiautomator = { module = "androidx.test.uiautomator:uiautomator", version = "2.3.0" }
Copy link
Owner Author

@opatry opatry May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • check latest version before merging


mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" }

Expand Down
10 changes: 10 additions & 0 deletions tasks-app-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ android {
manifestPlaceholders["crashlyticsEnabled"] = false
}

create("demo") {
initWith(getByName("dev"))
applicationIdSuffix = ".demo"
}

create("store") {
dimension = "target"
}
Expand Down Expand Up @@ -165,10 +170,15 @@ dependencies {
implementation(projects.google.tasks)
implementation(projects.tasksAppShared)

"demoImplementation"(projects.tasksCore) {
because("needed for prefilled content for screenshot generation")
}
Comment on lines +173 to +175

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider moving this dependency to androidTestImplementation if it's only needed for tests. This avoids including it in the main application code, reducing the app size. If tasksCore is truly needed for prefilled content in the demo flavor's main code (not just the tests), then this placement is correct.


debugImplementation(libs.androidx.ui.test.manifest)

androidTestImplementation(libs.androidx.ui.test.junit4)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.androidx.uiautomator)
}

aboutLibraries {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2025 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.tasks.app.test

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import java.io.File

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
annotation class NoScreenshot

private fun defaultScreenshotDir() = File(InstrumentationRegistry.getInstrumentation().targetContext.cacheDir, "test_failed_screenshots")

class ScreenshotOnFailureRule(private val screenshotsDir: File = defaultScreenshotDir()) : TestWatcher() {
private val Description.allowScreenshot: Boolean
get() = getAnnotation(NoScreenshot::class.java) == null

override fun failed(e: Throwable?, description: Description?) {
description?.let { testDescription ->
if (testDescription.allowScreenshot) {
try {
takeScreenshot(testDescription)
} catch (_: Exception) {
// ignore screenshot processing errors
}
}
}
super.failed(e, description)
}

private fun takeScreenshot(testDescription: Description) {
val fileName = testDescription.displayName.take(150)
val outputFile = File(screenshotsDir, "$fileName.png").also {
it.parentFile?.mkdirs()
}
if (outputFile.exists()) {
outputFile.delete()
}
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
.takeScreenshot(outputFile)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright (c) 2025 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.tasks.app.test.screenshot

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.ui.test.AndroidComposeUiTest
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.test.runAndroidComposeUiTest
import androidx.compose.ui.test.waitUntilAtLeastOneExists
import androidx.compose.ui.test.waitUntilExactlyOneExists
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import net.opatry.tasks.app.MainActivity
import net.opatry.tasks.app.R
import net.opatry.tasks.app.test.ScreenshotOnFailureRule
import net.opatry.tasks.app.ui.component.TaskEditorBottomSheetTestTag.NOTES_FIELD
import net.opatry.tasks.app.ui.component.TaskEditorBottomSheetTestTag.TITLE_FIELD
import net.opatry.tasks.app.ui.component.TaskListScaffoldTestTag.ADD_TASK_FAB
import net.opatry.tasks.app.ui.component.TasksColumnTestTag.COMPLETED_TASKS_TOGGLE
import org.junit.Rule
import org.junit.Test
import java.io.File


@OptIn(ExperimentalTestApi::class)
class StoreScreenshotTest {

@get:Rule
val screenshotOnFailureRule = ScreenshotOnFailureRule()

private val targetContext: Context
get() = InstrumentationRegistry.getInstrumentation().targetContext

private fun takeScreenshot(name: String) {
val instrumentation = InstrumentationRegistry.getInstrumentation()
val outputDir = File(instrumentation.targetContext.cacheDir, "store_screenshots").also(File::mkdirs)
val outputFile = File(outputDir, "$name.png")
if (outputFile.exists()) {
outputFile.delete()
}
UiDevice.getInstance(instrumentation)
.takeScreenshot(outputFile)
}

private fun AndroidComposeUiTest<*>.pressBack() {
// FIXME how to "press back" with `runAndroidComposeUiTest` (without Espresso)
// `UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressBack()`
// UI Automator doesn't work for navigation (but does for IME dismiss)
activity?.onBackPressed()
}

private fun dismissKeyboard() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressBack()
}

private fun <A : AppCompatActivity> AndroidComposeUiTest<A>.switchToNightMode(nightMode: Int) {
activity?.runOnUiThread {
activity?.delegate?.localNightMode = nightMode
activity?.recreate()
}
waitForIdle()
}

/**
* This test should be executed with the `demo` flavor which stub content for store screenshots.
*/
@Test
fun storeScreenshotSequence() = runAndroidComposeUiTest<MainActivity> {
val initialNightMode = activity?.delegate?.localNightMode ?: AppCompatDelegate.MODE_NIGHT_UNSPECIFIED

waitForIdle()
takeScreenshot("initial_screen")

switchToNightMode(AppCompatDelegate.MODE_NIGHT_NO)

val defaultTaskTitle = targetContext.getString(R.string.demo_task_list_default)
waitUntilAtLeastOneExists(hasText(defaultTaskTitle))
onNodeWithText(defaultTaskTitle)
.assertIsDisplayed()

val homeTaskTitle = targetContext.getString(R.string.demo_task_list_home)
onNodeWithText(homeTaskTitle)
.assertIsDisplayed()

val groceriesTaskTitle = targetContext.getString(R.string.demo_task_list_groceries)
onNodeWithText(groceriesTaskTitle)
.assertIsDisplayed()

val workTaskTitle = targetContext.getString(R.string.demo_task_list_work)
onNodeWithText(workTaskTitle)
.assertIsDisplayed()

takeScreenshot("task_lists_light")

onNodeWithText(defaultTaskTitle)
.assertIsDisplayed()
.performClick()
val defaultTask1Title = targetContext.getString(R.string.demo_task_list_default_task1)
waitUntilAtLeastOneExists(hasText(defaultTask1Title))
// FIXME unreliable, need to wait for something else?
takeScreenshot("my_tasks_light")

waitUntilExactlyOneExists(hasTestTag(ADD_TASK_FAB))
onNodeWithTag(ADD_TASK_FAB)
.assertIsDisplayed()
.performClick()
waitUntilExactlyOneExists(isDialog())

waitUntilExactlyOneExists(hasTestTag(TITLE_FIELD))
onNodeWithTag(TITLE_FIELD)
.performTextInput("Wash the car 🧽")
waitForIdle()
dismissKeyboard()

waitUntilExactlyOneExists(hasTestTag(NOTES_FIELD))
onNodeWithTag(NOTES_FIELD)
.performTextInput("Keys are in the drawer")

dismissKeyboard()

waitForIdle()
takeScreenshot("add_task_light")

// FIXME how to dismiss bottom sheet without clicking on the button? (press back somehow? tap outside?)
// FIXME how to use Res strings from :tasks-app-shared?
onNodeWithText("Cancel")
.assertIsDisplayed()
.performClick()
// go back
pressBack()
waitUntilAtLeastOneExists(hasText(groceriesTaskTitle))

onNodeWithText(groceriesTaskTitle)
.assertIsDisplayed()
.performClick()
val groceriesTask1Title = targetContext.getString(R.string.demo_task_list_groceries_task1)
waitUntilAtLeastOneExists(hasText(groceriesTask1Title))
onNodeWithTag(COMPLETED_TASKS_TOGGLE)
.assertIsDisplayed()
.performClick()
val groceriesTask3Title = targetContext.getString(R.string.demo_task_list_groceries_task3)
waitUntilAtLeastOneExists(hasText(groceriesTask3Title))
takeScreenshot("groceries_light")

pressBack()
waitUntilAtLeastOneExists(hasText(workTaskTitle))

onNodeWithText(workTaskTitle)
.assertIsDisplayed()
.performClick()
val workTask1Title = targetContext.getString(R.string.demo_task_list_work_task1)
waitUntilAtLeastOneExists(hasText(workTask1Title))
takeScreenshot("work_light")

pressBack()
waitUntilAtLeastOneExists(hasText(homeTaskTitle))

onNodeWithText(homeTaskTitle)
.assertIsDisplayed()
.performClick()
val homeTask1Title = targetContext.getString(R.string.demo_task_list_home_task1)
waitUntilAtLeastOneExists(hasText(homeTask1Title))
takeScreenshot("home_light")

switchToNightMode(AppCompatDelegate.MODE_NIGHT_YES)
waitUntilAtLeastOneExists(hasText(homeTask1Title))
takeScreenshot("home_dark")
switchToNightMode(initialNightMode)
}
}
Binary file added tasks-app-android/src/demo/assets/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tasks-app-android/src/demo/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"project_info": {
"project_number": "44853535682",
"project_id": "tasks-app-c632e",
"storage_bucket": "tasks-app-c632e.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:44853535682:android:912604c7085bbeb11e2c5b",
"android_client_info": {
"package_name": "net.opatry.tasks.app.demo"
}
},
"api_key": [
{
"current_key": ""
}
]
}
],
"configuration_version": "1"
}
Loading