From d815369bad654403b3a0297ab6b4b2d259acf922 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 13 Nov 2026 03:27:41 -0800 Subject: [PATCH 1/2] ADFA-7142: Keep plugin resource loaders off the activity's Resources Plugins built with their own resource package id resolved resources through the host activity's own Resources, and their first use added the plugin's ResourcesLoader to them. On Android 11+ addLoaders makes ResourcesManager build a new ResourcesImpl for the activity and rebase its themes, in the middle of the tab's first LayoutInflater.inflate; the views built then drew sp text at about half size (Note 20 Ultra, display size overridden to 720x1544). That the swap carries the wrong scale is inferred, not confirmed. PluginResourceContext now adds the loader to a copy made with createConfigurationContext from the activity's current configuration, and builds the plugin theme and PluginTheme lookup on that copy. The copy is cached against the activity (held weakly) and its full configuration, so a fontScale, uiMode or density change the activity handles in place rebuilds the copy and the theme. Before Android 11 there is no ResourcesLoader, and the in-place addAssetPath path stays as it was; the legacy (0x7f) path is untouched. A debug log prints the copy's fontScale, density and scaledDensity when it is built. PluginResourceContextActivityResourcesTest pins that a first inflation through the plugin inflater leaves the activity's Resources with no loaders and the same AssetManager, puts the loader on the copy, and that 15sp text through the plugin context matches the activity's, before and after a font-scale change applied to the activity in place. Robolectric does not reproduce the half-size text. Before the fix the loader assertion fails, and the two size tests crash in Robolectric's applyStyle CHECK because it has no shadow for the native theme rebase the swap triggers. Dropping the configuration from the cache key fails the font-scale test (81px vs 45px). Two existing tests that pinned resources === activity.resources now compare configurations. Needs a device check: plugin tab text (Shell-Hooks "Hooks", AI Core "Agent") at font scale 0.8, 1.0 and 2.0 on the Note 20 Ultra at 720x1544, with the logged values compared against the activity's. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../manager/loaders/PluginResourceContext.kt | 52 ++++++-- ...ginResourceContextActivityResourcesTest.kt | 111 ++++++++++++++++++ ...uginResourceContextActivitySnapshotTest.kt | 4 +- .../PluginResourceContextAssetsTest.kt | 8 +- 4 files changed, 161 insertions(+), 14 deletions(-) create mode 100644 plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt index 62c93dc09b..c080194c87 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt @@ -21,6 +21,7 @@ import com.itsaky.androidide.plugins.manager.core.PluginManager import java.io.File import java.io.FileNotFoundException import java.io.IOException +import java.lang.ref.WeakReference class PluginResourceContext( baseContext: Context, @@ -130,6 +131,42 @@ class PluginResourceContext( } } + private var hostResourcesOwner: WeakReference? = null + private var hostResourcesConfig: Configuration? = null + private var hostResources: Resources? = null + + /** + * [actCtx]'s resources with this plugin's APK added. On Android 11+ that goes on a copy pinned to + * the activity's current configuration: adding a loader to the activity's own Resources rebuilds + * them and rebases its themes during the tab's first inflation, and the views built then drew sp + * text at about half size (ADFA-7142). Before R there are no loaders and the activity's assets are + * patched in place. + */ + private fun pluginResourcesFor(actCtx: Context): Resources { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + ensurePluginResourcesAdded(actCtx.resources) + return actCtx.resources + } + val config = actCtx.resources.configuration + val cached = hostResources + if (cached != null && hostResourcesOwner?.get() === actCtx && hostResourcesConfig == config) return cached + + val pinned = Configuration(config) + val res = actCtx.createConfigurationContext(pinned).resources + ensurePluginResourcesAdded(res) + hostResourcesOwner = WeakReference(actCtx) + hostResourcesConfig = pinned + hostResources = res + val metrics = res.displayMetrics + @Suppress("DEPRECATION") + Log.d( + TAG, + "Resources for $pluginId: fontScale=${res.configuration.fontScale} " + + "density=${metrics.density} scaledDensity=${metrics.scaledDensity}", + ) + return res + } + private fun resolveActivityContextAndTheme(): Pair? { val fragmentCtx = PluginFragmentHelper.getCurrentActivityContext(pluginId) val fragmentTheme = PluginFragmentHelper.getCurrentActivityTheme(pluginId) @@ -144,12 +181,11 @@ class PluginResourceContext( if (!usesCustomPackageId) return pluginResources val (actCtx, _) = resolveActivityContextAndTheme() ?: return pluginResources - ensurePluginResourcesAdded(actCtx.resources) - return actCtx.resources + return pluginResourcesFor(actCtx) } - // Never the activity's: every plugin with its own package id adds its APK to those, and asset - // paths carry no package id, so a path two plugins both ship opens whichever was added last. + // Never the resources': those also hold the host APK (and before Android 11 every other such + // plugin's), and asset paths carry no package id, so a path shipped twice can open the wrong one. override fun getAssets(): AssetManager = pluginResources.assets override fun getTheme(): Theme { @@ -184,14 +220,14 @@ class PluginResourceContext( } val (actCtx, actTheme) = resolveActivityContextAndTheme() ?: return pluginTheme ?: baseContext.theme - ensurePluginResourcesAdded(actCtx.resources) - if (pluginTheme == null || lastActivityTheme !== actTheme) { + val res = pluginResourcesFor(actCtx) + if (pluginTheme?.resources !== res || lastActivityTheme !== actTheme) { lastActivityTheme = actTheme inflater = null if (!pluginThemeResIdResolved) { pluginThemeResId = - actCtx.resources.getIdentifier( + res.getIdentifier( PLUGIN_THEME_NAME, "style", pluginPackageInfo?.packageName, @@ -200,7 +236,7 @@ class PluginResourceContext( } pluginTheme = - actCtx.resources.newTheme().apply { + res.newTheme().apply { setTo(actTheme) if (appThemeResId != 0) { diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt new file mode 100644 index 0000000000..03dca976cf --- /dev/null +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt @@ -0,0 +1,111 @@ +package com.itsaky.androidide.plugins.manager.loaders + +import android.app.Activity +import android.content.Context +import android.content.pm.ApplicationInfo +import android.content.pm.PackageInfo +import android.content.res.Configuration +import android.content.res.Resources +import android.content.res.loader.ResourcesLoader +import android.util.TypedValue +import android.view.LayoutInflater +import android.widget.TextView +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.plugins.base.PluginFragmentHelper +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * A plugin with its own resource package id must leave the activity's Resources alone: adding its + * loader there makes Android 11+ rebuild them during the tab's first inflation, and the views built + * then drew sp text at about half size (ADFA-7142). Robolectric does not reproduce the size error, + * so this pins the mutation and that sp text through the plugin keeps matching the activity's. + */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], qualifiers = "xxhdpi") +class PluginResourceContextActivityResourcesTest { + private val activity = Robolectric.buildActivity(Activity::class.java).setup().get() + + // Robolectric 4.11 cannot load an APK through ResourcesProvider on R+, so an empty loader + // stands in for the one built from the plugin's APK. + private val pluginLoader = ResourcesLoader() + + private val pluginContext = + PluginResourceContext( + activity.applicationContext, + PLUGIN_ID, + activity.applicationContext.resources, + PackageInfo().apply { + packageName = PLUGIN_ID + applicationInfo = ApplicationInfo().apply { sourceDir = "plugin.apk" } + }, + javaClass.classLoader, + ).also { + PluginResourceContext::class.java + .getDeclaredField("cachedLoader") + .apply { isAccessible = true } + .set(it, pluginLoader) + } + + private lateinit var pluginInflater: LayoutInflater + + @Before + fun registerPlugin() { + assertThat(pluginContext.usesCustomPackageId()).isTrue() + PluginFragmentHelper.registerPluginContext(PLUGIN_ID, pluginContext) + pluginInflater = PluginFragmentHelper.getPluginInflater(PLUGIN_ID, LayoutInflater.from(activity)) + } + + @After + fun unregisterPlugin() { + PluginFragmentHelper.unregisterPluginContext(PLUGIN_ID) + } + + @Test + fun theFirstInflationAddsThePluginsLoaderToACopyNotToTheActivity() { + val activityAssets = activity.resources.assets + + val view = pluginInflater.inflate(android.R.layout.simple_list_item_1, null, false) + + assertThat(view).isInstanceOf(TextView::class.java) + assertThat(loadersOf(activity.resources)).isEmpty() + assertThat(activity.resources.assets).isSameInstanceAs(activityAssets) + assertThat(loadersOf(pluginInflater.context.resources)).containsExactly(pluginLoader) + } + + @Test + fun spTextThroughThePluginMatchesTheActivity() { + assertThat(spTextSize(pluginInflater.context)).isEqualTo(spTextSize(activity)) + } + + @Test + fun spTextThroughThePluginFollowsAFontScaleChangeTheActivityHandlesItself() { + val before = spTextSize(activity) + assertThat(spTextSize(pluginInflater.context)).isEqualTo(before) + + // EditorActivityKt declares fontScale in configChanges, so its Resources object is kept and + // updated in place rather than replaced. + @Suppress("DEPRECATION") + activity.resources.updateConfiguration( + Configuration(activity.resources.configuration).apply { fontScale = 2f }, + activity.resources.displayMetrics, + ) + + assertThat(spTextSize(activity)).isGreaterThan(before) + assertThat(spTextSize(pluginInflater.context)).isEqualTo(spTextSize(activity)) + } + + private fun spTextSize(context: Context): Float = TextView(context).apply { setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f) }.textSize + + /** Resources.getLoaders() is hidden; Robolectric's framework jar still has it. */ + private fun loadersOf(resources: Resources): List<*> = Resources::class.java.getMethod("getLoaders").invoke(resources) as List<*> + + private companion object { + const val PLUGIN_ID = "com.itsaky.androidide.plugins.manager.loaders.fakeplugin" + } +} diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivitySnapshotTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivitySnapshotTest.kt index 81f88442d5..7261eb212e 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivitySnapshotTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivitySnapshotTest.kt @@ -49,7 +49,7 @@ class PluginResourceContextActivitySnapshotTest { PluginFragmentHelper.getPluginInflater(PLUGIN_ID, LayoutInflater.from(pluginContext)) assertThat(PluginFragmentHelper.getCurrentActivityContext(PLUGIN_ID)).isSameInstanceAs(app) - assertThat(pluginContext.resources).isSameInstanceAs(app.resources) + assertThat(pluginContext.resources.configuration).isEqualTo(app.resources.configuration) } @Test @@ -58,7 +58,7 @@ class PluginResourceContextActivitySnapshotTest { PluginFragmentHelper.getPluginInflater(PLUGIN_ID, wrapped) assertThat(PluginFragmentHelper.getCurrentActivityContext(PLUGIN_ID)).isSameInstanceAs(app) - assertThat(pluginContext.resources).isSameInstanceAs(app.resources) + assertThat(pluginContext.resources.configuration).isEqualTo(app.resources.configuration) } private companion object { diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt index c6c49505d1..35a59cf81f 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt @@ -17,9 +17,9 @@ import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.Config /** - * A plugin with its own resource package id resolves its resources through the activity's, which - * every such plugin adds its APK to. Asset paths carry no package id, so a path two plugins both - * ship must still open the reading plugin's own copy. + * A plugin with its own resource package id resolves its resources through the activity's, or a + * copy of them, which also hold the host APK. Asset paths carry no package id, so a path shipped + * twice must still open the reading plugin's own copy. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [34]) @@ -61,7 +61,7 @@ class PluginResourceContextAssetsTest { @Test fun assetsComeFromThePluginsOwnApkWhileResourcesComeFromTheActivity() { - assertThat(pluginContext.resources).isSameInstanceAs(app.resources) + assertThat(pluginContext.resources.configuration).isEqualTo(app.resources.configuration) assertThat(pluginContext.assets).isSameInstanceAs(ownResources.assets) } From 25e3d07abe504d624d3a3f27e4de2b0cfb86c897 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 13 Nov 2026 05:14:39 -0800 Subject: [PATCH 2/2] ADFA-7142: Keep plugin loaders on the activity too, for plugins that use it d815369ba stopped adding a plugin's ResourcesLoader to the activity's own Resources. On the Note 20 Ultra that crashed AI Core's Agent tab ("No package ID 42 found for ID 0x420a0007"): AI Core resolves its own ids through the activity as well as through the plugin context. ChatFragment calls Fragment.getString and resources.getInteger, which go through requireContext(), and builds its dialogs on ContextThemeWrapper(requireContext(), R.style.PluginTheme), as FilePickerDialogFragment does. Those lookups only ever worked because every such plugin's loader sat on the activity's Resources. The activity's Resources get the loader again, as on stage, and before the copy is built, so that rebuild is done before any plugin view exists. getResources() and getTheme() still hand out the per-activity, per-configuration copy, so plugin views are never built on Resources that addLoaders has just rebuilt. ensurePluginResourcesAdded's patchedResources slot now tracks only the activity's Resources; each copy gets the loader once, through addPluginResources, when it is built. createConfigurationContext passes the base context's loaders on, so the copy usually starts with the loader already. The pre-R and legacy paths are unchanged, and getAssets()'s comment is back to stage's. PluginResourceContextActivityResourcesTest now pins: once the plugin context is used, its loader is on the activity's Resources and on a ContextThemeWrapper over the activity (AI Core's pattern); the plugin context's Resources are a different object that also carries it; inflated plugin views and the plugin theme are built on that copy; and 15sp resolves to the same pixels through both, also after a font-scale change applied to the activity in place. On d815369ba's code the activity-loader test fails and the rest pass. Robolectric cannot load an APK into a ResourcesLoader, so no plugin id is resolved, and it has no shadow for the native theme rebase that adding a loader to the activity triggers. A theme made on the activity's Resources before the patch then trips a CHECK when copied or used, so sp is compared through DisplayMetrics, and the inflation test snapshots a host theme made after the patch. Needs a device check: the Agent and Hooks tabs open without the package-id errors, plugin dialogs and strings resolve, plugin text is the right size at font scale 0.8, 1.0 and 2.0 at 720x1544, and the plugin theme still looks right now that it is copied from the activity's theme across AssetManagers. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../manager/loaders/PluginResourceContext.kt | 36 +++++----- ...ginResourceContextActivityResourcesTest.kt | 67 +++++++++++++------ 2 files changed, 67 insertions(+), 36 deletions(-) diff --git a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt index c080194c87..c9789553b6 100644 --- a/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt +++ b/plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt @@ -85,13 +85,19 @@ class PluginResourceContext( oldAssetManager?.close() } + // The activity's Resources only: each copy gets the loader once, when it is built. private var patchedResources: Resources? = null private var cachedLoader: ResourcesLoader? = null private var cachedProvider: ResourcesProvider? = null - @Suppress("NewApi") private fun ensurePluginResourcesAdded(targetResources: Resources) { if (patchedResources === targetResources) return + addPluginResources(targetResources) + patchedResources = targetResources + } + + @Suppress("NewApi") + private fun addPluginResources(targetResources: Resources) { val sourceDir = pluginSourceDir ?: return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { @@ -103,20 +109,16 @@ class PluginResourceContext( } } targetResources.addLoaders(cachedLoader!!) - patchedResources = targetResources Log.d(TAG, "Added plugin resources via ResourcesLoader for ${pluginPackageInfo?.packageName}") } catch (e: FileNotFoundException) { Log.e(TAG, "ResourcesLoader failed, falling back to addAssetPath", e) addAssetPathFallback(targetResources.assets, sourceDir) - patchedResources = targetResources } catch (e: IOException) { Log.e(TAG, "ResourcesLoader failed, falling back to addAssetPath", e) addAssetPathFallback(targetResources.assets, sourceDir) - patchedResources = targetResources } } else { addAssetPathFallback(targetResources.assets, sourceDir) - patchedResources = targetResources } } @@ -136,24 +138,24 @@ class PluginResourceContext( private var hostResources: Resources? = null /** - * [actCtx]'s resources with this plugin's APK added. On Android 11+ that goes on a copy pinned to - * the activity's current configuration: adding a loader to the activity's own Resources rebuilds - * them and rebases its themes during the tab's first inflation, and the views built then drew sp - * text at about half size (ADFA-7142). Before R there are no loaders and the activity's assets are - * patched in place. + * The Resources this plugin's views are built on. On Android 11+ that is a copy of [actCtx]'s, + * pinned to its current configuration and never changed once handed out: built on the activity's + * own, which addLoaders had just rebuilt, a tab's first views drew sp text at about half size + * (ADFA-7142). The activity's Resources still get the loader, and first, because plugins also + * resolve their ids through the activity, as AI Core's ChatFragment does with Fragment.getString + * and ContextThemeWrapper(requireContext(), R.style.PluginTheme). Before R there are no loaders, + * and the activity's assets are patched in place. */ private fun pluginResourcesFor(actCtx: Context): Resources { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { - ensurePluginResourcesAdded(actCtx.resources) - return actCtx.resources - } + ensurePluginResourcesAdded(actCtx.resources) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return actCtx.resources val config = actCtx.resources.configuration val cached = hostResources if (cached != null && hostResourcesOwner?.get() === actCtx && hostResourcesConfig == config) return cached val pinned = Configuration(config) val res = actCtx.createConfigurationContext(pinned).resources - ensurePluginResourcesAdded(res) + addPluginResources(res) hostResourcesOwner = WeakReference(actCtx) hostResourcesConfig = pinned hostResources = res @@ -184,8 +186,8 @@ class PluginResourceContext( return pluginResourcesFor(actCtx) } - // Never the resources': those also hold the host APK (and before Android 11 every other such - // plugin's), and asset paths carry no package id, so a path shipped twice can open the wrong one. + // Never the activity's: every plugin with its own package id adds its APK to those, and asset + // paths carry no package id, so a path two plugins both ship opens whichever was added last. override fun getAssets(): AssetManager = pluginResources.assets override fun getTheme(): Theme { diff --git a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt index 03dca976cf..dd95ac6b4a 100644 --- a/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt +++ b/plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt @@ -1,13 +1,13 @@ package com.itsaky.androidide.plugins.manager.loaders import android.app.Activity -import android.content.Context import android.content.pm.ApplicationInfo import android.content.pm.PackageInfo import android.content.res.Configuration import android.content.res.Resources import android.content.res.loader.ResourcesLoader import android.util.TypedValue +import android.view.ContextThemeWrapper import android.view.LayoutInflater import android.widget.TextView import com.google.common.truth.Truth.assertThat @@ -21,10 +21,18 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** - * A plugin with its own resource package id must leave the activity's Resources alone: adding its - * loader there makes Android 11+ rebuild them during the tab's first inflation, and the views built - * then drew sp text at about half size (ADFA-7142). Robolectric does not reproduce the size error, - * so this pins the mutation and that sp text through the plugin keeps matching the activity's. + * A plugin with its own resource package id inflates its views against a copy of the activity's + * Resources, which adding its loader never rebuilds mid-inflation (ADFA-7142). The activity's own + * Resources carry the loader too, because plugins also resolve their ids through the activity: + * AI Core's ChatFragment calls Fragment.getString and builds dialogs on + * ContextThemeWrapper(requireContext(), R.style.PluginTheme). + * + * Robolectric does not reproduce the half-size text and cannot load an APK into a ResourcesLoader, + * so these pin where the loader goes, not that a plugin id resolves. It also has no shadow for the + * native theme rebase that adding a loader to the activity triggers: any theme made on the + * activity's Resources before that, the activity's own included, can then neither build views nor + * be copied. So sp sizes are resolved through each side's DisplayMetrics, and the one inflation + * test snapshots a host theme made after the patch. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [34], qualifiers = "xxhdpi") @@ -67,26 +75,47 @@ class PluginResourceContextActivityResourcesTest { } @Test - fun theFirstInflationAddsThePluginsLoaderToACopyNotToTheActivity() { - val activityAssets = activity.resources.assets + fun onceThePluginContextIsUsedItsLoaderIsReachableThroughTheActivity() { + pluginInflater.context.resources + + // AI Core: Fragment.getString resolves through requireContext(), and its dialogs through + // ContextThemeWrapper(requireContext(), R.style.PluginTheme). + assertThat(loadersOf(activity.resources)).contains(pluginLoader) + assertThat(loadersOf(ContextThemeWrapper(activity, android.R.style.Theme_Material).resources)) + .contains(pluginLoader) + } + + @Test + fun thePluginContextHandsOutACopyOfTheActivitysResourcesThatCarriesTheLoader() { + val resources = pluginInflater.context.resources + + assertThat(resources).isNotSameInstanceAs(activity.resources) + assertThat(loadersOf(resources)).contains(pluginLoader) + } + + @Test + fun pluginViewsAndThemeAreBuiltOnTheCopy() { + pluginInflater.context.resources + val host = ContextThemeWrapper(activity, android.R.style.Theme_Material) + val inflater = PluginFragmentHelper.getPluginInflater(PLUGIN_ID, LayoutInflater.from(host)) - val view = pluginInflater.inflate(android.R.layout.simple_list_item_1, null, false) + val view = inflater.inflate(android.R.layout.simple_list_item_1, null, false) assertThat(view).isInstanceOf(TextView::class.java) - assertThat(loadersOf(activity.resources)).isEmpty() - assertThat(activity.resources.assets).isSameInstanceAs(activityAssets) - assertThat(loadersOf(pluginInflater.context.resources)).containsExactly(pluginLoader) + assertThat(view.resources).isNotSameInstanceAs(activity.resources) + assertThat(view.resources).isSameInstanceAs(inflater.context.resources) + assertThat(inflater.context.theme.resources).isSameInstanceAs(view.resources) } @Test - fun spTextThroughThePluginMatchesTheActivity() { - assertThat(spTextSize(pluginInflater.context)).isEqualTo(spTextSize(activity)) + fun spThroughThePluginMatchesTheActivity() { + assertThat(sp15(pluginInflater.context.resources)).isEqualTo(sp15(activity.resources)) } @Test - fun spTextThroughThePluginFollowsAFontScaleChangeTheActivityHandlesItself() { - val before = spTextSize(activity) - assertThat(spTextSize(pluginInflater.context)).isEqualTo(before) + fun spThroughThePluginFollowsAFontScaleChangeTheActivityHandlesItself() { + val before = sp15(pluginInflater.context.resources) + assertThat(before).isEqualTo(sp15(activity.resources)) // EditorActivityKt declares fontScale in configChanges, so its Resources object is kept and // updated in place rather than replaced. @@ -96,11 +125,11 @@ class PluginResourceContextActivityResourcesTest { activity.resources.displayMetrics, ) - assertThat(spTextSize(activity)).isGreaterThan(before) - assertThat(spTextSize(pluginInflater.context)).isEqualTo(spTextSize(activity)) + assertThat(sp15(activity.resources)).isGreaterThan(before) + assertThat(sp15(pluginInflater.context.resources)).isEqualTo(sp15(activity.resources)) } - private fun spTextSize(context: Context): Float = TextView(context).apply { setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f) }.textSize + private fun sp15(resources: Resources): Float = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15f, resources.displayMetrics) /** Resources.getLoaders() is hidden; Robolectric's framework jar still has it. */ private fun loadersOf(resources: Resources): List<*> = Resources::class.java.getMethod("getLoaders").invoke(resources) as List<*>