|
| 1 | +/******************************************************************************* |
| 2 | + * Created by Patricio Jofré |
| 3 | + * Copyright 2026 Prey Inc. All rights reserved. |
| 4 | + * License: GPLv3 |
| 5 | + * Full license at "/LICENSE" |
| 6 | + ******************************************************************************/ |
| 7 | +package com.prey.activities; |
| 8 | + |
| 9 | +import android.app.Activity; |
| 10 | +import android.app.Instrumentation; |
| 11 | +import android.content.Context; |
| 12 | +import android.content.res.Configuration; |
| 13 | +import android.content.res.Resources; |
| 14 | +import android.os.Build; |
| 15 | +import android.view.KeyEvent; |
| 16 | + |
| 17 | +import com.prey.R; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | + |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import androidx.test.core.app.ActivityScenario; |
| 27 | +import androidx.test.core.app.ApplicationProvider; |
| 28 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 29 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 30 | + |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | +import static org.junit.Assert.assertNotNull; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | +import static org.junit.Assert.fail; |
| 35 | +import static org.junit.Assume.assumeTrue; |
| 36 | + |
| 37 | +/** |
| 38 | + * On-device checks for the two behaviours Android 16 (API 36) changed for this app. |
| 39 | + * <p> |
| 40 | + * Run this on a display whose smallest width is at least 600dp — a tablet or foldable |
| 41 | + * AVD — because that is where API 36 stops honouring the portrait lock every activity |
| 42 | + * here requests. On a phone-sized AVD the orientation assertions still pass but prove |
| 43 | + * much less, so {@link #displayIsLargeEnoughToBeMeaningful()} reports the configuration |
| 44 | + * the run actually exercised. |
| 45 | + * <p> |
| 46 | + * These live in androidTest rather than test because they need the real resource |
| 47 | + * resolution and the real back dispatch, and because non-exported activities cannot be |
| 48 | + * started from the adb shell — instrumentation runs as the app's own UID, so it can. |
| 49 | + */ |
| 50 | +@RunWith(AndroidJUnit4.class) |
| 51 | +public class LargeScreenBehaviorInstrumentedTest { |
| 52 | + |
| 53 | + private static final long BACK_NAVIGATION_TIMEOUT_MS = 8000; |
| 54 | + |
| 55 | + // ========================================================================= |
| 56 | + // Configuration the run is actually exercising |
| 57 | + // ========================================================================= |
| 58 | + |
| 59 | + /** |
| 60 | + * Asserts that the portrait lock really is being ignored, which is the premise every |
| 61 | + * other test here rests on. |
| 62 | + * <p> |
| 63 | + * PanelWebActivity declares {@code screenOrientation="portrait"} in the manifest, so |
| 64 | + * on a large screen running API 36 it should come up in landscape anyway. If it comes |
| 65 | + * up portrait, the platform is still honouring the lock and this run is not exercising |
| 66 | + * the change at all. |
| 67 | + * <p> |
| 68 | + * The preconditions are assumptions rather than assertions on purpose: below API 36, on |
| 69 | + * a phone-sized display, or with the device in portrait, this reports as skipped instead |
| 70 | + * of passed, so a green suite cannot be mistaken for coverage it did not provide. The |
| 71 | + * layout sweeps below still run either way — plain landscape is enough to catch the |
| 72 | + * welcomebatch class of bug, on any API level. |
| 73 | + */ |
| 74 | + @Test |
| 75 | + public void portraitLockIsIgnoredOnThisDisplay() { |
| 76 | + Configuration config = resources().getConfiguration(); |
| 77 | + int smallestWidthDp = config.smallestScreenWidthDp; |
| 78 | + |
| 79 | + // Below API 36 the platform still honours the lock, so the assertion below would be |
| 80 | + // wrong rather than merely uninformative. This matters for CI, which runs the suite |
| 81 | + // on an older API level too, to cover BackNavigationCompat's pre-Android 13 path. |
| 82 | + assumeTrue( |
| 83 | + String.format("API %d is below 36, where the portrait lock is still honoured", |
| 84 | + Build.VERSION.SDK_INT), |
| 85 | + Build.VERSION.SDK_INT >= 36); |
| 86 | + assumeTrue( |
| 87 | + String.format("smallestScreenWidthDp=%d is below 600 — run on a tablet AVD to " |
| 88 | + + "exercise the API 36 orientation change", smallestWidthDp), |
| 89 | + smallestWidthDp >= 600); |
| 90 | + assumeTrue( |
| 91 | + "Device is in portrait, so an ignored portrait lock is indistinguishable from " |
| 92 | + + "an honoured one; rotate the AVD to landscape", |
| 93 | + config.orientation == Configuration.ORIENTATION_LANDSCAPE); |
| 94 | + |
| 95 | + try (ActivityScenario<PanelWebActivity> scenario = |
| 96 | + ActivityScenario.launch(PanelWebActivity.class)) { |
| 97 | + scenario.onActivity(activity -> assertEquals( |
| 98 | + String.format("PanelWebActivity asks for portrait, but on a %ddp display " |
| 99 | + + "running API 36 that request must be ignored", |
| 100 | + smallestWidthDp), |
| 101 | + Configuration.ORIENTATION_LANDSCAPE, |
| 102 | + activity.getResources().getConfiguration().orientation)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // ========================================================================= |
| 107 | + // Resource resolution in the configuration the device is actually in |
| 108 | + // ========================================================================= |
| 109 | + |
| 110 | + @Test |
| 111 | + public void welcomeBatchLayoutResolvesInThisConfiguration() { |
| 112 | + assertNotNull( |
| 113 | + "welcomebatch must resolve in the current configuration; it used to exist " |
| 114 | + + "only under -port qualifiers, which threw once API 36 let large " |
| 115 | + + "screens ignore WelcomeBatchActivity's portrait lock", |
| 116 | + resources().getLayout(R.layout.welcomebatch) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Name prefixes of layouts that come from AppCompat, Material and other AndroidX |
| 122 | + * libraries. {@code R.layout} holds the merged resources of every dependency, and some |
| 123 | + * library layouts are scoped to one configuration on purpose — |
| 124 | + * {@code material_clock_period_toggle_land} exists only for landscape, and the library |
| 125 | + * only reaches for it there — so sweeping them reports failures that are not ours. |
| 126 | + * <p> |
| 127 | + * Keep in sync with the copy in {@code LayoutConfigurationCoverageRobolectricTest}, |
| 128 | + * which documents how the list was verified against the dependency set. |
| 129 | + */ |
| 130 | + private static final String[] THIRD_PARTY_LAYOUT_PREFIXES = { |
| 131 | + "abc_", "m3_", "material_", "mtrl_", "design_", "notification_", |
| 132 | + "select_dialog", "support_", "preference", "browser_actions", |
| 133 | + "custom_dialog", "expand_button", "image_frame", "test_", |
| 134 | + "fingerprint_dialog", "ime_", |
| 135 | + }; |
| 136 | + |
| 137 | + @Test |
| 138 | + public void everyLayoutResolvesInThisConfiguration() { |
| 139 | + Resources resources = resources(); |
| 140 | + List<String> missing = new ArrayList<>(); |
| 141 | + int checked = 0; |
| 142 | + int skipped = 0; |
| 143 | + |
| 144 | + for (Field field : R.layout.class.getFields()) { |
| 145 | + int id; |
| 146 | + try { |
| 147 | + id = field.getInt(null); |
| 148 | + } catch (IllegalAccessException e) { |
| 149 | + continue; |
| 150 | + } |
| 151 | + if (isThirdPartyLayout(field.getName())) { |
| 152 | + skipped++; |
| 153 | + continue; |
| 154 | + } |
| 155 | + checked++; |
| 156 | + try { |
| 157 | + resources.getLayout(id); |
| 158 | + } catch (Resources.NotFoundException e) { |
| 159 | + missing.add(field.getName()); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + assertTrue("Expected to find app-owned layouts to check", checked > 0); |
| 164 | + if (!missing.isEmpty()) { |
| 165 | + fail(String.format( |
| 166 | + "%d of %d app layout(s) do not resolve on this device " |
| 167 | + + "(smallestScreenWidthDp=%d, %d library layouts skipped): %s", |
| 168 | + missing.size(), checked, |
| 169 | + resources.getConfiguration().smallestScreenWidthDp, skipped, missing)); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static boolean isThirdPartyLayout(String layoutName) { |
| 174 | + for (String prefix : THIRD_PARTY_LAYOUT_PREFIXES) { |
| 175 | + if (layoutName.startsWith(prefix)) { |
| 176 | + return true; |
| 177 | + } |
| 178 | + } |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + // ========================================================================= |
| 183 | + // Back navigation — one activity per implementation |
| 184 | + // ========================================================================= |
| 185 | + |
| 186 | + /** |
| 187 | + * SecurityActivity extends AppCompatActivity, so its back handling goes through |
| 188 | + * AndroidX's OnBackPressedDispatcher. |
| 189 | + */ |
| 190 | + @Test |
| 191 | + public void securityActivityBackReturnsToPasswordScreen() { |
| 192 | + assertBackReachesPasswordScreen(SecurityActivity.class); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * PanelWebActivity extends the platform Activity, so its back handling goes through |
| 197 | + * BackNavigationCompat and the platform OnBackInvokedDispatcher instead. Covering |
| 198 | + * one activity from each family is what makes this pair worth running. |
| 199 | + */ |
| 200 | + @Test |
| 201 | + public void panelWebActivityBackReturnsToPasswordScreen() { |
| 202 | + assertBackReachesPasswordScreen(PanelWebActivity.class); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Launches {@code activityClass}, presses back, and requires that |
| 207 | + * CheckPasswordHtmlActivity comes up. |
| 208 | + * <p> |
| 209 | + * Asserting on the destination rather than on the launched activity finishing is |
| 210 | + * deliberate: the platform's default back behaviour also finishes it, so a test |
| 211 | + * that only checked for finishing would pass even with the back handler dead — which |
| 212 | + * is exactly the bug this guards. |
| 213 | + */ |
| 214 | + private void assertBackReachesPasswordScreen(Class<? extends Activity> activityClass) { |
| 215 | + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); |
| 216 | + Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor( |
| 217 | + CheckPasswordHtmlActivity.class.getName(), null, false); |
| 218 | + try { |
| 219 | + try (ActivityScenario<? extends Activity> scenario = |
| 220 | + ActivityScenario.launch(activityClass)) { |
| 221 | + instrumentation.waitForIdleSync(); |
| 222 | + instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); |
| 223 | + |
| 224 | + Activity destination = |
| 225 | + instrumentation.waitForMonitorWithTimeout(monitor, BACK_NAVIGATION_TIMEOUT_MS); |
| 226 | + assertNotNull( |
| 227 | + activityClass.getSimpleName() + ": back must land on " |
| 228 | + + "CheckPasswordHtmlActivity. A null destination means the back " |
| 229 | + + "handler never ran — the failure mode when only onBackPressed " |
| 230 | + + "is overridden while enableOnBackInvokedCallback is true.", |
| 231 | + destination |
| 232 | + ); |
| 233 | + destination.finish(); |
| 234 | + } |
| 235 | + } finally { |
| 236 | + instrumentation.removeMonitor(monitor); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + // ========================================================================= |
| 241 | + // Not covered here: the lock screens |
| 242 | + // ========================================================================= |
| 243 | + // |
| 244 | + // PinNativeActivity and PasswordNativeActivity are the highest-risk activities for |
| 245 | + // the API 36 orientation change — they declare |
| 246 | + // configChanges="keyboardHidden|orientation", which does not cover screenSize, so a |
| 247 | + // rotation destroys and recreates them, and a lock screen that does not come back is |
| 248 | + // an unlocked device. |
| 249 | + // |
| 250 | + // They cannot be covered by simply launching them: the app has a guard that tears |
| 251 | + // down a lock screen which should not be up (CloseActivity is started, and the lock |
| 252 | + // activity goes PAUSED -> STOPPED -> DESTROYED within about a second). That is |
| 253 | + // correct behaviour, so a test that launches the lock cold fails for a reason that |
| 254 | + // has nothing to do with rotation. |
| 255 | + // |
| 256 | + // Verifying this needs a real lock command from the panel, then a physical rotation. |
| 257 | + // Left as a manual check rather than a test that would pass or fail for the wrong |
| 258 | + // reason. |
| 259 | + |
| 260 | + private Resources resources() { |
| 261 | + Context context = ApplicationProvider.getApplicationContext(); |
| 262 | + return context.getResources(); |
| 263 | + } |
| 264 | +} |
0 commit comments