|
| 1 | +package com.hotcodepush.reactnative |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.ContextWrapper |
| 5 | +import android.content.res.AssetManager |
| 6 | +import com.facebook.react.bridge.JSBundleLoader |
| 7 | +import com.facebook.react.bridge.JSBundleLoaderDelegate |
| 8 | +import com.hotcodepush.core.Core |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Test |
| 11 | +import org.junit.runner.RunWith |
| 12 | +import org.mockito.MockedConstruction |
| 13 | +import org.mockito.Mockito.`when` |
| 14 | +import org.mockito.Mockito.mock |
| 15 | +import org.mockito.Mockito.mockConstruction |
| 16 | +import org.mockito.Mockito.verify |
| 17 | +import org.mockito.Mockito.verifyNoMoreInteractions |
| 18 | +import org.robolectric.RobolectricTestRunner |
| 19 | +import org.robolectric.RuntimeEnvironment |
| 20 | +import org.robolectric.annotation.Config |
| 21 | +import java.io.File |
| 22 | + |
| 23 | +/** |
| 24 | + * The bundle React Native loads, as its host asks the runtime at the start and at every reload, over a core whose start answers |
| 25 | + * the bundle its store names. A downloaded bundle is laid out and persisted through the SDK's own loader, where the core puts it. |
| 26 | + */ |
| 27 | +@RunWith(RobolectricTestRunner::class) |
| 28 | +@Config(sdk = [35]) |
| 29 | +class HotCodePushRuntimeTest { |
| 30 | + private val context = contextWithResourceFile() |
| 31 | + private val runtime = HotCodePushRuntime(context) |
| 32 | + private val bundleLoader = ReactNativeBundleLoader(context, File(context.filesDir, "hotcodepush")) {} |
| 33 | + private val embeddedBundleLoader = JSBundleLoader.createAssetLoader(context, EMBEDDED_BUNDLE_URL, true) |
| 34 | + |
| 35 | + @Test |
| 36 | + fun shouldServeTheBundleFileWhenTheCoresStoreNamesABundle() { |
| 37 | + val bundlePath = layOutBundle("b1") |
| 38 | + mockCoreWhoseStartAnswers("b1").use { |
| 39 | + assertEquals(bundlePath, askForBundle()) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun shouldServeTheEmbeddedBundleWhenTheCoresStoreNamesNone() { |
| 45 | + mockCoreWhoseStartAnswers(null).use { |
| 46 | + assertEquals(EMBEDDED_BUNDLE_URL, askForBundle()) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun shouldServeTheRunningBundleAndTellTheCoreNothingWhenTheHostReloadsWithoutTheSdkAskingForIt() { |
| 52 | + val bundlePath = layOutBundle("b1") |
| 53 | + mockCoreWhoseStartAnswers("b1").use { cores -> |
| 54 | + askForBundle() |
| 55 | + downloadForNextStart("b2") |
| 56 | + |
| 57 | + assertEquals(bundlePath, askForBundle()) |
| 58 | + val core = cores.constructed().single() |
| 59 | + verify(core).handleAppStartBlocking(false) |
| 60 | + verifyNoMoreInteractions(core) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** The core the runtime creates, whose start answers the bundle its store names; the start is in the foreground, as Robolectric's process is. */ |
| 65 | + private fun mockCoreWhoseStartAnswers(bundleId: String?): MockedConstruction<Core> = |
| 66 | + mockConstruction(Core::class.java) { core, _ -> `when`(core.handleAppStartBlocking(false)).thenReturn(bundleId) } |
| 67 | + |
| 68 | + /** React Native asks its host for the bundle and loads what the answer points at: the path it loads. */ |
| 69 | + private fun askForBundle(): String = runtime.resolveBundleLoader(embeddedBundleLoader).loadScript(mock(JSBundleLoaderDelegate::class.java)) |
| 70 | + |
| 71 | + /** A downloaded bundle's JavaScript where the core's download lays it out: answers the file's path. */ |
| 72 | + private fun layOutBundle(bundleId: String): String { |
| 73 | + val bundleDirectory = bundleLoader.projectionDirectory(bundleId).also { it.mkdirs() } |
| 74 | + return File(bundleDirectory, ReactNativeBundleLoader.BUNDLE_FILE_NAME).also { it.writeText("") }.path |
| 75 | + } |
| 76 | + |
| 77 | + /** A release downloaded for the next start, laid out and persisted as the core does through the loader. */ |
| 78 | + private fun downloadForNextStart(bundleId: String) { |
| 79 | + layOutBundle(bundleId) |
| 80 | + bundleLoader.persistServedBundle(bundleId) |
| 81 | + } |
| 82 | + |
| 83 | + private fun contextWithResourceFile(): Context { |
| 84 | + val resourceAssets = mock(AssetManager::class.java) |
| 85 | + `when`(resourceAssets.open("hotcodepush.json")).thenAnswer { RESOURCE_FILE.byteInputStream() } |
| 86 | + return object : ContextWrapper(RuntimeEnvironment.getApplication()) { |
| 87 | + override fun getAssets(): AssetManager = resourceAssets |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + companion object { |
| 92 | + private const val EMBEDDED_BUNDLE_URL = "assets://index.android.bundle" |
| 93 | + private const val RESOURCE_FILE = """{"appId":"0b6d5c1e-2f3a-4b5c-8d9e-0f1a2b3c4d5e","channelId":null,"builtAt":"2026-10-08T00:00:00.000Z","embeddedBundleManifest":null}""" |
| 94 | + } |
| 95 | +} |
0 commit comments