setup.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package test
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "time"
  10. "unicode/utf8"
  11. "github.com/browsh-org/browsh/interfacer/src/browsh"
  12. "github.com/gdamore/tcell"
  13. "github.com/gdamore/tcell/terminfo"
  14. ginkgo "github.com/onsi/ginkgo"
  15. gomega "github.com/onsi/gomega"
  16. "github.com/spf13/viper"
  17. )
  18. var (
  19. staticFileServerPort = "4444"
  20. simScreen tcell.SimulationScreen
  21. startupWait = 60 * time.Second
  22. perTestTimeout = 2000 * time.Millisecond
  23. rootDir = browsh.Shell("git rev-parse --show-toplevel")
  24. testSiteURL = "http://localhost:" + staticFileServerPort
  25. ti *terminfo.Terminfo
  26. framesLogFileName string
  27. frameLogger *slog.Logger
  28. )
  29. func init() {
  30. dir, err := os.Getwd()
  31. if err != nil {
  32. panic(err)
  33. }
  34. framesLogFileName = fmt.Sprintf(filepath.Join(dir, "frames.log"))
  35. framesLogFile, err := os.OpenFile(framesLogFileName,
  36. os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
  37. 0o644,
  38. )
  39. if err != nil {
  40. panic(err)
  41. }
  42. frameLogger = slog.New(slog.NewTextHandler(framesLogFile, nil))
  43. }
  44. func initTerm() {
  45. // The tests check for true colour RGB values. The only downside to forcing true colour
  46. // in tests is that snapshots of frames with true colour ANSI codes are output to logs.
  47. // Some people may not have true colour terminals, for example like on Travis, so cat'ing
  48. // logs may appear corrupt.
  49. ti, _ = terminfo.LookupTerminfo("xterm-truecolor")
  50. }
  51. // GetFrame returns the current Browsh frame's text
  52. func GetFrame() string {
  53. var frame, log string
  54. line := 0
  55. styleDefault := ti.TParm(ti.SetFgBg, int(tcell.ColorWhite), int(tcell.ColorBlack))
  56. width, _ := simScreen.Size()
  57. cells, _, _ := simScreen.GetContents()
  58. for _, element := range cells {
  59. line++
  60. frame += string(element.Runes)
  61. log += elementColourForTTY(element) + string(element.Runes)
  62. if line == width {
  63. frame += "\n"
  64. log += styleDefault + "\n"
  65. line = 0
  66. }
  67. }
  68. frameLogger.Info("================================================")
  69. frameLogger.Info(ginkgo.CurrentGinkgoTestDescription().FullTestText)
  70. frameLogger.Info("================================================\n")
  71. return frame
  72. }
  73. // Trigger the key definition specified by name
  74. func triggerUserKeyFor(name string) {
  75. key := viper.GetStringSlice(name)
  76. intKey, _ := strconv.Atoi(key[1])
  77. modifierKey, _ := strconv.Atoi(key[2])
  78. simScreen.InjectKey(tcell.Key(intKey), []rune(key[0])[0], tcell.ModMask(modifierKey))
  79. }
  80. // SpecialKey injects a special key into the TTY. See Tcell's `keys.go` file for all
  81. // the available special keys.
  82. func SpecialKey(key tcell.Key) {
  83. simScreen.InjectKey(key, 0, tcell.ModNone)
  84. time.Sleep(100 * time.Millisecond)
  85. }
  86. // Keyboard types a string of keys into the TTY, as if a user would
  87. func Keyboard(keys string) {
  88. for _, char := range keys {
  89. simScreen.InjectKey(tcell.KeyRune, char, tcell.ModNone)
  90. time.Sleep(10 * time.Millisecond)
  91. }
  92. }
  93. // SpecialMouse injects a special mouse event into the TTY. See Tcell's `mouse.go` file for all
  94. // the available special mouse values.
  95. func SpecialMouse(mouse tcell.ButtonMask) {
  96. simScreen.InjectMouse(0, 0, mouse, tcell.ModNone)
  97. time.Sleep(100 * time.Millisecond)
  98. }
  99. func waitForNextFrame() {
  100. // Need to wait so long because the frame rate is currently so slow
  101. // TODO: Reduce the wait when the FPS is higher
  102. time.Sleep(250 * time.Millisecond)
  103. }
  104. // WaitForText waits for a particular string at particular position in the frame
  105. func WaitForText(text string, x, y int) {
  106. var found string
  107. start := time.Now()
  108. for time.Since(start) < perTestTimeout {
  109. found = GetText(x, y, runeCount(text))
  110. if found == text {
  111. return
  112. }
  113. time.Sleep(100 * time.Millisecond)
  114. }
  115. panic("Waiting for '" + text + "' to appear but it didn't")
  116. }
  117. // WaitForPageLoad waits for the page to load
  118. func WaitForPageLoad() {
  119. sleepUntilPageLoad(perTestTimeout)
  120. }
  121. func sleepUntilPageLoad(maxTime time.Duration) {
  122. start := time.Now()
  123. time.Sleep(1000 * time.Millisecond)
  124. for time.Since(start) < maxTime {
  125. if browsh.CurrentTab != nil {
  126. if browsh.CurrentTab.PageState == "parsing_complete" {
  127. time.Sleep(200 * time.Millisecond)
  128. return
  129. }
  130. }
  131. time.Sleep(50 * time.Millisecond)
  132. }
  133. panic("Page didn't load within timeout")
  134. }
  135. // GotoURL sends the browsh browser to the specified URL
  136. func GotoURL(url string) {
  137. SpecialKey(tcell.KeyCtrlL)
  138. Keyboard(url)
  139. SpecialKey(tcell.KeyEnter)
  140. WaitForPageLoad()
  141. // TODO: Looking for the URL isn't optimal because it could be the same URL
  142. // as the previous test.
  143. gomega.Expect(url).To(BeInFrameAt(0, 1))
  144. // TODO: hack to work around bug where text sometimes doesn't render on page load.
  145. // Clicking with the mouse triggers a reparse by the web extension
  146. mouseClick(3, 6)
  147. time.Sleep(100 * time.Millisecond)
  148. mouseClick(3, 6)
  149. time.Sleep(500 * time.Millisecond)
  150. }
  151. func mouseClick(x, y int) {
  152. simScreen.InjectMouse(x, y, 1, tcell.ModNone)
  153. simScreen.InjectMouse(x, y, 0, tcell.ModNone)
  154. }
  155. func elementColourForTTY(element tcell.SimCell) string {
  156. var fg, bg tcell.Color
  157. fg, bg, _ = element.Style.Decompose()
  158. r1, g1, b1 := fg.RGB()
  159. r2, g2, b2 := bg.RGB()
  160. return ti.TParm(ti.SetFgBgRGB,
  161. int(r1), int(g1), int(b1),
  162. int(r2), int(g2), int(b2))
  163. }
  164. // GetText retruns an individual piece of a frame
  165. func GetText(x, y, length int) string {
  166. var text string
  167. frame := []rune(GetFrame())
  168. width, _ := simScreen.Size()
  169. index := ((width + 1) * y) + x
  170. for {
  171. text += string(frame[index])
  172. index++
  173. if runeCount(text) == length {
  174. break
  175. }
  176. }
  177. return text
  178. }
  179. // GetFgColour returns the foreground colour of a single cell
  180. func GetFgColour(x, y int) [3]int32 {
  181. GetFrame()
  182. cells, _, _ := simScreen.GetContents()
  183. width, _ := simScreen.Size()
  184. index := (width * y) + x
  185. fg, _, _ := cells[index].Style.Decompose()
  186. r1, g1, b1 := fg.RGB()
  187. return [3]int32{r1, g1, b1}
  188. }
  189. // GetBgColour returns the background colour of a single cell
  190. func GetBgColour(x, y int) [3]int32 {
  191. GetFrame()
  192. cells, _, _ := simScreen.GetContents()
  193. width, _ := simScreen.Size()
  194. index := (width * y) + x
  195. _, bg, _ := cells[index].Style.Decompose()
  196. r1, g1, b1 := bg.RGB()
  197. return [3]int32{r1, g1, b1}
  198. }
  199. func ensureOnlyOneTab() {
  200. if len(browsh.Tabs) > 1 {
  201. SpecialKey(tcell.KeyCtrlW)
  202. }
  203. }
  204. func startStaticFileServer() {
  205. serverMux := http.NewServeMux()
  206. serverMux.Handle("/", http.FileServer(http.Dir(rootDir+"/interfacer/test/sites")))
  207. http.ListenAndServe(":"+staticFileServerPort, serverMux)
  208. }
  209. func initBrowsh() {
  210. browsh.IsTesting = true
  211. simScreen = tcell.NewSimulationScreen("UTF-8")
  212. browsh.Initialise()
  213. }
  214. func stopFirefox() {
  215. slog.Info("Attempting to kill all firefox processes")
  216. browsh.IsConnectedToWebExtension = false
  217. browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
  218. time.Sleep(500 * time.Millisecond)
  219. }
  220. func runeCount(text string) int {
  221. return utf8.RuneCountInString(text)
  222. }
  223. var _ = ginkgo.BeforeEach(func() {
  224. slog.Info("Attempting to restart WER Firefox...")
  225. stopFirefox()
  226. browsh.ResetTabs()
  227. browsh.StartFirefox()
  228. sleepUntilPageLoad(startupWait)
  229. browsh.IsMonochromeMode = false
  230. slog.Info("\n---------")
  231. slog.Info(ginkgo.CurrentGinkgoTestDescription().FullTestText)
  232. slog.Info("---------")
  233. })
  234. var _ = ginkgo.BeforeSuite(func() {
  235. os.Truncate(framesLogFileName, 0)
  236. initTerm()
  237. initBrowsh()
  238. stopFirefox()
  239. go startStaticFileServer()
  240. go browsh.TTYStart(simScreen)
  241. // Firefox seems to take longer to die after its first run
  242. time.Sleep(500 * time.Millisecond)
  243. stopFirefox()
  244. time.Sleep(5000 * time.Millisecond)
  245. })
  246. var _ = ginkgo.AfterSuite(func() {
  247. stopFirefox()
  248. })