package web import "testing" // Chrome's smooth scrolling splits one physical notch (100px) into a few // fractional wheel events. Each event must not become a notch on its own, and // the whole physical notch must inject exactly one. func TestWheelNotchesSmoothScrollSequence(t *testing.T) { var residue float64 seq := []float64{33.33, 33.33, 33.34} total := 0 for _, d := range seq { total += abs(wheelNotches(&residue, d, 0)) } if total != 1 { t.Fatalf("one 100px notch in %d events gave %d notches, want 1", len(seq), total) } if residue > 0.5 || residue < -0.5 { t.Fatalf("residue must stay within half a notch, got %v", residue) } } // Touchpads report sub-pixel deltas: they used to truncate to 0 and the wheel // did nothing at all. They must accumulate into one notch. func TestWheelNotchesSubPixelDeltas(t *testing.T) { var residue float64 got := 0 for i := 0; i < 167; i++ { // 167 events x 0.6px = 100.2px got += wheelNotches(&residue, 0.6, 0) } if got != 1 { t.Fatalf("167 x 0.6px gave %d notches, want 1", got) } } // The same split in three equal parts must also land on exactly one notch: // 33.33*3 is 99.99 in float, and truncating would never reach a whole notch. func TestWheelNotchesEqualThirds(t *testing.T) { var residue float64 got := 0 for i := 0; i < 3; i++ { got += wheelNotches(&residue, 33.33, 0) } if got != 1 { t.Fatalf("3 x 33.33px gave %d notches, want 1", got) } } func TestWheelNotchesDirection(t *testing.T) { var residue float64 if n := wheelNotches(&residue, 100, 0); n != 1 { t.Fatalf("down: got %d notches, want 1", n) } if n := wheelNotches(&residue, -100, 0); n != -1 { t.Fatalf("up: got %d notches, want -1", n) } // A half-scrolled notch does not block a reversal: the carry is a position, // so 40px down then a full notch up is one notch up. residue = 0.4 if n := wheelNotches(&residue, -100, 0); n != -1 { t.Fatalf("flip: got %d notches, want -1", n) } } func TestWheelNotchesDeltaModes(t *testing.T) { var pixel, line, page float64 if n := wheelNotches(&pixel, 100, 0); n != 1 { t.Fatalf("pixel mode: got %d, want 1", n) } if n := wheelNotches(&line, 3, 1); n != 1 { t.Fatalf("line mode: got %d, want 1", n) } if n := wheelNotches(&page, 1, 2); n != 1 { t.Fatalf("page mode: got %d, want 1", n) } } func TestWheelNotchesClamp(t *testing.T) { var residue float64 if n := wheelNotches(&residue, 100000, 0); n != maxWheelNotches { t.Fatalf("huge delta gave %d notches, want %d", n, maxWheelNotches) } if n := wheelNotches(&residue, -100000, 0); n != -maxWheelNotches { t.Fatalf("huge negative delta gave %d notches, want %d", n, -maxWheelNotches) } } func abs(n int) int { if n < 0 { return -n } return n }