|
| 1 | +package ui |
| 2 | + |
| 3 | +import ( |
| 4 | + "image" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gioui.org/layout" |
| 8 | + "gioui.org/op" |
| 9 | + "gioui.org/unit" |
| 10 | +) |
| 11 | + |
| 12 | +// makeGtx creates a minimal layout.Context with the given viewport height. |
| 13 | +// Uses 1:1 dp-to-px ratio for test simplicity. |
| 14 | +func makeGtx(viewportHeight int) C { |
| 15 | + var ops op.Ops |
| 16 | + |
| 17 | + return layout.Context{ |
| 18 | + Ops: &ops, |
| 19 | + Constraints: layout.Constraints{ |
| 20 | + Max: image.Pt(800, viewportHeight), |
| 21 | + }, |
| 22 | + Metric: unit.Metric{PxPerDp: 1, PxPerSp: 1}, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestCalculateSmartScrollPositionVariable_AlreadyVisible(t *testing.T) { |
| 27 | + gtx := makeGtx(300) |
| 28 | + heights := []int{50, 50, 50, 50, 50, 50} |
| 29 | + |
| 30 | + // Item 2 is visible when first=0 (items 0-4 fit in 300px with 20px margin) |
| 31 | + newFirst, shouldScroll := calculateSmartScrollPositionVariable(gtx, 0, 2, heights) |
| 32 | + if shouldScroll { |
| 33 | + t.Errorf("Expected no scroll needed, got newFirst=%d", newFirst) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestCalculateSmartScrollPositionVariable_AboveViewport(t *testing.T) { |
| 38 | + gtx := makeGtx(300) |
| 39 | + heights := []int{50, 50, 50, 50, 50, 50, 50, 50, 50, 50} |
| 40 | + |
| 41 | + // currentFirst=5, selected=2 (above viewport) |
| 42 | + newFirst, shouldScroll := calculateSmartScrollPositionVariable(gtx, 5, 2, heights) |
| 43 | + if !shouldScroll { |
| 44 | + t.Fatal("Expected scroll needed") |
| 45 | + } |
| 46 | + |
| 47 | + if newFirst != 2 { |
| 48 | + t.Errorf("Expected newFirst=2, got %d", newFirst) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestCalculateSmartScrollPositionVariable_BelowViewport(t *testing.T) { |
| 53 | + gtx := makeGtx(200) |
| 54 | + heights := []int{50, 50, 50, 50, 50, 50, 50, 50, 50, 50} |
| 55 | + |
| 56 | + // currentFirst=0, selected=8 (below viewport - only ~3 items visible in 200-20=180px) |
| 57 | + newFirst, shouldScroll := calculateSmartScrollPositionVariable(gtx, 0, 8, heights) |
| 58 | + if !shouldScroll { |
| 59 | + t.Fatal("Expected scroll needed") |
| 60 | + } |
| 61 | + // Should scroll so item 8 is near the bottom |
| 62 | + if newFirst > 8 || newFirst < 5 { |
| 63 | + t.Errorf("Expected newFirst between 5 and 8, got %d", newFirst) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestCalculateSmartScrollPositionVariable_EmptyHeights(t *testing.T) { |
| 68 | + gtx := makeGtx(300) |
| 69 | + |
| 70 | + newFirst, shouldScroll := calculateSmartScrollPositionVariable(gtx, 0, 0, []int{}) |
| 71 | + if shouldScroll { |
| 72 | + t.Errorf("Expected no scroll for empty heights, got newFirst=%d", newFirst) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestCalculateSmartScrollPositionVariable_NegativeSelected(t *testing.T) { |
| 77 | + gtx := makeGtx(300) |
| 78 | + heights := []int{50, 50, 50} |
| 79 | + |
| 80 | + newFirst, shouldScroll := calculateSmartScrollPositionVariable(gtx, 0, -1, heights) |
| 81 | + if shouldScroll { |
| 82 | + t.Errorf("Expected no scroll for negative index, got newFirst=%d", newFirst) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestCalculateSmartScrollPositionVariable_LargeItemExceedsViewport(t *testing.T) { |
| 87 | + gtx := makeGtx(100) |
| 88 | + heights := []int{50, 50, 200, 50} // item 2 is larger than viewport |
| 89 | + |
| 90 | + newFirst, shouldScroll := calculateSmartScrollPositionVariable(gtx, 0, 2, heights) |
| 91 | + if !shouldScroll { |
| 92 | + t.Fatal("Expected scroll needed for large item") |
| 93 | + } |
| 94 | + // When item exceeds viewport, it should be placed at the top |
| 95 | + if newFirst != 2 { |
| 96 | + t.Errorf("Expected newFirst=2 for large item, got %d", newFirst) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestCalculateSmartScrollPositionVariable_ZeroViewport(t *testing.T) { |
| 101 | + gtx := makeGtx(0) |
| 102 | + heights := []int{50, 50, 50} |
| 103 | + |
| 104 | + _, shouldScroll := calculateSmartScrollPositionVariable(gtx, 0, 1, heights) |
| 105 | + if shouldScroll { |
| 106 | + t.Error("Expected no scroll for zero viewport") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestCalculateSmartScrollPositionVariable_UnmeasuredItems(t *testing.T) { |
| 111 | + gtx := makeGtx(300) |
| 112 | + // Some items unmeasured (0 height) — should use fallback |
| 113 | + heights := []int{50, 0, 0, 50, 0, 50, 50, 50, 50, 50} |
| 114 | + |
| 115 | + // Should not panic and should produce valid result |
| 116 | + newFirst, _ := calculateSmartScrollPositionVariable(gtx, 0, 8, heights) |
| 117 | + if newFirst < 0 || newFirst > 8 { |
| 118 | + t.Errorf("Expected valid newFirst, got %d", newFirst) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestEstimateFallbackHeight_AllMeasured(t *testing.T) { |
| 123 | + gtx := makeGtx(300) |
| 124 | + heights := []int{40, 60, 50} |
| 125 | + got := estimateFallbackHeight(gtx, heights) |
| 126 | + |
| 127 | + want := 50 // (40+60+50)/3 |
| 128 | + if got != want { |
| 129 | + t.Errorf("estimateFallbackHeight = %d, want %d", got, want) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestEstimateFallbackHeight_NoneMeasured(t *testing.T) { |
| 134 | + gtx := makeGtx(300) |
| 135 | + heights := []int{0, 0, 0} |
| 136 | + got := estimateFallbackHeight(gtx, heights) |
| 137 | + // Should use TreeRowHeight + TreeRowInsetHeight via gtx.Dp |
| 138 | + if got <= 0 { |
| 139 | + t.Errorf("estimateFallbackHeight = %d, want > 0", got) |
| 140 | + } |
| 141 | +} |
0 commit comments