@onekeyfe/react-native-native-list 6.7.0-alpha.344 → 6.7.0-alpha.359
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/android/src/main/java/com/onekey/nativelist/HybridNativeList.kt +7 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListAccessoryStack.kt +240 -7
- package/android/src/main/java/com/onekey/nativelist/NativeListActivityDetailsView.kt +7 -4
- package/android/src/main/java/com/onekey/nativelist/NativeListActivityRenderer.kt +6 -3
- package/android/src/main/java/com/onekey/nativelist/NativeListButtonGroupRenderer.kt +102 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListContainerSlotAdapter.kt +12 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListDataRowRenderer.kt +87 -7
- package/android/src/main/java/com/onekey/nativelist/NativeListIdentityRenderer.kt +135 -1
- package/android/src/main/java/com/onekey/nativelist/NativeListLeadingVisual.kt +43 -14
- package/android/src/main/java/com/onekey/nativelist/NativeListMessageRenderer.kt +25 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListModels.kt +17 -2
- package/android/src/main/java/com/onekey/nativelist/NativeListPrimitives.kt +22 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListRendererRegistry.kt +2 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListRendererRowView.kt +2 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListResolvedText.kt +3 -2
- package/android/src/main/java/com/onekey/nativelist/NativeListSectionHeaderRenderer.kt +69 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListTextView.kt +55 -1
- package/android/src/main/java/com/onekey/nativelist/NativeListView.kt +585 -21
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListPressPolicyTest.kt +1 -0
- package/docs/ROW_TEMPLATES.md +60 -9
- package/docs/SPEC.md +361 -8
- package/docs/STYLE_SPEC.md +6 -0
- package/ios/HybridNativeList.swift +10 -0
- package/ios/NativeListAccessoryStack.swift +220 -37
- package/ios/NativeListActivityDetailsView.swift +18 -8
- package/ios/NativeListActivityRenderer.swift +8 -2
- package/ios/NativeListButtonGroupRenderer.swift +89 -0
- package/ios/NativeListDataRowRenderer.swift +128 -16
- package/ios/NativeListDesignAssets.swift +3 -0
- package/ios/NativeListIdentityRenderer.swift +123 -5
- package/ios/NativeListLeadingVisual.swift +49 -14
- package/ios/NativeListMessageRenderer.swift +34 -1
- package/ios/NativeListModels.swift +16 -2
- package/ios/NativeListRendererRegistry.swift +1 -0
- package/ios/NativeListResolvedText.swift +2 -1
- package/ios/NativeListRowHost.swift +14 -0
- package/ios/NativeListSectionHeaderRenderer.swift +88 -0
- package/ios/RNCNativeListView.swift +335 -29
- package/ios/Resources/NativeListIcons.xcassets/onekey_crypto_coin_outline.imageset/Contents.json +15 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_crypto_coin_outline.imageset/icon.svg +4 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_gas_solid.imageset/Contents.json +15 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_gas_solid.imageset/icon.svg +3 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_refresh_ccw_outline.imageset/Contents.json +15 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_refresh_ccw_outline.imageset/icon.svg +3 -0
- package/ios/tests/NativeListActivityPresentationTests.swift +56 -0
- package/ios/tests/NativeListLifecycleTests.swift +1283 -3
- package/lib/module/NativeList.js +64 -27
- package/lib/module/NativeList.web.js +84 -17
- package/lib/module/models.js +1 -0
- package/lib/module/selection.js +1 -1
- package/lib/module/validation.js +532 -18
- package/lib/module/web/NativeListWebEngine.js +391 -24
- package/lib/module/web/templates/ActivityRowRenderer.js +8 -7
- package/lib/module/web/templates/ButtonGroupRowRenderer.js +47 -0
- package/lib/module/web/templates/DataRowRenderer.js +40 -5
- package/lib/module/web/templates/IdentityRowRenderer.js +90 -4
- package/lib/module/web/templates/MessageRowRenderer.js +18 -2
- package/lib/module/web/templates/RowContainerStyle.js +1 -0
- package/lib/module/web/templates/RowRendererRegistry.js +3 -0
- package/lib/module/web/templates/SectionHeaderRowRenderer.js +36 -2
- package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +9 -0
- package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +2 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +5 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +6 -0
- package/lib/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +7 -0
- package/lib/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +6 -0
- package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +1 -0
- package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +11 -0
- package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +2 -0
- package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +2 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +2 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +3 -0
- package/lib/nitrogen/generated/shared/json/NativeListConfig.json +1 -0
- package/lib/typescript/src/NativeList.d.ts +2 -2
- package/lib/typescript/src/NativeList.nitro.d.ts +1 -0
- package/lib/typescript/src/NativeList.types.d.ts +8 -0
- package/lib/typescript/src/models.d.ts +101 -9
- package/lib/typescript/src/validation.d.ts +13 -0
- package/lib/typescript/src/web/NativeListWebEngine.d.ts +26 -0
- package/lib/typescript/src/web/templates/ButtonGroupRowRenderer.d.ts +14 -0
- package/lib/typescript/src/web/templates/IdentityRowRenderer.d.ts +1 -1
- package/lib/typescript/src/web/templates/MessageRowRenderer.d.ts +9 -3
- package/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +9 -0
- package/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +2 -0
- package/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +5 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +6 -0
- package/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +7 -0
- package/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +6 -0
- package/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +1 -0
- package/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +11 -0
- package/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +2 -0
- package/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +2 -0
- package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +2 -0
- package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +3 -0
- package/nitrogen/generated/shared/json/NativeListConfig.json +1 -0
- package/package.json +3 -3
- package/react-native-native-list.podspec +1 -0
- package/src/NativeList.nitro.ts +1 -0
- package/src/NativeList.tsx +155 -69
- package/src/NativeList.types.ts +9 -0
- package/src/NativeList.web.tsx +159 -49
- package/src/models.ts +113 -3
- package/src/selection.ts +1 -0
- package/src/validation.ts +1156 -18
- package/src/web/NativeListWebEngine.ts +594 -28
- package/src/web/templates/ActivityRowRenderer.ts +14 -6
- package/src/web/templates/ButtonGroupRowRenderer.ts +58 -0
- package/src/web/templates/DataRowRenderer.ts +93 -20
- package/src/web/templates/IdentityRowRenderer.ts +150 -5
- package/src/web/templates/MessageRowRenderer.ts +23 -7
- package/src/web/templates/RowContainerStyle.ts +2 -0
- package/src/web/templates/RowRendererRegistry.ts +11 -1
- package/src/web/templates/SectionHeaderRowRenderer.ts +47 -4
|
@@ -15,7 +15,7 @@ final class NativeListLifecycleTests: XCTestCase {
|
|
|
15
15
|
let originalStyle = list.topEdgeEffect.style
|
|
16
16
|
let originalInset = list.contentInset.top
|
|
17
17
|
let overlay = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 64))
|
|
18
|
-
view.mountContainerSlot(overlay, atIndex:
|
|
18
|
+
view.mountContainerSlot(overlay, atIndex: 4)
|
|
19
19
|
view.setTopScrollEdge(effect: .hard, overlayHeight: 64)
|
|
20
20
|
XCTAssertEqual(list.topEdgeEffect.style, .hard)
|
|
21
21
|
XCTAssertEqual(list.contentInset.top, originalInset + 64, accuracy: 0.01)
|
|
@@ -153,6 +153,571 @@ final class NativeListLifecycleTests: XCTestCase {
|
|
|
153
153
|
XCTAssertNotEqual(cell.bindingEpoch, changedEpoch)
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
+
func testRiskIdentityTitleIconAndButtonResetOnProductionCellBind() throws {
|
|
157
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 320, height: 56))
|
|
158
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 200))
|
|
159
|
+
window.addSubview(cell)
|
|
160
|
+
let titleIcon = try XCTUnwrap(
|
|
161
|
+
Mirror(reflecting: cell).children.first { $0.label == "titleIconView" }?.value as? UIImageView)
|
|
162
|
+
let titleStack = try XCTUnwrap(
|
|
163
|
+
Mirror(reflecting: cell).children.first { $0.label == "titleRowStack" }?.value as? UIStackView)
|
|
164
|
+
let titleLabel = try XCTUnwrap(
|
|
165
|
+
Mirror(reflecting: cell).children.first { $0.label == "titleLabel" }?.value as? UILabel)
|
|
166
|
+
let trailing = try XCTUnwrap(
|
|
167
|
+
Mirror(reflecting: cell).children.first { $0.label == "trailingStack" }?.value
|
|
168
|
+
as? NativeListAccessoryStack)
|
|
169
|
+
func bind(_ data: [String: Any]) throws {
|
|
170
|
+
let item = try NativeListItem(data: data)
|
|
171
|
+
cell.bind(item: item, theme: nil, layout: "linear", selected: false,
|
|
172
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
173
|
+
cell.layoutIfNeeded()
|
|
174
|
+
}
|
|
175
|
+
let base: [String: Any] = ["key": "risk", "type": "identity", "title": "Risk",
|
|
176
|
+
"height": 56, "leading": ["kind": "token",
|
|
177
|
+
"fallbackIcon": ["name": "CryptoCoinOutline"]]]
|
|
178
|
+
try bind(base)
|
|
179
|
+
XCTAssertTrue(titleIcon.isHidden)
|
|
180
|
+
XCTAssertLessThanOrEqual(titleIcon.bounds.width, 0.5)
|
|
181
|
+
try bind(base.merging(["titleIcon": ["name": "GasSolid"],
|
|
182
|
+
"trailing": [["kind": "button", "label": "Show", "actionKey": "show",
|
|
183
|
+
"backgroundColor": "#DDDDDD", "pressedBackgroundColor": "#AAAAAA"]]]) { _, newer in newer })
|
|
184
|
+
XCTAssertFalse(titleIcon.isHidden)
|
|
185
|
+
XCTAssertNotNil(titleIcon.image)
|
|
186
|
+
XCTAssertEqual(titleIcon.bounds.width, 20, accuracy: 0.5)
|
|
187
|
+
XCTAssertEqual(titleStack.customSpacing(after: titleLabel), 4, accuracy: 0.1)
|
|
188
|
+
let button = try XCTUnwrap(trailing.arrangedSubviews.compactMap { $0 as? NativeListAccessoryButton }
|
|
189
|
+
.first { !$0.isHidden })
|
|
190
|
+
XCTAssertEqual(button.intrinsicContentSize.height, 30, accuracy: 0.5)
|
|
191
|
+
let normal = button.backgroundColor
|
|
192
|
+
button.isHighlighted = true
|
|
193
|
+
XCTAssertNotEqual(button.backgroundColor, normal)
|
|
194
|
+
button.isHighlighted = false
|
|
195
|
+
XCTAssertEqual(button.backgroundColor, normal)
|
|
196
|
+
try bind(base)
|
|
197
|
+
XCTAssertTrue(titleIcon.isHidden)
|
|
198
|
+
XCTAssertLessThanOrEqual(titleIcon.bounds.width, 0.5)
|
|
199
|
+
XCTAssertTrue(trailing.arrangedSubviews.compactMap { $0 as? NativeListAccessoryButton }
|
|
200
|
+
.allSatisfy { $0.isHidden })
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
func testIdentityTitleSuffixesUsePlainLabelsAndClearOnRebind() throws {
|
|
204
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 320, height: 60))
|
|
205
|
+
let titleStack = try XCTUnwrap(
|
|
206
|
+
Mirror(reflecting: cell).children.first { $0.label == "titleRowStack" }?.value as? UIStackView)
|
|
207
|
+
func bind(_ suffixes: [[String: Any]]?) throws {
|
|
208
|
+
var data: [String: Any] = [
|
|
209
|
+
"key": "recent", "type": "identity", "title": "Alice",
|
|
210
|
+
"leading": ["kind": "account", "fallbackText": "A"],
|
|
211
|
+
]
|
|
212
|
+
if let suffixes { data["titleSuffixes"] = suffixes }
|
|
213
|
+
let item = try NativeListItem(data: data)
|
|
214
|
+
cell.bind(item: item, theme: nil, layout: "linear", selected: false,
|
|
215
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
216
|
+
cell.layoutIfNeeded()
|
|
217
|
+
}
|
|
218
|
+
try bind([
|
|
219
|
+
["text": "EVM", "tone": "secondary", "maxWidth": 128],
|
|
220
|
+
["text": "2 days ago", "tone": "disabled"],
|
|
221
|
+
])
|
|
222
|
+
let suffixes = titleStack.arrangedSubviews.compactMap { $0 as? UILabel }
|
|
223
|
+
.filter { ["EVM", "2 days ago"].contains($0.text ?? "") }
|
|
224
|
+
XCTAssertEqual(suffixes.map(\.text), ["EVM", "2 days ago"])
|
|
225
|
+
XCTAssertTrue(suffixes.allSatisfy { $0.font.pointSize == 12 && $0.numberOfLines == 1 })
|
|
226
|
+
XCTAssertFalse(suffixes.contains { $0 is NativeListInsetLabel })
|
|
227
|
+
try bind(nil)
|
|
228
|
+
XCTAssertFalse(titleStack.arrangedSubviews.compactMap { $0 as? UILabel }
|
|
229
|
+
.contains { ["EVM", "2 days ago"].contains($0.text ?? "") })
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
func testDataRowTitleIconsClearWhenCellIsRebound() throws {
|
|
233
|
+
let cell = NativeListDataRowCell(frame: CGRect(x: 0, y: 0, width: 380, height: 60))
|
|
234
|
+
func bind(_ column: [String: Any]) throws {
|
|
235
|
+
let item = try NativeListItem(data: [
|
|
236
|
+
"key": "asset", "type": "dataRow", "columns": [column],
|
|
237
|
+
])
|
|
238
|
+
cell.bind(item: item, theme: nil, layout: "table", selected: false,
|
|
239
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
240
|
+
cell.layoutIfNeeded()
|
|
241
|
+
}
|
|
242
|
+
func titleIcons(in view: UIView) -> [UIImageView] {
|
|
243
|
+
let current = (view as? UIImageView).map { [$0] } ?? []
|
|
244
|
+
return current + view.subviews.flatMap(titleIcons)
|
|
245
|
+
}
|
|
246
|
+
try bind([
|
|
247
|
+
"key": "asset", "text": "ETH",
|
|
248
|
+
"titleIcon": ["name": "GasSolid", "accessibilityLabel": "Gas help"],
|
|
249
|
+
"secondaryTitleIcon": ["name": "InfoCircleOutline", "accessibilityLabel": "Info help"],
|
|
250
|
+
])
|
|
251
|
+
let visible = titleIcons(in: cell).filter { !$0.isHidden && $0.accessibilityLabel != nil }
|
|
252
|
+
XCTAssertEqual(visible.compactMap(\.accessibilityLabel), ["Gas help", "Info help"])
|
|
253
|
+
XCTAssertTrue(visible.allSatisfy { $0.image != nil })
|
|
254
|
+
|
|
255
|
+
try bind(["key": "asset", "text": "USDC"])
|
|
256
|
+
XCTAssertTrue(titleIcons(in: cell).allSatisfy {
|
|
257
|
+
$0.accessibilityLabel != "Gas help" && $0.accessibilityLabel != "Info help"
|
|
258
|
+
})
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
func testIdentityTrailingActionUsesDescriptorSlotAfterCheckboxAndRebind() throws {
|
|
262
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 386, height: 200))
|
|
263
|
+
let controller = UIViewController()
|
|
264
|
+
window.rootViewController = controller
|
|
265
|
+
window.isHidden = false
|
|
266
|
+
defer { window.isHidden = true }
|
|
267
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 386, height: 56))
|
|
268
|
+
controller.view.addSubview(cell)
|
|
269
|
+
let trailing = try XCTUnwrap(
|
|
270
|
+
Mirror(reflecting: cell).children.first { $0.label == "trailingStack" }?.value
|
|
271
|
+
as? NativeListAccessoryStack)
|
|
272
|
+
var actions: [(rowKey: String, key: String, source: String, slot: Int?)] = []
|
|
273
|
+
cell.onAction = { item, key, _, origin in
|
|
274
|
+
actions.append((item.key, key, origin?.source ?? "", origin?.slot))
|
|
275
|
+
}
|
|
276
|
+
func bind(_ trailingData: [[String: Any]], key: String = "row") throws {
|
|
277
|
+
let item = try NativeListItem(data: [
|
|
278
|
+
"key": key, "type": "identity", "title": "Row", "trailing": trailingData,
|
|
279
|
+
])
|
|
280
|
+
cell.bind(item: item, theme: nil, layout: "linear", selected: false,
|
|
281
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
282
|
+
window.layoutIfNeeded()
|
|
283
|
+
cell.layoutIfNeeded()
|
|
284
|
+
}
|
|
285
|
+
func visibleButtons() -> [NativeListAccessoryButton] {
|
|
286
|
+
trailing.arrangedSubviews.compactMap { $0 as? NativeListAccessoryButton }
|
|
287
|
+
.filter { !$0.isHidden }
|
|
288
|
+
}
|
|
289
|
+
let checkbox: [String: Any] = [
|
|
290
|
+
"kind": "checkbox", "state": "unchecked", "actionKey": "selection",
|
|
291
|
+
]
|
|
292
|
+
let first: [String: Any] = ["kind": "button", "label": "First", "actionKey": "first"]
|
|
293
|
+
let second: [String: Any] = ["kind": "button", "label": "Second", "actionKey": "second"]
|
|
294
|
+
|
|
295
|
+
try bind([checkbox, second])
|
|
296
|
+
XCTAssertEqual(visibleButtons().count, 1)
|
|
297
|
+
XCTAssertTrue(visibleButtons()[0].isEnabled)
|
|
298
|
+
XCTAssertTrue(visibleButtons()[0].isUserInteractionEnabled)
|
|
299
|
+
XCTAssertTrue(visibleButtons()[0].allTargets.contains(trailing))
|
|
300
|
+
XCTAssertNotNil(trailing.onAction)
|
|
301
|
+
visibleButtons()[0].sendActions(for: .touchUpInside)
|
|
302
|
+
XCTAssertEqual(actions.last?.rowKey, "row")
|
|
303
|
+
XCTAssertEqual(actions.last?.key, "second")
|
|
304
|
+
XCTAssertEqual(actions.last?.source, "trailingAccessory")
|
|
305
|
+
XCTAssertEqual(actions.last?.slot, 1)
|
|
306
|
+
let selection = try XCTUnwrap(trailing.arrangedSubviews.compactMap { $0 as? UIButton }
|
|
307
|
+
.first { !($0 is NativeListAccessoryButton) && !$0.isHidden })
|
|
308
|
+
selection.sendActions(for: .touchUpInside)
|
|
309
|
+
XCTAssertEqual(actions.last?.key, "selection")
|
|
310
|
+
XCTAssertNil(actions.last?.slot)
|
|
311
|
+
|
|
312
|
+
try bind([first, second])
|
|
313
|
+
XCTAssertEqual(visibleButtons().count, 2)
|
|
314
|
+
visibleButtons()[0].sendActions(for: .touchUpInside)
|
|
315
|
+
XCTAssertEqual(actions.last?.key, "first")
|
|
316
|
+
XCTAssertEqual(actions.last?.slot, 0)
|
|
317
|
+
visibleButtons()[1].sendActions(for: .touchUpInside)
|
|
318
|
+
XCTAssertEqual(actions.last?.key, "second")
|
|
319
|
+
XCTAssertEqual(actions.last?.slot, 1)
|
|
320
|
+
|
|
321
|
+
try bind([second], key: "replacement")
|
|
322
|
+
XCTAssertEqual(visibleButtons().count, 1)
|
|
323
|
+
visibleButtons()[0].sendActions(for: .touchUpInside)
|
|
324
|
+
XCTAssertEqual(actions.last?.rowKey, "replacement")
|
|
325
|
+
XCTAssertEqual(actions.last?.key, "second")
|
|
326
|
+
XCTAssertEqual(actions.last?.slot, 0)
|
|
327
|
+
XCTAssertEqual(actions.count, 5)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
func testIdentityTertiaryIconAnchorInsetSurvivesAccessoryRebind() throws {
|
|
331
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 386, height: 200))
|
|
332
|
+
let controller = UIViewController()
|
|
333
|
+
window.rootViewController = controller
|
|
334
|
+
window.isHidden = false
|
|
335
|
+
defer { window.isHidden = true }
|
|
336
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 386, height: 88))
|
|
337
|
+
controller.view.addSubview(cell)
|
|
338
|
+
let trailing = try XCTUnwrap(
|
|
339
|
+
Mirror(reflecting: cell).children.first { $0.label == "trailingStack" }?.value
|
|
340
|
+
as? NativeListAccessoryStack)
|
|
341
|
+
var actions: [(rowKey: String, source: String, slot: Int?, inset: CGFloat,
|
|
342
|
+
width: CGFloat)] = []
|
|
343
|
+
cell.onAction = { item, _, _, origin in
|
|
344
|
+
actions.append((item.key, origin?.source ?? "", origin?.slot, origin?.anchorInset ?? -1,
|
|
345
|
+
origin?.sourceView?.bounds.width ?? -1))
|
|
346
|
+
}
|
|
347
|
+
func bind(_ descriptors: [[String: Any]], key: String,
|
|
348
|
+
presentation: String? = nil) throws -> NativeListAccessoryButton {
|
|
349
|
+
var data: [String: Any] = [
|
|
350
|
+
"key": key, "type": "identity", "title": "Row", "height": 88,
|
|
351
|
+
"trailing": descriptors,
|
|
352
|
+
]
|
|
353
|
+
if let presentation { data["presentation"] = presentation }
|
|
354
|
+
cell.bind(item: try NativeListItem(data: data), theme: nil, layout: "linear",
|
|
355
|
+
selected: false, checkboxState: { _, _, fallback in fallback },
|
|
356
|
+
fonts: NativeListFontFamilies())
|
|
357
|
+
window.layoutIfNeeded()
|
|
358
|
+
cell.layoutIfNeeded()
|
|
359
|
+
return try XCTUnwrap(trailing.firstVisibleControl as? NativeListAccessoryButton)
|
|
360
|
+
}
|
|
361
|
+
let tertiary: [String: Any] = [
|
|
362
|
+
"kind": "icon", "name": "CryptoCoinOutline", "buttonStyle": "tertiary",
|
|
363
|
+
"actionKey": "menu",
|
|
364
|
+
]
|
|
365
|
+
let ordinary: [String: Any] = [
|
|
366
|
+
"kind": "icon", "name": "CryptoCoinOutline", "actionKey": "menu",
|
|
367
|
+
]
|
|
368
|
+
|
|
369
|
+
let first = try bind([
|
|
370
|
+
["kind": "checkbox", "state": "unchecked", "actionKey": "selection"], tertiary,
|
|
371
|
+
], key: "tertiary-first")
|
|
372
|
+
XCTAssertEqual(first.bounds.width, 38, accuracy: 0.5)
|
|
373
|
+
first.sendActions(for: .touchUpInside)
|
|
374
|
+
XCTAssertEqual(actions.last?.rowKey, "tertiary-first")
|
|
375
|
+
XCTAssertEqual(actions.last?.source, "trailingAccessory")
|
|
376
|
+
XCTAssertEqual(actions.last?.slot, 1)
|
|
377
|
+
XCTAssertEqual(actions.last?.inset, 7)
|
|
378
|
+
let firstOrigin = try XCTUnwrap(actions.last)
|
|
379
|
+
XCTAssertEqual(firstOrigin.width - 2 * firstOrigin.inset, 24, accuracy: 0.5)
|
|
380
|
+
|
|
381
|
+
let normal = try bind([ordinary], key: "ordinary")
|
|
382
|
+
XCTAssertEqual(normal.bounds.width, 36, accuracy: 0.5)
|
|
383
|
+
normal.sendActions(for: .touchUpInside)
|
|
384
|
+
XCTAssertEqual(actions.last?.rowKey, "ordinary")
|
|
385
|
+
XCTAssertEqual(actions.last?.slot, 0)
|
|
386
|
+
XCTAssertEqual(actions.last?.inset, 0)
|
|
387
|
+
XCTAssertEqual(actions.last?.width, 36)
|
|
388
|
+
|
|
389
|
+
let value = try bind([["kind": "value", "text": "Value"]], key: "value")
|
|
390
|
+
value.bounds.size.width = 38
|
|
391
|
+
XCTAssertEqual(trailing.anchorInset(for: value), 0)
|
|
392
|
+
|
|
393
|
+
let rebound = try bind([tertiary], key: "tertiary-again")
|
|
394
|
+
rebound.sendActions(for: .touchUpInside)
|
|
395
|
+
XCTAssertEqual(actions.last?.rowKey, "tertiary-again")
|
|
396
|
+
XCTAssertEqual(actions.last?.slot, 0)
|
|
397
|
+
XCTAssertEqual(actions.last?.inset, 7)
|
|
398
|
+
let reboundOrigin = try XCTUnwrap(actions.last)
|
|
399
|
+
XCTAssertEqual(reboundOrigin.width - 2 * reboundOrigin.inset, 24, accuracy: 0.5)
|
|
400
|
+
|
|
401
|
+
let account = try bind([ordinary], key: "account", presentation: "accountSelector")
|
|
402
|
+
XCTAssertEqual(account.bounds.width, 38, accuracy: 0.5)
|
|
403
|
+
account.sendActions(for: .touchUpInside)
|
|
404
|
+
XCTAssertEqual(actions.last?.rowKey, "account")
|
|
405
|
+
XCTAssertEqual(actions.last?.slot, 0)
|
|
406
|
+
XCTAssertEqual(actions.last?.inset, 7)
|
|
407
|
+
let accountOrigin = try XCTUnwrap(actions.last)
|
|
408
|
+
XCTAssertEqual(accountOrigin.width - 2 * accountOrigin.inset, 24, accuracy: 0.5)
|
|
409
|
+
|
|
410
|
+
let finalOrdinary = try bind([ordinary], key: "ordinary-again")
|
|
411
|
+
finalOrdinary.sendActions(for: .touchUpInside)
|
|
412
|
+
XCTAssertEqual(actions.last?.rowKey, "ordinary-again")
|
|
413
|
+
XCTAssertEqual(actions.last?.inset, 0)
|
|
414
|
+
XCTAssertEqual(actions.count, 5)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
func testValuePairSegmentsStyleAndPlainRebindOnProductionAccessoryStack() throws {
|
|
418
|
+
let stack = NativeListAccessoryStack(frame: CGRect(x: 0, y: 0, width: 180, height: 48))
|
|
419
|
+
let item = try NativeListItem(data: [
|
|
420
|
+
"key": "pair", "type": "identity", "title": "Pair",
|
|
421
|
+
"leading": ["kind": "icon", "name": "coin"],
|
|
422
|
+
])
|
|
423
|
+
let pair: [String: Any] = [
|
|
424
|
+
"kind": "valuePair", "primary": "0.001", "secondary": "$0.01",
|
|
425
|
+
"primaryTextSegments": [["text": "0.00"], ["text": "1", "style": "subscript"]],
|
|
426
|
+
"secondaryTextSegments": [["text": "$0.0"], ["text": "1", "style": "subscript"]],
|
|
427
|
+
]
|
|
428
|
+
func bind(_ descriptor: [String: Any], size: Int? = nil) {
|
|
429
|
+
let style: [String: Any] = size.map { ["value": ["fontSize": $0]] } ?? [:]
|
|
430
|
+
stack.bind(item, descriptors: [descriptor], theme: nil, style: style,
|
|
431
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
432
|
+
}
|
|
433
|
+
func fontSize(_ title: NSAttributedString, at index: Int) throws -> CGFloat {
|
|
434
|
+
try XCTUnwrap(title.attribute(.font, at: index, effectiveRange: nil) as? UIFont).pointSize
|
|
435
|
+
}
|
|
436
|
+
bind(pair)
|
|
437
|
+
let button = try XCTUnwrap(stack.arrangedSubviews.compactMap { $0 as? UIButton }
|
|
438
|
+
.first { !$0.isHidden })
|
|
439
|
+
let rich = try XCTUnwrap(button.attributedTitle(for: .normal))
|
|
440
|
+
XCTAssertEqual(rich.string, "0.001\n$0.01")
|
|
441
|
+
XCTAssertEqual(try fontSize(rich, at: 0), 16)
|
|
442
|
+
XCTAssertEqual(try fontSize(rich, at: 4), 10)
|
|
443
|
+
XCTAssertEqual(try fontSize(rich, at: 6), 14)
|
|
444
|
+
XCTAssertEqual(try fontSize(rich, at: 10), 9)
|
|
445
|
+
bind(pair, size: 20)
|
|
446
|
+
let styled = try XCTUnwrap(button.attributedTitle(for: .normal))
|
|
447
|
+
XCTAssertEqual(try fontSize(styled, at: 0), 20)
|
|
448
|
+
XCTAssertEqual(try fontSize(styled, at: 4), 12)
|
|
449
|
+
XCTAssertEqual(try fontSize(styled, at: 10), 12)
|
|
450
|
+
bind(["kind": "valuePair", "primary": "10", "secondary": "$2"])
|
|
451
|
+
let plain = try XCTUnwrap(button.attributedTitle(for: .normal))
|
|
452
|
+
XCTAssertEqual(plain.string, "10\n$2")
|
|
453
|
+
XCTAssertEqual(try fontSize(plain, at: 0), 16)
|
|
454
|
+
XCTAssertEqual(try fontSize(plain, at: 3), 14)
|
|
455
|
+
bind(["kind": "valuePair", "primary": "10", "secondary": "$2",
|
|
456
|
+
"primaryTextSegments": [], "secondaryTextSegments": []])
|
|
457
|
+
XCTAssertEqual(button.attributedTitle(for: .normal)?.string, "10\n$2")
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
func testDecorativeRefreshKeepsSixteenPointFrameAcrossActionRebind() throws {
|
|
461
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 386, height: 56))
|
|
462
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 386, height: 80))
|
|
463
|
+
window.addSubview(cell)
|
|
464
|
+
let stack = try XCTUnwrap(Mirror(reflecting: cell).children.first {
|
|
465
|
+
$0.label == "trailingStack"
|
|
466
|
+
}?.value as? NativeListAccessoryStack)
|
|
467
|
+
func bind(actionKey: String? = nil) throws {
|
|
468
|
+
var icon: [String: Any] = ["kind": "icon", "name": "RefreshCcwOutline"]
|
|
469
|
+
if let actionKey { icon["actionKey"] = actionKey }
|
|
470
|
+
let item = try NativeListItem(data: [
|
|
471
|
+
"key": "refresh", "type": "identity", "title": "Network", "height": 56,
|
|
472
|
+
"leading": ["kind": "icon", "name": "coin"], "trailing": [icon],
|
|
473
|
+
])
|
|
474
|
+
cell.bind(item: item, theme: nil, layout: "linear", selected: false,
|
|
475
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
476
|
+
window.layoutIfNeeded()
|
|
477
|
+
cell.layoutIfNeeded()
|
|
478
|
+
}
|
|
479
|
+
try bind()
|
|
480
|
+
let button = try XCTUnwrap(stack.firstVisibleControl as? UIButton)
|
|
481
|
+
XCTAssertEqual(button.bounds.width, 16, accuracy: 0.5)
|
|
482
|
+
XCTAssertEqual(button.bounds.height, 16, accuracy: 0.5)
|
|
483
|
+
XCTAssertNotNil(button.image(for: .normal))
|
|
484
|
+
XCTAssertEqual(button.image(for: .normal)?.size.width, 16)
|
|
485
|
+
XCTAssertEqual(button.image(for: .normal)?.size.height, 16)
|
|
486
|
+
XCTAssertFalse(button.isUserInteractionEnabled)
|
|
487
|
+
try bind(actionKey: "refresh")
|
|
488
|
+
XCTAssertEqual(button.bounds.width, 36, accuracy: 0.5)
|
|
489
|
+
XCTAssertEqual(button.bounds.height, 36, accuracy: 0.5)
|
|
490
|
+
XCTAssertTrue(button.isUserInteractionEnabled)
|
|
491
|
+
try bind()
|
|
492
|
+
XCTAssertEqual(button.bounds.width, 16, accuracy: 0.5)
|
|
493
|
+
XCTAssertEqual(button.bounds.height, 16, accuracy: 0.5)
|
|
494
|
+
XCTAssertEqual(button.image(for: .normal)?.size.width, 16)
|
|
495
|
+
XCTAssertEqual(button.image(for: .normal)?.size.height, 16)
|
|
496
|
+
XCTAssertFalse(button.isUserInteractionEnabled)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
func testLowValueAndOverviewAccessoryLayoutsOnProductionIdentityCell() throws {
|
|
500
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 386, height: 60))
|
|
501
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 386, height: 180))
|
|
502
|
+
window.addSubview(cell)
|
|
503
|
+
let main = try XCTUnwrap(Mirror(reflecting: cell).children.first { $0.label == "mainStack" }?
|
|
504
|
+
.value as? UIStackView)
|
|
505
|
+
let trailing = try XCTUnwrap(Mirror(reflecting: cell).children.first {
|
|
506
|
+
$0.label == "trailingStack"
|
|
507
|
+
}?.value as? NativeListAccessoryStack)
|
|
508
|
+
func bind(_ data: [String: Any], height: CGFloat) throws {
|
|
509
|
+
cell.frame.size.height = height
|
|
510
|
+
cell.bind(item: try NativeListItem(data: data), theme: nil, layout: "linear",
|
|
511
|
+
selected: false, checkboxState: { _, _, fallback in fallback },
|
|
512
|
+
fonts: NativeListFontFamilies())
|
|
513
|
+
window.layoutIfNeeded()
|
|
514
|
+
cell.layoutIfNeeded()
|
|
515
|
+
}
|
|
516
|
+
let pair: [String: Any] = ["kind": "valuePair", "primary": "1.0193B",
|
|
517
|
+
"secondary": "$1,018,849,363.70"]
|
|
518
|
+
let lowValue: [String: Any] = ["key": "low", "type": "identity", "height": 60,
|
|
519
|
+
"title": "FRAX", "subtitleSegments": [
|
|
520
|
+
["text": "$0.9918", "tone": "secondary", "textSegments": [
|
|
521
|
+
["text": "$0.99"], ["text": "18", "style": "subscript"]]],
|
|
522
|
+
["text": "\u{00a0}+0.05%", "tone": "positive"]],
|
|
523
|
+
"leading": ["kind": "token", "fallbackIcon": ["name": "CryptoCoinOutline"]],
|
|
524
|
+
"trailing": [pair], "style": ["horizontalPadding": 12, "verticalPadding": 8,
|
|
525
|
+
"leadingGap": 12, "image": ["width": 40, "height": 40]]]
|
|
526
|
+
try bind(lowValue, height: 60)
|
|
527
|
+
let priceLabels = try XCTUnwrap(Mirror(reflecting: cell).children.first {
|
|
528
|
+
$0.label == "semanticSubtitleLabels"
|
|
529
|
+
}?.value as? [UILabel])
|
|
530
|
+
XCTAssertEqual(priceLabels.count, 2)
|
|
531
|
+
XCTAssertGreaterThan(main.bounds.width, 150)
|
|
532
|
+
XCTAssertTrue(priceLabels.allSatisfy { $0.bounds.width > 0 && !$0.isHidden })
|
|
533
|
+
XCTAssertLessThanOrEqual(priceLabels.last?.frame.maxX ?? .infinity, main.bounds.width + 0.5)
|
|
534
|
+
XCTAssertEqual(trailing.bounds.width, trailing.firstVisibleControl?.bounds.width ?? -1,
|
|
535
|
+
accuracy: 0.5)
|
|
536
|
+
let detail: [String: Any] = ["key": "detail", "type": "identity", "height": 60,
|
|
537
|
+
"title": "Ethereum", "subtitle": "100.0%",
|
|
538
|
+
"leading": ["kind": "network", "fallbackIcon": ["name": "GlobusOutline"]],
|
|
539
|
+
"trailing": [pair, ["kind": "chevron"]],
|
|
540
|
+
"style": ["horizontalPadding": 12, "verticalPadding": 8, "leadingGap": 12,
|
|
541
|
+
"trailingGap": 12, "image": ["width": 32, "height": 32]]]
|
|
542
|
+
try bind(detail, height: 60)
|
|
543
|
+
XCTAssertEqual(trailing.axis, .horizontal)
|
|
544
|
+
let detailControls = trailing.arrangedSubviews.filter { !$0.isHidden }
|
|
545
|
+
XCTAssertEqual(detailControls.count, 2)
|
|
546
|
+
let detailRects = detailControls.map { $0.convert($0.bounds, to: cell) }
|
|
547
|
+
XCTAssertTrue(detailRects.allSatisfy { $0.minY >= 0 && $0.maxY <= 60 })
|
|
548
|
+
XCTAssertEqual(detailRects[0].midY, detailRects[1].midY, accuracy: 1)
|
|
549
|
+
XCTAssertEqual(detailRects[1].minX - detailRects[0].maxX, 12, accuracy: 0.5)
|
|
550
|
+
XCTAssertEqual(detailRects[1].maxX, cell.bounds.maxX - 12, accuracy: 0.5)
|
|
551
|
+
let missing: [String: Any] = ["key": "missing", "type": "identity", "height": 48,
|
|
552
|
+
"title": "Ethereum", "leading": ["kind": "network",
|
|
553
|
+
"fallbackIcon": ["name": "GlobusOutline"]],
|
|
554
|
+
"trailing": [["kind": "icon", "name": "RefreshCcwOutline"], ["kind": "chevron"]],
|
|
555
|
+
"style": ["horizontalPadding": 12, "verticalPadding": 8, "leadingGap": 12,
|
|
556
|
+
"trailingGap": 12, "image": ["width": 32, "height": 32]]]
|
|
557
|
+
try bind(missing, height: 48)
|
|
558
|
+
XCTAssertEqual(trailing.axis, .horizontal)
|
|
559
|
+
let missingControls = trailing.arrangedSubviews.filter { !$0.isHidden }
|
|
560
|
+
XCTAssertEqual(missingControls.count, 2)
|
|
561
|
+
XCTAssertEqual(missingControls[0].bounds.width, 16, accuracy: 0.5)
|
|
562
|
+
XCTAssertEqual(missingControls[1].bounds.width, 24, accuracy: 0.5)
|
|
563
|
+
let missingRects = missingControls.map { $0.convert($0.bounds, to: cell) }
|
|
564
|
+
XCTAssertTrue(missingRects.allSatisfy { $0.minY >= 0 && $0.maxY <= 48 })
|
|
565
|
+
XCTAssertEqual(missingRects[0].midY, missingRects[1].midY, accuracy: 1)
|
|
566
|
+
XCTAssertEqual(missingRects[1].minX - missingRects[0].maxX, 12, accuracy: 0.5)
|
|
567
|
+
XCTAssertEqual(missingRects[1].maxX, cell.bounds.maxX - 12, accuracy: 0.5)
|
|
568
|
+
try bind(detail, height: 60)
|
|
569
|
+
XCTAssertEqual(trailing.axis, .horizontal)
|
|
570
|
+
try bind(["key": "plain", "type": "identity", "title": "Plain", "height": 56,
|
|
571
|
+
"leading": ["kind": "token", "fallbackIcon": ["name": "CryptoCoinOutline"]]],
|
|
572
|
+
height: 56)
|
|
573
|
+
let remainingConstraints = try XCTUnwrap(Mirror(reflecting: cell).children.first {
|
|
574
|
+
$0.label == "selectorConstraints"
|
|
575
|
+
}?.value as? [NSLayoutConstraint])
|
|
576
|
+
XCTAssertTrue(remainingConstraints.isEmpty)
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
func testSwapValuePairAndTertiaryMenuIconFitAndKeepActionState() throws {
|
|
580
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 386, height: 60))
|
|
581
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 386, height: 120))
|
|
582
|
+
window.addSubview(cell)
|
|
583
|
+
defer { cell.removeFromSuperview() }
|
|
584
|
+
let trailing = try XCTUnwrap(Mirror(reflecting: cell).children.first {
|
|
585
|
+
$0.label == "trailingStack"
|
|
586
|
+
}?.value as? NativeListAccessoryStack)
|
|
587
|
+
var actions: [(String, Int?)] = []
|
|
588
|
+
cell.onAction = { _, action, _, origin in actions.append((action, origin?.slot)) }
|
|
589
|
+
func bind(disabled: Bool) throws {
|
|
590
|
+
let item = try NativeListItem(data: [
|
|
591
|
+
"key": "swap-token", "type": "identity", "height": 60,
|
|
592
|
+
"title": "ETH", "subtitle": "Ethereum",
|
|
593
|
+
"leading": ["kind": "token", "fallbackIcon": ["name": "CryptoCoinOutline"]],
|
|
594
|
+
"trailing": [
|
|
595
|
+
["kind": "valuePair", "primary": "0.001328", "secondary": "$3.08"],
|
|
596
|
+
["kind": "icon", "name": "DotVerSolid", "buttonStyle": "tertiary",
|
|
597
|
+
"actionKey": "menu", "disabled": disabled],
|
|
598
|
+
],
|
|
599
|
+
"style": ["horizontalPadding": 20, "verticalPadding": 8,
|
|
600
|
+
"leadingGap": 12, "trailingGap": 8,
|
|
601
|
+
"image": ["width": 40, "height": 40]],
|
|
602
|
+
])
|
|
603
|
+
cell.bind(item: item, theme: nil, layout: "linear", selected: false,
|
|
604
|
+
checkboxState: { _, _, fallback in fallback }, fonts: NativeListFontFamilies())
|
|
605
|
+
window.layoutIfNeeded()
|
|
606
|
+
cell.layoutIfNeeded()
|
|
607
|
+
}
|
|
608
|
+
try bind(disabled: false)
|
|
609
|
+
XCTAssertEqual(trailing.axis, .horizontal)
|
|
610
|
+
let controls = trailing.arrangedSubviews.compactMap { !$0.isHidden ? $0 as? UIButton : nil }
|
|
611
|
+
XCTAssertEqual(controls.count, 2)
|
|
612
|
+
let rects = controls.map { $0.convert($0.bounds, to: cell) }
|
|
613
|
+
XCTAssertTrue(rects.allSatisfy { $0.minY >= 0 && $0.maxY <= cell.bounds.height })
|
|
614
|
+
let amountLabel = try XCTUnwrap(controls[0].titleLabel)
|
|
615
|
+
let amountRect = amountLabel.convert(amountLabel.bounds, to: cell)
|
|
616
|
+
XCTAssertEqual(amountLabel.numberOfLines, 2)
|
|
617
|
+
XCTAssertGreaterThan(amountRect.height, 30)
|
|
618
|
+
XCTAssertGreaterThanOrEqual(amountRect.minY, 0)
|
|
619
|
+
XCTAssertLessThanOrEqual(amountRect.maxY, cell.bounds.height)
|
|
620
|
+
XCTAssertEqual(rects[0].midY, rects[1].midY, accuracy: 1)
|
|
621
|
+
XCTAssertEqual(rects[1].minX - rects[0].maxX, 8, accuracy: 0.5)
|
|
622
|
+
XCTAssertEqual(controls[1].bounds.height, 38, accuracy: 0.5)
|
|
623
|
+
XCTAssertTrue(controls[1].isEnabled)
|
|
624
|
+
controls[1].sendActions(for: .touchUpInside)
|
|
625
|
+
XCTAssertEqual(actions.count, 1)
|
|
626
|
+
XCTAssertEqual(actions[0].0, "menu")
|
|
627
|
+
XCTAssertEqual(actions[0].1, 1)
|
|
628
|
+
|
|
629
|
+
try bind(disabled: true)
|
|
630
|
+
let disabledIcon = try XCTUnwrap(trailing.arrangedSubviews.filter { !$0.isHidden }.last as? UIButton)
|
|
631
|
+
XCTAssertFalse(disabledIcon.isEnabled)
|
|
632
|
+
disabledIcon.sendActions(for: .touchUpInside)
|
|
633
|
+
XCTAssertEqual(actions.count, 1)
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
func testRiskIdentityRatioKeepsTitleVisibleWithAndWithoutButton() throws {
|
|
637
|
+
let cell = NativeListIdentityCell(frame: CGRect(x: 0, y: 0, width: 386, height: 56))
|
|
638
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 386, height: 200))
|
|
639
|
+
window.addSubview(cell)
|
|
640
|
+
let titleLabel = try XCTUnwrap(
|
|
641
|
+
Mirror(reflecting: cell).children.first { $0.label == "titleLabel" }?.value as? UILabel)
|
|
642
|
+
let mainStack = try XCTUnwrap(
|
|
643
|
+
Mirror(reflecting: cell).children.first { $0.label == "mainStack" }?.value as? UIStackView)
|
|
644
|
+
let trailing = try XCTUnwrap(
|
|
645
|
+
Mirror(reflecting: cell).children.first { $0.label == "trailingStack" }?.value
|
|
646
|
+
as? NativeListAccessoryStack)
|
|
647
|
+
let base: [String: Any] = [
|
|
648
|
+
"key": "risk", "type": "identity", "title": "XSR", "height": 56,
|
|
649
|
+
"leading": ["kind": "token", "fallbackIcon": ["name": "CryptoCoinOutline"]],
|
|
650
|
+
"style": [
|
|
651
|
+
"horizontalPadding": 12, "verticalPadding": 8, "leadingGap": 12,
|
|
652
|
+
"leadingContentMaxWidthRatio": 0.6,
|
|
653
|
+
"image": ["width": 40, "height": 40, "shape": "circle"],
|
|
654
|
+
"title": ["fontSize": 16, "lineHeight": 24, "fontWeight": "medium", "lines": 1],
|
|
655
|
+
],
|
|
656
|
+
]
|
|
657
|
+
|
|
658
|
+
func bind(_ data: [String: Any], capped: Bool = true) throws {
|
|
659
|
+
cell.bind(item: try NativeListItem(data: data), theme: nil, layout: "linear",
|
|
660
|
+
selected: false, checkboxState: { _, _, fallback in fallback },
|
|
661
|
+
fonts: NativeListFontFamilies())
|
|
662
|
+
window.layoutIfNeeded()
|
|
663
|
+
cell.layoutIfNeeded()
|
|
664
|
+
XCTAssertFalse(titleLabel.isHidden)
|
|
665
|
+
XCTAssertGreaterThan(titleLabel.bounds.width, 0.5,
|
|
666
|
+
"A visible token title must not collapse when the row reserves trailing space")
|
|
667
|
+
XCTAssertEqual(titleLabel.bounds.height, 24, accuracy: 0.5)
|
|
668
|
+
let intrinsicWidth = titleLabel.intrinsicContentSize.width
|
|
669
|
+
let capWidth = cell.root.bounds.width * 0.6 - 52
|
|
670
|
+
let requiredTitleWidth = capped ? min(intrinsicWidth, capWidth) : intrinsicWidth
|
|
671
|
+
XCTAssertGreaterThanOrEqual(titleLabel.bounds.width, requiredTitleWidth - 0.5,
|
|
672
|
+
"title=\(data["title"] ?? "") intrinsic=\(intrinsicWidth) "
|
|
673
|
+
+ "rendered=\(titleLabel.bounds.width) root=\(cell.root.bounds.width) "
|
|
674
|
+
+ "trailing=\(trailing.bounds.width)")
|
|
675
|
+
XCTAssertGreaterThanOrEqual(mainStack.bounds.width, titleLabel.bounds.width - 0.5)
|
|
676
|
+
XCTAssertEqual(trailing.frame.maxX, cell.root.bounds.maxX, accuracy: 0.5)
|
|
677
|
+
if capped {
|
|
678
|
+
XCTAssertLessThanOrEqual(mainStack.bounds.width, cell.root.bounds.width * 0.6 - 52 + 0.5)
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
try bind(base)
|
|
683
|
+
XCTAssertNil(trailing.firstVisibleControl)
|
|
684
|
+
try bind(base.merging(["trailing": [[
|
|
685
|
+
"kind": "button", "label": "Show on home", "actionKey": "show",
|
|
686
|
+
]]]) { _, newer in newer })
|
|
687
|
+
let button = try XCTUnwrap(trailing.firstVisibleControl)
|
|
688
|
+
XCTAssertGreaterThan(button.bounds.width, 100)
|
|
689
|
+
XCTAssertEqual(trailing.frame.minX + button.frame.maxX, cell.root.bounds.maxX, accuracy: 0.5)
|
|
690
|
+
cell.frame.size.height = 88
|
|
691
|
+
try bind(base.merging(["trailing": [
|
|
692
|
+
["kind": "button", "label": "A", "actionKey": "first"],
|
|
693
|
+
["kind": "button", "label": "Longer action", "actionKey": "second"],
|
|
694
|
+
], "height": 88]) { _, newer in newer })
|
|
695
|
+
let visibleButtons = trailing.arrangedSubviews.compactMap { $0 as? NativeListAccessoryButton }
|
|
696
|
+
.filter { !$0.isHidden }
|
|
697
|
+
XCTAssertEqual(visibleButtons.count, 2)
|
|
698
|
+
XCTAssertGreaterThan(visibleButtons[1].bounds.width, visibleButtons[0].bounds.width)
|
|
699
|
+
XCTAssertEqual(trailing.frame.minX + visibleButtons[1].frame.maxX,
|
|
700
|
+
cell.root.bounds.maxX, accuracy: 0.5)
|
|
701
|
+
cell.frame.size.height = 56
|
|
702
|
+
try bind(base.merging(["title": "A very long token symbol that must truncate within the cap"])
|
|
703
|
+
{ _, newer in newer })
|
|
704
|
+
XCTAssertLessThan(titleLabel.bounds.width, titleLabel.intrinsicContentSize.width)
|
|
705
|
+
try bind(base)
|
|
706
|
+
XCTAssertNil(trailing.firstVisibleControl)
|
|
707
|
+
try bind(base.merging(["key": "searched-risk", "title": "rkl"]) { _, newer in newer })
|
|
708
|
+
XCTAssertNil(trailing.firstVisibleControl)
|
|
709
|
+
try bind(base)
|
|
710
|
+
var unrestricted = base
|
|
711
|
+
var unrestrictedStyle = try XCTUnwrap(base["style"] as? [String: Any])
|
|
712
|
+
unrestrictedStyle.removeValue(forKey: "leadingContentMaxWidthRatio")
|
|
713
|
+
unrestricted["style"] = unrestrictedStyle
|
|
714
|
+
try bind(unrestricted, capped: false)
|
|
715
|
+
XCTAssertEqual(cell.root.arrangedSubviews.count, 3)
|
|
716
|
+
XCTAssertEqual(mainStack.contentHuggingPriority(for: .horizontal).rawValue, 1, accuracy: 0.1)
|
|
717
|
+
let spacing = cell.root.customSpacing(after: mainStack)
|
|
718
|
+
XCTAssertTrue(spacing == UIStackView.spacingUseDefault || spacing == cell.root.spacing)
|
|
719
|
+
}
|
|
720
|
+
|
|
156
721
|
func testFixedFooterReplacesRendererAndKeepsActionRouting() throws {
|
|
157
722
|
let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
|
|
158
723
|
var actions: [[String: Any]] = []
|
|
@@ -246,6 +811,35 @@ final class NativeListLifecycleTests: XCTestCase {
|
|
|
246
811
|
XCTAssertEqual(footer.accessibilityLabel, "Continue")
|
|
247
812
|
}
|
|
248
813
|
|
|
814
|
+
func testPaddedSquareNetworkOverlayRoundsInnerImageAndResetsOnRebind() throws {
|
|
815
|
+
let visual = NativeListLeadingVisual(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
|
|
816
|
+
func bind(_ overlay: [String: Any]) {
|
|
817
|
+
visual.bind(["kind": "token", "overlays": [overlay]], style: [:], key: "overlay",
|
|
818
|
+
theme: nil, isUnread: false, fonts: NativeListFontFamilies())
|
|
819
|
+
visual.layoutIfNeeded()
|
|
820
|
+
}
|
|
821
|
+
let image: [String: Any] = ["uri": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9xvY0AAAAASUVORK5CYII="]
|
|
822
|
+
bind(["position": "bottomRight", "size": 20, "width": 20, "height": 20,
|
|
823
|
+
"padding": 2, "image": image, "fallbackIcon": ["name": "GlobusOutline"]])
|
|
824
|
+
let slots = try XCTUnwrap(Mirror(reflecting: visual).children.first {
|
|
825
|
+
$0.label == "overlaySlots"
|
|
826
|
+
}?.value as? [NativeListImageSlot])
|
|
827
|
+
let slot = try XCTUnwrap(slots.first?.view)
|
|
828
|
+
XCTAssertEqual(slot.bounds.size, CGSize(width: 16, height: 16))
|
|
829
|
+
XCTAssertEqual(slot.layer.cornerRadius, 8, accuracy: 0.5)
|
|
830
|
+
XCTAssertTrue(slot.clipsToBounds)
|
|
831
|
+
bind(["position": "bottomRight", "width": 20, "height": 14,
|
|
832
|
+
"padding": 2, "image": image])
|
|
833
|
+
XCTAssertEqual(slot.layer.cornerRadius, 0)
|
|
834
|
+
XCTAssertFalse(slot.clipsToBounds)
|
|
835
|
+
bind(["position": "bottomRight", "size": 20, "padding": 2, "text": "E"])
|
|
836
|
+
XCTAssertEqual(slot.layer.cornerRadius, 0)
|
|
837
|
+
XCTAssertFalse(slot.clipsToBounds)
|
|
838
|
+
bind(["position": "bottomRight", "size": 20, "padding": 2, "image": image])
|
|
839
|
+
XCTAssertEqual(slot.layer.cornerRadius, 8, accuracy: 0.5)
|
|
840
|
+
visual.recycle()
|
|
841
|
+
}
|
|
842
|
+
|
|
249
843
|
func testNestedWalletMembersAndDragBadgeFollowEachBindMapping() throws {
|
|
250
844
|
let group = NativeListWalletGroupCell(frame: CGRect(x: 0, y: 0, width: 320, height: 200))
|
|
251
845
|
let item = try NativeListItem(data: [
|
|
@@ -348,7 +942,7 @@ final class NativeListLifecycleTests: XCTestCase {
|
|
|
348
942
|
XCTAssertTrue(header.clipsToBounds)
|
|
349
943
|
XCTAssertEqual(header.bounds.height, 0, "An unmeasured header cannot draw over the first rows")
|
|
350
944
|
|
|
351
|
-
view.setContainerSlotHeightsJson("[44,44,60]")
|
|
945
|
+
view.setContainerSlotHeightsJson("[44,44,60,0]")
|
|
352
946
|
view.layoutIfNeeded()
|
|
353
947
|
list.layoutIfNeeded()
|
|
354
948
|
XCTAssertEqual(slots[0].convert(CGPoint.zero, to: list), CGPoint(x: 16, y: 0))
|
|
@@ -363,10 +957,696 @@ final class NativeListLifecycleTests: XCTestCase {
|
|
|
363
957
|
XCTAssertNil(slots[2].superview)
|
|
364
958
|
}
|
|
365
959
|
|
|
960
|
+
func testSidecarCentersRowsWhenConfiguredBeforeFirstWidth() throws {
|
|
961
|
+
try assertHomeSidecarGeometry(sidecarBeforeFirstLayout: true)
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
func testSidecarCentersRowsWhenConfiguredAfterFirstWidth() throws {
|
|
965
|
+
try assertHomeSidecarGeometry(sidecarBeforeFirstLayout: false)
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
func testHomeLinearColumnsStayInsideTheirStackAcrossEmptyMiddleAndReuse() throws {
|
|
969
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 1376, height: 900))
|
|
970
|
+
let controller = UIViewController()
|
|
971
|
+
window.rootViewController = controller
|
|
972
|
+
let view = NativeListView(frame: window.bounds)
|
|
973
|
+
controller.view.addSubview(view)
|
|
974
|
+
window.isHidden = false
|
|
975
|
+
defer {
|
|
976
|
+
view.dispose()
|
|
977
|
+
view.removeFromSuperview()
|
|
978
|
+
window.isHidden = true
|
|
979
|
+
window.rootViewController = nil
|
|
980
|
+
}
|
|
981
|
+
for index in 0..<4 { view.mountContainerSlot(UIView(), atIndex: index) }
|
|
982
|
+
view.setContainerSlotHeightsJson("[0,0,0,1200]")
|
|
983
|
+
let sidecar = "{\"width\":400,\"gap\":24,\"maxContentWidth\":1140}"
|
|
984
|
+
view.setSidecarLayoutJson(sidecar)
|
|
985
|
+
|
|
986
|
+
func columns(middle: String?, two: Bool = false) -> [[String: Any]] {
|
|
987
|
+
let first: [String: Any] = ["key": "asset", "text": "USD1",
|
|
988
|
+
"secondaryText": "Public fixture", "weight": 1]
|
|
989
|
+
let balance: [String: Any] = ["key": "balance", "text": middle ?? "",
|
|
990
|
+
"secondaryText": middle == nil ? "" : "$0.01", "alignment": "end", "weight": 1]
|
|
991
|
+
let price: [String: Any] = ["key": "price", "text": "$1.00",
|
|
992
|
+
"secondaryText": "0.01%", "alignment": "end", "weight": 1]
|
|
993
|
+
return two ? [first, price] : [first, balance, price]
|
|
994
|
+
}
|
|
995
|
+
let rows: [[String: Any]] = (0..<30).map { index in
|
|
996
|
+
["key": "row-\(index)", "type": "dataRow", "height": 60,
|
|
997
|
+
"leading": ["kind": "token", "fallbackIcon": ["name": "CryptoCoinOutline"]],
|
|
998
|
+
"columns": index == 20 ? columns(middle: "0.01") : homeSidecarColumns(index, updated: false),
|
|
999
|
+
"trailing": [["kind": "button", "label": "Swap", "actionKey": "swap"]],
|
|
1000
|
+
"style": ["horizontalPadding": 12, "verticalPadding": 8, "leadingGap": 12,
|
|
1001
|
+
"columns": ["token": "$bodyLg", "fontWeight": "medium", "lines": 1],
|
|
1002
|
+
"columnSecondary": ["token": "$bodyMd", "lines": 1],
|
|
1003
|
+
"image": ["width": 32, "height": 32]]]
|
|
1004
|
+
}
|
|
1005
|
+
let snapshot: [String: Any] = ["schemaVersion": 1, "generation": 1,
|
|
1006
|
+
"layout": ["kind": "linear", "contentPaddingHorizontal": 8], "rows": rows]
|
|
1007
|
+
view.applySnapshotJson(String(decoding: try JSONSerialization.data(withJSONObject: snapshot),
|
|
1008
|
+
as: UTF8.self))
|
|
1009
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1010
|
+
list.contentInsetAdjustmentBehavior = .never
|
|
1011
|
+
func layout() {
|
|
1012
|
+
window.layoutIfNeeded()
|
|
1013
|
+
view.layoutIfNeeded()
|
|
1014
|
+
list.layoutIfNeeded()
|
|
1015
|
+
}
|
|
1016
|
+
func check(_ phase: String, count: Int, width: CGFloat, weights: [Int]? = nil) throws {
|
|
1017
|
+
layout()
|
|
1018
|
+
let cell = try XCTUnwrap(list.cellForItem(at: IndexPath(item: 20, section: 0))
|
|
1019
|
+
as? NativeListDataRowCell, phase)
|
|
1020
|
+
let stack = try XCTUnwrap(cell.root.arrangedSubviews[3] as? UIStackView, phase)
|
|
1021
|
+
let visible = stack.arrangedSubviews.filter { !$0.isHidden }
|
|
1022
|
+
XCTAssertEqual(stack.arrangedSubviews.count, 4, phase)
|
|
1023
|
+
XCTAssertEqual(stack.arrangedSubviews.map(\.isHidden),
|
|
1024
|
+
(0..<4).map { $0 >= count }, phase)
|
|
1025
|
+
XCTAssertEqual(visible.count, count, phase)
|
|
1026
|
+
let cellFrame = cell.convert(cell.bounds, to: list)
|
|
1027
|
+
let stackFrame = stack.convert(stack.bounds, to: list)
|
|
1028
|
+
let attributes = try XCTUnwrap(list.layoutAttributesForItem(
|
|
1029
|
+
at: IndexPath(item: 20, section: 0)), phase)
|
|
1030
|
+
XCTAssertEqual(attributes.frame.width, width, accuracy: 1, phase)
|
|
1031
|
+
XCTAssertEqual(cellFrame.width, width, accuracy: 1,
|
|
1032
|
+
"\(phase) window=\(window.frame) controller=\(controller.view.frame) view=\(view.frame) list=\(list.frame) bounds=\(list.bounds) attributes=\(attributes.frame)")
|
|
1033
|
+
let rowConstraints = Mirror(reflecting: cell).children.first {
|
|
1034
|
+
$0.label == "rowConstraints"
|
|
1035
|
+
}?.value as? [NSLayoutConstraint] ?? []
|
|
1036
|
+
let columnWidthConstraints = Mirror(reflecting: cell).children.first {
|
|
1037
|
+
$0.label == "columnWidthConstraints"
|
|
1038
|
+
}?.value as? [NSLayoutConstraint] ?? []
|
|
1039
|
+
let constraints = rowConstraints + columnWidthConstraints
|
|
1040
|
+
let constraintState = constraints.map { constraint in
|
|
1041
|
+
let first = stack.arrangedSubviews.firstIndex {
|
|
1042
|
+
$0 === constraint.firstItem as? UIView
|
|
1043
|
+
} ?? -1
|
|
1044
|
+
let second = stack.arrangedSubviews.firstIndex {
|
|
1045
|
+
$0 === constraint.secondItem as? UIView
|
|
1046
|
+
} ?? -1
|
|
1047
|
+
return "\(first):\(second):\(constraint.multiplier):\(constraint.isActive)"
|
|
1048
|
+
}.joined(separator: ",")
|
|
1049
|
+
let hidingState = stack.arrangedSubviews.enumerated().map { index, column in
|
|
1050
|
+
let hiding = (stack.constraints + column.constraints).filter {
|
|
1051
|
+
$0.isActive && $0.identifier == "UISV-hiding"
|
|
1052
|
+
}.map { "\($0.constant)" }.joined(separator: ",")
|
|
1053
|
+
return "\(index):\(column.isHidden):\(hiding)"
|
|
1054
|
+
}.joined(separator: ",")
|
|
1055
|
+
let geometry = "\(phase) model=\(count) cell=\(cellFrame) stack=\(stackFrame) "
|
|
1056
|
+
+ "columns=\(stack.arrangedSubviews.map { "\($0.isHidden):\($0.convert($0.bounds, to: list))" }) "
|
|
1057
|
+
+ "constraints=\(constraintState) hiding=\(hidingState)"
|
|
1058
|
+
XCTAssertGreaterThanOrEqual(stackFrame.minX, cellFrame.minX - 0.5, geometry)
|
|
1059
|
+
XCTAssertLessThanOrEqual(stackFrame.maxX, cellFrame.maxX + 0.5, geometry)
|
|
1060
|
+
for column in visible {
|
|
1061
|
+
let columnFrame = column.convert(column.bounds, to: list)
|
|
1062
|
+
XCTAssertGreaterThan(columnFrame.width, 0, geometry)
|
|
1063
|
+
XCTAssertGreaterThanOrEqual(columnFrame.minX, stackFrame.minX - 0.5, geometry)
|
|
1064
|
+
XCTAssertLessThanOrEqual(columnFrame.maxX, stackFrame.maxX + 0.5, geometry)
|
|
1065
|
+
let primaryLine = (column as? UIStackView)?.arrangedSubviews.first
|
|
1066
|
+
if let primaryLine {
|
|
1067
|
+
let lineFrame = primaryLine.convert(primaryLine.bounds, to: list)
|
|
1068
|
+
XCTAssertGreaterThanOrEqual(lineFrame.minX, columnFrame.minX - 0.5, geometry)
|
|
1069
|
+
XCTAssertLessThanOrEqual(lineFrame.maxX, columnFrame.maxX + 0.5, geometry)
|
|
1070
|
+
}
|
|
1071
|
+
for label in labels(in: column) where !label.isHidden {
|
|
1072
|
+
let labelFrame = label.convert(label.bounds, to: list)
|
|
1073
|
+
XCTAssertGreaterThanOrEqual(labelFrame.minX, columnFrame.minX - 0.5,
|
|
1074
|
+
"\(geometry) label=\(labelFrame)")
|
|
1075
|
+
XCTAssertLessThanOrEqual(labelFrame.maxX, columnFrame.maxX + 0.5,
|
|
1076
|
+
"\(geometry) label=\(labelFrame)")
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
if count >= 2 {
|
|
1080
|
+
let expectedWeights = weights ?? Array(repeating: 1, count: count)
|
|
1081
|
+
XCTAssertEqual(expectedWeights.count, count, geometry)
|
|
1082
|
+
for index in 1..<count {
|
|
1083
|
+
XCTAssertEqual(visible[index].bounds.width,
|
|
1084
|
+
visible[0].bounds.width * CGFloat(expectedWeights[index])
|
|
1085
|
+
/ CGFloat(expectedWeights[0]),
|
|
1086
|
+
accuracy: 1, geometry)
|
|
1087
|
+
let previous = visible[index - 1].convert(visible[index - 1].bounds, to: stack)
|
|
1088
|
+
let current = visible[index].convert(visible[index].bounds, to: stack)
|
|
1089
|
+
XCTAssertGreaterThanOrEqual(current.minX, previous.maxX - 0.5, geometry)
|
|
1090
|
+
XCTAssertEqual(current.minX - previous.maxX, stack.spacing,
|
|
1091
|
+
accuracy: 0.5, geometry)
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
func patch(_ next: [[String: Any]]) throws {
|
|
1096
|
+
let patches: [[String: Any]] = [["key": "row-20", "type": "dataRow",
|
|
1097
|
+
"changes": ["columns": next]]]
|
|
1098
|
+
view.applyPatchesJson(String(decoding: try JSONSerialization.data(withJSONObject: patches),
|
|
1099
|
+
as: UTF8.self))
|
|
1100
|
+
}
|
|
1101
|
+
func widthConstraints() throws -> [NSLayoutConstraint] {
|
|
1102
|
+
let cell = try XCTUnwrap(list.cellForItem(at: IndexPath(item: 20, section: 0))
|
|
1103
|
+
as? NativeListDataRowCell)
|
|
1104
|
+
return try XCTUnwrap(Mirror(reflecting: cell).children.first {
|
|
1105
|
+
$0.label == "columnWidthConstraints"
|
|
1106
|
+
}?.value as? [NSLayoutConstraint])
|
|
1107
|
+
}
|
|
1108
|
+
layout()
|
|
1109
|
+
list.setContentOffset(CGPoint(x: 0, y: 18 * 60), animated: false)
|
|
1110
|
+
try check("three populated", count: 3, width: 700)
|
|
1111
|
+
try patch(columns(middle: nil))
|
|
1112
|
+
try check("three empty middle", count: 3, width: 700)
|
|
1113
|
+
try patch(columns(middle: "0.02"))
|
|
1114
|
+
try check("three restored", count: 3, width: 700)
|
|
1115
|
+
try patch(columns(middle: nil, two: true))
|
|
1116
|
+
try check("two columns", count: 2, width: 700)
|
|
1117
|
+
try patch(columns(middle: nil))
|
|
1118
|
+
try check("three after two", count: 3, width: 700)
|
|
1119
|
+
let stableConstraints = try widthConstraints()
|
|
1120
|
+
XCTAssertEqual(stableConstraints.count, 2)
|
|
1121
|
+
for update in 0..<10 {
|
|
1122
|
+
var next = columns(middle: nil)
|
|
1123
|
+
next[2]["text"] = "$1.\(update)"
|
|
1124
|
+
try patch(next)
|
|
1125
|
+
try check("same-shape price \(update)", count: 3, width: 700)
|
|
1126
|
+
let retained = try widthConstraints()
|
|
1127
|
+
XCTAssertEqual(retained.count, stableConstraints.count)
|
|
1128
|
+
XCTAssertTrue(zip(stableConstraints, retained).allSatisfy { $0 === $1 })
|
|
1129
|
+
XCTAssertTrue(retained.allSatisfy(\.isActive))
|
|
1130
|
+
}
|
|
1131
|
+
var equivalentWeights = columns(middle: nil)
|
|
1132
|
+
for index in equivalentWeights.indices { equivalentWeights[index]["weight"] = 2 }
|
|
1133
|
+
try patch(equivalentWeights)
|
|
1134
|
+
try check("equivalent weights", count: 3, width: 700, weights: [2, 2, 2])
|
|
1135
|
+
let equivalentConstraints = try widthConstraints()
|
|
1136
|
+
XCTAssertEqual(equivalentConstraints.count, stableConstraints.count)
|
|
1137
|
+
XCTAssertTrue(zip(stableConstraints, equivalentConstraints).allSatisfy { $0 === $1 })
|
|
1138
|
+
var unequalWeights = columns(middle: nil)
|
|
1139
|
+
unequalWeights[0]["weight"] = 2
|
|
1140
|
+
try patch(unequalWeights)
|
|
1141
|
+
try check("unequal weights", count: 3, width: 700, weights: [2, 1, 1])
|
|
1142
|
+
let unequalConstraints = try widthConstraints()
|
|
1143
|
+
XCTAssertEqual(unequalConstraints.count, 2)
|
|
1144
|
+
XCTAssertTrue(stableConstraints.allSatisfy { !$0.isActive })
|
|
1145
|
+
try patch(columns(middle: nil, two: true))
|
|
1146
|
+
try check("two after weights", count: 2, width: 700)
|
|
1147
|
+
let twoConstraints = try widthConstraints()
|
|
1148
|
+
XCTAssertEqual(twoConstraints.count, 1)
|
|
1149
|
+
XCTAssertTrue(unequalConstraints.allSatisfy { !$0.isActive })
|
|
1150
|
+
try patch([])
|
|
1151
|
+
try check("zero columns", count: 0, width: 700)
|
|
1152
|
+
XCTAssertTrue((try widthConstraints()).isEmpty)
|
|
1153
|
+
XCTAssertTrue(twoConstraints.allSatisfy { !$0.isActive })
|
|
1154
|
+
try patch(columns(middle: nil))
|
|
1155
|
+
try check("three after zero", count: 3, width: 700)
|
|
1156
|
+
list.setContentOffset(.zero, animated: false)
|
|
1157
|
+
layout()
|
|
1158
|
+
XCTAssertNil(list.cellForItem(at: IndexPath(item: 20, section: 0)))
|
|
1159
|
+
list.setContentOffset(CGPoint(x: 0, y: 18 * 60), animated: false)
|
|
1160
|
+
try check("after scroll reuse", count: 3, width: 700)
|
|
1161
|
+
window.frame.size.width = 1032
|
|
1162
|
+
controller.view.frame = window.bounds
|
|
1163
|
+
view.frame = window.bounds
|
|
1164
|
+
view.setSidecarLayoutJson("")
|
|
1165
|
+
drainMainQueue()
|
|
1166
|
+
try check("narrow no sidecar", count: 3, width: 1016)
|
|
1167
|
+
window.frame.size.width = 1376
|
|
1168
|
+
controller.view.frame = window.bounds
|
|
1169
|
+
view.frame = window.bounds
|
|
1170
|
+
view.setSidecarLayoutJson(sidecar)
|
|
1171
|
+
drainMainQueue()
|
|
1172
|
+
try check("wide sidecar restored", count: 3, width: 700)
|
|
1173
|
+
func animatedPatch(_ next: [[String: Any]], phase: String, count: Int) throws {
|
|
1174
|
+
let patches: [[String: Any]] = [["key": "row-20", "type": "dataRow",
|
|
1175
|
+
"changes": ["columns": next]]]
|
|
1176
|
+
let json = String(decoding: try JSONSerialization.data(withJSONObject: patches), as: UTF8.self)
|
|
1177
|
+
let finished = expectation(description: "Home column animation: \(phase)")
|
|
1178
|
+
UIView.animate(withDuration: 0.2, delay: 0, options: .beginFromCurrentState) {
|
|
1179
|
+
view.applyPatchesJson(json)
|
|
1180
|
+
view.layoutIfNeeded()
|
|
1181
|
+
list.layoutIfNeeded()
|
|
1182
|
+
} completion: { _ in finished.fulfill() }
|
|
1183
|
+
wait(for: [finished], timeout: 1)
|
|
1184
|
+
try check(phase, count: count, width: 700)
|
|
1185
|
+
waitForStructuralFrames(0.1)
|
|
1186
|
+
try check("\(phase) stable", count: count, width: 700)
|
|
1187
|
+
}
|
|
1188
|
+
try animatedPatch(columns(middle: nil, two: true), phase: "animated two", count: 2)
|
|
1189
|
+
try animatedPatch(columns(middle: nil), phase: "animated three", count: 3)
|
|
1190
|
+
try animatedPatch(columns(middle: "0.06"), phase: "animated nonempty", count: 3)
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
func testStructuralShrinkUsesTheTallerFinalColumnForOffsetClamping() throws {
|
|
1194
|
+
let cases: [(name: String, sidecar: Int, offset: CGFloat, height: CGFloat,
|
|
1195
|
+
zeroInset: Bool)] = [
|
|
1196
|
+
("sidecar stays taller", 2200, 1450, 2200, false),
|
|
1197
|
+
("left column stays taller", 1000, 1100, 1560, false),
|
|
1198
|
+
("left column zero inset", 1000, 1100, 1560, true),
|
|
1199
|
+
("sidecar becomes taller", 1700, 1050, 1700, false),
|
|
1200
|
+
]
|
|
1201
|
+
for testCase in cases {
|
|
1202
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 1000, height: 600))
|
|
1203
|
+
let controller = UIViewController()
|
|
1204
|
+
window.rootViewController = controller
|
|
1205
|
+
let view = NativeListView(frame: window.bounds)
|
|
1206
|
+
controller.view.addSubview(view)
|
|
1207
|
+
window.isHidden = false
|
|
1208
|
+
defer {
|
|
1209
|
+
view.dispose()
|
|
1210
|
+
view.removeFromSuperview()
|
|
1211
|
+
window.isHidden = true
|
|
1212
|
+
window.rootViewController = nil
|
|
1213
|
+
}
|
|
1214
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1215
|
+
// Keep the viewport insets fixed while testing structural offset clamping.
|
|
1216
|
+
let explicitInset = testCase.zeroInset ? UIEdgeInsets.zero :
|
|
1217
|
+
UIEdgeInsets(top: 62, left: 0, bottom: 34, right: 0)
|
|
1218
|
+
list.contentInsetAdjustmentBehavior = .never
|
|
1219
|
+
list.contentInset = explicitInset
|
|
1220
|
+
view.mountContainerSlot(UIView(), atIndex: 0)
|
|
1221
|
+
view.mountContainerSlot(UIView(), atIndex: 1)
|
|
1222
|
+
let footer = UIView()
|
|
1223
|
+
view.mountContainerSlot(footer, atIndex: 2)
|
|
1224
|
+
let sidecar = UIView()
|
|
1225
|
+
view.mountContainerSlot(sidecar, atIndex: 3)
|
|
1226
|
+
view.setContainerSlotHeightsJson("[0,0,60,\(testCase.sidecar)]")
|
|
1227
|
+
view.setSidecarLayoutJson("{\"width\":300,\"gap\":24,\"maxContentWidth\":1000}")
|
|
1228
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 30))
|
|
1229
|
+
view.layoutIfNeeded()
|
|
1230
|
+
list.layoutIfNeeded()
|
|
1231
|
+
drainMainQueue()
|
|
1232
|
+
XCTAssertEqual(list.adjustedContentInset, explicitInset, testCase.name)
|
|
1233
|
+
XCTAssertEqual(list.contentSize.height, max(1860, CGFloat(testCase.sidecar)),
|
|
1234
|
+
accuracy: 1, testCase.name)
|
|
1235
|
+
list.contentOffset.y = testCase.offset
|
|
1236
|
+
list.layoutIfNeeded()
|
|
1237
|
+
XCTAssertEqual(list.contentOffset.y, testCase.offset, accuracy: 1, testCase.name)
|
|
1238
|
+
|
|
1239
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 25, transitionID: testCase.name))
|
|
1240
|
+
let completed = expectation(description: "Native structural display link: \(testCase.name)")
|
|
1241
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { completed.fulfill() }
|
|
1242
|
+
wait(for: [completed], timeout: 1)
|
|
1243
|
+
list.layoutIfNeeded()
|
|
1244
|
+
XCTAssertEqual(list.adjustedContentInset, explicitInset, testCase.name)
|
|
1245
|
+
XCTAssertEqual(list.contentSize.height, testCase.height, accuracy: 1, testCase.name)
|
|
1246
|
+
let inset = list.adjustedContentInset
|
|
1247
|
+
let finalMaximum = max(-inset.top, testCase.height - list.bounds.height + inset.bottom)
|
|
1248
|
+
let expectedOffset = min(testCase.offset, finalMaximum)
|
|
1249
|
+
XCTAssertEqual(list.contentOffset.y, expectedOffset, accuracy: 1, testCase.name)
|
|
1250
|
+
XCTAssertNotNil(sidecar.superview, testCase.name)
|
|
1251
|
+
XCTAssertEqual(footer.convert(.zero, to: list).y, 1500, accuracy: 1, testCase.name)
|
|
1252
|
+
waitForStructuralFrames(0.1)
|
|
1253
|
+
XCTAssertEqual(list.adjustedContentInset, explicitInset, testCase.name)
|
|
1254
|
+
XCTAssertEqual(list.contentSize.height, testCase.height, accuracy: 1, testCase.name)
|
|
1255
|
+
XCTAssertEqual(list.contentOffset.y, expectedOffset, accuracy: 1, testCase.name)
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
func testVisibleNativeCellsRemainBoundedAsRowsGrowTenfold() throws {
|
|
1260
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
|
|
1261
|
+
let controller = UIViewController()
|
|
1262
|
+
window.rootViewController = controller
|
|
1263
|
+
let view = NativeListView(frame: window.bounds)
|
|
1264
|
+
controller.view.addSubview(view)
|
|
1265
|
+
window.isHidden = false
|
|
1266
|
+
defer { window.isHidden = true }
|
|
1267
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1268
|
+
func sampleCells(count: Int) throws -> [(visible: Int, attached: Int)] {
|
|
1269
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: count))
|
|
1270
|
+
view.layoutIfNeeded()
|
|
1271
|
+
list.layoutIfNeeded()
|
|
1272
|
+
let lastOffset = CGFloat(count * 60 - 600)
|
|
1273
|
+
return [CGFloat(0), lastOffset / 2, lastOffset].map { offset in
|
|
1274
|
+
list.setContentOffset(CGPoint(x: 0, y: offset), animated: false)
|
|
1275
|
+
list.layoutIfNeeded()
|
|
1276
|
+
return (list.visibleCells.count,
|
|
1277
|
+
list.subviews.compactMap { $0 as? NativeListRowHost }.count)
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
let hundredCells = try sampleCells(count: 100)
|
|
1281
|
+
let thousandCells = try sampleCells(count: 1000)
|
|
1282
|
+
print("NativeList 100 visible/attached first/mid/tail: \(hundredCells)")
|
|
1283
|
+
print("NativeList 1000 visible/attached first/mid/tail: \(thousandCells)")
|
|
1284
|
+
XCTAssertTrue(hundredCells.allSatisfy {
|
|
1285
|
+
$0.visible > 0 && $0.visible <= 24 && $0.attached > 0 && $0.attached <= 24
|
|
1286
|
+
})
|
|
1287
|
+
XCTAssertTrue(thousandCells.allSatisfy {
|
|
1288
|
+
$0.visible > 0 && $0.visible <= 24 && $0.attached > 0 && $0.attached <= 24
|
|
1289
|
+
})
|
|
1290
|
+
XCTAssertLessThanOrEqual(thousandCells.map(\.attached).max() ?? 0,
|
|
1291
|
+
(hundredCells.map(\.attached).max() ?? 0) + 4)
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
func testStructuralTransitionRapidReversalKeepsRetiringContentAtNaturalHeight() throws {
|
|
1295
|
+
try requireStructuralAnimation()
|
|
1296
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
|
|
1297
|
+
let controller = UIViewController()
|
|
1298
|
+
window.rootViewController = controller
|
|
1299
|
+
let view = NativeListView(frame: window.bounds)
|
|
1300
|
+
controller.view.addSubview(view)
|
|
1301
|
+
window.isHidden = false
|
|
1302
|
+
defer { view.dispose(); view.removeFromSuperview(); window.isHidden = true }
|
|
1303
|
+
let footer = UIView()
|
|
1304
|
+
for index in 0..<2 { view.mountContainerSlot(UIView(), atIndex: index) }
|
|
1305
|
+
view.mountContainerSlot(footer, atIndex: 2)
|
|
1306
|
+
view.setContainerSlotHeightsJson("[0,0,60,0]")
|
|
1307
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 25, generation: 1))
|
|
1308
|
+
view.layoutIfNeeded()
|
|
1309
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1310
|
+
list.layoutIfNeeded()
|
|
1311
|
+
drainMainQueue()
|
|
1312
|
+
list.contentOffset.y = 900
|
|
1313
|
+
list.layoutIfNeeded()
|
|
1314
|
+
|
|
1315
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 30, generation: 2,
|
|
1316
|
+
transitionID: "expand-first", durationMs: 400))
|
|
1317
|
+
waitForStructuralFrames(0.08)
|
|
1318
|
+
list.contentOffset.y = 1100
|
|
1319
|
+
list.layoutIfNeeded()
|
|
1320
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 25, generation: 3,
|
|
1321
|
+
transitionID: "collapse-middle", durationMs: 400))
|
|
1322
|
+
waitForStructuralFrames(0.08)
|
|
1323
|
+
list.layoutIfNeeded()
|
|
1324
|
+
let source = try XCTUnwrap(list.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
|
|
1325
|
+
let retiring = try XCTUnwrap(list.indexPathsForVisibleItems.compactMap { indexPath -> NativeListRowHost? in
|
|
1326
|
+
guard let key = source.itemIdentifier(for: indexPath),
|
|
1327
|
+
let index = Int(key.replacingOccurrences(of: "row-", with: "")), index >= 25 else {
|
|
1328
|
+
return nil
|
|
1329
|
+
}
|
|
1330
|
+
return list.cellForItem(at: indexPath) as? NativeListRowHost
|
|
1331
|
+
}.first)
|
|
1332
|
+
XCTAssertGreaterThan(retiring.bounds.height, 0)
|
|
1333
|
+
XCTAssertLessThan(retiring.bounds.height, 60)
|
|
1334
|
+
XCTAssertEqual(retiring.contentView.bounds.height, 60, accuracy: 1)
|
|
1335
|
+
XCTAssertNotNil(retiring.structuralContentHeight)
|
|
1336
|
+
|
|
1337
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 30, generation: 4,
|
|
1338
|
+
transitionID: "expand-last", durationMs: 400))
|
|
1339
|
+
waitForStructuralFrames(0.5)
|
|
1340
|
+
list.layoutIfNeeded()
|
|
1341
|
+
let keys = source.snapshot().itemIdentifiers
|
|
1342
|
+
XCTAssertEqual(keys, (0..<30).map { "row-\($0)" })
|
|
1343
|
+
XCTAssertEqual(Set(keys).count, 30)
|
|
1344
|
+
XCTAssertEqual(list.contentSize.height, 1860, accuracy: 1)
|
|
1345
|
+
XCTAssertEqual(footer.convert(.zero, to: list).y, 1800, accuracy: 1)
|
|
1346
|
+
XCTAssertGreaterThanOrEqual(list.contentOffset.y, -list.adjustedContentInset.top - 1)
|
|
1347
|
+
XCTAssertLessThanOrEqual(list.contentOffset.y,
|
|
1348
|
+
list.contentSize.height - list.bounds.height + list.adjustedContentInset.bottom + 1)
|
|
1349
|
+
for case let cell as NativeListRowHost in list.visibleCells {
|
|
1350
|
+
XCTAssertEqual(cell.alpha, 1, accuracy: 0.01)
|
|
1351
|
+
XCTAssertNil(cell.structuralContentHeight)
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
func testStructuralTransitionSurvivesVisibleDataRowPatch() throws {
|
|
1356
|
+
try requireStructuralAnimation()
|
|
1357
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
|
|
1358
|
+
let controller = UIViewController()
|
|
1359
|
+
window.rootViewController = controller
|
|
1360
|
+
let view = NativeListView(frame: window.bounds)
|
|
1361
|
+
controller.view.addSubview(view)
|
|
1362
|
+
window.isHidden = false
|
|
1363
|
+
defer { view.dispose(); view.removeFromSuperview(); window.isHidden = true }
|
|
1364
|
+
let footer = UIView()
|
|
1365
|
+
for index in 0..<2 { view.mountContainerSlot(UIView(), atIndex: index) }
|
|
1366
|
+
view.mountContainerSlot(footer, atIndex: 2)
|
|
1367
|
+
view.setContainerSlotHeightsJson("[0,0,60,0]")
|
|
1368
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 30, generation: 1, dataRows: true))
|
|
1369
|
+
view.layoutIfNeeded()
|
|
1370
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1371
|
+
list.layoutIfNeeded()
|
|
1372
|
+
drainMainQueue()
|
|
1373
|
+
list.contentOffset.y = 1100
|
|
1374
|
+
list.layoutIfNeeded()
|
|
1375
|
+
|
|
1376
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 25, generation: 2,
|
|
1377
|
+
transitionID: "collapse-with-patch", durationMs: 800, dataRows: true))
|
|
1378
|
+
waitForStructuralFrames(0.3)
|
|
1379
|
+
let source = try XCTUnwrap(list.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
|
|
1380
|
+
let tail = try XCTUnwrap(source.indexPath(for: "row-25"))
|
|
1381
|
+
let retiring = try XCTUnwrap(list.cellForItem(at: tail) as? NativeListRowHost)
|
|
1382
|
+
let heightBeforePatch = retiring.bounds.height
|
|
1383
|
+
XCTAssertGreaterThan(heightBeforePatch, 0)
|
|
1384
|
+
XCTAssertLessThan(heightBeforePatch, 60)
|
|
1385
|
+
let patch: [[String: Any]] = [[
|
|
1386
|
+
"key": "row-20", "type": "dataRow", "changes": [
|
|
1387
|
+
"revision": 2,
|
|
1388
|
+
"columns": [["key": "asset", "text": "Asset"],
|
|
1389
|
+
["key": "price", "text": "$123.45"]],
|
|
1390
|
+
],
|
|
1391
|
+
]]
|
|
1392
|
+
view.applyPatchesJson(String(decoding: try JSONSerialization.data(withJSONObject: patch),
|
|
1393
|
+
as: UTF8.self))
|
|
1394
|
+
list.layoutIfNeeded()
|
|
1395
|
+
let patched = try XCTUnwrap(list.cellForItem(at: IndexPath(item: 20, section: 0)))
|
|
1396
|
+
XCTAssertTrue(labels(in: patched).contains { $0.text == "$123.45" })
|
|
1397
|
+
waitForStructuralFrames(0.05)
|
|
1398
|
+
list.layoutIfNeeded()
|
|
1399
|
+
XCTAssertLessThan(retiring.bounds.height, heightBeforePatch)
|
|
1400
|
+
waitForStructuralFrames(0.55)
|
|
1401
|
+
list.layoutIfNeeded()
|
|
1402
|
+
XCTAssertEqual(source.snapshot().itemIdentifiers, (0..<25).map { "row-\($0)" })
|
|
1403
|
+
XCTAssertEqual(list.contentSize.height, 1560, accuracy: 1)
|
|
1404
|
+
XCTAssertEqual(footer.convert(.zero, to: list).y, 1500, accuracy: 1)
|
|
1405
|
+
let finalPatched = try XCTUnwrap(list.cellForItem(at: IndexPath(item: 20, section: 0)))
|
|
1406
|
+
XCTAssertTrue(labels(in: finalPatched).contains { $0.text == "$123.45" })
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
func testStructuralTransitionDoesNotOverrideUserDrag() throws {
|
|
1410
|
+
try requireStructuralAnimation()
|
|
1411
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
|
|
1412
|
+
let controller = UIViewController()
|
|
1413
|
+
window.rootViewController = controller
|
|
1414
|
+
let view = NativeListView(frame: window.bounds)
|
|
1415
|
+
controller.view.addSubview(view)
|
|
1416
|
+
window.isHidden = false
|
|
1417
|
+
defer { view.dispose(); view.removeFromSuperview(); window.isHidden = true }
|
|
1418
|
+
for index in 0..<3 { view.mountContainerSlot(UIView(), atIndex: index) }
|
|
1419
|
+
view.setContainerSlotHeightsJson("[0,0,60,0]")
|
|
1420
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 30, generation: 1))
|
|
1421
|
+
view.layoutIfNeeded()
|
|
1422
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1423
|
+
list.layoutIfNeeded()
|
|
1424
|
+
drainMainQueue()
|
|
1425
|
+
list.contentOffset.y = 1100
|
|
1426
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 25, generation: 2,
|
|
1427
|
+
transitionID: "collapse-drag", durationMs: 400))
|
|
1428
|
+
waitForStructuralFrames(0.08)
|
|
1429
|
+
view.scrollViewWillBeginDragging(list)
|
|
1430
|
+
list.contentOffset.y = 600
|
|
1431
|
+
list.layoutIfNeeded()
|
|
1432
|
+
waitForStructuralFrames(0.15)
|
|
1433
|
+
XCTAssertEqual(list.contentOffset.y, 600, accuracy: 1)
|
|
1434
|
+
waitForStructuralFrames(0.35)
|
|
1435
|
+
list.layoutIfNeeded()
|
|
1436
|
+
XCTAssertEqual(list.contentOffset.y, 600, accuracy: 1)
|
|
1437
|
+
let source = try XCTUnwrap(list.dataSource as? UICollectionViewDiffableDataSource<Int, String>)
|
|
1438
|
+
XCTAssertEqual(source.snapshot().itemIdentifiers, (0..<25).map { "row-\($0)" })
|
|
1439
|
+
XCTAssertEqual(list.contentSize.height, 1560, accuracy: 1)
|
|
1440
|
+
XCTAssertTrue(list.visibleCells.allSatisfy { abs($0.alpha - 1) < 0.01 })
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
func testStructuralTransitionDetachAndDisposeReleaseView() throws {
|
|
1444
|
+
try requireStructuralAnimation()
|
|
1445
|
+
for dispose in [false, true] {
|
|
1446
|
+
var callbacks = 0
|
|
1447
|
+
let reference = try detachStructuralTransition(dispose: dispose) { callbacks += 1 }
|
|
1448
|
+
if dispose {
|
|
1449
|
+
drainMainQueue()
|
|
1450
|
+
XCTAssertNil(reference.view)
|
|
1451
|
+
let countAtDispose = callbacks
|
|
1452
|
+
waitForStructuralFrames(0.5)
|
|
1453
|
+
XCTAssertEqual(callbacks, countAtDispose)
|
|
1454
|
+
} else {
|
|
1455
|
+
waitForStructuralFrames(0.5)
|
|
1456
|
+
XCTAssertNil(reference.view)
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
private final class WeakListReference {
|
|
1462
|
+
weak var view: NativeListView?
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
private func detachStructuralTransition(
|
|
1466
|
+
dispose: Bool, onCallback: @escaping () -> Void
|
|
1467
|
+
) throws -> WeakListReference {
|
|
1468
|
+
try autoreleasepool {
|
|
1469
|
+
let reference = WeakListReference()
|
|
1470
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 600))
|
|
1471
|
+
let controller = UIViewController()
|
|
1472
|
+
window.rootViewController = controller
|
|
1473
|
+
let view = NativeListView(frame: window.bounds)
|
|
1474
|
+
controller.view.addSubview(view)
|
|
1475
|
+
window.isHidden = false
|
|
1476
|
+
for index in 0..<3 { view.mountContainerSlot(UIView(), atIndex: index) }
|
|
1477
|
+
view.setContainerSlotHeightsJson("[0,0,60,0]")
|
|
1478
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 30, generation: 1))
|
|
1479
|
+
view.layoutIfNeeded()
|
|
1480
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1481
|
+
view.onVisibleRangeChanged = { _ in onCallback() }
|
|
1482
|
+
view.onEndReached = { _ in onCallback() }
|
|
1483
|
+
list.layoutIfNeeded()
|
|
1484
|
+
drainMainQueue()
|
|
1485
|
+
view.applySnapshotJson(try structuralRowsSnapshot(count: 25, generation: 2,
|
|
1486
|
+
transitionID: dispose ? "dispose-mid-transition" : "detach-mid-transition",
|
|
1487
|
+
durationMs: 400))
|
|
1488
|
+
waitForStructuralFrames(0.08)
|
|
1489
|
+
reference.view = view
|
|
1490
|
+
if dispose {
|
|
1491
|
+
view.dispose()
|
|
1492
|
+
XCTAssertNil(list.delegate)
|
|
1493
|
+
XCTAssertNil(list.dataSource)
|
|
1494
|
+
}
|
|
1495
|
+
view.removeFromSuperview()
|
|
1496
|
+
window.isHidden = true
|
|
1497
|
+
window.rootViewController = nil
|
|
1498
|
+
return reference
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
private func requireStructuralAnimation() throws {
|
|
1503
|
+
if UIAccessibility.isReduceMotionEnabled {
|
|
1504
|
+
throw XCTSkip("Structural animation is disabled by Reduce Motion")
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
private func waitForStructuralFrames(_ seconds: TimeInterval) {
|
|
1509
|
+
let completed = expectation(description: "Native structural display link ran for \(seconds)s")
|
|
1510
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) { completed.fulfill() }
|
|
1511
|
+
wait(for: [completed], timeout: seconds + 1)
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
private func labels(in view: UIView) -> [UILabel] {
|
|
1515
|
+
let current = (view as? UILabel).map { [$0] } ?? []
|
|
1516
|
+
return current + view.subviews.flatMap(labels(in:))
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
private func structuralRowsSnapshot(
|
|
1520
|
+
count: Int, generation: Int? = nil, transitionID: String? = nil,
|
|
1521
|
+
durationMs: Int = 80, dataRows: Bool = false
|
|
1522
|
+
) throws -> String {
|
|
1523
|
+
let rows: [[String: Any]] = (0..<count).map {
|
|
1524
|
+
if dataRows {
|
|
1525
|
+
return ["key": "row-\($0)", "type": "dataRow", "height": 60,
|
|
1526
|
+
"columns": [["key": "asset", "text": "Asset"],
|
|
1527
|
+
["key": "price", "text": "$1.00"]]]
|
|
1528
|
+
}
|
|
1529
|
+
return ["key": "row-\($0)", "type": "action", "title": "Row", "height": 60]
|
|
1530
|
+
}
|
|
1531
|
+
var value: [String: Any] = [
|
|
1532
|
+
"schemaVersion": 1, "generation": generation ?? count + 1,
|
|
1533
|
+
"layout": ["kind": "linear"], "rows": rows,
|
|
1534
|
+
]
|
|
1535
|
+
if let transitionID {
|
|
1536
|
+
value["structuralTransition"] = [
|
|
1537
|
+
"id": transitionID, "durationMs": durationMs, "anchorKey": "row-24",
|
|
1538
|
+
]
|
|
1539
|
+
}
|
|
1540
|
+
return String(decoding: try JSONSerialization.data(withJSONObject: value), as: UTF8.self)
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
private func assertHomeSidecarGeometry(sidecarBeforeFirstLayout: Bool) throws {
|
|
1544
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 1376, height: 900))
|
|
1545
|
+
let controller = UIViewController()
|
|
1546
|
+
window.rootViewController = controller
|
|
1547
|
+
let view = NativeListView(frame: window.bounds)
|
|
1548
|
+
controller.view.addSubview(view)
|
|
1549
|
+
window.isHidden = false
|
|
1550
|
+
defer {
|
|
1551
|
+
view.dispose()
|
|
1552
|
+
view.removeFromSuperview()
|
|
1553
|
+
window.isHidden = true
|
|
1554
|
+
window.rootViewController = nil
|
|
1555
|
+
}
|
|
1556
|
+
for index in 0..<4 { view.mountContainerSlot(UIView(), atIndex: index) }
|
|
1557
|
+
view.setContainerSlotHeightsJson("[0,0,0,1200]")
|
|
1558
|
+
let sidecar = "{\"width\":400,\"gap\":24,\"maxContentWidth\":1140}"
|
|
1559
|
+
if sidecarBeforeFirstLayout { view.setSidecarLayoutJson(sidecar) }
|
|
1560
|
+
view.applySnapshotJson(try homeSidecarSnapshot())
|
|
1561
|
+
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|
|
1562
|
+
list.contentInsetAdjustmentBehavior = .never
|
|
1563
|
+
func layout() {
|
|
1564
|
+
window.layoutIfNeeded()
|
|
1565
|
+
view.layoutIfNeeded()
|
|
1566
|
+
list.layoutIfNeeded()
|
|
1567
|
+
}
|
|
1568
|
+
func assertRows(x: CGFloat, width: CGFloat) {
|
|
1569
|
+
let cells = list.visibleCells.compactMap { $0 as? NativeListDataRowCell }
|
|
1570
|
+
XCTAssertFalse(cells.isEmpty)
|
|
1571
|
+
for cell in cells {
|
|
1572
|
+
let frame = cell.convert(cell.bounds, to: list)
|
|
1573
|
+
XCTAssertEqual(frame.minX, x, accuracy: 1)
|
|
1574
|
+
XCTAssertEqual(frame.width, width, accuracy: 1)
|
|
1575
|
+
if let path = list.indexPath(for: cell),
|
|
1576
|
+
let attributes = list.layoutAttributesForItem(at: path)
|
|
1577
|
+
{
|
|
1578
|
+
XCTAssertEqual(attributes.frame.minX, x, accuracy: 1)
|
|
1579
|
+
XCTAssertEqual(attributes.frame.width, width, accuracy: 1)
|
|
1580
|
+
}
|
|
1581
|
+
let root = cell.root.convert(cell.root.bounds, to: list)
|
|
1582
|
+
XCTAssertGreaterThanOrEqual(root.minX, frame.minX - 1)
|
|
1583
|
+
XCTAssertLessThanOrEqual(root.maxX, frame.maxX + 1)
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
layout()
|
|
1587
|
+
if !sidecarBeforeFirstLayout {
|
|
1588
|
+
assertRows(x: 8, width: 1360)
|
|
1589
|
+
view.setSidecarLayoutJson(sidecar)
|
|
1590
|
+
layout()
|
|
1591
|
+
}
|
|
1592
|
+
assertRows(x: 126, width: 700)
|
|
1593
|
+
list.contentOffset.y = 18 * 60
|
|
1594
|
+
layout()
|
|
1595
|
+
assertRows(x: 126, width: 700)
|
|
1596
|
+
let patch: [[String: Any]] = [[
|
|
1597
|
+
"key": "row-20", "type": "dataRow",
|
|
1598
|
+
"changes": ["columns": homeSidecarColumns(20, updated: true),
|
|
1599
|
+
"trailing": [["kind": "button", "label": "Swap", "actionKey": "swap"]]],
|
|
1600
|
+
]]
|
|
1601
|
+
view.applyPatchesJson(String(decoding: try JSONSerialization.data(withJSONObject: patch),
|
|
1602
|
+
as: UTF8.self))
|
|
1603
|
+
layout()
|
|
1604
|
+
assertRows(x: 126, width: 700)
|
|
1605
|
+
window.frame.size.width = 1024
|
|
1606
|
+
controller.view.frame = window.bounds
|
|
1607
|
+
view.frame = window.bounds
|
|
1608
|
+
layout()
|
|
1609
|
+
assertRows(x: 8, width: 584)
|
|
1610
|
+
window.frame.size.width = 1376
|
|
1611
|
+
controller.view.frame = window.bounds
|
|
1612
|
+
view.frame = window.bounds
|
|
1613
|
+
layout()
|
|
1614
|
+
assertRows(x: 126, width: 700)
|
|
1615
|
+
view.setSidecarLayoutJson("")
|
|
1616
|
+
layout()
|
|
1617
|
+
assertRows(x: 8, width: 1360)
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
private func homeSidecarSnapshot() throws -> String {
|
|
1621
|
+
let rows: [[String: Any]] = (0..<30).map { index in
|
|
1622
|
+
["key": "row-\(index)", "type": "dataRow", "height": 60,
|
|
1623
|
+
"leading": ["kind": "token", "fallbackIcon": ["name": "CryptoCoinOutline"]],
|
|
1624
|
+
"columns": homeSidecarColumns(index, updated: false),
|
|
1625
|
+
"trailing": [["kind": "button", "label": "Swap", "actionKey": "swap"]],
|
|
1626
|
+
"style": ["horizontalPadding": 12, "verticalPadding": 8, "leadingGap": 12,
|
|
1627
|
+
"image": ["width": 32, "height": 32]]]
|
|
1628
|
+
}
|
|
1629
|
+
let snapshot: [String: Any] = [
|
|
1630
|
+
"schemaVersion": 1, "generation": 1,
|
|
1631
|
+
"layout": ["kind": "linear", "contentPaddingHorizontal": 8], "rows": rows,
|
|
1632
|
+
]
|
|
1633
|
+
return String(decoding: try JSONSerialization.data(withJSONObject: snapshot), as: UTF8.self)
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
private func homeSidecarColumns(_ index: Int, updated: Bool) -> [[String: Any]] {
|
|
1637
|
+
[
|
|
1638
|
+
["key": "asset", "text": "Asset \(index)", "secondaryText": "Public fixture", "weight": 1],
|
|
1639
|
+
["key": "balance", "text": updated ? "0.2966" : "0.000175",
|
|
1640
|
+
"secondaryText": "$0.13", "alignment": "end", "weight": 1],
|
|
1641
|
+
["key": "price", "text": updated ? "$739.22" : "$0.9989",
|
|
1642
|
+
"secondaryText": "-0.04%", "alignment": "end", "weight": 1],
|
|
1643
|
+
]
|
|
1644
|
+
}
|
|
1645
|
+
|
|
366
1646
|
private func makeScrollingList() throws -> NativeListView {
|
|
367
1647
|
let view = NativeListView(frame: CGRect(x: 0, y: 0, width: 320, height: 708))
|
|
368
1648
|
for index in 0..<3 { view.mountContainerSlot(UIView(), atIndex: index) }
|
|
369
|
-
view.setContainerSlotHeightsJson("[44,44,44]")
|
|
1649
|
+
view.setContainerSlotHeightsJson("[44,44,44,0]")
|
|
370
1650
|
view.applySnapshotJson(try scrollingSnapshot(count: 40))
|
|
371
1651
|
view.layoutIfNeeded()
|
|
372
1652
|
let list = try XCTUnwrap(view.subviews.compactMap { $0 as? UICollectionView }.first)
|