github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/io/router/clipboard_test.go (about) 1 package router 2 3 import ( 4 "testing" 5 6 "github.com/cybriq/giocore/io/clipboard" 7 "github.com/cybriq/giocore/io/event" 8 "github.com/cybriq/giocore/op" 9 ) 10 11 func TestClipboardDuplicateEvent(t *testing.T) { 12 ops, router, handler := new(op.Ops), new(Router), make([]int, 2) 13 14 // Both must receive the event once 15 clipboard.ReadOp{Tag: &handler[0]}.Add(ops) 16 clipboard.ReadOp{Tag: &handler[1]}.Add(ops) 17 18 router.Frame(ops) 19 event := clipboard.Event{Text: "Test"} 20 router.Queue(event) 21 assertClipboardReadOp(t, router, 0) 22 assertClipboardEvent(t, router.Events(&handler[0]), true) 23 assertClipboardEvent(t, router.Events(&handler[1]), true) 24 ops.Reset() 25 26 // No ReadOp 27 28 router.Frame(ops) 29 assertClipboardReadOp(t, router, 0) 30 assertClipboardEvent(t, router.Events(&handler[0]), false) 31 assertClipboardEvent(t, router.Events(&handler[1]), false) 32 ops.Reset() 33 34 clipboard.ReadOp{Tag: &handler[0]}.Add(ops) 35 36 router.Frame(ops) 37 // No ClipboardEvent sent 38 assertClipboardReadOp(t, router, 1) 39 assertClipboardEvent(t, router.Events(&handler[0]), false) 40 assertClipboardEvent(t, router.Events(&handler[1]), false) 41 ops.Reset() 42 } 43 44 func TestQueueProcessReadClipboard(t *testing.T) { 45 ops, router, handler := new(op.Ops), new(Router), make([]int, 2) 46 ops.Reset() 47 48 // Request read 49 clipboard.ReadOp{Tag: &handler[0]}.Add(ops) 50 51 router.Frame(ops) 52 assertClipboardReadOp(t, router, 1) 53 ops.Reset() 54 55 for i := 0; i < 3; i++ { 56 // No ReadOp 57 // One receiver must still wait for response 58 59 router.Frame(ops) 60 assertClipboardReadOpDuplicated(t, router, 1) 61 ops.Reset() 62 } 63 64 router.Frame(ops) 65 // Send the clipboard event 66 event := clipboard.Event{Text: "Text 2"} 67 router.Queue(event) 68 assertClipboardReadOp(t, router, 0) 69 assertClipboardEvent(t, router.Events(&handler[0]), true) 70 ops.Reset() 71 72 // No ReadOp 73 // There's no receiver waiting 74 75 router.Frame(ops) 76 assertClipboardReadOp(t, router, 0) 77 assertClipboardEvent(t, router.Events(&handler[0]), false) 78 ops.Reset() 79 } 80 81 func TestQueueProcessWriteClipboard(t *testing.T) { 82 ops, router := new(op.Ops), new(Router) 83 ops.Reset() 84 85 clipboard.WriteOp{Text: "Write 1"}.Add(ops) 86 87 router.Frame(ops) 88 assertClipboardWriteOp(t, router, "Write 1") 89 ops.Reset() 90 91 // No WriteOp 92 93 router.Frame(ops) 94 assertClipboardWriteOp(t, router, "") 95 ops.Reset() 96 97 clipboard.WriteOp{Text: "Write 2"}.Add(ops) 98 99 router.Frame(ops) 100 assertClipboardReadOp(t, router, 0) 101 assertClipboardWriteOp(t, router, "Write 2") 102 ops.Reset() 103 } 104 105 func assertClipboardEvent(t *testing.T, events []event.Event, expected bool) { 106 t.Helper() 107 var evtClipboard int 108 for _, e := range events { 109 switch e.(type) { 110 case clipboard.Event: 111 evtClipboard++ 112 } 113 } 114 if evtClipboard <= 0 && expected { 115 t.Error("expected to receive some event") 116 } 117 if evtClipboard > 0 && !expected { 118 t.Error("unexpected event received") 119 } 120 } 121 122 func assertClipboardReadOp(t *testing.T, router *Router, expected int) { 123 t.Helper() 124 if len(router.cqueue.receivers) != expected { 125 t.Error("unexpected number of receivers") 126 } 127 if router.cqueue.ReadClipboard() != (expected > 0) { 128 t.Error("missing requests") 129 } 130 } 131 132 func assertClipboardReadOpDuplicated(t *testing.T, router *Router, expected int) { 133 t.Helper() 134 if len(router.cqueue.receivers) != expected { 135 t.Error("receivers removed") 136 } 137 if router.cqueue.ReadClipboard() != false { 138 t.Error("duplicated requests") 139 } 140 } 141 142 func assertClipboardWriteOp(t *testing.T, router *Router, expected string) { 143 t.Helper() 144 if (router.cqueue.text != nil) != (expected != "") { 145 t.Error("text not defined") 146 } 147 text, ok := router.cqueue.WriteClipboard() 148 if ok != (expected != "") { 149 t.Error("duplicated requests") 150 } 151 if text != expected { 152 t.Errorf("got text %s, expected %s", text, expected) 153 } 154 }