setup.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. "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. browsh.URLBarFocus(true)
  132. Keyboard(url)
  133. SpecialKey(tcell.KeyEnter)
  134. WaitForPageLoad()
  135. // Hack to force text to be rerendered. Because there's a bug where text sometimes doesn't get
  136. // rendered.
  137. mouseClick(3, 3)
  138. // TODO: Looking for the URL isn't optimal because it could be the same URL
  139. // as the previous test.
  140. gomega.Expect(url).To(BeInFrameAt(0, 1))
  141. // TODO: hack to work around bug where text sometimes doesn't render on page load.
  142. // Clicking with the mouse triggers a reparse by the web extension
  143. mouseClick(3, 6)
  144. time.Sleep(100 * time.Millisecond)
  145. mouseClick(3, 6)
  146. time.Sleep(500 * time.Millisecond)
  147. }
  148. func mouseClick(x, y int) {
  149. simScreen.InjectMouse(x, y, 1, tcell.ModNone)
  150. simScreen.InjectMouse(x, y, 0, tcell.ModNone)
  151. }
  152. func elementColourForTTY(element tcell.SimCell) string {
  153. var fg, bg tcell.Color
  154. fg, bg, _ = element.Style.Decompose()
  155. r1, g1, b1 := fg.RGB()
  156. r2, g2, b2 := bg.RGB()
  157. return ti.TParm(ti.SetFgBgRGB,
  158. int(r1), int(g1), int(b1),
  159. int(r2), int(g2), int(b2))
  160. }
  161. // GetText retruns an individual piece of a frame
  162. func GetText(x, y, length int) string {
  163. var text string
  164. frame := []rune(GetFrame())
  165. width, _ := simScreen.Size()
  166. index := ((width + 1) * y) + x
  167. for {
  168. text += string(frame[index])
  169. index++
  170. if runeCount(text) == length {
  171. break
  172. }
  173. }
  174. return text
  175. }
  176. // GetFgColour returns the foreground colour of a single cell
  177. func GetFgColour(x, y int) [3]int32 {
  178. GetFrame()
  179. cells, _, _ := simScreen.GetContents()
  180. width, _ := simScreen.Size()
  181. index := (width * y) + x
  182. fg, _, _ := cells[index].Style.Decompose()
  183. r1, g1, b1 := fg.RGB()
  184. return [3]int32{r1, g1, b1}
  185. }
  186. // GetBgColour returns the background colour of a single cell
  187. func GetBgColour(x, y int) [3]int32 {
  188. GetFrame()
  189. cells, _, _ := simScreen.GetContents()
  190. width, _ := simScreen.Size()
  191. index := (width * y) + x
  192. _, bg, _ := cells[index].Style.Decompose()
  193. r1, g1, b1 := bg.RGB()
  194. return [3]int32{r1, g1, b1}
  195. }
  196. func ensureOnlyOneTab() {
  197. for len(browsh.Tabs) > 1 {
  198. SpecialKey(tcell.KeyCtrlW)
  199. }
  200. }
  201. func startStaticFileServer() {
  202. serverMux := http.NewServeMux()
  203. serverMux.Handle("/", http.FileServer(http.Dir(rootDir+"/interfacer/test/sites")))
  204. http.ListenAndServe(":"+staticFileServerPort, serverMux)
  205. }
  206. func initBrowsh() {
  207. browsh.IsTesting = true
  208. simScreen = tcell.NewSimulationScreen("UTF-8")
  209. browsh.Initialise()
  210. }
  211. func stopFirefox() {
  212. browsh.Log("Attempting to kill all firefox processes")
  213. browsh.IsConnectedToWebExtension = false
  214. browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
  215. time.Sleep(500 * time.Millisecond)
  216. }
  217. func runeCount(text string) int {
  218. return utf8.RuneCountInString(text)
  219. }
  220. var _ = ginkgo.BeforeEach(func() {
  221. browsh.Log("Attempting to restart WER Firefox...")
  222. stopFirefox()
  223. browsh.ResetTabs()
  224. browsh.StartFirefox()
  225. sleepUntilPageLoad(startupWait)
  226. browsh.IsMonochromeMode = false
  227. browsh.Log("\n---------")
  228. browsh.Log(ginkgo.CurrentGinkgoTestDescription().FullTestText)
  229. browsh.Log("---------")
  230. })
  231. var _ = ginkgo.BeforeSuite(func() {
  232. os.Truncate(framesLogFile, 0)
  233. initTerm()
  234. initBrowsh()
  235. stopFirefox()
  236. go startStaticFileServer()
  237. go browsh.TTYStart(simScreen)
  238. // Firefox seems to take longer to die after its first run
  239. time.Sleep(500 * time.Millisecond)
  240. stopFirefox()
  241. time.Sleep(5000 * time.Millisecond)
  242. })
  243. var _ = ginkgo.AfterSuite(func() {
  244. stopFirefox()
  245. })