menu_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package door
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "net"
  7. "os"
  8. "runtime"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestMenuRender(t *testing.T) {
  14. var bracket []byte = ColorText("BLUE")
  15. var option []byte = ColorText("BRI GREEN")
  16. var upper []byte = ColorText("CYAN")
  17. var lower []byte = ColorText("MAGENTA")
  18. var render ColorRender = MakeMenuRender(bracket, option, upper, lower)
  19. // Fake menu line
  20. var input string = "[X] BUGZ test"
  21. var buff *bytes.Buffer = &bytes.Buffer{}
  22. // var got string = string(render(buff, []byte(input)))
  23. render(buff, []byte(input))
  24. var got string = buff.String()
  25. var expected string = string(bracket) + "[" + string(option) + "X" + string(bracket) + "]"
  26. expected += string(lower) + " " + string(upper) + "BUGZ" + string(lower) + " test"
  27. if got != expected {
  28. t.Errorf("MenuRender expected %#v, got %#v", expected, got)
  29. }
  30. }
  31. func TestMenuSize(t *testing.T) {
  32. defer func() {
  33. if r := recover(); r == nil {
  34. t.Error("Menu Exceeding Width did not panic.")
  35. }
  36. }()
  37. var m Menu = Menu{Panel: Panel{Width: 5,
  38. X: 1,
  39. Y: 1,
  40. Style: DOUBLE,
  41. }}
  42. // len("[A] ABC") = 7 (which is > 5)
  43. m.AddSelection("A", "ABC")
  44. }
  45. func TestMenuConnection(t *testing.T) {
  46. var tmpFile *os.File
  47. var err error
  48. tmpFile, err = os.CreateTemp("", "test-door32.sys-*")
  49. if err != nil {
  50. panic("Cannot create temporary file")
  51. }
  52. // Remember to clean up the file afterwards
  53. defer os.Remove(tmpFile.Name())
  54. // establish network socket connection to set Comm_handle
  55. var server, client net.Conn
  56. server, client = setupSockets()
  57. // We're not testing closed connections, so:
  58. defer server.Close()
  59. defer client.Close()
  60. // pre-load unicode 90x40 response
  61. var buffer []byte = []byte(UnicodeWidthHeight(90, 40))
  62. // "\x1b[1;1R\x1b[2;3R\x1b[40;90R")
  63. _, err = server.Write(buffer)
  64. if err != nil {
  65. t.Error("server.Write:", err)
  66. }
  67. // Access Fd (File descriptor) of client for dropfile
  68. var fd int = socket_to_fd(client)
  69. defer close_fd(fd)
  70. // Create door32.sys file
  71. var node int = 8
  72. var dfc DropfileConfig = DropfileConfig{2, fd, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, node}
  73. err = CreateDoor32File(&dfc, tmpFile)
  74. if err != nil {
  75. t.Error("tmpFile.WriteString:", err)
  76. }
  77. err = tmpFile.Close()
  78. if err != nil {
  79. t.Error("tmpFile.Close:", err)
  80. }
  81. var d Door = Door{}
  82. // Reset global variables
  83. Unicode = false
  84. CP437 = false
  85. Full_CP437 = false
  86. Width = 0
  87. Height = 0
  88. // If I call d.Init() more then once flag complains about flag redefined.
  89. // Reset flags
  90. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  91. // preset commandline args so door can init
  92. os.Args = []string{"door", "-d", tmpFile.Name()}
  93. d.Init("menu-test")
  94. defer d.Close()
  95. // clean up log file - not needed in tests
  96. defer os.Remove(fmt.Sprintf("menu-test-%d.log", node))
  97. // Ok!
  98. if !Unicode {
  99. t.Errorf("Unicode not true %t", Unicode)
  100. }
  101. if Width != 90 {
  102. t.Errorf("Width not 90: %d", Width)
  103. }
  104. if Height != 40 {
  105. t.Errorf("Height not 40: %d", Height)
  106. }
  107. clear_socket(server, t)
  108. /*
  109. // These are the commands sent to detect ... throw this all away.
  110. buffer = make([]byte, 1024)
  111. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  112. if err != nil {
  113. t.Error("server.SetReadDeadLine:", err)
  114. }
  115. _, err = server.Read(buffer)
  116. if err != nil {
  117. t.Errorf("server.Read: %s", err)
  118. }
  119. // t.Errorf("Buffer : %#v\n", buffer[:r])
  120. err = server.SetReadDeadline(time.Time{})
  121. if err != nil {
  122. t.Error("server.SetReadDeadLine:", err)
  123. }
  124. */
  125. // Ready to test!
  126. var m Menu = Menu{Panel: Panel{Width: 10,
  127. X: 2,
  128. Y: 2,
  129. Style: DOUBLE,
  130. }}
  131. // Use simple renders for testing
  132. m.SelectedR = func(output *bytes.Buffer, text []byte) {
  133. output.Reset()
  134. output.Write(ColorText("BLACK ON WHITE"))
  135. output.Write(text)
  136. }
  137. m.UnselectedR = func(output *bytes.Buffer, text []byte) {
  138. output.Reset()
  139. output.Write(ColorText("WHI ON BLA"))
  140. output.Write(text)
  141. }
  142. m.AddSelection("A", "ART")
  143. m.AddSelection("B", "BOO")
  144. m.AddSelection("C", "Cat")
  145. var choice int
  146. // preload the key buffer with commands
  147. var keys string = "\x00\x50\x1b[B\r" // down, down, ENTER
  148. _, err = server.Write([]byte(keys))
  149. if err != nil {
  150. t.Error("server.Write:", err)
  151. }
  152. time.Sleep(time.Millisecond)
  153. if d.Disconnect() {
  154. t.Errorf("Disconnected")
  155. }
  156. choice = m.Choose(&d)
  157. if choice < 0 {
  158. t.Errorf("Error <0 from Choose: %d", choice)
  159. }
  160. var option rune = m.GetOption(choice)
  161. if choice != 3 {
  162. t.Errorf("Expected menu option 3, got %d / %c", choice, option)
  163. }
  164. if option != 'C' {
  165. t.Errorf("Expected menu option C, got %c / %d", option, choice)
  166. }
  167. // Read the display output
  168. output := clear_socket(server, t)
  169. /*
  170. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  171. if err != nil {
  172. t.Error("server.SetReadDeadLine:", err)
  173. }
  174. var r int
  175. r, err = server.Read(buffer)
  176. if err != nil {
  177. t.Errorf("server.Read: %s", err)
  178. }
  179. err = server.SetReadDeadline(time.Time{})
  180. if err != nil {
  181. t.Error("server.SetReadDeadLine:", err)
  182. }
  183. output := string(buffer[:r])
  184. */
  185. // t.Errorf("OUTPUT: (len %d) %#v", r, output)
  186. parts := strings.SplitAfter(output, "H")
  187. for _, part := range parts {
  188. t.Logf("Got: %#v", part)
  189. }
  190. keys = "\x1b[A\r" // up, ENTER
  191. _, err = server.Write([]byte(keys))
  192. if err != nil {
  193. t.Errorf("server.Write: %#v", err)
  194. }
  195. time.Sleep(time.Millisecond)
  196. runtime.Gosched()
  197. if d.Disconnect() {
  198. t.Errorf("Disconnected")
  199. }
  200. m.Chosen = 1
  201. choice = m.Choose(&d)
  202. if choice < 0 {
  203. t.Errorf("Error <0 from Choose: %d", choice)
  204. }
  205. option = m.GetOption(choice)
  206. if choice != 1 {
  207. t.Errorf("Expected menu option 1, got %d / %c", choice, option)
  208. }
  209. if option != 'A' {
  210. t.Errorf("Expected menu option A, got %c / %d", option, choice)
  211. }
  212. // Read the display output
  213. clear_socket(server, t)
  214. /*
  215. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  216. if err != nil {
  217. t.Error("server.SetReadDeadLine:", err)
  218. }
  219. _, err = server.Read(buffer)
  220. if err != nil {
  221. t.Errorf("server.Read: %s", err)
  222. }
  223. err = server.SetReadDeadline(time.Time{})
  224. if err != nil {
  225. t.Error("server.SetReadDeadLine:", err)
  226. }
  227. */
  228. // TODO: Check Menu output and verify it is correct.
  229. // output = string(buffer[:r])
  230. // Test HOME, END, selecting an option, 8 & 2.
  231. keys = "\x00\x4f\r" // END, ENTER
  232. _, err = server.Write([]byte(keys))
  233. if err != nil {
  234. t.Errorf("server.Write: %#v", err)
  235. }
  236. time.Sleep(time.Millisecond)
  237. if d.Disconnect() {
  238. t.Errorf("Disconnected")
  239. }
  240. m.Chosen = 1
  241. choice = m.Choose(&d)
  242. if choice < 0 {
  243. t.Errorf("Error <0 from Choose: %d", choice)
  244. }
  245. option = m.GetOption(choice)
  246. if choice != 3 || option != 'C' {
  247. t.Errorf("Expected 3/C, got %d/%c", choice, option)
  248. }
  249. // Read the display output
  250. output = clear_socket(server, t)
  251. /*
  252. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  253. if err != nil {
  254. t.Error("server.SetReadDeadLine:", err)
  255. }
  256. _, err = server.Read(buffer)
  257. if err != nil {
  258. t.Errorf("server.Read: %s", err)
  259. }
  260. err = server.SetReadDeadline(time.Time{})
  261. if err != nil {
  262. t.Error("server.SetReadDeadLine:", err)
  263. }
  264. */
  265. // output = string(buffer[:r])
  266. keys = "\x00\x47\r" // HOME, ENTER
  267. _, err = server.Write([]byte(keys))
  268. if err != nil {
  269. t.Errorf("server.Write: %#v", err)
  270. }
  271. time.Sleep(time.Millisecond)
  272. m.Chosen = 2
  273. choice = m.Choose(&d)
  274. if choice < 0 {
  275. t.Errorf("Error <0 from Choose: %d", choice)
  276. }
  277. option = m.GetOption(choice)
  278. if choice != 1 || option != 'A' {
  279. t.Errorf("Expected 1/A, got %d/%c", choice, option)
  280. }
  281. // Read the display output
  282. output = clear_socket(server, t)
  283. /*
  284. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  285. if err != nil {
  286. t.Error("server.SetReadDeadLine:", err)
  287. }
  288. _, err = server.Read(buffer)
  289. if err != nil {
  290. t.Errorf("server.Read: %s", err)
  291. }
  292. err = server.SetReadDeadline(time.Time{})
  293. if err != nil {
  294. t.Error("server.SetReadDeadLine:", err)
  295. }
  296. */
  297. // output = string(buffer[:r])
  298. _, err = server.Write([]byte("B"))
  299. if err != nil {
  300. t.Errorf("server.Write: %#v", err)
  301. }
  302. time.Sleep(time.Millisecond)
  303. m.Chosen = 0
  304. choice = m.Choose(&d)
  305. if choice < 0 {
  306. t.Errorf("Error <0 from Choose: %d", choice)
  307. }
  308. option = m.GetOption(choice)
  309. if choice != 2 || option != 'B' {
  310. t.Errorf("Expected 2/B, got %d/%c", choice, option)
  311. }
  312. // Read the display output
  313. output = clear_socket(server, t)
  314. /*
  315. // i/o timeout
  316. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
  317. if err != nil {
  318. t.Error("server.SetReadDeadLine:", err)
  319. }
  320. _, err = server.Read(buffer)
  321. if err != nil {
  322. t.Errorf("server.Read: %s", err)
  323. }
  324. err = server.SetReadDeadline(time.Time{})
  325. if err != nil {
  326. t.Error("server.SetReadDeadLine:", err)
  327. }
  328. */
  329. // output = string(buffer[:r])
  330. keys = "2\r" // "Down", ENTER
  331. _, err = server.Write([]byte(keys))
  332. if err != nil {
  333. t.Errorf("server.Write: %#v", err)
  334. }
  335. time.Sleep(time.Millisecond)
  336. m.Chosen = 1
  337. choice = m.Choose(&d)
  338. if choice < 0 {
  339. t.Errorf("Error <0 from Choose: %d", choice)
  340. }
  341. option = m.GetOption(choice)
  342. if choice != 3 || option != 'C' {
  343. t.Errorf("Expected 3/C, got %d/%c", choice, option)
  344. }
  345. // Read the display output
  346. output = clear_socket(server, t)
  347. /*
  348. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 50))
  349. if err != nil {
  350. t.Error("server.SetReadDeadLine:", err)
  351. }
  352. _, err = server.Read(buffer)
  353. if err != nil {
  354. t.Errorf("server.Read: %s", err)
  355. }
  356. err = server.SetReadDeadline(time.Time{})
  357. if err != nil {
  358. t.Error("server.SetReadDeadLine:", err)
  359. }
  360. */
  361. // output = string(buffer[:r])
  362. server.Close()
  363. client.Close()
  364. // d.Write("\x00")
  365. time.Sleep(time.Millisecond)
  366. }