@onekeyfe/react-native-image 6.8.0-alpha.514 → 6.8.0-alpha.539

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
  }
@@ -8,3 +8,12 @@
8
8
  *** rewind();
9
9
  }
10
10
  -dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder
11
+
12
+ # @onekeyfe/react-native-native-overlay draws toast icons through this registry
13
+ # by reflection (Class.forName + method name and arity), so R8 must keep the
14
+ # class name, the singleton field and these members unrenamed in app builds.
15
+ -keep class com.margelo.nitro.onekeyimage.OneKeyNamedSVG {
16
+ public static final com.margelo.nitro.onekeyimage.OneKeyNamedSVG INSTANCE;
17
+ public final void request(java.lang.Object, android.content.res.AssetManager, java.lang.String, java.lang.String, int, int, float, kotlin.jvm.functions.Function1);
18
+ public final void cancel(java.lang.Object);
19
+ }
@@ -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
+ }
@@ -79,8 +79,9 @@ final class OneKeyIconTests: XCTestCase {
79
79
  let mode = entry["recolorMode"] as? String ?? "raster"
80
80
  let colorKey = mode == "raster" ? color : mode == "multiple" ? "1.0" : ""
81
81
  let key = "\(name)|\(digest)|37|29|\(scale)|\(mode)|\(colorKey)"
82
- let disk = SDImageCache(namespace: "onekey-named-svg-raster-v1")
82
+ let disk = OneKeyNamedSVG.shared.rasterDiskCacheForTesting
83
83
  let original = try XCTUnwrap(resolve(name, color: color, width: 37, height: 29, scale: scale))
84
+ OneKeyNamedSVG.shared.flushStartupForTesting(forceClose: false)
84
85
  eventually("Worker output is persisted as a PNG") { disk.diskImageData(forKey: key) != nil }
85
86
  let persisted = try XCTUnwrap(disk.imageFromDiskCache(forKey: key))
86
87
  XCTAssertEqual(persisted.cgImage?.width, 37)
@@ -524,24 +525,34 @@ final class OneKeyIconTests: XCTestCase {
524
525
  try? FileManager.default.removeItem(at: tempFile)
525
526
  }
526
527
  OneKeyNamedSVG.shared.resetStartupForTesting(fileURL: tempFile)
527
- OneKeyMainThreadDecode.resetForTesting()
528
-
529
- // Warm one raster to disk so diskRender and prewarm can replay it.
530
- let disk = SDImageCache(namespace: "onekey-named-svg-raster-v1")
531
- _ = try XCTUnwrap(resolve("DotVerSolid", color: "rgba(0,0,0,1)", width: 48, height: 48, scale: 2))
532
- eventually("Disk raster written for DotVerSolid 48x48") {
533
- OneKeyNamedSVG.shared.clearMemoryCacheForTesting()
534
- return OneKeyNamedSVG.shared.diskRender(
528
+ let rasterPath = try XCTUnwrap(
529
+ OneKeyNamedSVG.shared.rasterDiskCachePathForTesting(
535
530
  name: "DotVerSolid",
536
531
  color: "rgba(0,0,0,1)",
537
532
  width: 48,
538
533
  height: 48,
539
534
  scale: 2
540
- ) != nil
535
+ )
536
+ )
537
+ let rasterURL = URL(fileURLWithPath: rasterPath)
538
+ let fileManager = FileManager.default
539
+ if fileManager.fileExists(atPath: rasterPath) {
540
+ try fileManager.removeItem(at: rasterURL)
541
+ }
542
+ defer { try? fileManager.removeItem(at: rasterURL) }
543
+
544
+ // Poll for the worker's atomic write independently of the frame decode budget.
545
+ OneKeyNamedSVG.shared.clearMemoryCacheForTesting()
546
+ OneKeyMainThreadDecode.resetForTesting()
547
+ _ = try XCTUnwrap(resolve("DotVerSolid", color: "rgba(0,0,0,1)", width: 48, height: 48, scale: 2))
548
+ eventually("Disk raster written for DotVerSolid 48x48") {
549
+ fileManager.fileExists(atPath: rasterPath)
541
550
  }
542
- _ = disk
551
+ let originalRasterData = try Data(contentsOf: rasterURL)
543
552
 
544
- // 1. Synchronous callback on OneKeyNamedSVGBridge.request (BottomTabs path).
553
+ // 1. Synchronous callback from a disk hit on the BottomTabs bridge path.
554
+ OneKeyNamedSVG.shared.clearMemoryCacheForTesting()
555
+ OneKeyMainThreadDecode.resetForTesting()
545
556
  var syncCallbackFired = false
546
557
  let owner = NSObject()
547
558
  OneKeyNamedSVGBridge.request(
@@ -569,22 +580,8 @@ final class OneKeyIconTests: XCTestCase {
569
580
  scale: 2
570
581
  )
571
582
  )
572
- let rasterPath = try XCTUnwrap(
573
- OneKeyNamedSVG.shared.rasterDiskCachePathForTesting(
574
- name: "DotVerSolid",
575
- color: "rgba(0,0,0,1)",
576
- width: 48,
577
- height: 48,
578
- scale: 2
579
- )
580
- )
581
- let rasterURL = URL(fileURLWithPath: rasterPath)
582
- let originalRasterData = try Data(contentsOf: rasterURL)
583
583
  try Data(repeating: 0, count: OneKeyMainThreadDecode.maximumEncodedBytes + 1024)
584
584
  .write(to: rasterURL, options: .atomic)
585
- defer {
586
- try? originalRasterData.write(to: rasterURL, options: .atomic)
587
- }
588
585
  XCTAssertNil(
589
586
  OneKeyNamedSVG.shared.diskRender(
590
587
  name: "DotVerSolid",
@@ -594,7 +591,9 @@ final class OneKeyIconTests: XCTestCase {
594
591
  scale: 2
595
592
  )
596
593
  )
594
+ XCTAssertEqual(OneKeyMainThreadDecode.metricsSnapshot().limitRejections, 1)
597
595
  try originalRasterData.write(to: rasterURL, options: .atomic)
596
+ OneKeyMainThreadDecode.resetForTesting()
598
597
  XCTAssertNotNil(
599
598
  OneKeyNamedSVG.shared.diskRender(
600
599
  name: "DotVerSolid",
@@ -604,9 +603,7 @@ final class OneKeyIconTests: XCTestCase {
604
603
  scale: 2
605
604
  )
606
605
  )
607
- let decodeMetrics = OneKeyMainThreadDecode.metricsSnapshot()
608
- XCTAssertEqual(decodeMetrics.hits, 1)
609
- XCTAssertEqual(decodeMetrics.limitRejections, 1)
606
+ XCTAssertEqual(OneKeyMainThreadDecode.metricsSnapshot().hits, 1)
610
607
 
611
608
  // 2b. Synchronous diskRender must not wait on SDImageCache.ioQueue when a concurrent
612
609
  // write or cache cleanup is occupying ioQueue, and must handle removed files cleanly.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-image",
3
- "version": "6.8.0-alpha.514",
3
+ "version": "6.8.0-alpha.539",
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",