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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package pile
import (
"fmt"
"strings"
"testing"
"github.com/gcla/gowid"
"github.com/gcla/gowid/gwtest"
"github.com/gcla/gowid/widgets/button"
"github.com/gcla/gowid/widgets/fill"
"github.com/gcla/gowid/widgets/framed"
"github.com/gcla/gowid/widgets/list"
"github.com/gcla/gowid/widgets/selectable"
"github.com/gcla/gowid/widgets/text"
tcell "github.com/gdamore/tcell/v2"
"github.com/stretchr/testify/assert"
)
// Test that a mouse wheel down inside the region of the list within a pile
// is correctly translated and passed to the list.
func TestPile5(t *testing.T) {
bws := make([]gowid.IWidget, 50)
for i := 0; i < len(bws); i++ {
bws[i] = button.New(text.New(fmt.Sprintf("%03d", i)))
}
walker := list.NewSimpleListWalker(bws)
lb := list.New(walker)
// Framed is needed because it validates the mouse y coordinate before passing it on
// to its subwidget.
flb := framed.New(lb)
pws := make([]gowid.IContainerWidget, 3)
pws[0] = &gowid.ContainerWidget{button.New(text.New("top ")), gowid.RenderWithUnits{U: 1}}
pws[1] = &gowid.ContainerWidget{flb, gowid.RenderWithWeight{W: 1}} // WEIGHT!!
pws[2] = &gowid.ContainerWidget{button.New(text.New("bot ")), gowid.RenderWithUnits{U: 1}}
pl := New(pws)
sta := make([]string, 0)
sta = append(sta, "<top >")
sta = append(sta, "-------")
for i := 0; i < 6; i++ {
sta = append(sta, fmt.Sprintf("|<%03d>|", i))
}
sta = append(sta, "-------")
sta = append(sta, "<bot >")
csize := gowid.RenderBox{C: 7, R: 10}
c := pl.Render(csize, gowid.Focused, gwtest.D)
assert.Equal(t, strings.Join(sta, "\n"), c.String())
assert.Equal(t, 0, pl.Focus())
evmdown := tcell.NewEventMouse(1, 4, tcell.WheelDown, 0)
pl.UserInput(evmdown, csize, gowid.Focused, gwtest.D)
assert.Equal(t, 1, pl.Focus()) // Now at list widget
assert.Equal(t, 0, lb.Walker().Focus().(list.ListPos).ToInt())
pl.UserInput(evmdown, csize, gowid.Focused, gwtest.D)
assert.Equal(t, 1, pl.Focus()) // Now at list widget
assert.Equal(t, 1, lb.Walker().Focus().(list.ListPos).ToInt())
for i := 0; i < 40; i++ {
pl.UserInput(evmdown, csize, gowid.Focused, gwtest.D)
}
assert.Equal(t, 1, pl.Focus()) // Now at list widget
assert.Equal(t, 41, lb.Walker().Focus().(list.ListPos).ToInt())
}
func TestPile2(t *testing.T) {
btns := make([]gowid.IContainerWidget, 0)
//clicks := make([]*gwtest.ButtonTester, 0)
for i := 0; i < 3; i++ {
btn := button.New(text.New("abc"))
click := &gwtest.ButtonTester{Gotit: false}
btn.OnClick(click)
btns = append(btns, &gowid.ContainerWidget{btn, gowid.RenderFixed{}})
//clicks = append(clicks, click)
}
pl := New(btns)
st1 := "<abc>\n<abc>\n<abc>"
st2 := "<abc> \n<abc> \n<abc> "
c := pl.Render(gowid.RenderFixed{}, gowid.Focused, gwtest.D)
assert.Equal(t, c.String(), st1)
c2 := pl.Render(gowid.RenderFlowWith{C: 6}, gowid.Focused, gwtest.D)
assert.Equal(t, c2.String(), st2)
assert.Equal(t, pl.Focus(), 0)
// evright := tcell.NewEventKey(tcell.KeyRight, ' ', tcell.ModNone)
// evleft := tcell.NewEventKey(tcell.KeyLeft, ' ', tcell.ModNone)
// evdown := tcell.NewEventKey(tcell.KeyDown, ' ', tcell.ModNone)
// evspace := tcell.NewEventKey(tcell.KeyRune, ' ', tcell.ModNone)
evmdown := tcell.NewEventMouse(1, 1, tcell.WheelDown, 0)
evmup := tcell.NewEventMouse(1, 1, tcell.WheelUp, 0)
// evmright := tcell.NewEventMouse(1, 1, tcell.WheelRight, 0)
// evmleft := tcell.NewEventMouse(1, 1, tcell.WheelLeft, 0)
cbcalled := false
pl.OnFocusChanged(gowid.WidgetCallback{"cb", func(app gowid.IApp, w gowid.IWidget) {
assert.Equal(t, w, pl)
cbcalled = true
}})
pl.UserInput(evmdown, gowid.RenderFixed{}, gowid.Focused, gwtest.D)
assert.Equal(t, 1, pl.Focus())
assert.Equal(t, true, cbcalled)
cbcalled = false
pl.UserInput(evmdown, gowid.RenderFixed{}, gowid.Focused, gwtest.D)
assert.Equal(t, 2, pl.Focus())
assert.Equal(t, true, cbcalled)
cbcalled = false
pl.UserInput(evmdown, gowid.RenderFixed{}, gowid.Focused, gwtest.D)
assert.Equal(t, 2, pl.Focus())
assert.Equal(t, false, cbcalled)
pl.UserInput(evmup, gowid.RenderFixed{}, gowid.Focused, gwtest.D)
assert.Equal(t, 1, pl.Focus())
assert.Equal(t, true, cbcalled)
cbcalled = false
pl.UserInput(evmup, gowid.RenderFixed{}, gowid.Focused, gwtest.D)
assert.Equal(t, 0, pl.Focus())
assert.Equal(t, true, cbcalled)
cbcalled = false
pl.UserInput(evmup, gowid.RenderFixed{}, gowid.Focused, gwtest.D)
assert.Equal(t, 0, pl.Focus())
assert.Equal(t, false, cbcalled)
}
func TestPile1(t *testing.T) {
w1 := New([]gowid.IContainerWidget{
&gowid.ContainerWidget{fill.New('x'), gowid.RenderWithUnits{U: 2}},
&gowid.ContainerWidget{fill.New('y'), gowid.RenderWithUnits{U: 2}},
})
c1 := w1.Render(gowid.RenderBox{C: 3, R: 4}, gowid.Focused, gwtest.D)
assert.Equal(t, c1.String(), "xxx\nxxx\nyyy\nyyy")
w2 := New([]gowid.IContainerWidget{
&gowid.ContainerWidget{fill.New('x'), gowid.RenderWithUnits{U: 1}},
&gowid.ContainerWidget{fill.New('y'), gowid.RenderWithUnits{U: 2}},
})
c2 := w2.Render(gowid.RenderFlowWith{C: 3}, gowid.Focused, gwtest.D)
assert.Equal(t, c2.String(), "xxx\nyyy\nyyy")
w3 := New([]gowid.IContainerWidget{
&gowid.ContainerWidget{fill.New('x'), gowid.RenderWithWeight{1}},
&gowid.ContainerWidget{fill.New('y'), gowid.RenderWithWeight{2}},
})
assert.Panics(t, func() {
w3.Render(gowid.RenderFlowWith{C: 3}, gowid.Focused, gwtest.D)
})
w4 := New([]gowid.IContainerWidget{
&gowid.ContainerWidget{fill.New('x'), gowid.RenderWithRatio{0.25}},
&gowid.ContainerWidget{fill.New('y'), gowid.RenderWithRatio{0.5}},
})
c4 := w4.Render(gowid.RenderBox{C: 3, R: 3}, gowid.Focused, gwtest.D)
assert.Equal(t, c4.String(), "xxx\nyyy\nyyy")
c41 := w4.Render(gowid.RenderBox{C: 3, R: 4}, gowid.Focused, gwtest.D)
assert.Equal(t, c41.String(), "xxx\nyyy\nyyy\n ")
for _, w := range []gowid.IWidget{w1, w2, w4} {
gwtest.RenderBoxManyTimes(t, w, 0, 10, 0, 10)
}
gwtest.RenderFlowManyTimes(t, w2, 0, 10)
}
func TestPile3(t *testing.T) {
w1 := New([]gowid.IContainerWidget{
&gowid.ContainerWidget{fill.New('x'), gowid.RenderWithUnits{U: 2}},
&gowid.ContainerWidget{text.New("y"), gowid.RenderFlow{}},
})
// Test that a pile can render in flow mode with a single embedded flow widget
c1 := w1.Render(gowid.RenderFlowWith{C: 3}, gowid.Focused, gwtest.D)
assert.Equal(t, c1.String(), "xxx\nxxx\ny ")
w1 = New([]gowid.IContainerWidget{
&gowid.ContainerWidget{fill.New('x'), gowid.RenderWithUnits{U: 2}},
&gowid.ContainerWidget{text.New("y"), gowid.RenderWithWeight{1}},
})
// Test that a pile can render in flow mode with a single embedded flow widget
c1 = w1.Render(gowid.RenderFlowWith{C: 3}, gowid.Focused, gwtest.D)
assert.Equal(t, c1.String(), "xxx\nxxx\ny ")
w1 = New([]gowid.IContainerWidget{
&gowid.ContainerWidget{fill.New('x'), gowid.RenderWithUnits{U: 2}},
&gowid.ContainerWidget{text.New("y"), gowid.RenderWithWeight{1}},
&gowid.ContainerWidget{text.New("z"), gowid.RenderWithWeight{1}},
})
// Two weight widgets don't work in flow mode, how do you restrict their vertical ratio?
assert.Panics(t, func() {
w1.Render(gowid.RenderFlowWith{C: 3}, gowid.Focused, gwtest.D)
})
}
func makep(c rune) gowid.IWidget {
return selectable.New(fill.New(c))
}
func makepfixed(c rune) gowid.IContainerWidget {
return &gowid.ContainerWidget{
IWidget: makep(c),
D: gowid.RenderFixed{},
}
}
type renderWeightUpTo struct {
gowid.RenderWithWeight
max int
}
func (s renderWeightUpTo) MaxUnits() int {
return s.max
}
func weightupto(w int, max int) renderWeightUpTo {
return renderWeightUpTo{gowid.RenderWithWeight{W: w}, max}
}
func TestPile4(t *testing.T) {
subs := []gowid.IContainerWidget{
&gowid.ContainerWidget{makep('x'), gowid.RenderWithWeight{W: 1}},
&gowid.ContainerWidget{makep('y'), gowid.RenderWithWeight{W: 1}},
&gowid.ContainerWidget{makep('z'), gowid.RenderWithWeight{W: 1}},
}
w := New(subs)
c := w.Render(gowid.RenderBox{C: 1, R: 12}, gowid.Focused, gwtest.D)
assert.Equal(t, `
x
x
x
x
y
y
y
y
z
z
z
z`[1:], c.String())
subs[2] = &gowid.ContainerWidget{makep('z'), renderWeightUpTo{gowid.RenderWithWeight{W: 1}, 2}}
w = New(subs)
c = w.Render(gowid.RenderBox{C: 1, R: 12}, gowid.Focused, gwtest.D)
assert.Equal(t, `
x
x
x
x
x
y
y
y
y
y
z
z`[1:], c.String())
}
func TestPile6(t *testing.T) {
subs := []gowid.IContainerWidget{
&gowid.ContainerWidget{text.New("foo"), gowid.RenderFixed{}},
&gowid.ContainerWidget{text.New("bar"), gowid.RenderWithWeight{W: 1}},
&gowid.ContainerWidget{text.New("baz"), gowid.RenderFixed{}},
}
w := New(subs)
c := w.Render(gowid.RenderBox{C: 3, R: 5}, gowid.Focused, gwtest.D)
assert.Equal(t, `
foo
bar
baz`[1:], c.String())
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End:
|