menu_test.go 9.2 KB

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