summaryrefslogtreecommitdiff
path: root/redo/zz_test.go
blob: 2477de03cdb0311a923291f5a0865dcbd897b660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// 8 july 2014

package ui

// This file is called zz_test.go to keep it separate from the other files in this package (and because go test won't accept just test.go)

import (
	"fmt"
	"flag"
	"reflect"
	"testing"
	"image"
	"image/color"
	"image/draw"
	"time"
)

var closeOnClick = flag.Bool("close", false, "close on click")
var smallWindow = flag.Bool("small", false, "open a small window (test Mac OS X initial control sizing)")

type dtype struct {
	Name	string
	Address	string
}
var ddata = []dtype{
	{ "alpha", "beta" },
	{ "gamma", "delta" },
	{ "epsilon", "zeta" },
	{ "eta", "theta" },
	{ "iota", "kappa" },
}

type testwin struct {
	t		Tab
	w		Window
	fe		*ForeignEvent
	festack	Stack
	festart	Button
	felabel	Label
	festop	Button
	openbtn	Button
	fnlabel	Label
	icons	[]icon
	il		ImageList
	icontbl	Table
	group2	Group
	group	Group
	grid		Grid
	nt		Tab
	a		Area
	spw		Stack
	sph		Stack
	s		Stack
	l		Label
	table		Table
	b		Button
	c		Checkbox
	e		TextField
	e2		TextField

	wsmall	Window
}

type areaHandler struct{}
func (a *areaHandler) Paint(r image.Rectangle) *image.RGBA {
	i := image.NewRGBA(r)
	draw.Draw(i, r, &image.Uniform{color.RGBA{128,0,128,255}}, image.ZP, draw.Src)
	return i
}
func (a *areaHandler) Mouse(me MouseEvent) { fmt.Printf("%#v\n", me) }
func (a *areaHandler) Key(ke KeyEvent) { fmt.Printf("%#v %q\n", ke, ke.Key) }

func (tw *testwin) addfe() {
	tw.festart = NewButton("Start")
	tw.festart.OnClicked(func() {
		if tw.fe != nil {
			tw.fe.Stop()
		}
		ticker := time.NewTicker(1 * time.Second)
		tw.fe = NewForeignEvent(ticker.C, func(d interface{}) {
			t := d.(time.Time)
			tw.felabel.SetText(t.String())
		})
	})
	tw.felabel = NewStandaloneLabel("<stopped>")
	tw.festop = NewButton("Stop")
	tw.festop.OnClicked(func() {
		if tw.fe != nil {
			tw.fe.Stop()
			tw.felabel.SetText("<stopped>")
			tw.fe = nil
		}
	})
	tw.openbtn = NewButton("Open")
	tw.openbtn.OnClicked(func() {
		fn := OpenFile()
		if fn == "" {
			fn = "<no file selected>"
		}
		tw.fnlabel.SetText(fn)
	})
	tw.fnlabel = NewStandaloneLabel("<no file selected>")
	tw.festack = NewVerticalStack(tw.festart, tw.felabel, tw.festop,
		Space(),
		tw.openbtn, tw.fnlabel)
	tw.festack.SetStretchy(3)
	tw.t.Append("Foreign Events", tw.festack)
}

