@onekeyfe/react-native-native-list 6.7.0-alpha.466 → 6.8.0-10

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.
Files changed (46) hide show
  1. package/README.md +312 -379
  2. package/android/src/main/java/com/onekey/nativelist/NativeListActivityDetailsView.kt +15 -6
  3. package/android/src/main/java/com/onekey/nativelist/NativeListMediaPlaceholderView.kt +244 -0
  4. package/android/src/main/java/com/onekey/nativelist/NativeListMediaPreviewSlot.kt +66 -31
  5. package/android/src/main/java/com/onekey/nativelist/NativeListMediaTileRenderer.kt +32 -11
  6. package/android/src/main/java/com/onekey/nativelist/NativeListModels.kt +60 -0
  7. package/android/src/main/java/com/onekey/nativelist/NativeListResolvedText.kt +5 -1
  8. package/android/src/main/java/com/onekey/nativelist/NativeListTextView.kt +75 -0
  9. package/android/src/main/java/com/onekey/nativelist/NativeListView.kt +25 -19
  10. package/android/src/test/java/com/margelo/nitro/nativelist/NativeListDescriptionTouchPolicyTest.kt +153 -0
  11. package/docs/ROW_TEMPLATES.md +11 -4
  12. package/docs/SPEC.md +296 -20
  13. package/docs/STYLE_SPEC.md +12 -0
  14. package/ios/NativeListActivityDetailsView.swift +13 -5
  15. package/ios/NativeListMediaPlaceholderView.swift +189 -0
  16. package/ios/NativeListMediaPreviewSlot.swift +122 -61
  17. package/ios/NativeListMediaTileRenderer.swift +13 -13
  18. package/ios/NativeListResolvedText.swift +15 -4
  19. package/ios/NativeListTextLabel.swift +106 -0
  20. package/ios/RNCNativeListView.swift +129 -32
  21. package/ios/tests/NativeListActivityPresentationTests.swift +270 -0
  22. package/ios/tests/NativeListLifecycleTests.swift +369 -40
  23. package/ios/tests/NativeListMediaVisibilityTests.swift +41 -7
  24. package/lib/module/NativeList.js +17 -4
  25. package/lib/module/NativeList.web.js +3 -1
  26. package/lib/module/validation.js +53 -4
  27. package/lib/module/web/NativeListGeneratedIcons.js +1 -0
  28. package/lib/module/web/NativeListWebEngine.js +13 -11
  29. package/lib/module/web/templates/ActivityRowRenderer.js +29 -23
  30. package/lib/module/web/templates/DataRowRenderer.js +54 -1
  31. package/lib/module/web/templates/IdentityRowRenderer.js +3 -1
  32. package/lib/module/web/templates/MediaTileRowRenderer.js +84 -15
  33. package/lib/typescript/src/models.d.ts +18 -1
  34. package/package.json +1 -1
  35. package/scripts/generate-icons.test.mjs +1 -0
  36. package/scripts/native-list-icons.json +1 -1
  37. package/src/NativeList.tsx +25 -4
  38. package/src/NativeList.web.tsx +1 -1
  39. package/src/models.ts +18 -0
  40. package/src/validation.ts +96 -1
  41. package/src/web/NativeListGeneratedIcons.ts +1 -0
  42. package/src/web/NativeListWebEngine.ts +16 -11
  43. package/src/web/templates/ActivityRowRenderer.ts +42 -32
  44. package/src/web/templates/DataRowRenderer.ts +76 -1
  45. package/src/web/templates/IdentityRowRenderer.ts +3 -3
  46. package/src/web/templates/MediaTileRowRenderer.ts +90 -15
@@ -1,9 +1,97 @@
1
+ import ObjectiveC
1
2
  import UIKit
2
3
  import XCTest
3
4
  @testable import NativeListModule
4
5
 
5
6
  @MainActor
