123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
- // file at the top-level directory of this distribution and at
- // https://github.com/go-vgo/robotgo/blob/master/LICENSE
- //
- // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
- // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
- // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
- // option. This file may not be copied, modified, or distributed
- // except according to those terms.
- //go:build darwin || windows
- // +build darwin windows
- package robotgo
- import (
- "testing"
- "github.com/vcaesar/tt"
- )
- func TestColor(t *testing.T) {
- s := GetPixelColor(10, 10)
- tt.IsType(t, "string", s)
- tt.NotEmpty(t, s)
- c := GetPxColor(10, 10)
- s1 := PadHex(c)
- tt.Equal(t, s, s1)
- }
- func TestSize(t *testing.T) {
- x, y := GetScreenSize()
- tt.NotZero(t, x)
- tt.NotZero(t, y)
- x, y = GetScaleSize()
- tt.NotZero(t, x)
- tt.NotZero(t, y)
- }
- func TestMoveMouse(t *testing.T) {
- Move(20, 20)
- MilliSleep(50)
- x, y := Location()
- tt.Equal(t, 20, x)
- tt.Equal(t, 20, y)
- }
- func TestMoveMouseSmooth(t *testing.T) {
- b := MoveSmooth(100, 100)
- MilliSleep(50)
- x, y := Location()
- tt.True(t, b)
- tt.Equal(t, 100, x)
- tt.Equal(t, 100, y)
- }
- func TestDragMouse(t *testing.T) {
- DragSmooth(500, 500)
- MilliSleep(50)
- x, y := Location()
- tt.Equal(t, 500, x)
- tt.Equal(t, 500, y)
- }
- func TestScrollMouse(t *testing.T) {
- ScrollDir(120, "up")
- ScrollDir(100, "right")
- Scroll(0, 120)
- MilliSleep(100)
- Scroll(210, 210)
- MilliSleep(10)
- }
- func TestMoveRelative(t *testing.T) {
- Move(200, 200)
- MilliSleep(50)
- MoveRelative(10, -10)
- MilliSleep(50)
- x, y := Location()
- tt.Equal(t, 210, x)
- tt.Equal(t, 190, y)
- }
- func TestMoveSmoothRelative(t *testing.T) {
- Move(200, 200)
- MilliSleep(50)
- MoveSmoothRelative(10, -10)
- MilliSleep(50)
- x, y := Location()
- tt.Equal(t, 210, x)
- tt.Equal(t, 190, y)
- }
- func TestMouseToggle(t *testing.T) {
- e := Toggle("right")
- tt.Nil(t, e)
- e = Toggle("right", "up")
- tt.Nil(t, e)
- e = MouseDown("left")
- tt.Nil(t, e)
- e = MouseUp("left")
- tt.Nil(t, e)
- }
- func TestKey(t *testing.T) {
- e := KeyTap("v", "cmd")
- tt.Nil(t, e)
- e = KeyTap("enter")
- tt.Nil(t, e)
- e = KeyToggle("v", "up")
- tt.Nil(t, e)
- e = KeyDown("a")
- tt.Nil(t, e)
- e = KeyUp("a")
- tt.Nil(t, e)
- e = KeyPress("b")
- tt.Nil(t, e)
- }
- func TestClip(t *testing.T) {
- err := WriteAll("s")
- tt.Nil(t, err)
- s, e := ReadAll()
- tt.Equal(t, "s", s)
- tt.Nil(t, e)
- }
- func TestTypeStr(t *testing.T) {
- c := CharCodeAt("s", 0)
- tt.Equal(t, 115, c)
- e := PasteStr("s")
- tt.Nil(t, e)
- s1 := "abc\\\\cd/s@世界"
- uc := ToUC(s1)
- tt.Equal(t, "[a b c \\ \\ c d / s @ U4e16 U754c]", uc)
- }
- func TestKeyCode(t *testing.T) {
- m := MouseMap["left"]
- tt.Equal(t, 1, m)
- k := Keycode["1"]
- tt.Equal(t, 2, k)
- s := Special["+"]
- tt.Equal(t, "=", s)
- tt.Equal(t, "0", Key0)
- tt.Equal(t, "a", KeyA)
- }
- func TestImage(t *testing.T) {
- bit := CaptureScreen()
- defer FreeBitmap(bit)
- tt.NotNil(t, bit)
- img := ToImage(bit)
- err := SavePng(img, "robot_test.png")
- tt.Nil(t, err)
- img1, err := CaptureImg(10, 10, 20, 20)
- tt.Nil(t, err)
- e := Save(img1, "robot_img.jpeg", 50)
- tt.Nil(t, e)
- tt.Equal(t, 20, Width(img1))
- tt.Equal(t, 20, Height(img1))
- bit1 := ImgToBitmap(img1)
- tt.Equal(t, bit1.Width, Width(img1))
- tt.Equal(t, bit1.Height, Height(img1))
- }
- func TestPs(t *testing.T) {
- id, err := Pids()
- tt.Not(t, "[]", id)
- tt.IsType(t, "[]int", id)
- tt.Nil(t, err)
- ps, e := Process()
- tt.Not(t, "[]", ps)
- tt.IsType(t, "[]robotgo.Nps", ps)
- tt.Nil(t, e)
- b, e := PidExists(id[0])
- tt.Bool(t, b)
- tt.Nil(t, e)
- n, e := FindName(id[0])
- tt.NotEmpty(t, n)
- tt.Nil(t, e)
- n1, e := FindNames()
- tt.Not(t, "[]", n1)
- tt.IsType(t, "[]string", n1)
- tt.Nil(t, e)
- id, err = FindIds(n1[0])
- tt.Not(t, "[]", id)
- tt.IsType(t, "[]int", id)
- tt.Nil(t, err)
- if len(id) > 0 {
- e := KeyTap("v", id[0], "cmd")
- tt.Nil(t, e)
- }
- // n, e = FindPath(id[0])
- // tt.NotEmpty(t, n)
- // tt.Nil(t, e)
- }
- // func TestAlert(t *testing.T) {
- // go func() {
- // MilliSleep(200)
- // KeyTap("enter")
- // log.Println("tap...")
- // }()
- // i := Alert("t", "msg")
- // tt.True(t, i)
- // }
|