| 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 | |
| 5 | // Package apptest provides utilities for testing an app. |
| 6 | // |
| 7 | // It is extremely incomplete, hence it being internal. |
| 8 | // For starters, it should support iOS. |
| 9 | package apptestscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/ |
| 10 | |
| 11 | import ( |
| 12 | "bufioscip-go gomod github.com/golang/go/src go1.24.0 bufio/" |
| 13 | "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/" |
| 14 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 15 | "netscip-go gomod github.com/golang/go/src go1.24.0 net/" |
| 16 | ) |
| 17 | |
| 18 | // Port is the TCP port used to communicate with the test app. |
| 19 | // |
| 20 | // TODO(crawshaw): find a way to make this configurable. adb am extras? |
| 21 | const Portscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Port. = "12533" |
| 22 | |
| 23 | // Comm is a simple text-based communication protocol. |
| 24 | // |
| 25 | // Assumes all sides are friendly and cooperative and that the |
| 26 | // communication is over at the first sign of trouble. |
| 27 | type Commscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm# struct { |
| 28 | Connscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Conn. netscip-go gomod github.com/golang/go/src go1.24.0 net/.Connscip-go gomod github.com/golang/go/src go1.24.0 net/Conn# |
| 29 | Fatalfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Fatalf. func(format string, args ...interface{}) |
| 30 | Printfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Printf. func(format string, args ...interface{}) |
| 31 | |
| 32 | scannerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#scanner. *bufioscip-go gomod github.com/golang/go/src go1.24.0 bufio/.Scannerscip-go gomod github.com/golang/go/src go1.24.0 bufio/Scanner# |
| 33 | } |
| 34 | |
| 35 | func (c *Commscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#) Sendscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Send().(cmd string, args ...interface{}) { |
| 36 | buf := new(bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#) |
| 37 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#WriteString().(cmd) |
| 38 | for _, arg := range args { |
| 39 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#WriteRune().(' ') |
| 40 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(buf, "%v", arg) |
| 41 | } |
| 42 | buf.WriteRunescip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#WriteRune().('\n') |
| 43 | b := buf.Bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#Bytes().() |
| 44 | c.Printfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Printf.("comm.send: %s\n", b) |
| 45 | if _, err := c.Connscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Conn..Writescip-go gomod github.com/golang/go/src go1.24.0 net/Conn#Write().(b); err != nil { |
| 46 | c.Fatalfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Fatalf.("failed to send %s: %v", b, err) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func (c *Commscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#) Recvscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Recv().(cmd string, a ...interface{}) { |
| 51 | if c.scannerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#scanner. == nil { |
| 52 | c.scannerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#scanner. = bufioscip-go gomod github.com/golang/go/src go1.24.0 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.24.0 bufio/NewScanner().(c.Connscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Conn.) |
| 53 | } |
| 54 | if !c.scannerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#scanner..Scanscip-go gomod github.com/golang/go/src go1.24.0 bufio/Scanner#Scan().() { |
| 55 | c.Fatalfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Fatalf.("failed to recv %q: %v", cmd, c.scannerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#scanner..Errscip-go gomod github.com/golang/go/src go1.24.0 bufio/Scanner#Err().()) |
| 56 | } |
| 57 | text := c.scannerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#scanner..Textscip-go gomod github.com/golang/go/src go1.24.0 bufio/Scanner#Text().() |
| 58 | c.Printfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Printf.("comm.recv: %s\n", text) |
| 59 | var recvCmd string |
| 60 | args := append([]interface{}{&recvCmd}, a...) |
| 61 | if _, err := fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sscanscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sscan().(text, args...); err != nil { |
| 62 | c.Fatalfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Fatalf.("cannot scan recv command %s: %q: %v", cmd, text, err) |
| 63 | } |
| 64 | if cmd != recvCmd { |
| 65 | c.Fatalfscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app/internal/apptest`/Comm#Fatalf.("expecting recv %q, got %v", cmd, text) |
| 66 | } |
| 67 | } |