6
7
  final class NativeListLifecycleTests: XCTestCase {
8
+ func testFilteredSnapshotSuppliesCellsDuringOutgoingUIKitLayout() throws {
9
+ let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 390, height: 697))
10
+ let window = UIWindow(frame: view.frame)
11
+ let controller = UIViewController()
12
+ window.rootViewController = controller
13
+ window.makeKeyAndVisible()
14
+ controller.view.addSubview(view)
15
+ defer {
16
+ view.dispose()
17
+ window.isHidden = true
18
+ }
19
+
20
+ let collection = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
21
+ let dataSource = try XCTUnwrap(collection.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
22
+ collection.isPrefetchingEnabled = false
23
+ collection.contentInsetAdjustmentBehavior = .never
24
+ collection.contentInset = UIEdgeInsets(top: 190, left: 0, bottom: 449, right: 0)
25
+ func snapshot(_ indices: Range<Int>) throws -> String {
26
+ let rows: [[String: Any]] = indices.map { index in
27
+ ["key": "account-\(index)", "type": "identity", "title": "Account \(index)",
28
+ "testID": "recipient-\(index)", "height": 64,
29
+ "leading": ["kind": "icon", "name": "Circle"]]
30
+ }
31
+ let value: [String: Any] = ["schemaVersion": 1, "generation": 1,
32
+ "layout": ["kind": "linear"], "rows": rows]
33
+ return String(decoding: try JSONSerialization.data(withJSONObject: value), as: UTF8.self)
34
+ }
35
+ view.applySnapshotJson(try snapshot(0..<18))
36
+ view.layoutIfNeeded()
37
+ let initialOffset = CGPoint(x: 0, y: -190)
38
+ collection.contentOffset = initialOffset
39
+ collection.layoutIfNeeded()
40
+ XCTAssertEqual(dataSource.snapshot().numberOfItems, 18)
41
+ let outgoingIndexPath = IndexPath(item: 8, section: 0)
42
+ let outgoingFrame = try XCTUnwrap(collection.layoutAttributesForItem(at: outgoingIndexPath)).frame
43
+ XCTAssertFalse(collection.bounds.intersects(outgoingFrame))
44
+ XCTAssertNil(collection.cellForItem(at: outgoingIndexPath))
45
+
46
+ // Force a real UIKit layout at the invalidation inside applySnapshotJson,
47
+ // while UIKit still owns the old snapshot. Scope the hook to this layout.
48
+ let layout = collection.collectionViewLayout
49
+ let layoutClass: AnyClass = type(of: layout)
50
+ let selector = NSSelectorFromString("invalidateLayout")
51
+ let method = try XCTUnwrap(class_getInstanceMethod(layoutClass, selector))
52
+ let encoding = try XCTUnwrap(method_getTypeEncoding(method))
53
+ let original = method_getImplementation(method)
54
+ let callOriginal = unsafeBitCast(original, to: (@convention(c) (AnyObject, Selector) -> Void).self)
55
+ var didLayoutOutgoingSnapshot = false
56
+ let invalidate: @convention(block) (UICollectionViewLayout) -> Void = { current in
57
+ callOriginal(current, selector)
58
+ guard current === layout, !didLayoutOutgoingSnapshot,
59
+ dataSource.snapshot().numberOfItems == 18 else { return }
60
+ didLayoutOutgoingSnapshot = true
61
+ let size = view.collectionView(collection, layout: current, sizeForItemAt: outgoingIndexPath)
62
+ XCTAssertEqual(size.height, 64, "UIKit still needs the outgoing row’s geometry")
63
+ XCTAssertNil(collection.cellForItem(at: outgoingIndexPath))
64
+ collection.contentOffset.y = outgoingFrame.midY - collection.bounds.height / 2
65
+ collection.setNeedsLayout()
66
+ collection.layoutIfNeeded()
67
+ XCTAssertTrue(collection.bounds.intersects(outgoingFrame))
68
+ guard let outgoingCell = collection.cellForItem(at: outgoingIndexPath) else {
69
+ XCTFail("UIKit must request the previously offscreen outgoing cell")
70
+ return
71
+ }
72
+ XCTAssertEqual(outgoingCell.reuseIdentifier,
73
+ NativeListRendererRegistry.reuseIdentifier(for: .identity))
74
+ XCTAssertFalse(outgoingCell.isUserInteractionEnabled)
75
+ XCTAssertTrue(outgoingCell.accessibilityElementsHidden)
76
+ XCTAssertFalse(view.collectionView(collection, shouldSelectItemAt: outgoingIndexPath))
77
+ }
78
+ let replacement = imp_implementationWithBlock(invalidate)
79
+ class_replaceMethod(layoutClass, selector, replacement, encoding)
80
+ defer {
81
+ class_replaceMethod(layoutClass, selector, original, encoding)
82
+ imp_removeBlock(replacement)
83
+ }
84
+
85
+ view.applySnapshotJson(try snapshot(12..<15))
86
+ collection.contentOffset = initialOffset
87
+ collection.layoutIfNeeded()
88
+ XCTAssertTrue(didLayoutOutgoingSnapshot)
89
+ XCTAssertEqual(dataSource.snapshot().itemIdentifiers, ["account-12", "account-13", "account-14"])
90
+ let first = try XCTUnwrap(collection.cellForItem(at: IndexPath(item: 0, section: 0)))
91
+ XCTAssertFalse(first.isHidden)
92
+ XCTAssertEqual(first.accessibilityIdentifier, "recipient-12")
93
+ }
94
+
7
95
  func testCanonicalNullPatchRemovesStoredOptionalFields() throws {
8
96
  let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
9
97
  let row: [String: Any] = ["key": "token", "type": "identity", "title": "Token",
@@ -135,11 +223,17 @@ final class NativeListLifecycleTests: XCTestCase {
135
223
  window.rootViewController = controller
136
224
  controller.view.addSubview(view)
137
225
  window.isHidden = false
138
- defer { window.isHidden = true }
139
226
  window.layoutIfNeeded()
140
227
  view.layoutIfNeeded()
141
228
  drainMainQueue()
142
229
  let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
230
+ defer {
231
+ list.setContentOffset(list.contentOffset, animated: false)
232
+ view.dispose()
233
+ view.removeFromSuperview()
234
+ window.isHidden = true
235
+ window.rootViewController = nil
236
+ }
143
237
  XCTAssertNotNil(view.window)
144
238
  XCTAssertGreaterThan(list.contentSize.height - list.bounds.height +
145
239
  list.adjustedContentInset.top + list.adjustedContentInset.bottom, 800)
@@ -1047,6 +1141,68 @@ final class NativeListLifecycleTests: XCTestCase {
1047
1141
  XCTAssertTrue(second.isHidden)
1048
1142
  }
1049
1143
 
1144
+ func testFixedFooterSharesRetiringRowKeyDuringStructuralTransition() throws {
1145
+ try requireStructuralAnimation()
1146
+ let footerData: [String: Any] = [
1147
+ "key": "a", "type": "action", "title": "Continue", "actionKey": "continue",
1148
+ ]
1149
+ func snapshot(_ keys: [String], animate: Bool) throws -> String {
1150
+ let rows: [[String: Any]] = keys.map {
1151
+ ["key": $0, "type": "action", "title": $0, "actionKey": "row-\($0)"]
1152
+ }
1153
+ var value: [String: Any] = ["schemaVersion": 1, "generation": animate ? 2 : 1,
1154
+ "layout": ["kind": "linear"], "rows": rows, "fixedFooter": footerData]
1155
+ if animate { value["structuralTransition"] = ["id": "remove-a", "durationMs": 800] }
1156
+ return String(decoding: try JSONSerialization.data(withJSONObject: value), as: UTF8.self)
1157
+ }
1158
+
1159
+ let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
1160
+ let controller = UIViewController()
1161
+ window.rootViewController = controller
1162
+ let view = NativeListView(frame: window.bounds)
1163
+ controller.view.addSubview(view)
1164
+ window.isHidden = false
1165
+ defer {
1166
+ view.dispose()
1167
+ window.isHidden = true
1168
+ }
1169
+
1170
+ let collection = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
1171
+ let dataSource = try XCTUnwrap(collection.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
1172
+ var actions: [String] = []
1173
+ view.onRowAction = { payload in
1174
+ if let action = (try? JSONSerialization.jsonObject(with: Data(payload.utf8))) as? [String: Any],
1175
+ let key = action["actionKey"] as? String { actions.append(key) }
1176
+ }
1177
+ view.applySnapshotJson(try snapshot(["a", "b", "c"], animate: false))
1178
+ view.layoutIfNeeded()
1179
+ collection.layoutIfNeeded()
1180
+ let outgoingIndex = IndexPath(item: 0, section: 0)
1181
+ let outgoing = try XCTUnwrap(collection.cellForItem(at: outgoingIndex) as? NativeListRowHost)
1182
+ let outgoingAction = try XCTUnwrap(outgoing.onAction)
1183
+
1184
+ view.applySnapshotJson(try snapshot(["b", "c"], animate: true))
1185
+ XCTAssertEqual(dataSource.itemIdentifier(for: outgoingIndex), "a")
1186
+ XCTAssertFalse(view.collectionView(collection, shouldSelectItemAt: outgoingIndex))
1187
+ view.collectionView(collection, didSelectItemAt: outgoingIndex)
1188
+ // Child renderers can emit a different key than the enclosing collection row.
1189
+ let child = try NativeListItem(data: ["key": "child", "type": "action", "title": "Child"])
1190
+ outgoingAction(child, "child-press", nil, outgoing.rowActionOrigin())
1191
+ XCTAssertTrue(actions.isEmpty)
1192
+
1193
+ let footer = try XCTUnwrap(footerCell(in: view))
1194
+ XCTAssertEqual(footer.accessibilityLabel, "Continue")
1195
+ XCTAssertEqual(footer.alpha, 1)
1196
+ XCTAssertTrue(footer.isUserInteractionEnabled)
1197
+ XCTAssertFalse(footer.accessibilityElementsHidden)
1198
+ // Dispatch the gesture's target directly in this hostless XCTest.
1199
+ let footerPress = NSSelectorFromString("footerPressed")
1200
+ XCTAssertTrue(view.responds(to: footerPress))
1201
+ _ = view.perform(footerPress)
1202
+ footer.onAction?(try NativeListItem(data: footerData), "footer-accessory", nil, footer.rowActionOrigin())
1203
+ XCTAssertEqual(actions, ["continue", "footer-accessory"])
1204
+ }
1205
+
1050
1206
  func testFontResolversMeasureBindResetAndKeepIndependentInstances() throws {
1051
1207
  let first = NativeListFontFamilies(names: ["regular": "Courier", "bold": "Courier-Bold"])
1052
1208
  let second = NativeListFontFamilies(names: ["regular": "Georgia", "bold": "Georgia-Bold"])
@@ -1228,7 +1384,9 @@ final class NativeListLifecycleTests: XCTestCase {
1228
1384
  list.layoutIfNeeded()
1229
1385
  let header = try XCTUnwrap(slots[0].superview)
1230
1386
  XCTAssertTrue(header.clipsToBounds)
1231
- XCTAssertEqual(header.bounds.height, 0, "An unmeasured header cannot draw over the first rows")
1387
+ XCTAssertEqual(header.bounds.height, 44, "The mounted Fabric header reserves its first-frame height")
1388
+ let firstRow = try XCTUnwrap(list.layoutAttributesForItem(at: IndexPath(item: 0, section: 0)))
1389
+ XCTAssertEqual(firstRow.frame.minY, 44, accuracy: 0.5)
1232
1390
 
1233
1391
  view.setContainerSlotHeightsJson("[44,44,60,0]")
1234
1392
  view.layoutIfNeeded()
@@ -1245,6 +1403,143 @@ final class NativeListLifecycleTests: XCTestCase {
1245
1403
  XCTAssertNil(slots[2].superview)
1246
1404
  }
1247
1405
 
1406
+ func testPendingHeaderReservesMountedGeometryInEitherPropertyOrder() throws {
1407
+ for pendingBeforeMount in [true, false] {
1408
+ let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 320, height: 708))
1409
+ view.applySnapshotJson(try scrollingSnapshot(count: 3))
1410
+ let header = UIView(frame: CGRect(x: 16, y: 0, width: 288, height: 44.2))
1411
+ if pendingBeforeMount { view.setContainerSlotHeightsJson("[null,0,0,0,0,0]") }
1412
+ view.mountContainerSlot(header, atIndex: 0)
1413
+ if !pendingBeforeMount { view.setContainerSlotHeightsJson("[null,0,0,0,0,0]") }
1414
+ view.layoutIfNeeded()
1415
+ let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
1416
+ list.layoutIfNeeded()
1417
+ let host = try XCTUnwrap(header.superview)
1418
+ XCTAssertTrue(host.clipsToBounds)
1419
+ XCTAssertEqual(host.bounds.height, 45, accuracy: 0.5)
1420
+ let firstRow = try XCTUnwrap(list.layoutAttributesForItem(at: IndexPath(item: 0, section: 0)))
1421
+ XCTAssertEqual(firstRow.frame.minY, 45, accuracy: 0.5)
1422
+
1423
+ view.setContainerSlotHeightsJson("[null,0,18,0,0,0]")
1424
+ view.layoutIfNeeded()
1425
+ list.layoutIfNeeded()
1426
+ XCTAssertEqual(host.bounds.height, 45, accuracy: 0.5)
1427
+ XCTAssertEqual(list.layoutAttributesForItem(
1428
+ at: IndexPath(item: 0, section: 0))?.frame.minY, 45)
1429
+
1430
+ view.setContainerSlotHeightsJson("[18,0,0,0,0,0]")
1431
+ view.layoutIfNeeded()
1432
+ list.layoutIfNeeded()
1433
+ XCTAssertEqual(host.bounds.height, 18, accuracy: 0.5)
1434
+ XCTAssertEqual(list.collectionViewLayout.layoutAttributesForItem(
1435
+ at: IndexPath(item: 0, section: 0))?.frame.minY, 18)
1436
+ view.setContainerSlotHeightsJson("[0,0,0,0,0,0]")
1437
+ view.layoutIfNeeded()
1438
+ list.layoutIfNeeded()
1439
+ XCTAssertEqual(host.bounds.height, 0)
1440
+ XCTAssertEqual(list.collectionViewLayout.layoutAttributesForItem(
1441
+ at: IndexPath(item: 0, section: 0))?.frame.minY, 0)
1442
+ view.dispose()
1443
+ }
1444
+ }
1445
+
1446
+ func testNumericHeaderHeightIsAuthoritativeBeforeRootMounts() throws {
1447
+ for height in [0, 24] {
1448
+ let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 320, height: 708))
1449
+ view.setContainerSlotHeightsJson("[\(height),0,0,0]")
1450
+ view.applySnapshotJson(try scrollingSnapshot(count: 3))
1451
+ let header = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
1452
+ view.mountContainerSlot(header, atIndex: 0)
1453
+ view.layoutIfNeeded()
1454
+ let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
1455
+ list.layoutIfNeeded()
1456
+ XCTAssertEqual(header.superview?.bounds.height, CGFloat(height))
1457
+ XCTAssertEqual(list.layoutAttributesForItem(
1458
+ at: IndexPath(item: 0, section: 0))?.frame.minY, CGFloat(height))
1459
+ view.dispose()
1460
+ }
1461
+ }
1462
+
1463
+ func testPendingHeaderDoesNotInheritAnUnmountedRootsHeight() throws {
1464
+ let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 320, height: 708))
1465
+ view.applySnapshotJson(try scrollingSnapshot(count: 3))
1466
+ view.setContainerSlotHeightsJson("[null,0,0,0,0,0]")
1467
+ let first = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
1468
+ view.mountContainerSlot(first, atIndex: 0)
1469
+ XCTAssertEqual(first.superview?.bounds.height, 44)
1470
+ view.unmountContainerSlot(first)
1471
+ let empty = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 0))
1472
+ view.mountContainerSlot(empty, atIndex: 0)
1473
+ XCTAssertEqual(empty.superview?.bounds.height, 0)
1474
+ view.unmountContainerSlot(empty)
1475
+ let replacement = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 27))
1476
+ view.mountContainerSlot(replacement, atIndex: 0)
1477
+ XCTAssertEqual(replacement.superview?.bounds.height, 27)
1478
+ view.setContainerSlotHeightsJson("[0,0,0,0,0,0]")
1479
+ view.unmountContainerSlot(replacement)
1480
+ let authoritativeZero = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 60))
1481
+ view.mountContainerSlot(authoritativeZero, atIndex: 0)
1482
+ XCTAssertEqual(authoritativeZero.superview?.bounds.height, 0)
1483
+ view.dispose()
1484
+ }
1485
+
1486
+ func testPendingHeaderOnlyAcceptsNullAtTheHeaderSlot() throws {
1487
+ let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 320, height: 708))
1488
+ view.setContainerSlotHeightsJson("[24,0,0,0,0,0]")
1489
+ let header = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
1490
+ view.mountContainerSlot(header, atIndex: 0)
1491
+ for invalid in ["[null,null,0,0,0,0]", "[\"invalid\",0,0,0,0,0]",
1492
+ "[null,0,0,0,-1,0]", "[null,0,0,0,0,null]"] {
1493
+ view.setContainerSlotHeightsJson(invalid)
1494
+ XCTAssertEqual(header.superview?.bounds.height, 24)
1495
+ }
1496
+ view.dispose()
1497
+ }
1498
+
1499
+ func testFirstUsableBoundsNotifiesPagerBeforeSlotLayout() throws {
1500
+ let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844))
1501
+ let root = UIViewController()
1502
+ window.rootViewController = root
1503
+ window.isHidden = false
1504
+ let view = NativeListView(frame: .zero)
1505
+ root.view.addSubview(view)
1506
+ defer {
1507
+ view.dispose()
1508
+ view.removeFromSuperview()
1509
+ window.rootViewController = nil
1510
+ window.isHidden = true
1511
+ }
1512
+
1513
+ let header = UIView(frame: CGRect(x: 0, y: 0, width: 390, height: 65))
1514
+ view.mountContainerSlot(header, atIndex: 0)
1515
+ view.setContainerSlotHeightsJson("[65,0,0,0,0,0]")
1516
+ view.applySnapshotJson(try scrollingSnapshot(count: 6))
1517
+
1518
+ let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
1519
+ let owner = NSObject()
1520
+ var readyCount = 0
1521
+ let observer = NotificationCenter.default.addObserver(
1522
+ forName: Notification.Name("OneKeyNativeScrollViewDidBecomeReady"),
1523
+ object: list,
1524
+ queue: nil
1525
+ ) { _ in
1526
+ readyCount += 1
1527
+ view.setPagerStickyHeight(52, forOwner: owner)
1528
+ list.contentInset = UIEdgeInsets(top: 345, left: 0, bottom: 0, right: 0)
1529
+ list.setContentOffset(CGPoint(x: 0, y: -345), animated: false)
1530
+ }
1531
+ defer { NotificationCenter.default.removeObserver(observer) }
1532
+
1533
+ view.frame = CGRect(x: 0, y: 0, width: 390, height: 697)
1534
+ view.layoutIfNeeded()
1535
+
1536
+ let host = try XCTUnwrap(header.superview)
1537
+ XCTAssertEqual(readyCount, 1)
1538
+ XCTAssertEqual(list.contentInset.top, 345, accuracy: 0.01)
1539
+ XCTAssertEqual(list.contentOffset.y, -345, accuracy: 0.01)
1540
+ XCTAssertNil(host.layer.mask)
1541
+ }
1542
+
1248
1543
  func testSidecarCentersRowsWhenConfiguredBeforeFirstWidth() throws {
1249
1544
  try assertHomeSidecarGeometry(sidecarBeforeFirstLayout: true)
1250
1545
  }
