package main import ( "math" "testing" ) func fixtureFrame(w, h int) pointerFrame { return pointerFrame{"current", w, h, obj{"left": -2560, "top": 120, "width": 2560, "height": 1540}} } func TestScrollPixelsReachUpstreamWithoutDoubleScaling(t *testing.T) { for _, size := range [][2]int{{1280, 770}, {2558, 1538}, {2560, 1540}} { frame := fixtureFrame(size[0], size[1]) args := obj{"pid": 1, "window_id": 2, "capture_id": "current", "x": 900.5, "y": 600.25, "direction": "down"} if err := frame.validate("scroll", args); err != nil { t.Fatal(err) } got := withoutCaptureBinding(args) if got["x"] != 900.5 || got["y"] != 600.25 || got["pid"] != 1 || got["window_id"] != 2 { t.Fatal(got) } if _, ok := got["capture_id"]; ok { t.Fatal("upstream scroll rejects capture_id") } if args["capture_id"] != "current" { t.Fatal("input evidence mutated") } } } func TestInvalidScrollCoordinatesAreRejectedBeforeDispatch(t *testing.T) { frame := fixtureFrame(1280, 770) for _, bad := range []obj{ {"capture_id": "current", "x": 1700, "y": 1000}, {"capture_id": "old", "x": 900, "y": 600}, {"x": 900, "y": 600}, {"capture_id": "current", "x": -1, "y": 0}, {"capture_id": "current", "x": 1280, "y": 0}, {"capture_id": "current", "x": 0, "y": 770}, {"capture_id": "current", "x": "900", "y": 600}, {"capture_id": "current", "x": math.NaN(), "y": 600}, {"capture_id": "current", "x": 900, "y": math.Inf(1)}, } { if frame.validate("scroll", bad) == nil { t.Fatalf("accepted %v", bad) } } } func TestMovedOrResizedWindowInvalidatesFrame(t *testing.T) { frame := fixtureFrame(1280, 770) for _, key := range []string{"left", "top", "width", "height"} { changed := copyArgs(frame.window) changed[key] = num(changed[key]) + 1 if sameWindow(frame.window, changed) { t.Fatal("accepted changed " + key) } } } func TestClickAndBothDragEndpointsUseLatestFrame(t *testing.T) { f := fixtureFrame(1280, 770) click := obj{"capture_id": "current", "x": 1279, "y": 769} if err := f.validate("click_at", click); err != nil { t.Fatal(err) } if click["x"] != 1279 || click["y"] != 769 { t.Fatal("click scaled twice") } drag := obj{"capture_id": "current", "from_x": 0, "from_y": 0, "to_x": 1279, "to_y": 769} if err := f.validate("drag", drag); err != nil { t.Fatal(err) } for _, key := range []string{"from_x", "from_y", "to_x", "to_y"} { bad := copyArgs(drag) bad[key] = 3000 if f.validate("drag", bad) == nil { t.Fatal("accepted out of bounds " + key) } } drag["capture_id"] = "old" if f.validate("drag", drag) == nil { t.Fatal("accepted stale drag") } } func TestAllPixelSchemasRequireCapture(t *testing.T) { a := newAdapter(nil) for _, name := range []string{"click_at", "scroll", "drag"} { found := false for _, key := range items(a.schemas[name]["required"]) { if key == "capture_id" { found = true } } if !found { t.Fatal(name + " lacks capture binding") } } }