@onekeyfe/react-native-image 6.8.0-9 → 6.8.0-alpha.538

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -56,7 +56,10 @@ android {
56
56
  }
57
57
  buildFeatures { buildConfig true; prefab true }
58
58
  buildTypes { release { minifyEnabled false } }
59
- testOptions { unitTests.returnDefaultValues = true }
59
+ testOptions {
60
+ unitTests.returnDefaultValues = true
61
+ unitTests.includeAndroidResources = true
62
+ }
60
63
  lintOptions { disable "GradleCompatible" }
61
64
  compileOptions {
62
65
  sourceCompatibility JavaVersion.VERSION_1_8
@@ -86,4 +89,5 @@ dependencies {
86
89
  implementation project(":onekeyfe_react-native-skeleton")
87
90
  testImplementation "junit:junit:4.13.2"
88
91
  testImplementation "org.json:json:20231013"
92
+ testImplementation "org.robolectric:robolectric:4.17"
89
93
  }
@@ -34,7 +34,6 @@ import com.bumptech.glide.request.transition.Transition
34
34
  import com.facebook.proguard.annotations.DoNotStrip
35
35
  import com.facebook.react.uimanager.ThemedReactContext
36
36
  import com.margelo.nitro.skeleton.OneKeySkeletonRenderer
37
- import com.margelo.nitro.views.RecyclableView
38
37
  import kotlin.math.ceil
39
38
 
40
39
  private fun Context.findActivity(): Activity? {
@@ -227,9 +226,9 @@ internal fun oneKeyImageRequestSignature(
227
226
  density.toString(),
228
227
  ).joinToString("|")
229
228
 
229
+ // Fabric's recycle pool zeroes view bounds while ScreenStack can still draw the host.
230
230
  @DoNotStrip
231
- class HybridOneKeyImage(private val context: ThemedReactContext) :
232
- HybridOneKeyImageSpec(), RecyclableView {
231
+ class HybridOneKeyImage(private val context: ThemedReactContext) : HybridOneKeyImageSpec() {
233
232
  private enum class DisplayState { LOADING, IMAGE, ERROR, FALLBACK }
234
233
  private companion object {
235
234
  const val FADE_DELAY_THRESHOLD_MS = 100L
@@ -399,8 +398,8 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
399
398
  showLoading(requestIsActive = false)
400
399
  }
401
400
 
402
- override fun prepareForRecycle() {
403
- resetForReuse()
401
+ internal fun prepareForReuse() {
402
+ if (!disposed) resetForReuse()
404
403
  }
405
404
 
406
405
  override fun onDropView() {
@@ -465,6 +464,7 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
465
464
  displayState = DisplayState.LOADING
466
465
  isPreservingDisplayedImage = false
467
466
  hostView.setImageDrawable(null)
467
+ hostView.round = false
468
468
  hostView.drawStateSymbol = false
469
469
  hostView.skeletonRequested = false
470
470
  hostView.setBackgroundColor(Color.TRANSPARENT)
@@ -77,7 +77,7 @@ class OneKeyImageReusableView(context: ThemedReactContext) : FrameLayout(context
77
77
  ): Boolean = image.isDisplaying(sourceUri, sourceHeadersJson, recyclingKey)
78
78
 
79
79
  fun prepareForReuse() {
80
- image.prepareForRecycle()
80
+ image.prepareForReuse()
81
81
  }
82
82
 
83
83
  fun dispose() {
@@ -0,0 +1,188 @@
1
+ package com.margelo.nitro.onekeyimage
2
+
3
+ import android.app.Activity
4
+ import android.graphics.Bitmap
5
+ import android.graphics.Color
6
+ import android.os.Looper
7
+ import android.view.View
8
+ import android.widget.FrameLayout
9
+ import android.widget.ImageView
10
+ import com.bumptech.glide.request.target.Target
11
+ import com.facebook.react.bridge.BridgeReactContext
12
+ import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
13
+ import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults
14
+ import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
15
+ import com.facebook.react.uimanager.DisplayMetricsHolder
16
+ import com.facebook.react.uimanager.ThemedReactContext
17
+ import com.margelo.nitro.R.id.associated_hybrid_view_tag
18
+ import com.margelo.nitro.onekeyimage.views.HybridOneKeyImageManager
19
+ import java.io.File
20
+ import java.nio.ByteBuffer
21
+ import org.junit.After
22
+ import org.junit.Assert.assertEquals
23
+ import org.junit.Assert.assertFalse
24
+ import org.junit.Assert.assertNotSame
25
+ import org.junit.Assert.assertNull
26
+ import org.junit.Assert.assertSame
27
+ import org.junit.Assert.assertTrue
28
+ import org.junit.Before
29
+ import org.junit.Test
30
+ import org.junit.runner.RunWith
31
+ import org.robolectric.Robolectric
32
+ import org.robolectric.RobolectricTestRunner
33
+ import org.robolectric.Shadows.shadowOf
34
+ import org.robolectric.annotation.Config
35
+ import org.robolectric.annotation.Implementation
36
+ import org.robolectric.annotation.Implements
37
+
38
+ @RunWith(RobolectricTestRunner::class)
39
+ @Config(sdk = [34], shadows = [PngOnlyAvifDecoder::class])
40
+ class OneKeyImageLifecycleTest {
41
+ private val controller = Robolectric.buildActivity(Activity::class.java)
42
+ private lateinit var context: ThemedReactContext
43
+ private lateinit var container: FrameLayout
44
+ private lateinit var source: File
45
+
46
+ @Before
47
+ fun setUp() {
48
+ ReactNativeFeatureFlagsForTests.setUp()
49
+ ReactNativeFeatureFlags.override(object : ReactNativeFeatureFlagsDefaults() {
50
+ override fun enableViewRecycling() = true
51
+ })
52
+ val activity = controller.setup().visible().get()
53
+ DisplayMetricsHolder.initDisplayMetrics(activity)
54
+ container = FrameLayout(activity)
55
+ activity.setContentView(container)
56
+ context = ThemedReactContext(BridgeReactContext(activity.application), activity, "test", 1)
57
+ source = File.createTempFile("image-lifecycle", ".png", activity.cacheDir)
58
+ val bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888).apply { eraseColor(Color.GREEN) }
59
+ source.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) }
60
+ bitmap.recycle()
61
+ }
62
+
63
+ @After
64
+ fun tearDown() {
65
+ controller.pause().stop().destroy()
66
+ source.delete()
67
+ ReactNativeFeatureFlags.dangerouslyReset()
68
+ }
69
+
70
+ @Test
71
+ fun fabricDropRetainsTransitionFrameAndNextBindingUsesFreshHost() {
72
+ val manager = HybridOneKeyImageManager()
73
+ val outgoing = manager.createView(1, context, null, null, null) as ImageView
74
+ val image = hybridImage(outgoing)
75
+ attach(outgoing)
76
+ image.round = true
77
+ loadImage(image)
78
+ val drawable = outgoing.drawable
79
+ val request = requireNotNull(target(image)?.request)
80
+
81
+ container.startViewTransition(outgoing)
82
+ container.removeView(outgoing)
83
+ assertTrue(outgoing.isAttachedToWindow)
84
+ manager.onDropViewInstance(outgoing)
85
+
86
+ assertEquals(32, outgoing.width)
87
+ assertEquals(32, outgoing.height)
88
+ assertTrue(outgoing.clipToOutline)
89
+ assertSame(drawable, outgoing.drawable)
90
+ assertFalse(request.isCleared)
91
+
92
+ val next = manager.createView(2, context, null, null, null) as ImageView
93
+ assertNotSame(outgoing, next)
94
+ val rebound = hybridImage(next)
95
+ attach(next)
96
+ rebound.round = false
97
+ rebound.sourceUri = ""
98
+ rebound.afterUpdate()
99
+ assertFalse(next.clipToOutline)
100
+ assertNull(next.drawable)
101
+
102
+ container.endViewTransition(outgoing)
103
+ assertFalse(outgoing.isAttachedToWindow)
104
+ assertFalse(outgoing.clipToOutline)
105
+ assertNull(outgoing.drawable)
106
+ assertTrue(request.isCleared)
107
+ assertNull(target(image))
108
+
109
+ loadImage(rebound)
110
+ container.removeView(next)
111
+ manager.onDropViewInstance(next)
112
+ assertNull(next.drawable)
113
+ }
114
+
115
+ @Test
116
+ fun nativeReuseClearsRoundClipBeforeAnEmptySourceAndCanLoadAgain() {
117
+ val image = HybridOneKeyImage(context).apply { usesApplicationRequestManager = true }
118
+ val host = image.view as ImageView
119
+ attach(host)
120
+ image.round = true
121
+ loadImage(image)
122
+ val request = requireNotNull(target(image)?.request)
123
+ assertTrue(host.clipToOutline)
124
+
125
+ image.prepareForReuse()
126
+ image.round = false
127
+ image.sourceUri = ""
128
+ image.afterUpdate()
129
+ assertFalse(host.clipToOutline)
130
+ assertNull(host.drawable)
131
+ assertTrue(request.isCleared)
132
+
133
+ loadImage(image)
134
+ val nextRequest = requireNotNull(target(image)?.request)
135
+ image.onDropView()
136
+ assertTrue(host.isAttachedToWindow)
137
+ assertNull(host.drawable)
138
+ assertTrue(nextRequest.isCleared)
139
+ }
140
+
141
+ private fun attach(view: View) {
142
+ container.addView(view, FrameLayout.LayoutParams(32, 32))
143
+ view.layout(0, 0, 32, 32)
144
+ }
145
+
146
+ private fun loadImage(image: HybridOneKeyImage) {
147
+ image.sourceUri = source.toURI().toString()
148
+ image.afterUpdate()
149
+ val host = image.view as ImageView
150
+ val deadline = System.nanoTime() + 10_000_000_000L
151
+ while (host.drawable == null && System.nanoTime() < deadline) {
152
+ shadowOf(Looper.getMainLooper()).idle()
153
+ Thread.sleep(10)
154
+ }
155
+ checkNotNull(host.drawable) { "The local PNG must finish decoding" }
156
+ }
157
+
158
+ private fun hybridImage(view: View): HybridOneKeyImage {
159
+ val holder = view.getTag(associated_hybrid_view_tag)
160
+ return holder.javaClass.getDeclaredField("hybridView").run {
161
+ isAccessible = true
162
+ get(holder) as HybridOneKeyImage
163
+ }
164
+ }
165
+
166
+ private fun target(image: HybridOneKeyImage): Target<*>? =
167
+ HybridOneKeyImage::class.java.getDeclaredField("currentTarget").run {
168
+ isAccessible = true
169
+ get(image) as? Target<*>
170
+ }
171
+ }
172
+
173
+ // PNG fixtures do not require Glide's Android-only AVIF JNI library.
174
+ @Implements(className = "org.aomedia.avif.android.AvifDecoder", isInAndroidSdk = false)
175
+ class PngOnlyAvifDecoder {
176
+ companion object {
177
+ @JvmStatic
178
+ @Implementation
179
+ fun __staticInitializer__() = Unit
180
+
181
+ @JvmStatic
182
+ @Implementation
183
+ fun isAvifImage(data: ByteBuffer): Boolean {
184
+ check(data.limit() >= 4 && data.getInt(0) == 0x89504E47.toInt())
185
+ return false
186
+ }
187
+ }
188
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-image",
3
- "version": "6.8.0-9",
3
+ "version": "6.8.0-alpha.538",
4
4
  "description": "High-performance native image view for OneKey",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",