@@ -1481,7 +1776,7 @@ final class NativeListLifecycleTests: XCTestCase {
1481
1776
  view.layoutIfNeeded()
1482
1777
  list.layoutIfNeeded()
1483
1778
  } completion: { _ in finished.fulfill() }
1484
- wait(for: [finished], timeout: 1)
1779
+ wait(for: [finished], timeout: 5)
1485
1780
  try check(phase, count: count, width: 700)
1486
1781
  waitForStructuralFrames(0.1)
1487
1782
  try check("\(phase) stable", count: count, width: 700)
@@ -1538,9 +1833,12 @@ final class NativeListLifecycleTests: XCTestCase {
1538
1833
  XCTAssertEqual(list.contentOffset.y, testCase.offset, accuracy: 1, testCase.name)
1539
1834
 
1540
1835
  view.applySnapshotJson(try structuralRowsSnapshot(count: 25, transitionID: testCase.name))
1541
- let completed = expectation(description: "Native structural display link: \(testCase.name)")
1542
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { completed.fulfill() }
1543
- wait(for: [completed], timeout: 1)
1836
+ let settleDeadline = Date().addingTimeInterval(5)
1837
+ while Mirror(reflecting: view).descendant("structuralDisplayLink") as? CADisplayLink != nil,
1838
+ Date() < settleDeadline {
1839
+ RunLoop.main.run(until: Date().addingTimeInterval(0.01))
1840
+ }
1841
+ waitForStructuralFrames(0.05)
1544
1842
  list.layoutIfNeeded()
1545
1843
  XCTAssertEqual(list.adjustedContentInset, explicitInset, testCase.name)
1546
1844
  XCTAssertEqual(list.contentSize.height, testCase.height, accuracy: 1, testCase.name)
@@ -1662,17 +1960,20 @@ final class NativeListLifecycleTests: XCTestCase {
1662
1960
  list.contentOffset.y = 900
1663
1961
  list.layoutIfNeeded()
1664
1962
 
1963
+ let flow = try XCTUnwrap(list.collectionViewLayout as? NativeListFlowLayout)
1665
1964
  view.applySnapshotJson(try structuralRowsSnapshot(count: 30, generation: 2,
1666
- transitionID: "expand-first", durationMs: 400))
1667
- waitForStructuralFrames(0.08)
1965
+ transitionID: "expand-first", durationMs: 1200))
1966
+ let expandDeadline = Date().addingTimeInterval(3)
1967
+ while (flow.tailClip?.endY ?? 0) <= 1505 && Date() < expandDeadline {
1968
+ RunLoop.main.run(until: Date().addingTimeInterval(0.01))
1969
+ }
1668
1970
  list.contentOffset.y = 1100
