robotgo_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //go:build darwin || windows
  11. // +build darwin windows
  12. package robotgo
  13. import (
  14. "testing"
  15. "github.com/vcaesar/tt"
  16. )
  17. func TestColor(t *testing.T) {
  18. s := GetPixelColor(10, 10)
  19. tt.IsType(t, "string", s)
  20. tt.NotEmpty(t, s)
  21. c := GetPxColor(10, 10)
  22. s1 := PadHex(c)
  23. tt.Equal(t, s, s1)
  24. }
  25. func TestSize(t *testing.T) {
  26. x, y := GetScreenSize()
  27. tt.NotZero(t, x)
  28. tt.NotZero(t, y)
  29. x, y = GetScaleSize()
  30. tt.NotZero(t, x)
  31. tt.NotZero(t, y)
  32. }
  33. func TestMoveMouse(t *testing.T) {
  34. Move(20, 20)
  35. MilliSleep(50)
  36. x, y := Location()
  37. tt.Equal(t, 20, x)
  38. tt.Equal(t, 20, y)
  39. }
  40. func TestMoveMouseSmooth(t *testing.T) {
  41. b := MoveSmooth(100, 100)
  42. MilliSleep(50)
  43. x, y := Location()
  44. tt.True(t, b)
  45. tt.Equal(t, 100, x)
  46. tt.Equal(t, 100, y)
  47. }
  48. func TestDragMouse(t *testing.T) {
  49. DragSmooth(500, 500)
  50. MilliSleep(50)
  51. x, y := Location()
  52. tt.Equal(t, 500, x)
  53. tt.Equal(t, 500, y)
  54. }
  55. func TestScrollMouse(t *testing.T) {
  56. ScrollDir(120, "up")
  57. ScrollDir(100, "right")
  58. Scroll(0, 120)
  59. MilliSleep(100)
  60. Scroll(210, 210)
  61. MilliSleep(10)
  62. }
  63. func TestMoveRelative(t *testing.T) {
  64. Move(200, 200)
  65. MilliSleep(50)
  66. MoveRelative(10, -10)
  67. MilliSleep(50)
  68. x, y := Location()
  69. tt.Equal(t, 210, x)
  70. tt.Equal(t, 190, y)
  71. }
  72. func TestMoveSmoothRelative(t *testing.T) {
  73. Move(200, 200)
  74. MilliSleep(50)
  75. MoveSmoothRelative(10, -10)
  76. MilliSleep(50)
  77. x, y := Location()
  78. tt.Equal(t, 210, x)
  79. tt.Equal(t, 190, y)
  80. }
  81. func TestMouseToggle(t *testing.T) {
  82. e := Toggle("right")
  83. tt.Nil(t, e)
  84. e = Toggle("right", "up")
  85. tt.Nil(t, e)
  86. e = MouseDown("left")
  87. tt.Nil(t, e)
  88. e = MouseUp("left")
  89. tt.Nil(t, e)
  90. }
  91. func TestKey(t *testing.T) {
  92. e := KeyTap("v", "cmd")
  93. tt.Nil(t, e)
  94. e = KeyTap("enter")
  95. tt.Nil(t, e)
  96. e = KeyToggle("v", "up")
  97. tt.Nil(t, e)
  98. e = KeyDown("a")
  99. tt.Nil(t, e)
  100. e = KeyUp("a")
  101. tt.Nil(t, e)
  102. e = KeyPress("b")
  103. tt.Nil(t, e)
  104. }
  105. func TestClip(t *testing.T) {
  106. err := WriteAll("s")
  107. tt.Nil(t, err)
  108. s, e := ReadAll()
  109. tt.Equal(t, "s", s)
  110. tt.Nil(t, e)
  111. }
  112. func TestTypeStr(t *testing.T) {
  113. c := CharCodeAt("s", 0)
  114. tt.Equal(t, 115, c)
  115. e := PasteStr("s")
  116. tt.Nil(t, e)
  117. s1 := "abc\\\\cd/s@世界"
  118. uc := ToUC(s1)
  119. tt.Equal(t, "[a b c \\ \\ c d / s @ U4e16 U754c]", uc)
  120. }
  121. func TestKeyCode(t *testing.T) {
  122. m := MouseMap["left"]
  123. tt.Equal(t, 1, m)
  124. k := Keycode["1"]
  125. tt.Equal(t, 2, k)
  126. s := Special["+"]
  127. tt.Equal(t, "=", s)
  128. tt.Equal(t, "0", Key0)
  129. tt.Equal(t, "a", KeyA)
  130. }
  131. func TestImage(t *testing.T) {
  132. bit := CaptureScreen()
  133. defer FreeBitmap(bit)
  134. tt.NotNil(t, bit)
  135. img := ToImage(bit)
  136. err := SavePng(img, "robot_test.png")
  137. tt.Nil(t, err)
  138. img1, err := CaptureImg(10, 10, 20, 20)
  139. tt.Nil(t, err)
  140. e := Save(img1, "robot_img.jpeg", 50)
  141. tt.Nil(t, e)
  142. tt.Equal(t, 20, Width(img1))
  143. tt.Equal(t, 20, Height(img1))
  144. bit1 := ImgToBitmap(img1)
  145. tt.Equal(t, bit1.Width, Width(img1))
  146. tt.Equal(t, bit1.Height, Height(img1))
  147. }
  148. func TestPs(t *testing.T) {
  149. id, err := Pids()
  150. tt.Not(t, "[]", id)
  151. tt.IsType(t, "[]int", id)
  152. tt.Nil(t, err)
  153. ps, e := Process()
  154. tt.Not(t, "[]", ps)
  155. tt.IsType(t, "[]robotgo.Nps", ps)
  156. tt.Nil(t, e)
  157. b, e := PidExists(id[0])
  158. tt.Bool(t, b)
  159. tt.Nil(t, e)
  160. n, e := FindName(id[0])
  161. tt.NotEmpty(t, n)
  162. tt.Nil(t, e)
  163. n1, e := FindNames()
  164. tt.Not(t, "[]", n1)
  165. tt.IsType(t, "[]string", n1)
  166. tt.Nil(t, e)
  167. id, err = FindIds(n1[0])
  168. tt.Not(t, "[]", id)
  169. tt.IsType(t, "[]int", id)
  170. tt.Nil(t, err)
  171. if len(id) > 0 {
  172. e := KeyTap("v", id[0], "cmd")
  173. tt.Nil(t, e)
  174. }
  175. // n, e = FindPath(id[0])
  176. // tt.NotEmpty(t, n)
  177. // tt.Nil(t, e)
  178. }
  179. // func TestAlert(t *testing.T) {
  180. // go func() {
  181. // MilliSleep(200)
  182. // KeyTap("enter")
  183. // log.Println("tap...")
  184. // }()
  185. // i := Alert("t", "msg")
  186. // tt.True(t, i)
  187. // }