func (tw *testwin) make(done chan struct{}) {
	tw.t = NewTab()
	tw.w = NewWindow("Hello", 320, 240, tw.t)
	tw.w.OnClosing(func() bool {
		if *closeOnClick {
			panic("window closed normally in close on click mode (should not happen)")
		}
		println("window close event received")
		Stop()
		done <- struct{}{}
		return true
	})
	tw.addfe()
	tw.icons, tw.il = readIcons()
	tw.icontbl = NewTable(reflect.TypeOf(icon{}))
	tw.icontbl.Lock()
	idq := tw.icontbl.Data().(*[]icon)
	*idq = tw.icons
	tw.icontbl.Unlock()
	tw.icontbl.LoadImageList(tw.il)
	tw.t.Append("Image List Table", tw.icontbl)
	tw.group2 = NewGroup("Group", NewButton("Button in Group"))
	tw.t.Append("Filled Group", tw.group2)
	tw.group = NewGroup("Group", NewVerticalStack(NewCheckbox("Checkbox in Group")))
	tw.t.Append("Group", tw.group)
	tw.grid = NewGrid(3,
		NewLabel("0,0"), NewTextField(), NewLabel("0,2"),
		NewButton("1,0"), NewButton("1,1"), NewButton("1,2"),
		NewLabel("2,0"), NewTextField(), NewStandaloneLabel("2,2"))
	tw.grid.SetFilling(2, 1)
	tw.grid.SetFilling(1, 2)
	tw.grid.SetStretchy(1, 1)
	tw.t.Append("Grid", tw.grid)
	tw.t.Append("Blank Tab", NewTab())
	tw.nt = NewTab()
	tw.nt.Append("Tab 1", Space())
	tw.nt.Append("Tab 2", Space())
	tw.t.Append("Tab", tw.nt)
	tw.t.Append("Space", Space())
	tw.a = NewArea(200, 200, &areaHandler{})
	tw.t.Append("Area", tw.a)
	tw.spw = NewHorizontalStack(
		NewButton("hello"),
		NewCheckbox("hello"),
		NewTextField(),
		NewPasswordField(),
		NewTable(reflect.TypeOf(struct{A,B,C int}{})),
		NewStandaloneLabel("hello"))
	tw.t.Append("Pref Width", tw.spw)
	tw.sph = NewVerticalStack(
		NewButton("hello"),
		NewCheckbox("hello"),
		NewTextField(),
		NewPasswordField(),
		NewTable(reflect.TypeOf(struct{A,B,C int}{})),
		NewStandaloneLabel("hello ÉÀÔ"))
	tw.t.Append("Pref Height", tw.sph)
	stack1 := NewHorizontalStack(NewLabel("Test"), NewTextField())
	stack1.SetStretchy(1)
	stack2 := NewHorizontalStack(NewLabel("ÉÀÔ"), NewTextField())
	stack2.SetStretchy(1)
	stack3 := NewHorizontalStack(NewLabel("Test 2"),
		NewTable(reflect.TypeOf(struct{A,B,C int}{})))
	stack3.SetStretchy(1)
	tw.s = NewVerticalStack(stack1, stack2, stack3)
	tw.s.SetStretchy(2)
	tw.t.Append("Stack", tw.s)
	tw.l = NewStandaloneLabel("hello")
	tw.t.Append("Label", tw.l)
	tw.table = NewTable(reflect.TypeOf(ddata[0]))
	tw.table.Lock()
	dq := tw.table.Data().(*[]dtype)
	*dq = ddata
	tw.table.Unlock()
	tw.t.Append("Table", tw.table)
	tw.b = NewButton("There")
	if *closeOnClick {
		tw.b.SetText("Click to Close")
	}
	tw.b.OnClicked(func() {
		println("in OnClicked()")
		if *closeOnClick {
			tw.w.Close()
			Stop()
			done <- struct{}{}
		}
	})
	tw.t.Append("Button", tw.b)
	tw.c = NewCheckbox("You Should Now See Me Instead")
	tw.c.OnToggled(func() {
		tw.w.SetTitle(fmt.Sprint(tw.c.Checked()))
	})
	tw.t.Append("Checkbox", tw.c)
	tw.e = NewTextField()
	tw.t.Append("Text Field", tw.e)
	tw.e2 = NewPasswordField()
	tw.t.Append("Password Field", tw.e2)
	tw.w.Show()
	if *smallWindow {
		tw.wsmall = NewWindow("Small", 80, 80,
			NewVerticalStack(
				NewButton("Small"),
				NewButton("Small 2"),
				NewArea(200, 200, &areaHandler{})))
		tw.wsmall.Show()
	}
}

// this must be on the heap thanks to moving stacks
// soon even this won't be enough...
var tw *testwin

// because Cocoa hates being run off the main thread, even if it's run exclusively off the main thread
func init() {
	flag.BoolVar(&spaced, "spaced", false, "enable spacing")
	flag.Parse()
	go func() {
		tw = new(testwin)
		done := make(chan struct{})
		Do(func() { tw.make(done) })
		<-done
	}()
	err := Go()
	if err != nil {
		panic(err)
	}
}

func TestDummy(t *testing.T) {
	// do nothing
}