@onekeyfe/react-native-image 6.7.0-alpha.475 → 6.8.0-11
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.
- package/README.md +144 -86
- package/android/build.gradle +1 -0
- package/android/consumer-rules.pro +9 -0
- package/android/src/main/AndroidManifest.xml +12 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyIcon.kt +5 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImage.kt +240 -6
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageCache.kt +4 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideModule.kt +17 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideRegistry.kt +1 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageSafety.kt +41 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupPrewarm.kt +1106 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupProvider.kt +54 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMainThreadDecode.kt +541 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMediaPreviewCache.kt +1 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyNamedSVG.kt +758 -61
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyStartupPrewarmTest.kt +600 -0
- package/ios/OneKeyAvatarImageLoader.swift +1 -1
- package/ios/OneKeyIcon.swift +6 -0
- package/ios/OneKeyImage.swift +85 -2
- package/ios/OneKeyImageCache.swift +2 -0
- package/ios/OneKeyImageCoderBridge.m +42 -1
- package/ios/OneKeyImageRequestContext.swift +16 -2
- package/ios/OneKeyImageStartupPrewarm.swift +602 -0
- package/ios/OneKeyMainThreadDecode.swift +338 -0
- package/ios/OneKeyMediaPreviewCache.swift +20 -12
- package/ios/OneKeyNamedSVG.swift +437 -9
- package/ios/tests/OneKeyIconTests.swift +238 -0
- package/ios/tests/OneKeyImageInfrastructureTests.swift +611 -0
- package/ios/tests/OneKeyMediaPreviewCacheTests.swift +49 -0
- package/lib/module/index.js +5 -1
- package/package.json +1 -1
- package/src/index.tsx +23 -2
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
package com.margelo.nitro.onekeyimage
|
|
2
|
+
|
|
3
|
+
import android.content.res.AssetManager
|
|
4
|
+
import android.graphics.Bitmap
|
|
5
|
+
import org.junit.After
|
|
6
|
+
import org.junit.Assert.assertEquals
|
|
7
|
+
import org.junit.Assert.assertFalse
|
|
8
|
+
import org.junit.Assert.assertNotNull
|
|
9
|
+
import org.junit.Assert.assertNull
|
|
10
|
+
import org.junit.Assert.assertSame
|
|
11
|
+
import org.junit.Assert.assertTrue
|
|
12
|
+
import org.junit.Before
|
|
13
|
+
import org.junit.Test
|
|
14
|
+
import java.io.ByteArrayOutputStream
|
|
15
|
+
import java.io.DataOutputStream
|
|
16
|
+
import java.io.File
|
|
17
|
+
import java.nio.file.Files
|
|
18
|
+
import java.util.Base64
|
|
19
|
+
import java.util.concurrent.CountDownLatch
|
|
20
|
+
import java.util.zip.CRC32
|
|
21
|
+
import java.util.zip.DeflaterOutputStream
|
|
22
|
+
import kotlin.concurrent.thread
|
|
23
|
+
|
|
24
|
+
class OneKeyStartupPrewarmTest {
|
|
25
|
+
private lateinit var tempDir: File
|
|
26
|
+
|
|
27
|
+
private fun dummyBitmap(): Bitmap {
|
|
28
|
+
val unsafeClass = Class.forName("sun.misc.Unsafe")
|
|
29
|
+
val theUnsafe = unsafeClass.getDeclaredField("theUnsafe").apply { isAccessible = true }.get(null)
|
|
30
|
+
val allocateInstance = unsafeClass.getMethod("allocateInstance", Class::class.java)
|
|
31
|
+
return allocateInstance.invoke(theUnsafe, Bitmap::class.java) as Bitmap
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private fun dummyAssetManager(): AssetManager {
|
|
35
|
+
val unsafeClass = Class.forName("sun.misc.Unsafe")
|
|
36
|
+
val theUnsafe = unsafeClass.getDeclaredField("theUnsafe").apply { isAccessible = true }.get(null)
|
|
37
|
+
val allocateInstance = unsafeClass.getMethod("allocateInstance", Class::class.java)
|
|
38
|
+
return allocateInstance.invoke(theUnsafe, AssetManager::class.java) as AssetManager
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private fun createValidPng(width: Int, height: Int): ByteArray {
|
|
42
|
+
val out = ByteArrayOutputStream()
|
|
43
|
+
out.write(byteArrayOf(0x89.toByte(), 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))
|
|
44
|
+
fun writeChunk(type: String, payload: ByteArray) {
|
|
45
|
+
val dos = DataOutputStream(out)
|
|
46
|
+
val typeBytes = type.toByteArray(Charsets.US_ASCII)
|
|
47
|
+
dos.writeInt(payload.size)
|
|
48
|
+
dos.write(typeBytes)
|
|
49
|
+
dos.write(payload)
|
|
50
|
+
val crc = CRC32().apply {
|
|
51
|
+
update(typeBytes)
|
|
52
|
+
update(payload)
|
|
53
|
+
}
|
|
54
|
+
dos.writeInt(crc.value.toInt())
|
|
55
|
+
}
|
|
56
|
+
val ihdr = ByteArrayOutputStream().also {
|
|
57
|
+
DataOutputStream(it).apply {
|
|
58
|
+
writeInt(width)
|
|
59
|
+
writeInt(height)
|
|
60
|
+
writeByte(8) // bit depth
|
|
61
|
+
writeByte(6) // RGBA
|
|
62
|
+
writeByte(0)
|
|
63
|
+
writeByte(0)
|
|
64
|
+
writeByte(0)
|
|
65
|
+
}
|
|
66
|
+
}.toByteArray()
|
|
67
|
+
writeChunk("IHDR", ihdr)
|
|
68
|
+
val idat = ByteArrayOutputStream().also { raw ->
|
|
69
|
+
DeflaterOutputStream(raw).use { deflater ->
|
|
70
|
+
deflater.write(ByteArray(height * (1 + minOf(width, 4) * 4)))
|
|
71
|
+
}
|
|
72
|
+
}.toByteArray()
|
|
73
|
+
writeChunk("IDAT", idat)
|
|
74
|
+
writeChunk("IEND", ByteArray(0))
|
|
75
|
+
return out.toByteArray()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@Before
|
|
79
|
+
fun setUp() {
|
|
80
|
+
tempDir = Files.createTempDirectory("onekey-android-startup-test").toFile()
|
|
81
|
+
OneKeyMainThreadDecode.isEnabled = true
|
|
82
|
+
OneKeyMainThreadDecode.mainThreadCheckOverrideForTesting = { true }
|
|
83
|
+
OneKeyMainThreadDecode.resetForTesting()
|
|
84
|
+
OneKeyImageStartupPrewarm.bitmapByteCountOverrideForTesting = { 4096L }
|
|
85
|
+
OneKeyImageStartupPrewarm.bitmapDecoderOverrideForTesting = { _, _, _, _, _, _ ->
|
|
86
|
+
dummyBitmap()
|
|
87
|
+
}
|
|
88
|
+
OneKeyImageStartupPrewarm.resetForTesting(
|
|
89
|
+
file = File(tempDir, "onekey-image-startup-v1.json"),
|
|
90
|
+
cacheDir = tempDir,
|
|
91
|
+
)
|
|
92
|
+
OneKeyNamedSVG.bitmapByteCountOverrideForTesting = { 4096L }
|
|
93
|
+
OneKeyNamedSVG.diskPngDecoderOverrideForTesting = { file, _, _ ->
|
|
94
|
+
if (file.isFile && file.length() > 0L) dummyBitmap() else null
|
|
95
|
+
}
|
|
96
|
+
OneKeyNamedSVG.clearMemoryCacheForTesting()
|
|
97
|
+
OneKeyNamedSVG.setManifestFallbackForTesting(
|
|
98
|
+
mapOf(
|
|
99
|
+
"DotVerSolid" to OneKeyNamedSVG.ManifestEntry(
|
|
100
|
+
digest = "digest-dot",
|
|
101
|
+
mode = OneKeyNamedSVG.RecolorMode.SINGLE,
|
|
102
|
+
),
|
|
103
|
+
"HomeOutline" to OneKeyNamedSVG.ManifestEntry(
|
|
104
|
+
digest = "digest-home",
|
|
105
|
+
mode = OneKeyNamedSVG.RecolorMode.SINGLE,
|
|
106
|
+
),
|
|
107
|
+
"ChartPositionOutline" to OneKeyNamedSVG.ManifestEntry(
|
|
108
|
+
digest = "digest-chart",
|
|
109
|
+
mode = OneKeyNamedSVG.RecolorMode.MULTIPLE,
|
|
110
|
+
),
|
|
111
|
+
"BotIllus" to OneKeyNamedSVG.ManifestEntry(
|
|
112
|
+
digest = "digest-bot",
|
|
113
|
+
mode = OneKeyNamedSVG.RecolorMode.FIXED,
|
|
114
|
+
),
|
|
115
|
+
),
|
|
116
|
+
)
|
|
117
|
+
OneKeyNamedSVG.resetStartupForTesting(
|
|
118
|
+
file = File(tempDir, "onekey-named-svg-startup-v1.json"),
|
|
119
|
+
cacheDir = tempDir,
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
@After
|
|
124
|
+
fun tearDown() {
|
|
125
|
+
OneKeyMainThreadDecode.mainThreadCheckOverrideForTesting = null
|
|
126
|
+
OneKeyMainThreadDecode.resetForTesting()
|
|
127
|
+
OneKeyImageStartupPrewarm.bitmapDecoderOverrideForTesting = null
|
|
128
|
+
OneKeyImageStartupPrewarm.bitmapByteCountOverrideForTesting = null
|
|
129
|
+
OneKeyImageStartupPrewarm.resetForTesting()
|
|
130
|
+
OneKeyNamedSVG.diskPngDecoderOverrideForTesting = null
|
|
131
|
+
OneKeyNamedSVG.bitmapByteCountOverrideForTesting = null
|
|
132
|
+
OneKeyNamedSVG.setManifestFallbackForTesting(null)
|
|
133
|
+
OneKeyNamedSVG.resetStartupForTesting()
|
|
134
|
+
tempDir.deleteRecursively()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@Test
|
|
138
|
+
fun mainThreadDecodeEnforcesBudgetAndTracksMetrics() {
|
|
139
|
+
// 1. Hit and miss tracking
|
|
140
|
+
val hit = OneKeyMainThreadDecode.run { 42 }
|
|
141
|
+
assertEquals(42, hit)
|
|
142
|
+
val miss = OneKeyMainThreadDecode.run<Int> { null }
|
|
143
|
+
assertNull(miss)
|
|
144
|
+
|
|
145
|
+
var s = OneKeyMainThreadDecode.metricsSnapshot()
|
|
146
|
+
assertEquals(2, s.attempts)
|
|
147
|
+
assertEquals(1, s.hits)
|
|
148
|
+
assertEquals(1, s.misses)
|
|
149
|
+
assertEquals(0, s.budgetRejections)
|
|
150
|
+
|
|
151
|
+
// 2. Exhaust budget within a 1s window
|
|
152
|
+
OneKeyMainThreadDecode.setBudgetForTesting(budgetNs = 1_000_000L, windowNs = 1_000_000_000L)
|
|
153
|
+
val slowHit = OneKeyMainThreadDecode.run {
|
|
154
|
+
val deadline = System.nanoTime() + 2_000_000L
|
|
155
|
+
while (System.nanoTime() < deadline) {
|
|
156
|
+
// Busy wait 2ms
|
|
157
|
+
}
|
|
158
|
+
true
|
|
159
|
+
}
|
|
160
|
+
assertEquals(true, slowHit)
|
|
161
|
+
|
|
162
|
+
val rejected = OneKeyMainThreadDecode.run { true }
|
|
163
|
+
assertNull(rejected)
|
|
164
|
+
s = OneKeyMainThreadDecode.metricsSnapshot()
|
|
165
|
+
assertEquals(1, s.budgetRejections)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@Test
|
|
169
|
+
fun mainThreadDecodePromotesSmallLocalAndCachedImagesAndRejectsOversized() {
|
|
170
|
+
OneKeyMainThreadDecode.setBudgetForTesting(budgetNs = 1_000_000_000L, windowNs = 10_000_000_000L)
|
|
171
|
+
val smallPng = OneKeyBlockieAvatar.png("onekey-avatar://blockie/v1/0xabc123")
|
|
172
|
+
val fileImg = File(tempDir, "small.png").apply { writeBytes(smallPng) }
|
|
173
|
+
val fileUrl = "file://${fileImg.absolutePath}"
|
|
174
|
+
|
|
175
|
+
// 1. Small local file:// promotes within budget
|
|
176
|
+
val promotedFile = OneKeyMainThreadDecode.run {
|
|
177
|
+
if (OneKeyMainThreadDecode.promoteToMemory(tempDir, fileUrl, fileUrl, 64, 64, false)) true else null
|
|
178
|
+
}
|
|
179
|
+
assertEquals(true, promotedFile)
|
|
180
|
+
assertNotNull(OneKeyImageStartupPrewarm.cachedBitmap(fileUrl, 64, 64, false))
|
|
181
|
+
|
|
182
|
+
// 2. Small data: URI promotes within budget
|
|
183
|
+
val dataUri = "data:image/png;base64," + Base64.getEncoder().encodeToString(smallPng)
|
|
184
|
+
val promotedData = OneKeyMainThreadDecode.run {
|
|
185
|
+
if (OneKeyMainThreadDecode.promoteToMemory(tempDir, dataUri, dataUri, 48, 48, true)) true else null
|
|
186
|
+
}
|
|
187
|
+
assertEquals(true, promotedData)
|
|
188
|
+
assertNotNull(OneKeyImageStartupPrewarm.cachedBitmap(dataUri, 48, 48, true))
|
|
189
|
+
|
|
190
|
+
// 3. Target > 256px rejected
|
|
191
|
+
assertFalse(OneKeyMainThreadDecode.promoteToMemory(tempDir, fileUrl, fileUrl, 300, 300, false))
|
|
192
|
+
|
|
193
|
+
// 4. Source > 512px rejected
|
|
194
|
+
val largeSourcePng = createValidPng(600, 600)
|
|
195
|
+
val largeSourceFile = File(tempDir, "large-source.png").apply { writeBytes(largeSourcePng) }
|
|
196
|
+
val largeSourceUrl = "file://${largeSourceFile.absolutePath}"
|
|
197
|
+
assertFalse(OneKeyMainThreadDecode.promoteToMemory(tempDir, largeSourceUrl, largeSourceUrl, 64, 64, false))
|
|
198
|
+
|
|
199
|
+
// 5. File > 32KiB rejected
|
|
200
|
+
val hugeBytes = smallPng + ByteArray(33 * 1024)
|
|
201
|
+
val hugeFile = File(tempDir, "huge.png").apply { writeBytes(hugeBytes) }
|
|
202
|
+
val hugeUrl = "file://${hugeFile.absolutePath}"
|
|
203
|
+
assertFalse(OneKeyMainThreadDecode.promoteToMemory(tempDir, hugeUrl, hugeUrl, 64, 64, false))
|
|
204
|
+
assertTrue(OneKeyMainThreadDecode.metricsSnapshot().limitRejections >= 3)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
@Test
|
|
208
|
+
fun imageStartupPrewarmHashesKeysEvictsByLaunchAndRespectsQuietWindow() {
|
|
209
|
+
val startupJsonFile = File(tempDir, "onekey-image-startup-v1.json")
|
|
210
|
+
val glideCacheDir = File(tempDir, OneKeyImageStartupPrewarm.GLIDE_DISK_CACHE_DIR).apply { mkdirs() }
|
|
211
|
+
|
|
212
|
+
val secretAvatarUrl = "onekey-avatar://blockie/v1/0xdeadbeefcafebabe1234567890"
|
|
213
|
+
val remoteUrl = "https://cdn.onekey.so/banner-secret-token.png"
|
|
214
|
+
val pngBytes = OneKeyBlockieAvatar.png(secretAvatarUrl)
|
|
215
|
+
|
|
216
|
+
// Seed Glide disk cache for remoteUrl
|
|
217
|
+
val remoteSafeKey = requireNotNull(OneKeyImageStartupPrewarm.glideDataSafeKey(remoteUrl))
|
|
218
|
+
File(glideCacheDir, "$remoteSafeKey.0").writeBytes(pngBytes)
|
|
219
|
+
|
|
220
|
+
// 1. Single entry (below MIN_SETTLED_COUNT = 2) keeps window open on non-forced debounce
|
|
221
|
+
OneKeyImageStartupPrewarm.note(
|
|
222
|
+
cacheDir = tempDir,
|
|
223
|
+
requestUrl = secretAvatarUrl,
|
|
224
|
+
rawUrl = secretAvatarUrl,
|
|
225
|
+
headersJson = null,
|
|
226
|
+
cachePolicy = OneKeyImageCachePolicy.MEMORY_DISK,
|
|
227
|
+
width = 96,
|
|
228
|
+
height = 96,
|
|
229
|
+
round = true,
|
|
230
|
+
)
|
|
231
|
+
OneKeyImageStartupPrewarm.flushForTesting(forceClose = false)
|
|
232
|
+
assertFalse("Window must remain open while < 2 images seen (App Lock)", startupJsonFile.exists())
|
|
233
|
+
|
|
234
|
+
// 2. Second entry after unlocking settles and persists Launch 1
|
|
235
|
+
OneKeyImageStartupPrewarm.note(
|
|
236
|
+
cacheDir = tempDir,
|
|
237
|
+
requestUrl = remoteUrl,
|
|
238
|
+
rawUrl = remoteUrl,
|
|
239
|
+
headersJson = null,
|
|
240
|
+
cachePolicy = OneKeyImageCachePolicy.MEMORY_DISK,
|
|
241
|
+
width = 120,
|
|
242
|
+
height = 60,
|
|
243
|
+
round = false,
|
|
244
|
+
)
|
|
245
|
+
OneKeyImageStartupPrewarm.flushForTesting(forceClose = true)
|
|
246
|
+
assertTrue(startupJsonFile.isFile)
|
|
247
|
+
|
|
248
|
+
// 3. Privacy check: persisted JSON must contain zero plaintext URLs or wallet addresses
|
|
249
|
+
val rawJson = startupJsonFile.readText(Charsets.UTF_8)
|
|
250
|
+
assertFalse(rawJson.contains("0xdeadbeef"))
|
|
251
|
+
assertFalse(rawJson.contains("onekey-avatar"))
|
|
252
|
+
assertFalse(rawJson.contains("banner-secret-token"))
|
|
253
|
+
assertFalse(rawJson.contains("https://"))
|
|
254
|
+
|
|
255
|
+
val launch1Entries = OneKeyImageStartupPrewarm.readEntries(startupJsonFile)
|
|
256
|
+
assertEquals(2, launch1Entries.size)
|
|
257
|
+
assertTrue(launch1Entries.all { it.launch == 1 })
|
|
258
|
+
|
|
259
|
+
// 4. Prewarm loads both entries from disk into memory cache by keyHash
|
|
260
|
+
OneKeyImageStartupPrewarm.clearMemoryCache()
|
|
261
|
+
OneKeyImageStartupPrewarm.prewarmStartupSet(tempDir)
|
|
262
|
+
val metrics = OneKeyImageStartupPrewarm.metricsSnapshot()
|
|
263
|
+
assertEquals(2, metrics.entryCount)
|
|
264
|
+
assertEquals(2, metrics.decodedCount)
|
|
265
|
+
assertNotNull(OneKeyImageStartupPrewarm.cachedBitmap(secretAvatarUrl, 96, 96, true))
|
|
266
|
+
assertNotNull(OneKeyImageStartupPrewarm.cachedBitmap(remoteUrl, 120, 60, false))
|
|
267
|
+
assertNull(
|
|
268
|
+
"Custom headers must bypass unauthenticated prewarmed bitmaps",
|
|
269
|
+
OneKeyImageStartupPrewarm.cachedBitmap(
|
|
270
|
+
remoteUrl,
|
|
271
|
+
120,
|
|
272
|
+
60,
|
|
273
|
+
false,
|
|
274
|
+
headersJson = """{"Authorization":"Bearer secret"}""",
|
|
275
|
+
),
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
// 5. Launch-generation eviction: after 3 launches of only remoteUrl, secretAvatarUrl is evicted
|
|
279
|
+
val avatarDiskSafeKey = requireNotNull(OneKeyImageStartupPrewarm.glideDataSafeKey(secretAvatarUrl))
|
|
280
|
+
val avatarFallbackFile = File(File(tempDir, OneKeyImageStartupPrewarm.FALLBACK_DISK_CACHE_DIR), avatarDiskSafeKey)
|
|
281
|
+
assertTrue("Avatar fallback file should exist after Launch 1", avatarFallbackFile.isFile)
|
|
282
|
+
for (expectedLaunch in 2..4) {
|
|
283
|
+
OneKeyImageStartupPrewarm.resetForTesting(file = startupJsonFile, cacheDir = tempDir)
|
|
284
|
+
OneKeyImageStartupPrewarm.note(
|
|
285
|
+
cacheDir = tempDir,
|
|
286
|
+
requestUrl = remoteUrl,
|
|
287
|
+
rawUrl = remoteUrl,
|
|
288
|
+
headersJson = null,
|
|
289
|
+
cachePolicy = OneKeyImageCachePolicy.MEMORY_DISK,
|
|
290
|
+
width = 120,
|
|
291
|
+
height = 60,
|
|
292
|
+
round = false,
|
|
293
|
+
)
|
|
294
|
+
OneKeyImageStartupPrewarm.flushForTesting(forceClose = true)
|
|
295
|
+
val entries = OneKeyImageStartupPrewarm.readEntries(startupJsonFile)
|
|
296
|
+
if (expectedLaunch < 4) {
|
|
297
|
+
assertEquals(2, entries.size)
|
|
298
|
+
assertTrue(avatarFallbackFile.isFile)
|
|
299
|
+
} else {
|
|
300
|
+
assertEquals("Stale avatar must be evicted at launch 4 (age 3)", 1, entries.size)
|
|
301
|
+
assertEquals(
|
|
302
|
+
OneKeyImageStartupPrewarm.keyHash(remoteUrl, 120, 60, false),
|
|
303
|
+
entries[0].keyHash,
|
|
304
|
+
)
|
|
305
|
+
assertFalse("Evicted fallback file must be cleaned from disk", avatarFallbackFile.exists())
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// 6. After the startup recorder closes, continuous promoteToMemory / writeFallbackDiskBytes
|
|
310
|
+
// for new local images must not add non-startup files, and pre-close writes stay bounded.
|
|
311
|
+
val fallbackDir = File(tempDir, OneKeyImageStartupPrewarm.FALLBACK_DISK_CACHE_DIR)
|
|
312
|
+
val baseSmallPng = createValidPng(16, 16)
|
|
313
|
+
val padded32KiB = baseSmallPng + ByteArray(OneKeyMainThreadDecode.MAX_ENCODED_BYTES - baseSmallPng.size)
|
|
314
|
+
for (i in 0 until 100) {
|
|
315
|
+
val localFile = File(tempDir, "post-close-$i.png").apply { writeBytes(baseSmallPng) }
|
|
316
|
+
val localUrl = "file://${localFile.absolutePath}"
|
|
317
|
+
assertTrue(OneKeyMainThreadDecode.promoteToMemory(tempDir, localUrl, localUrl, 32, 32, false))
|
|
318
|
+
OneKeyImageStartupPrewarm.writeFallbackDiskBytes(
|
|
319
|
+
cacheDir = tempDir,
|
|
320
|
+
safeKey = "post_close_key_$i",
|
|
321
|
+
bytes = padded32KiB,
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
OneKeyImageStartupPrewarm.drainSchedulerForTesting()
|
|
325
|
+
val postCloseFiles = fallbackDir.listFiles()?.filter { it.isFile } ?: emptyList()
|
|
326
|
+
assertEquals(
|
|
327
|
+
"No new fallback files may be written after startup recorder closes",
|
|
328
|
+
0,
|
|
329
|
+
postCloseFiles.size,
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
// 6b. Even before the startup recorder closes, fallback directory enforces file count & byte budget,
|
|
333
|
+
// including rejecting a single protected source that individually exceeds MAX_FALLBACK_DISK_BYTES.
|
|
334
|
+
OneKeyImageStartupPrewarm.resetForTesting(file = startupJsonFile, cacheDir = tempDir)
|
|
335
|
+
val oversizedSingleKey = "oversized_single_key"
|
|
336
|
+
val oversizedBytes = ByteArray(OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_BYTES.toInt() + 1)
|
|
337
|
+
OneKeyImageStartupPrewarm.writeFallbackDiskBytes(
|
|
338
|
+
cacheDir = tempDir,
|
|
339
|
+
safeKey = oversizedSingleKey,
|
|
340
|
+
bytes = oversizedBytes,
|
|
341
|
+
)
|
|
342
|
+
assertFalse(
|
|
343
|
+
"Single write exceeding MAX_FALLBACK_DISK_BYTES must be rejected before creating a file",
|
|
344
|
+
File(fallbackDir, oversizedSingleKey).exists(),
|
|
345
|
+
)
|
|
346
|
+
for (i in 0 until 100) {
|
|
347
|
+
OneKeyImageStartupPrewarm.writeFallbackDiskBytes(
|
|
348
|
+
cacheDir = tempDir,
|
|
349
|
+
safeKey = "open_window_key_$i",
|
|
350
|
+
bytes = padded32KiB,
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
val openWindowFiles = fallbackDir.listFiles()?.filter { it.isFile } ?: emptyList()
|
|
354
|
+
val openWindowBytes = openWindowFiles.sumOf { it.length() }
|
|
355
|
+
assertTrue(
|
|
356
|
+
"Fallback file count (${openWindowFiles.size}) must not exceed ${OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_FILES}",
|
|
357
|
+
openWindowFiles.size <= OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_FILES,
|
|
358
|
+
)
|
|
359
|
+
assertTrue(
|
|
360
|
+
"Fallback total bytes ($openWindowBytes) must not exceed ${OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_BYTES}",
|
|
361
|
+
openWindowBytes <= OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_BYTES,
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
// 7. clearDiskCache removes startup JSON and fallback disk cache directory,
|
|
365
|
+
// and invalidates any stale pre-clear fallback write generation.
|
|
366
|
+
val preClearGeneration = OneKeyImageStartupPrewarm.currentDiskClearGenerationForTesting()
|
|
367
|
+
OneKeyImageStartupPrewarm.clearDiskCache(tempDir)
|
|
368
|
+
OneKeyImageStartupPrewarm.writeFallbackDiskBytes(
|
|
369
|
+
cacheDir = tempDir,
|
|
370
|
+
safeKey = avatarDiskSafeKey,
|
|
371
|
+
bytes = pngBytes,
|
|
372
|
+
expectedGeneration = preClearGeneration,
|
|
373
|
+
)
|
|
374
|
+
assertFalse(startupJsonFile.exists())
|
|
375
|
+
assertFalse(File(tempDir, OneKeyImageStartupPrewarm.FALLBACK_DISK_CACHE_DIR).exists())
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
@Test
|
|
379
|
+
fun namedSvgDiskRenderBridgeSyncCallbackAndStartupEvictionWork() {
|
|
380
|
+
val startupJsonFile = File(tempDir, "onekey-named-svg-startup-v1.json")
|
|
381
|
+
val assets = dummyAssetManager()
|
|
382
|
+
|
|
383
|
+
// Seed disk raster PNG for DotVerSolid (key = "DotVerSolid|digest-dot|48|48|2.0|SINGLE|")
|
|
384
|
+
val dotKey = "DotVerSolid|digest-dot|48|48|2.0|SINGLE|"
|
|
385
|
+
val dotRasterFile = requireNotNull(OneKeyNamedSVG.diskRasterFile(dotKey))
|
|
386
|
+
dotRasterFile.parentFile?.mkdirs()
|
|
387
|
+
dotRasterFile.writeBytes(createValidPng(48, 48))
|
|
388
|
+
|
|
389
|
+
// 1. diskRender hits synchronously on main thread and populates memory cache
|
|
390
|
+
val diskHit = OneKeyNamedSVG.diskRender(assets, "DotVerSolid", "rgba(0,0,0,1)", 48, 48, 2f)
|
|
391
|
+
assertNotNull(diskHit)
|
|
392
|
+
val memHit = OneKeyNamedSVG.cachedRender("DotVerSolid", "rgba(0,0,0,1)", 48, 48, 2f)
|
|
393
|
+
assertSame(diskHit?.bitmap, memHit?.bitmap)
|
|
394
|
+
|
|
395
|
+
// 2. OneKeyNamedSVG.request invokes callback synchronously on main thread when cached or on disk
|
|
396
|
+
OneKeyNamedSVG.clearMemoryCacheForTesting()
|
|
397
|
+
OneKeyMainThreadDecode.resetForTesting()
|
|
398
|
+
var syncCallbackRan = false
|
|
399
|
+
var syncBitmap: Bitmap? = null
|
|
400
|
+
OneKeyNamedSVG.request(Any(), assets, "DotVerSolid", "rgba(0,0,0,1)", 48, 48, 2f) { bmp ->
|
|
401
|
+
syncCallbackRan = true
|
|
402
|
+
syncBitmap = bmp
|
|
403
|
+
}
|
|
404
|
+
assertTrue("OneKeyNamedSVG.request must complete synchronously on main thread", syncCallbackRan)
|
|
405
|
+
assertNotNull(syncBitmap)
|
|
406
|
+
|
|
407
|
+
// 3. Manifest lock contention makes diskRender return null without blocking and increments metric
|
|
408
|
+
OneKeyNamedSVG.clearMemoryCacheForTesting()
|
|
409
|
+
OneKeyMainThreadDecode.resetForTesting()
|
|
410
|
+
val locked = CountDownLatch(1)
|
|
411
|
+
val release = CountDownLatch(1)
|
|
412
|
+
val holder = thread {
|
|
413
|
+
assertTrue(OneKeyNamedSVG.tryAcquireManifestAccessLockForTesting())
|
|
414
|
+
locked.countDown()
|
|
415
|
+
release.await()
|
|
416
|
+
OneKeyNamedSVG.unlockManifestAccessLockForTesting()
|
|
417
|
+
}
|
|
418
|
+
locked.await()
|
|
419
|
+
val contendedResult = try {
|
|
420
|
+
OneKeyNamedSVG.diskRender(assets, "DotVerSolid", "rgba(0,0,0,1)", 48, 48, 2f)
|
|
421
|
+
} finally {
|
|
422
|
+
release.countDown()
|
|
423
|
+
holder.join()
|
|
424
|
+
}
|
|
425
|
+
assertNull(contendedResult)
|
|
426
|
+
val contendedMetrics = OneKeyMainThreadDecode.metricsSnapshot()
|
|
427
|
+
assertEquals(1, contendedMetrics.lockContentionMisses)
|
|
428
|
+
assertEquals(1, contendedMetrics.attempts)
|
|
429
|
+
assertEquals(1, contendedMetrics.misses)
|
|
430
|
+
|
|
431
|
+
// 4. Startup quiet window (< 8 icons stays open on non-forced flush) and 3-launch eviction
|
|
432
|
+
OneKeyNamedSVG.resetStartupForTesting(file = startupJsonFile, cacheDir = tempDir)
|
|
433
|
+
OneKeyNamedSVG.cachedRender("DotVerSolid", "rgba(0,0,0,1)", 48, 48, 2f)
|
|
434
|
+
OneKeyNamedSVG.flushStartupForTesting(forceClose = false)
|
|
435
|
+
assertFalse("Icon window must stay open when < 8 icons seen", startupJsonFile.exists())
|
|
436
|
+
|
|
437
|
+
OneKeyNamedSVG.cachedRender("HomeOutline", "rgba(0,0,0,1)", 48, 48, 2f)
|
|
438
|
+
OneKeyNamedSVG.flushStartupForTesting(forceClose = true)
|
|
439
|
+
assertEquals(2, OneKeyNamedSVG.readStartupEntries(startupJsonFile).size)
|
|
440
|
+
|
|
441
|
+
for (launch in 2..4) {
|
|
442
|
+
OneKeyNamedSVG.resetStartupForTesting(file = startupJsonFile, cacheDir = tempDir)
|
|
443
|
+
OneKeyNamedSVG.cachedRender("DotVerSolid", "rgba(0,0,0,1)", 48, 48, 2f)
|
|
444
|
+
OneKeyNamedSVG.flushStartupForTesting(forceClose = true)
|
|
445
|
+
val entries = OneKeyNamedSVG.readStartupEntries(startupJsonFile)
|
|
446
|
+
if (launch < 4) {
|
|
447
|
+
assertEquals(2, entries.size)
|
|
448
|
+
} else {
|
|
449
|
+
assertEquals(1, entries.size)
|
|
450
|
+
assertEquals("DotVerSolid", entries[0].name)
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// 5. Oversized icon raster file (> 32KiB) is rejected by diskRender with limitRejections
|
|
455
|
+
OneKeyNamedSVG.clearMemoryCacheForTesting()
|
|
456
|
+
OneKeyMainThreadDecode.resetForTesting()
|
|
457
|
+
dotRasterFile.writeBytes(ByteArray(OneKeyMainThreadDecode.MAX_ENCODED_BYTES + 1024))
|
|
458
|
+
assertNull(OneKeyNamedSVG.diskRender(assets, "DotVerSolid", "rgba(0,0,0,1)", 48, 48, 2f))
|
|
459
|
+
assertEquals(1, OneKeyMainThreadDecode.metricsSnapshot().limitRejections)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
@Test
|
|
463
|
+
fun exifOrientationParsingDimensionSwappingAndDensityFallbackWork() {
|
|
464
|
+
fun buildTiffOrientation(orientation: Int, littleEndian: Boolean): ByteArray {
|
|
465
|
+
val out = ByteArrayOutputStream()
|
|
466
|
+
fun writeU16(v: Int) {
|
|
467
|
+
if (littleEndian) {
|
|
468
|
+
out.write(v and 0xff)
|
|
469
|
+
out.write((v ushr 8) and 0xff)
|
|
470
|
+
} else {
|
|
471
|
+
out.write((v ushr 8) and 0xff)
|
|
472
|
+
out.write(v and 0xff)
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
fun writeU32(v: Int) {
|
|
476
|
+
if (littleEndian) {
|
|
477
|
+
out.write(v and 0xff)
|
|
478
|
+
out.write((v ushr 8) and 0xff)
|
|
479
|
+
out.write((v ushr 16) and 0xff)
|
|
480
|
+
out.write((v ushr 24) and 0xff)
|
|
481
|
+
} else {
|
|
482
|
+
out.write((v ushr 24) and 0xff)
|
|
483
|
+
out.write((v ushr 16) and 0xff)
|
|
484
|
+
out.write((v ushr 8) and 0xff)
|
|
485
|
+
out.write(v and 0xff)
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
out.write(if (littleEndian) "II".toByteArray() else "MM".toByteArray())
|
|
489
|
+
writeU16(0x002a)
|
|
490
|
+
writeU32(8) // IFD0 offset
|
|
491
|
+
writeU16(1) // 1 entry
|
|
492
|
+
writeU16(0x0112) // Orientation tag
|
|
493
|
+
writeU16(3) // SHORT
|
|
494
|
+
writeU32(1) // count = 1
|
|
495
|
+
writeU16(orientation)
|
|
496
|
+
writeU16(0)
|
|
497
|
+
writeU32(0) // next IFD
|
|
498
|
+
return out.toByteArray()
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
fun buildJpegWithExif(orientation: Int, littleEndian: Boolean): ByteArray {
|
|
502
|
+
val tiff = buildTiffOrientation(orientation, littleEndian)
|
|
503
|
+
val app1Payload = "Exif\u0000\u0000".toByteArray(Charsets.US_ASCII) + tiff
|
|
504
|
+
val segLen = app1Payload.size + 2
|
|
505
|
+
val out = ByteArrayOutputStream()
|
|
506
|
+
out.write(byteArrayOf(0xff.toByte(), 0xd8.toByte(), 0xff.toByte(), 0xe1.toByte()))
|
|
507
|
+
out.write((segLen ushr 8) and 0xff)
|
|
508
|
+
out.write(segLen and 0xff)
|
|
509
|
+
out.write(app1Payload)
|
|
510
|
+
out.write(byteArrayOf(0xff.toByte(), 0xd9.toByte()))
|
|
511
|
+
return out.toByteArray()
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
fun buildWebpWithExif(orientation: Int): ByteArray {
|
|
515
|
+
val tiff = buildTiffOrientation(orientation, littleEndian = true)
|
|
516
|
+
val out = ByteArrayOutputStream()
|
|
517
|
+
val riffSize = 4 + 8 + tiff.size + (tiff.size and 1)
|
|
518
|
+
fun writeLe32(v: Int) {
|
|
519
|
+
out.write(v and 0xff)
|
|
520
|
+
out.write((v ushr 8) and 0xff)
|
|
521
|
+
out.write((v ushr 16) and 0xff)
|
|
522
|
+
out.write((v ushr 24) and 0xff)
|
|
523
|
+
}
|
|
524
|
+
out.write("RIFF".toByteArray(Charsets.US_ASCII))
|
|
525
|
+
writeLe32(riffSize)
|
|
526
|
+
out.write("WEBP".toByteArray(Charsets.US_ASCII))
|
|
527
|
+
out.write("EXIF".toByteArray(Charsets.US_ASCII))
|
|
528
|
+
writeLe32(tiff.size)
|
|
529
|
+
out.write(tiff)
|
|
530
|
+
if ((tiff.size and 1) == 1) out.write(0)
|
|
531
|
+
return out.toByteArray()
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
val jpegRot90Le = buildJpegWithExif(orientation = 6, littleEndian = true)
|
|
535
|
+
val jpegRot270Be = buildJpegWithExif(orientation = 8, littleEndian = false)
|
|
536
|
+
val jpegRot180 = buildJpegWithExif(orientation = 3, littleEndian = true)
|
|
537
|
+
val webpTranspose = buildWebpWithExif(orientation = 5)
|
|
538
|
+
|
|
539
|
+
fun buildPngWithPostIdatExif(orientation: Int): ByteArray {
|
|
540
|
+
val basePng = createValidPng(32, 16)
|
|
541
|
+
val tiff = buildTiffOrientation(orientation, littleEndian = false)
|
|
542
|
+
// Insert eXIf chunk immediately before the 12-byte IEND chunk at the end of basePng
|
|
543
|
+
val iendStart = basePng.size - 12
|
|
544
|
+
val out = ByteArrayOutputStream()
|
|
545
|
+
out.write(basePng, 0, iendStart)
|
|
546
|
+
val dos = DataOutputStream(out)
|
|
547
|
+
val typeBytes = "eXIf".toByteArray(Charsets.US_ASCII)
|
|
548
|
+
dos.writeInt(tiff.size)
|
|
549
|
+
dos.write(typeBytes)
|
|
550
|
+
dos.write(tiff)
|
|
551
|
+
val crc = CRC32().apply {
|
|
552
|
+
update(typeBytes)
|
|
553
|
+
update(tiff)
|
|
554
|
+
}
|
|
555
|
+
dos.writeInt(crc.value.toInt())
|
|
556
|
+
out.write(basePng, iendStart, 12)
|
|
557
|
+
return out.toByteArray()
|
|
558
|
+
}
|
|
559
|
+
val pngPostIdatTransverse = buildPngWithPostIdatExif(orientation = 7)
|
|
560
|
+
|
|
561
|
+
assertEquals(6, OneKeyImageStartupPrewarm.extractExifOrientation(jpegRot90Le))
|
|
562
|
+
assertEquals(8, OneKeyImageStartupPrewarm.extractExifOrientation(jpegRot270Be))
|
|
563
|
+
assertEquals(3, OneKeyImageStartupPrewarm.extractExifOrientation(jpegRot180))
|
|
564
|
+
assertEquals(5, OneKeyImageStartupPrewarm.extractExifOrientation(webpTranspose))
|
|
565
|
+
assertEquals(7, OneKeyImageStartupPrewarm.extractExifOrientation(pngPostIdatTransverse))
|
|
566
|
+
assertEquals(1, OneKeyImageStartupPrewarm.extractExifOrientation(createValidPng(32, 32)))
|
|
567
|
+
|
|
568
|
+
// Verify decodeStaticBitmap swaps sourceWidth/sourceHeight when EXIF orientation is 5..8
|
|
569
|
+
var capturedWidth = 0
|
|
570
|
+
var capturedHeight = 0
|
|
571
|
+
OneKeyImageStartupPrewarm.bitmapDecoderOverrideForTesting = { _, srcW, srcH, _, _, _ ->
|
|
572
|
+
capturedWidth = srcW
|
|
573
|
+
capturedHeight = srcH
|
|
574
|
+
dummyBitmap()
|
|
575
|
+
}
|
|
576
|
+
OneKeyImageStartupPrewarm.decodeStaticBitmap(
|
|
577
|
+
bytes = jpegRot90Le,
|
|
578
|
+
sourceWidth = 400,
|
|
579
|
+
sourceHeight = 200,
|
|
580
|
+
targetWidth = 100,
|
|
581
|
+
targetHeight = 200,
|
|
582
|
+
round = false,
|
|
583
|
+
)
|
|
584
|
+
assertEquals(200, capturedWidth)
|
|
585
|
+
assertEquals(400, capturedHeight)
|
|
586
|
+
|
|
587
|
+
OneKeyImageStartupPrewarm.decodeStaticBitmap(
|
|
588
|
+
bytes = jpegRot180,
|
|
589
|
+
sourceWidth = 400,
|
|
590
|
+
sourceHeight = 200,
|
|
591
|
+
targetWidth = 100,
|
|
592
|
+
targetHeight = 200,
|
|
593
|
+
round = false,
|
|
594
|
+
)
|
|
595
|
+
assertEquals(400, capturedWidth)
|
|
596
|
+
assertEquals(200, capturedHeight)
|
|
597
|
+
|
|
598
|
+
assertTrue(OneKeyImageStartupPrewarm.defaultBitmapDensity() > 0)
|
|
599
|
+
}
|
|
600
|
+
}
|
|
@@ -9,7 +9,7 @@ final class OneKeyAvatarImageLoader: NSObject, SDImageLoader {
|
|
|
9
9
|
static let cache: SDImageCache = {
|
|
10
10
|
let config = SDImageCacheConfig()
|
|
11
11
|
config.maxDiskAge = 30 * 24 * 60 * 60
|
|
12
|
-
config.maxDiskSize =
|
|
12
|
+
config.maxDiskSize = 128 * 1024 * 1024
|
|
13
13
|
config.maxMemoryCost = 8 * 1024 * 1024
|
|
14
14
|
// OneKey patch: Keep several large-account wallets resident while the byte
|
|
15
15
|
// budget remains the hard memory bound.
|
package/ios/OneKeyIcon.swift
CHANGED
|
@@ -71,6 +71,12 @@ final class HybridOneKeyIcon: HybridOneKeyIconSpec, RecyclableView {
|
|
|
71
71
|
apply(hit, color: color)
|
|
72
72
|
return
|
|
73
73
|
}
|
|
74
|
+
if let hit = OneKeyNamedSVG.shared.diskRender(name: name, color: color,
|
|
75
|
+
width: Int(width), height: Int(height), scale: scale) {
|
|
76
|
+
displayedGeometry = geometry
|
|
77
|
+
apply(hit, color: color)
|
|
78
|
+
return
|
|
79
|
+
}
|
|
74
80
|
OneKeyNamedSVG.shared.requestRender(owner: owner, name: name, color: color,
|
|
75
81
|
width: Int(width), height: Int(height), scale: scale) { [weak self] result in
|
|
76
82
|
guard let self, self.generation == revision, self.host.window != nil else { return }
|