1669
1971
  list.layoutIfNeeded()
1670
1972
  view.applySnapshotJson(try structuralRowsSnapshot(count: 25, generation: 3,
1671
- transitionID: "collapse-middle", durationMs: 400))
1973
+ transitionID: "collapse-middle", durationMs: 2400))
1672
1974
  waitForStructuralFrames(0.08)
1673
1975
  list.layoutIfNeeded()
1674
1976
  let source = try XCTUnwrap(list.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
1675
- let flow = try XCTUnwrap(list.collectionViewLayout as? NativeListFlowLayout)
1676
1977
  let clip = try XCTUnwrap(flow.tailClip)
1677
1978
  let retiring = try XCTUnwrap(list.cellForItem(at: IndexPath(item: 25, section: 0)) as? NativeListRowHost)
1678
1979
  XCTAssertGreaterThan(clip.endY, 1500)
@@ -1990,7 +2291,7 @@ final class NativeListLifecycleTests: XCTestCase {
1990
2291
  view.applySnapshotJson(try structuralRowsSnapshot(count: 6, generation: 5,
1991
2292
  transitionID: "collapse-latest", durationMs: 400,
1992
2293
  disclosureExpanded: false))
1993
- wait(for: [latestSettled], timeout: 2)
2294
+ wait(for: [latestSettled], timeout: 5)
1994
2295
  XCTAssertEqual(settlements.map(\.0), ["same-rows", "collapse-latest"])
1995
2296
  XCTAssertEqual(settlements.last?.1, "completed")
1996
2297
  XCTAssertEqual(settlements.last?.2, false)
@@ -2182,13 +2483,13 @@ final class NativeListLifecycleTests: XCTestCase {
2182
2483
  defer { growthProbe.invalidate() }
2183
2484
  view.applySnapshotJson(try structuralRowsSnapshot(count: 50, generation: 2,
2184
2485
  transitionID: "grow", durationMs: 800, disclosureExpanded: true))
2185
- wait(for: [growthPhase], timeout: 1.5)
2486
+ wait(for: [growthPhase], timeout: 5)
2186
2487
  guard growthObserved else {
2187
2488
  XCTFail("No partial disclosure growth frame observed: \(growthLast)")
2188
2489
  return
2189
2490
  }
2190
2491
  growthProbe.invalidate()
2191
- wait(for: [growSettled], timeout: 1.5)
2492
+ wait(for: [growSettled], timeout: 5)
2192
2493
  XCTAssertEqual(contentHost.bounds.height, 140, accuracy: 1)
2193
2494
  XCTAssertNil(contentHost.layer.mask)
2194
2495
  XCTAssertTrue(contentHost.isUserInteractionEnabled)
@@ -2227,7 +2528,7 @@ final class NativeListLifecycleTests: XCTestCase {
2227
2528
  defer { collapseProbe.invalidate() }
2228
2529
  view.applySnapshotJson(try structuralRowsSnapshot(count: 6, generation: 3,
2229
2530
  transitionID: "drag", durationMs: 800, disclosureExpanded: false))
2230
- wait(for: [collapsePhase], timeout: 1.5)
2531
+ wait(for: [collapsePhase], timeout: 5)
2231
2532
  guard collapseObserved else {
2232
2533
  XCTFail("No partial disclosure collapse frame observed: \(collapseLast)")
2233
2534
  return
@@ -2559,8 +2860,10 @@ final class NativeListLifecycleTests: XCTestCase {
2559
2860
  view.setContainerSlotHeightsJson("[0,0,60,0]")
2560
2861
  let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
2561
2862
  list.contentInsetAdjustmentBehavior = .never
2562
- view.applySnapshotJson(try structuralRowsSnapshot(count: 1000, generation: 1,
2563
- uniformHeight: 1))
2863
+ // Six retained rows plus 129 retiring rows still exceed the 128-host limit
2864
+ // inside three viewports, without materializing hundreds of 1-point cells.
2865
+ view.applySnapshotJson(try structuralRowsSnapshot(count: 135, generation: 1,
2866
+ uniformHeight: 10))
2564
2867
  view.layoutIfNeeded()
2565
2868
  list.layoutIfNeeded()
2566
2869
  drainMainQueue()
@@ -2573,7 +2876,7 @@ final class NativeListLifecycleTests: XCTestCase {
2573
2876
  }
2574
2877
  defer { NotificationCenter.default.removeObserver(observer) }
2575
2878
  view.applySnapshotJson(try structuralRowsSnapshot(count: 6, generation: 2,
2576
- transitionID: "tail-host-limit", durationMs: 400, uniformHeight: 1))
2879
+ transitionID: "tail-host-limit", durationMs: 400, uniformHeight: 10))
2577
2880
  list.layoutIfNeeded()
2578
2881
  let source = try XCTUnwrap(list.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
2579
2882
  let flow = try XCTUnwrap(list.collectionViewLayout as? NativeListFlowLayout)
@@ -2610,22 +2913,41 @@ final class NativeListLifecycleTests: XCTestCase {
2610
2913
  list.layoutIfNeeded()
2611
2914
  view.applySnapshotJson(try structuralRowsSnapshot(count: 50, generation: 2,
2612
2915
  transitionID: "tail-6-to-50", durationMs: 400))
2613
- waitForStructuralFrames(0.16)
2614
2916
  list.layoutIfNeeded()
2615
2917
  let source = try XCTUnwrap(list.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
2616
2918
  let flow = try XCTUnwrap(list.collectionViewLayout as? NativeListFlowLayout)
2617
2919
  XCTAssertEqual(source.snapshot().itemIdentifiers, (0..<50).map { "row-\($0)" })
2618
- XCTAssertGreaterThan(try XCTUnwrap(flow.tailClip).endY, 360)
2619
- XCTAssertLessThan(try XCTUnwrap(flow.tailClip).endY, 3000)
2620
- let suffixCells = list.visibleCells.filter {
2621
- (list.indexPath(for: $0)?.item ?? -1) >= 6
2622
- }
2623
- XCTAssertFalse(suffixCells.isEmpty)
2624
- XCTAssertTrue(suffixCells.allSatisfy {
2625
- !$0.isUserInteractionEnabled && $0.accessibilityElementsHidden
2626
- })
2627
- XCTAssertEqual(list.contentOffset.y, 1000, accuracy: 1)
2628
- waitForStructuralFrames(0.3)
2920
+ var checkedActiveTail = false
2921
+ func checkActiveTail() {
2922
+ guard let clip = flow.tailClip else { return }
2923
+ checkedActiveTail = true
2924
+ list.layoutIfNeeded()
2925
+ XCTAssertGreaterThanOrEqual(clip.endY, 360)
2926
+ XCTAssertLessThanOrEqual(clip.endY, 3000)
2927
+ let suffixCells = list.visibleCells.filter {
2928
+ (list.indexPath(for: $0)?.item ?? -1) >= 6
2929
+ }
2930
+ if clip.endY > list.contentOffset.y + 60 {
2931
+ XCTAssertFalse(suffixCells.isEmpty)
2932
+ }
2933
+ XCTAssertTrue(suffixCells.allSatisfy {
2934
+ !$0.isUserInteractionEnabled && $0.accessibilityElementsHidden
2935
+ })
2936
+ XCTAssertEqual(list.contentOffset.y, 1000, accuracy: 1)
2937
+ }
2938
+ // Inspect actual active frames instead of assuming a delayed callback lands
2939
+ // inside a 400 ms animation on a busy CI runner.
2940
+ checkActiveTail()
2941
+ let probe = Timer(timeInterval: 1.0 / 60, repeats: true) { _ in checkActiveTail() }
2942
+ RunLoop.main.add(probe, forMode: .common)
2943
+ defer { probe.invalidate() }
2944
+ let finished = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in
2945
+ flow.tailClip == nil &&
2946
+ Mirror(reflecting: view).descendant("structuralDisplayLink") as? CADisplayLink == nil
2947
+ }, object: nil)
2948
+ wait(for: [finished], timeout: 5)
2949
+ probe.invalidate()
2950
+ XCTAssertTrue(checkedActiveTail)
2629
2951
  list.layoutIfNeeded()
2630
2952
  XCTAssertEqual(source.snapshot().itemIdentifiers, (0..<50).map { "row-\($0)" })
2631
2953
  XCTAssertEqual(list.contentSize.height, 3060, accuracy: 1)
@@ -2715,8 +3037,8 @@ final class NativeListLifecycleTests: XCTestCase {
2715
3037
  drainMainQueue()
2716
3038
  list.contentOffset.y = 300
2717
3039
  list.layoutIfNeeded()
2718
- view.applySnapshotJson(try structuralRowsSnapshot(count: 50, generation: 2,
2719
- transitionID: "interrupt-boundary", durationMs: 1200))
3040
+ view.applySnapshotJson(try structuralRowsSnapshot(count: 15, generation: 2,
3041
+ transitionID: "interrupt-boundary", durationMs: 2400))
2720
3042
  let footerBottomBeforeResize = footer.convert(.zero, to: list).y + 60
2721
3043
  view.setContainerSlotHeightsJson("[0,0,80,2200]")
2722
3044
  XCTAssertEqual(footer.convert(.zero, to: list).y + 80,
@@ -2730,7 +3052,7 @@ final class NativeListLifecycleTests: XCTestCase {
2730
3052
  XCTAssertFalse(footerHost.point(inside: CGPoint(x: 10, y: hiddenTop / 2), with: nil))
2731
3053
  XCTAssertTrue(footerHost.point(inside: CGPoint(x: 10, y: hiddenTop + 1), with: nil))
2732
3054
  var boundaryCell: NativeListRowHost?
2733
- let deadline = Date().addingTimeInterval(1)
3055
+ let deadline = Date().addingTimeInterval(3)
2734
3056
  while boundaryCell == nil && Date() < deadline {
2735
3057
  if let clip = flow.tailClip {
2736
3058
  for case let cell as NativeListRowHost in list.visibleCells {
@@ -3012,9 +3334,13 @@ final class NativeListLifecycleTests: XCTestCase {
3012
3334
  }
3013
3335
 
3014
3336
  private func waitForStructuralFrames(_ seconds: TimeInterval) {
3015
- let completed = expectation(description: "Native structural display link ran for \(seconds)s")
3016
- DispatchQueue.main.asyncAfter(deadline: .now() + seconds) { completed.fulfill() }
3017
- wait(for: [completed], timeout: seconds + 1)
3337
+ let deadline = Date().addingTimeInterval(seconds)
3338
+ while Date() < deadline {
3339
+ RunLoop.main.run(until: Date().addingTimeInterval(0.01))
3340
+ }
3341
+ for _ in 0..<4 {
3342
+ RunLoop.main.run(until: Date().addingTimeInterval(0.02))
3343
+ }
3018
3344
  }
3019
3345
 
3020
3346
  private func labels(in view: UIView) -> [UILabel] {
@@ -3183,14 +3509,17 @@ final class NativeListLifecycleTests: XCTestCase {
3183
3509
  }
3184
3510
 
3185
3511
  private func drainMainQueue() {
3186
- let settled = expectation(description: "Deferred content shrink correction")
3187
- DispatchQueue.main.async { settled.fulfill() }
3188
- wait(for: [settled], timeout: 1)
3512
+ var drained = false
3513
+ DispatchQueue.main.async { drained = true }
3514
+ let deadline = Date().addingTimeInterval(5)
3515
+ while !drained && Date() < deadline {
3516
+ RunLoop.main.run(until: Date().addingTimeInterval(0.01))
3517
+ }
3189
3518
  }
3190
3519
 
3191
3520
  private func footerCell(in view: NativeListView) -> NativeListRowHost? {
3192
- view.subviews.lazy.compactMap { container in
3193
- container.subviews.compactMap { $0 as? NativeListRowHost }.first
3521
+ view.subviews.lazy.filter { !($0 is UICollectionView) }.compactMap { container in
3522
+ container.subviews.first { $0 is NativeListRowHost } as? NativeListRowHost
3194
3523
  }.first
3195
3524
  }
3196
3525
  }
@@ -44,20 +44,29 @@ final class NativeListMediaVisibilityTests: XCTestCase {
44
44
  @MainActor
45
45
  final class NativeListMediaFrameCaptureTests: XCTestCase {
46
46
  func testVisibleVideoReleasesPlayerAfterCapturingFirstFrame() throws {
47
+ XCTAssertEqual(UIApplication.shared.applicationState, .active, "Video capture requires a foreground app host")
47
48
  let url = try writeVideo()
48
49
  defer { try? FileManager.default.removeItem(at: url) }
49
- let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
50
- window.rootViewController = UIViewController()
51
- window.isHidden = false
52
- defer { window.isHidden = true }
50
+ let sceneWindow = UIApplication.shared.connectedScenes
51
+ .compactMap { $0 as? UIWindowScene }
52
+ .first(where: { $0.activationState == .foregroundActive })?
53
+ .windows.first(where: { $0.isKeyWindow })
54
+ let window = try XCTUnwrap(sceneWindow ?? UIApplication.shared.delegate?.window ?? nil)
55
+ let rootView = try XCTUnwrap(window.rootViewController?.view)
56
+ XCTAssertFalse(window.isHidden, "Video capture requires a visible app window")
57
+ let container = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
58
+ rootView.addSubview(container)
59
+ defer { container.removeFromSuperview() }
53
60
  let slot = NativeListMediaPreviewSlot()
54
61
  slot.view.frame = CGRect(x: 0, y: 0, width: 120, height: 120)
55
- window.rootViewController?.view.addSubview(slot.view)
62
+ container.addSubview(slot.view)
56
63
  slot.view.layoutIfNeeded()
64
+ XCTAssertTrue(slot.view.window === window)
65
+ XCTAssertTrue(NativeListMediaPreviewSlot.intersectsViewport(slot.view))
57
66
 
58
67
  var loaded: Bool?
59
68
  slot.bind(["uri": url.absoluteString], key: "video", probeOrder: ["video"]) { loaded = $0 }
60
- XCTAssertTrue(slot.hasLivePlayer, "A visible tile starts one paused player")
69
+ // The cache lookup precedes player creation and can complete asynchronously.
61
70
  let deadline = Date().addingTimeInterval(10)
62
71
  while slot.capturedFrame == nil, Date() < deadline {
63
72
  RunLoop.main.run(until: Date().addingTimeInterval(0.05))
@@ -68,13 +77,38 @@ final class NativeListMediaFrameCaptureTests: XCTestCase {
68
77
  XCTAssertFalse(slot.hasLivePlayer)
69
78
 
70
79
  // Leaving and re-entering the viewport keeps the still without rebuilding a player.
80
+ let playersBeforeDetach = slot.createdPlayerCount
71
81
  slot.view.removeFromSuperview()
72
- window.rootViewController?.view.addSubview(slot.view)
82
+ XCTAssertTrue(slot.capturedFrame === frame, "Detaching has no replacement screen scale")
83
+ XCTAssertEqual(slot.createdPlayerCount, playersBeforeDetach)
84
+ container.addSubview(slot.view)
73
85
  slot.bind(["uri": url.absoluteString], key: "video", probeOrder: ["video"])
86
+ XCTAssertTrue(slot.capturedFrame === frame, "The same window retains the same tier")
87
+ XCTAssertEqual(slot.createdPlayerCount, playersBeforeDetach)
74
88
  XCTAssertFalse(slot.hasLivePlayer)
75
89
  XCTAssertNotNil(slot.capturedFrame)
76
90
  slot.recycle()
77
91
  XCTAssertNil(slot.capturedFrame)
92
+ let coldPlayers = slot.createdPlayerCount
93
+ slot.bind(["uri": url.absoluteString], key: "different-row", probeOrder: ["video"])
94
+ let warmDeadline = Date().addingTimeInterval(3)
95
+ while slot.capturedFrame == nil, Date() < warmDeadline {
96
+ RunLoop.main.run(until: Date().addingTimeInterval(0.01))
97
+ }
98
+ XCTAssertNotNil(slot.capturedFrame)
99
+ XCTAssertEqual(slot.createdPlayerCount, coldPlayers, "Recycled row reuses the cached still")
100
+ let second = NativeListMediaPreviewSlot()
101
+ second.view.frame = CGRect(x: 120, y: 0, width: 120, height: 120)
102
+ container.addSubview(second.view)
103
+ second.view.layoutIfNeeded()
104
+ second.bind(["uri": url.absoluteString], key: "another-cell", probeOrder: ["video"])
105
+ let crossCellDeadline = Date().addingTimeInterval(3)
106
+ while second.capturedFrame == nil, Date() < crossCellDeadline {
107
+ RunLoop.main.run(until: Date().addingTimeInterval(0.01))
108
+ }
109
+ XCTAssertNotNil(second.capturedFrame)
110
+ XCTAssertEqual(second.createdPlayerCount, 0, "A different cell uses shared cached pixels")
111
+ second.recycle(); slot.recycle()
78
112
  }
79
113
 
80
114
  /// A 64×48, ten-frame H.264 MP4 (generated with AVAssetWriter on macOS); the simulator