app_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package app_test
  5. import (
  6. "fmt"
  7. "image"
  8. "image/color"
  9. _ "image/png"
  10. "net"
  11. "os"
  12. "os/exec"
  13. "strings"
  14. "testing"
  15. "time"
  16. "golang.org/x/mobile/app/internal/apptest"
  17. "golang.org/x/mobile/event/size"
  18. )
  19. // TestAndroidApp tests the lifecycle, event, and window semantics of a
  20. // simple android app.
  21. //
  22. // Beyond testing the app package, the goal is to eventually have
  23. // helper libraries that make tests like these easy to write. Hopefully
  24. // having a user of such a fictional package will help illuminate the way.
  25. func TestAndroidApp(t *testing.T) {
  26. t.Skip("see issue #23835")
  27. if _, err := exec.Command("which", "adb").CombinedOutput(); err != nil {
  28. t.Skip("command adb not found, skipping")
  29. }
  30. devicesTxt, err := exec.Command("adb", "devices").CombinedOutput()
  31. if err != nil {
  32. t.Errorf("adb devices failed: %v: %v", err, devicesTxt)
  33. }
  34. deviceCount := 0
  35. for _, d := range strings.Split(strings.TrimSpace(string(devicesTxt)), "\n") {
  36. if strings.Contains(d, "List of devices") {
  37. continue
  38. }
  39. // TODO(crawshaw): I believe some unusable devices can appear in the
  40. // list with note on them, but I cannot reproduce this right now.
  41. deviceCount++
  42. }
  43. if deviceCount == 0 {
  44. t.Skip("no android devices attached")
  45. }
  46. run(t, "gomobile", "version")
  47. origWD, err := os.Getwd()
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. tmpdir, err := os.MkdirTemp("", "app-test-")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. defer os.RemoveAll(tmpdir)
  56. if err := os.Chdir(tmpdir); err != nil {
  57. t.Fatal(err)
  58. }
  59. defer os.Chdir(origWD)
  60. run(t, "gomobile", "install", "golang.org/x/mobile/app/internal/testapp")
  61. ln, err := net.Listen("tcp4", "localhost:0")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. defer ln.Close()
  66. localaddr := fmt.Sprintf("tcp:%d", ln.Addr().(*net.TCPAddr).Port)
  67. t.Logf("local address: %s", localaddr)
  68. exec.Command("adb", "reverse", "--remove", "tcp:"+apptest.Port).Run() // ignore failure
  69. run(t, "adb", "reverse", "tcp:"+apptest.Port, localaddr)
  70. const (
  71. KeycodePower = "26"
  72. KeycodeUnlock = "82"
  73. )
  74. run(t, "adb", "shell", "input", "keyevent", KeycodePower)
  75. run(t, "adb", "shell", "input", "keyevent", KeycodeUnlock)
  76. const (
  77. rotationPortrait = "0"
  78. rotationLandscape = "1"
  79. )
  80. rotate := func(rotation string) {
  81. run(t, "adb", "shell", "content", "insert", "--uri", "content://settings/system", "--bind", "name:s:user_rotation", "--bind", "value:i:"+rotation)
  82. }
  83. // turn off automatic rotation and start in portrait
  84. run(t, "adb", "shell", "content", "insert", "--uri", "content://settings/system", "--bind", "name:s:accelerometer_rotation", "--bind", "value:i:0")
  85. rotate(rotationPortrait)
  86. // start testapp
  87. run(t,
  88. "adb", "shell", "am", "start", "-n",
  89. "org.golang.testapp/org.golang.app.GoNativeActivity",
  90. )
  91. var conn net.Conn
  92. connDone := make(chan struct{})
  93. go func() {
  94. conn, err = ln.Accept()
  95. connDone <- struct{}{}
  96. }()
  97. select {
  98. case <-time.After(5 * time.Second):
  99. t.Fatal("timeout waiting for testapp to dial host")
  100. case <-connDone:
  101. if err != nil {
  102. t.Fatalf("ln.Accept: %v", err)
  103. }
  104. }
  105. defer conn.Close()
  106. comm := &apptest.Comm{
  107. Conn: conn,
  108. Fatalf: t.Fatalf,
  109. Printf: t.Logf,
  110. }
  111. var pixelsPerPt float32
  112. var orientation size.Orientation
  113. comm.Recv("hello_from_testapp")
  114. comm.Send("hello_from_host")
  115. comm.Recv("lifecycle_visible")
  116. comm.Recv("size", &pixelsPerPt, &orientation)
  117. if pixelsPerPt < 0.1 {
  118. t.Fatalf("bad pixelsPerPt: %f", pixelsPerPt)
  119. }
  120. // A single paint event is sent when the lifecycle enters
  121. // StageVisible, and after the end of a touch event.
  122. var color string
  123. comm.Recv("paint", &color)
  124. // Ignore the first paint color, it may be slow making it to the screen.
  125. rotate(rotationLandscape)
  126. comm.Recv("size", &pixelsPerPt, &orientation)
  127. if want := size.OrientationLandscape; orientation != want {
  128. t.Errorf("want orientation %d, got %d", want, orientation)
  129. }
  130. var x, y int
  131. var ty string
  132. tap(t, 50, 260)
  133. comm.Recv("touch", &ty, &x, &y)
  134. if ty != "begin" || x != 50 || y != 260 {
  135. t.Errorf("want touch begin(50, 260), got %s(%d,%d)", ty, x, y)
  136. }
  137. comm.Recv("touch", &ty, &x, &y)
  138. if ty != "end" || x != 50 || y != 260 {
  139. t.Errorf("want touch end(50, 260), got %s(%d,%d)", ty, x, y)
  140. }
  141. comm.Recv("paint", &color)
  142. if gotColor := currentColor(t); color != gotColor {
  143. t.Errorf("app reports color %q, but saw %q", color, gotColor)
  144. }
  145. rotate(rotationPortrait)
  146. comm.Recv("size", &pixelsPerPt, &orientation)
  147. if want := size.OrientationPortrait; orientation != want {
  148. t.Errorf("want orientation %d, got %d", want, orientation)
  149. }
  150. tap(t, 50, 260)
  151. comm.Recv("touch", &ty, &x, &y) // touch begin
  152. comm.Recv("touch", &ty, &x, &y) // touch end
  153. comm.Recv("paint", &color)
  154. if gotColor := currentColor(t); color != gotColor {
  155. t.Errorf("app reports color %q, but saw %q", color, gotColor)
  156. }
  157. // TODO: lifecycle testing (NOTE: adb shell input keyevent 4 is the back button)
  158. }
  159. func currentColor(t *testing.T) string {
  160. file := fmt.Sprintf("app-screen-%d.png", time.Now().Unix())
  161. run(t, "adb", "shell", "screencap", "-p", "/data/local/tmp/"+file)
  162. run(t, "adb", "pull", "/data/local/tmp/"+file)
  163. run(t, "adb", "shell", "rm", "/data/local/tmp/"+file)
  164. defer os.Remove(file)
  165. f, err := os.Open(file)
  166. if err != nil {
  167. t.Errorf("currentColor: cannot open screencap: %v", err)
  168. return ""
  169. }
  170. m, _, err := image.Decode(f)
  171. if err != nil {
  172. t.Errorf("currentColor: cannot decode screencap: %v", err)
  173. return ""
  174. }
  175. var center color.Color
  176. {
  177. b := m.Bounds()
  178. x, y := b.Min.X+(b.Max.X-b.Min.X)/2, b.Min.Y+(b.Max.Y-b.Min.Y)/2
  179. center = m.At(x, y)
  180. }
  181. r, g, b, _ := center.RGBA()
  182. switch {
  183. case r == 0xffff && g == 0x0000 && b == 0x0000:
  184. return "red"
  185. case r == 0x0000 && g == 0xffff && b == 0x0000:
  186. return "green"
  187. case r == 0x0000 && g == 0x0000 && b == 0xffff:
  188. return "blue"
  189. default:
  190. return fmt.Sprintf("indeterminate: %v", center)
  191. }
  192. }
  193. func tap(t *testing.T, x, y int) {
  194. run(t, "adb", "shell", "input", "tap", fmt.Sprintf("%d", x), fmt.Sprintf("%d", y))
  195. }
  196. func run(t *testing.T, cmdName string, arg ...string) {
  197. cmd := exec.Command(cmdName, arg...)
  198. t.Log(strings.Join(cmd.Args, " "))
  199. out, err := cmd.CombinedOutput()
  200. if err != nil {
  201. t.Fatalf("%s %v: %s", strings.Join(cmd.Args, " "), err, out)
  202. }
  203. }