|
| 1 | +// |
| 2 | +// LazyTests.swift |
| 3 | +// AndroidSwiftUICoreTests |
| 4 | +// |
| 5 | + |
| 6 | +import Testing |
| 7 | +@testable import AndroidSwiftUICore |
| 8 | + |
| 9 | +struct Item: Identifiable { let id: Int } |
| 10 | + |
| 11 | +@Suite("Lazy containers") |
| 12 | +struct LazyTests { |
| 13 | + |
| 14 | + @Test("List emits count and a provider, not inline rows") |
| 15 | + func listIsLazy() { |
| 16 | + struct Screen: View { |
| 17 | + var body: some View { |
| 18 | + List((1...1000).map(Item.init)) { Text("Row \($0.id)") } |
| 19 | + } |
| 20 | + } |
| 21 | + let node = ViewHost(Screen()).evaluate() |
| 22 | + #expect(node.type == "List") |
| 23 | + #expect(node.count == 1000) |
| 24 | + #expect(node.children.isEmpty) // rows are not materialized up front |
| 25 | + #expect(node.props["itemProvider"] != nil) |
| 26 | + } |
| 27 | + |
| 28 | + @Test("The item provider resolves a row subtree on demand") |
| 29 | + func providerResolvesRow() { |
| 30 | + struct Screen: View { |
| 31 | + var body: some View { |
| 32 | + List((1...100).map(Item.init)) { Text("Row \($0.id)") } |
| 33 | + } |
| 34 | + } |
| 35 | + let host = ViewHost(Screen()) |
| 36 | + let node = host.evaluate() |
| 37 | + guard case .int(let providerID)? = node.props["itemProvider"] else { |
| 38 | + Issue.record("no provider"); return |
| 39 | + } |
| 40 | + // rows 0 and 42 resolve independently, only when asked |
| 41 | + let row0 = host.callbacks.item(Int64(providerID), 0) |
| 42 | + let row42 = host.callbacks.item(Int64(providerID), 42) |
| 43 | + #expect(row0?.props["text"] == .string("Row 1")) |
| 44 | + #expect(row42?.props["text"] == .string("Row 43")) |
| 45 | + } |
| 46 | + |
| 47 | + @Test("Row identity path is keyed by element id") |
| 48 | + func rowKeyedByIdentity() { |
| 49 | + struct Screen: View { |
| 50 | + let items: [Item] |
| 51 | + var body: some View { List(items) { Text("Row \($0.id)") } } |
| 52 | + } |
| 53 | + func rowID(_ items: [Item], index: Int) -> String { |
| 54 | + let host = ViewHost(Screen(items: items)) |
| 55 | + let node = host.evaluate() |
| 56 | + guard case .int(let p)? = node.props["itemProvider"] else { return "" } |
| 57 | + return host.callbacks.item(Int64(p), index)?.id ?? "" |
| 58 | + } |
| 59 | + // element id 5 keeps its identity path whether it's at index 0 or 2 |
| 60 | + let atIndex0 = rowID([Item(id: 5), Item(id: 6)], index: 0) |
| 61 | + let atIndex2 = rowID([Item(id: 3), Item(id: 4), Item(id: 5)], index: 2) |
| 62 | + #expect(atIndex0 == atIndex2) |
| 63 | + } |
| 64 | + |
| 65 | + @Test("refreshable attaches a refresh callback to the list") |
| 66 | + func refreshable() { |
| 67 | + struct Screen: View { |
| 68 | + var body: some View { |
| 69 | + List((1...3).map(Item.init)) { Text("Row \($0.id)") } |
| 70 | + .refreshable { } |
| 71 | + } |
| 72 | + } |
| 73 | + let node = ViewHost(Screen()).evaluate() |
| 74 | + #expect(node.props["onRefresh"] != nil) |
| 75 | + } |
| 76 | + |
| 77 | + @Test("LazyVGrid carries its track spec and resolves cells") |
| 78 | + func vgridFixed() { |
| 79 | + struct Screen: View { |
| 80 | + var body: some View { |
| 81 | + LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) { |
| 82 | + ForEach(1...6, id: \.self) { Text("\($0)") } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + let node = ViewHost(Screen()).evaluate() |
| 87 | + #expect(node.type == "LazyVGrid") |
| 88 | + #expect(node.props["trackCount"] == .int(3)) |
| 89 | + #expect(node.children.count == 6) |
| 90 | + } |
| 91 | + |
| 92 | + @Test("Adaptive grid carries a minimum column width") |
| 93 | + func vgridAdaptive() { |
| 94 | + struct Screen: View { |
| 95 | + var body: some View { |
| 96 | + LazyVGrid(columns: [GridItem(.adaptive(minimum: 80))]) { |
| 97 | + ForEach(1...4, id: \.self) { Text("\($0)") } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + let node = ViewHost(Screen()).evaluate() |
| 102 | + #expect(node.props["adaptiveMin"] == .double(80)) |
| 103 | + #expect(node.props["trackCount"] == nil) |
| 104 | + } |
| 105 | +} |
0 commit comments