Skip to content

Commit

Permalink
Merge pull request #213 from caarmen/set-defalult-theme-auto
Browse files Browse the repository at this point in the history
Theming: Set the default theme to auto, and archive captured screenshots in github actions.
  • Loading branch information
caarmen authored Jan 4, 2025
2 parents b58e175 + ab69ba1 commit cc52c43
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 167 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
recipe_uuid: ${{ vars.GMSAAS_RECIPE_UUID }}
- name: Run tests
run: bash scripts/run_tests.bash
- name: Run screenshot tests
run: |
bash scripts/run_screenshots.sh
zip -r screenshots.zip screenshots
- name: Publish Unit Test Report
uses: mikepenz/action-junit-report@v4
if: always() # always run even if the previous step fails
Expand Down Expand Up @@ -83,3 +87,10 @@ jobs:
path: |
./app/build/outputs/apk/debug/app-debug.apk
./app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk
- name: Archive screenshots
if: always()
uses: actions/upload-artifact@v4
with:
name: screenshots
path: |
./screenshots.zip
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ dependencies {
androidTestImplementation "androidx.test.ext:junit:$androidx_junit_version"
androidTestImplementation "androidx.test.uiautomator:uiautomator:$androidx_test_uiautomator_version"
androidTestImplementation "org.robolectric:annotations:$robolectric_version"
androidTestImplementation "com.google.testparameterinjector:test-parameter-injector:$test_parameter_injector_version"

androidTestUtil "androidx.test:orchestrator:$androidx_test_orchestrator_version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void copyCleanLayoutTest() {
public void themeTest() {
openMenuItem(R.string.action_settings);
clickPreference(R.string.pref_theme_title);
onView(withText(R.string.pref_theme_value_light)).check(matches(isChecked()));
onView(withText(R.string.pref_theme_value_auto)).check(matches(isChecked()));
onView(withText(R.string.pref_theme_value_dark)).perform(click());
pressBack();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void deepLinkAfterThemeChangeTest(String deepLinkUrl) {
Intent intent = new Intent();
mActivityTestRule.launchActivity(intent);
clickPreference(R.string.pref_theme_title);
onView(withText(R.string.pref_theme_value_light)).check(matches(isChecked()));
onView(withText(R.string.pref_theme_value_auto)).check(matches(isChecked()));
onView(withText(R.string.pref_theme_value_dark)).perform(click());

// Open a deep link
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2017 - present Carmen Alvarez
*
* This file is part of Poet Assistant.
*
* Poet Assistant is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Poet Assistant is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.rmen.android.poetassistant.main

import android.app.Application
import android.graphics.Bitmap
import android.os.SystemClock
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.screenshot.ScreenCapture
import androidx.test.runner.screenshot.ScreenCaptureProcessor
import androidx.test.runner.screenshot.Screenshot
import ca.rmen.android.poetassistant.R
import ca.rmen.android.poetassistant.Theme.setThemeFromSettings
import ca.rmen.android.poetassistant.main.rules.PoetAssistantActivityTestRule
import ca.rmen.android.poetassistant.settings.SettingsPrefs
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import org.junit.Before
import org.junit.FixMethodOrder
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters
import java.io.File
import java.io.IOException


@LargeTest
@RunWith(TestParameterInjector::class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class ScreenshotTest {
enum class ThemePreference(val value: String) {
LIGHT(SettingsPrefs.THEME_LIGHT),
DARK(SettingsPrefs.THEME_DARK)
}

@TestParameter
lateinit var themePreference: ThemePreference

private lateinit var deviceScreenshotsFolder: File

@JvmField
@Rule
val activityTestRule: PoetAssistantActivityTestRule<MainActivity> = PoetAssistantActivityTestRule(
MainActivity::class.java, true
)

@Before
fun setup() {
// Set the theme
InstrumentationRegistry.getInstrumentation().runOnMainSync {
val settingsPrefs =
SettingsPrefs(ApplicationProvider.getApplicationContext())
settingsPrefs.theme = themePreference.value
setThemeFromSettings(settingsPrefs)
}

// Create a fresh folder to store the screenshots
deviceScreenshotsFolder = File(
ApplicationProvider.getApplicationContext<Application>().getExternalFilesDir(null),
"screenshots-${themePreference.value}"
)
if (deviceScreenshotsFolder.exists()) {
deviceScreenshotsFolder.deleteRecursively()
}
deviceScreenshotsFolder.mkdirs()
}

@Test
fun testTakeScreenshots() {
starWords(
"acquiesce",
"askance",
"benight",
"deferential",
"fractious",
"implacable",
"obfuscation",
"peon",
"possibleness"
)
TestAppUtils.search("chance")
takeScreenshot("rhymer")
TestUiUtils.swipeViewPagerLeft(1)
takeScreenshot("thesaurus")
TestUiUtils.swipeViewPagerLeft(1)
takeScreenshot("dictionary")
TestUiUtils.swipeViewPagerLeft(1)
TestAppUtils.typePoem("Roses are red.\nViolets are blue.\nIf you are a poet,\nthis app is for you.")
SystemClock.sleep(1000)
takeScreenshot("composer")
TestUiUtils.swipeViewPagerLeft(1)
takeScreenshot("favorites")
TestUiUtils.openMenuItem(R.string.action_settings)
takeScreenshot("settings")
}

private fun starWords(vararg words: String) {
for (word in words) {
TestAppUtils.search(word)
Espresso.onIdle()
TestAppUtils.starQueryWord()
}
}

private inner class Processor : ScreenCaptureProcessor {
override fun process(capture: ScreenCapture): String {
val deviceFile = File(deviceScreenshotsFolder, "${capture.name}.png")
deviceFile.outputStream().use {
capture.bitmap.compress(
Bitmap.CompressFormat.PNG,
0,
it,
)
}
return deviceFile.absolutePath
}
}

private val processor = Processor()

// https://stackoverflow.com/questions/38519568/how-to-take-screenshot-at-the-point-where-test-fail-in-espresso
private fun takeScreenshot(filename: String) {
SystemClock.sleep(500) // :(
Espresso.onIdle()
val capture = Screenshot.capture()
capture.setName(filename)
capture.setFormat(Bitmap.CompressFormat.PNG)

try {
capture.process(setOf(processor))
} catch (e: IOException) {
e.printStackTrace()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class SettingsPrefs(application: Application) {

var theme: String
get() {
return sharedPreferences.getString(PREF_THEME, THEME_LIGHT) ?: THEME_LIGHT
return sharedPreferences.getString(PREF_THEME, THEME_LIGHT) ?: THEME_AUTO
}
set(newValue) {
sharedPreferences.edit().putString(PREF_THEME, newValue).apply()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
android:title="@string/pref_category_ui">

<ListPreference
android:defaultValue="Light"
android:defaultValue="Auto"
android:entries="@array/pref_theme_titles"
android:entryValues="@array/pref_theme_values"
android:icon="@drawable/ic_settings_theme"
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ buildscript {
ext.androidx_test_uiautomator_version = "2.3.0"
ext.androidx_junit_version = "1.2.1"
ext.espresso_version = "3.6.1"
ext.test_parameter_injector_version = "1.18"

repositories {
google()
Expand Down
6 changes: 0 additions & 6 deletions etc/screenshots/rename.sh

This file was deleted.

27 changes: 0 additions & 27 deletions etc/screenshots/run.sh

This file was deleted.

Loading

0 comments on commit cc52c43

Please sign in to comment.