|
| 1 | +package com.sysadmindoc.callshield.service |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.res.Configuration |
| 5 | +import android.graphics.Typeface |
| 6 | +import android.text.StaticLayout |
| 7 | +import android.text.TextPaint |
| 8 | +import android.util.TypedValue |
| 9 | +import androidx.annotation.StringRes |
| 10 | +import androidx.test.core.app.ApplicationProvider |
| 11 | +import com.sysadmindoc.callshield.R |
| 12 | +import org.junit.Assert.assertTrue |
| 13 | +import org.junit.Test |
| 14 | +import org.junit.runner.RunWith |
| 15 | +import org.robolectric.RobolectricTestRunner |
| 16 | +import org.robolectric.annotation.Config |
| 17 | +import org.robolectric.annotation.GraphicsMode |
| 18 | +import java.util.Locale |
| 19 | + |
| 20 | +/** |
| 21 | + * Android 12 and later draw a toast in SystemUI's text_toast layout, which |
| 22 | + * shows two lines of 14sp text and cuts the rest. Measured with native text |
| 23 | + * layout at Android 14's largest font size, in both shipped locales. |
| 24 | + */ |
| 25 | +@RunWith(RobolectricTestRunner::class) |
| 26 | +@Config(sdk = [34], qualifiers = "w360dp-h640dp") |
| 27 | +@GraphicsMode(GraphicsMode.Mode.NATIVE) |
| 28 | +class ToastTextFitTest { |
| 29 | + private val base: Context = ApplicationProvider.getApplicationContext() |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `the Not spam and crash log toasts fit two lines at the largest font`() { |
| 33 | + for (locale in LOCALES) { |
| 34 | + val context = largestFont(locale) |
| 35 | + for (message in listOf(R.string.notif_not_spam_allowed, R.string.notif_not_spam_refused, R.string.more_no_crash_logs)) { |
| 36 | + val text = context.getString(message) |
| 37 | + assertTrue("$locale: $text", toastLines(context, message) <= 2) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** The reason moved into a notification because it can't fit, which also shows the measure rejects long text. */ |
| 43 | + @Test |
| 44 | + fun `the refusal's full reason would be cut off in a toast`() { |
| 45 | + for (locale in LOCALES) { |
| 46 | + assertTrue(locale.toString(), toastLines(largestFont(locale), R.string.notif_not_spam_refused_text) > 2) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private fun largestFont(locale: Locale): Context = |
| 51 | + base.createConfigurationContext( |
| 52 | + Configuration(base.resources.configuration).apply { |
| 53 | + fontScale = LARGEST_FONT_SCALE |
| 54 | + setLocale(locale) |
| 55 | + }, |
| 56 | + ) |
| 57 | + |
| 58 | + private fun toastLines( |
| 59 | + context: Context, |
| 60 | + @StringRes message: Int, |
| 61 | + ): Int { |
| 62 | + val text = context.getString(message) |
| 63 | + val metrics = context.resources.displayMetrics |
| 64 | + val paint = |
| 65 | + TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { |
| 66 | + textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, TOAST_TEXT_SP, metrics) |
| 67 | + typeface = Typeface.create("sans-serif", Typeface.NORMAL) |
| 68 | + } |
| 69 | + val width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, TOAST_TEXT_WIDTH_DP, metrics).toInt() |
| 70 | + return StaticLayout.Builder |
| 71 | + .obtain(text, 0, text.length, paint, width) |
| 72 | + .build() |
| 73 | + .lineCount |
| 74 | + } |
| 75 | + |
| 76 | + private companion object { |
| 77 | + val LOCALES = listOf(Locale.US, Locale.SIMPLIFIED_CHINESE) |
| 78 | + |
| 79 | + // Android 14's top Font size step; earlier versions stop at 1.3. |
| 80 | + const val LARGEST_FONT_SCALE = 2.0f |
| 81 | + const val TOAST_TEXT_SP = 14f |
| 82 | + |
| 83 | + // The toast's 300dp maxWidth less 16dp padding on each side and the |
| 84 | + // 24dp app icon with its 10dp gap. A 360dp screen leaves 262dp |
| 85 | + // without the max, so this is the narrower of the two. |
| 86 | + const val TOAST_TEXT_WIDTH_DP = 234f |
| 87 | + } |
| 88 | +} |
0 commit comments