setup.go 7.3 KB

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