menu_test.go 9.4 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 string = ColorText("BLUE")
  15. var option string = ColorText("BRI GREEN")
  16. var upper string = ColorText("CYAN")
  17. var lower string = 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. var expected string = bracket + "[" + option + "X" + bracket + "]"
  24. expected += lower + " " + upper + "BUGZ" + lower + " test"
  25. if got != expected {
  26. t.Errorf("MenuRender expected %#v, got %#v", expected, got)
  27. }
  28. }
  29. func TestMenuSize(t *testing.T) {
  30. defer func() {
  31. if r := recover(); r == nil {
  32. t.Error("Menu Exceeding Width did not panic.")
  33. }
  34. }()
  35. var m Menu = Menu{Panel: Panel{Width: 5,
  36. X: 1,
  37. Y: 1,
  38. Style: DOUBLE,
  39. }}
  40. // len("[A] ABC") = 7 (which is > 5)
  41. m.AddSelection("A", "ABC")
  42. }
  43. func TestMenuConnection(t *testing.T) {
  44. var tmpFile *os.File
  45. var err error
  46. tmpFile, err = os.CreateTemp("", "test-door32.sys-*")
  47. if err != nil {
  48. panic("Cannot create temporary file")
  49. }
  50. // Remember to clean up the file afterwards
  51. defer os.Remove(tmpFile.Name())
  52. // establish network socket connection to set Comm_handle
  53. var server, client net.Conn
  54. server, client = setupSockets()
  55. // We're not testing closed connections, so:
  56. defer server.Close()
  57. defer client.Close()
  58. // pre-load unicode 90x40 response
  59. var buffer []byte = []byte(UnicodeWidthHeight(90, 40))
  60. // "\x1b[1;1R\x1b[2;3R\x1b[40;90R")
  61. _, err = server.Write(buffer)
  62. if err != nil {
  63. t.Error("server.Write:", err)
  64. }
  65. // Access Fd (File descriptor) of client for dropfile
  66. var fd int = socket_to_fd(client)
  67. defer close_fd(fd)
  68. // Create door32.sys file
  69. var node int = 8
  70. var dfc DropfileConfig = DropfileConfig{2, fd, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, node}
  71. err = CreateDoor32File(&dfc, tmpFile)
  72. if err != nil {
  73. t.Error("tmpFile.WriteString:", err)
  74. }
  75. err = tmpFile.Close()
  76. if err != nil {
  77. t.Error("tmpFile.Close:", err)
  78. }
  79. var d Door = Door{}
  80. // Reset global variables
  81. Unicode = false
  82. CP437 = false
  83. Full_CP437 = false
  84. Width = 0
  85. Height = 0
  86. // If I call d.Init() more then once flag complains about flag redefined.
  87. // Reset flags
  88. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  89. // preset commandline args so door can init
  90. os.Args = []string{"door", "-d", tmpFile.Name()}
  91. d.Init("menu-test")
  92. defer d.Close()
  93. // clean up log file - not needed in tests
  94. defer os.Remove(fmt.Sprintf("menu-test-%d.log", node))
  95. // Ok!
  96. if !Unicode {
  97. t.Errorf("Unicode not true %t", Unicode)
  98. }
  99. if Width != 90 {
  100. t.Errorf("Width not 90: %d", Width)
  101. }
  102. if Height != 40 {
  103. t.Errorf("Height not 40: %d", Height)
  104. }
  105. clear_socket(server, t)
  106. /*
  107. // These are the commands sent to detect ... throw this all away.
  108. buffer = make([]byte, 1024)
  109. err = server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  110. if err != nil {
  111. t.Error("server.SetReadDeadLine:", err)
  112. }
  113. _, err = server.Read(buffer)
  114. if err != nil {
  115. t.Errorf("server.Read: %s", err)
  116. }
  117. // t.Errorf("Buffer : %#v\n", buffer[:r])
  118. err = server.SetReadDeadline(time.Time{})
  119. if err != nil {
  120. t.Error("server.SetReadDeadLine:", err)
  121. }
  122. */
  123. // Ready to test!
  124. var m Menu = Menu{Panel: Panel{Width: 10,
  125. X: 2,
  126. Y: 2,
  127. Style: DOUBLE,
  128. }}
  129. // Use simple renders for testing
  130. m.SelectedR = func(output *bytes.Buffer, text []byte) []byte {
  131. output.Reset()
  132. output.WriteString(ColorText("BLACK ON WHITE"))
  133. output.Write(text)
  134. return output.Bytes()
  135. }
  136. m.UnselectedR = func(output *bytes.Buffer, text []byte) []byte {
  137. output.Reset()
  138. output.WriteString(ColorText("WHI ON BLA"))
  139. output.Write(text)
  140. return output.Bytes()
  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. }