package beatglow import ( "testing" "github.com/BeatGlow/beatglow/app" "github.com/BeatGlow/beatglow/hardware" ) type navigationTestLayout struct { app.Layout name string visible bool } func (l *navigationTestLayout) String() string { return l.name } func (l *navigationTestLayout) Show(app.Context) { l.visible = true } func (l *navigationTestLayout) Hide(app.Context) { l.visible = false } func (l *navigationTestLayout) IsVisible() bool { return l.visible } func TestSettingsReturnsToActiveLayout(t *testing.T) { settings := &navigationTestLayout{name: "settings"} playing := &navigationTestLayout{name: "playing"} analyzer := &navigationTestLayout{name: "analyzer"} c := &controllerContext{settingsLayout: settings, layouts: []app.Layout{settings, playing, analyzer}} c.Show(playing) c.Show(analyzer) for i := 0; i < 2; i++ { c.Show(settings) if c.active != settings || c.previous != analyzer { t.Fatal("settings did not remember the active screen") } c.Show(nil) if c.active != analyzer || !analyzer.visible || settings.visible { t.Fatal("exiting settings did not restore analyzer") } } } type inputTestLayout struct { navigationTestLayout onPress func(app.Context) presses int } func (l *inputTestLayout) HandleButtonPress(ctx app.Context) { l.presses++ if l.onPress != nil { l.onPress(ctx) } } func (l *inputTestLayout) HandleButtonLongPress(ctx app.Context) { l.HandleButtonPress(ctx) } func TestButtonEventIsNotRedispatchedAfterScreenChange(t *testing.T) { for _, long := range []bool{false, true} { first := &inputTestLayout{navigationTestLayout: navigationTestLayout{name: "first"}} second := &inputTestLayout{navigationTestLayout: navigationTestLayout{name: "second"}} c := &controllerContext{layouts: []app.Layout{first, second}} c.Show(first) first.onPress = func(ctx app.Context) { ctx.Show(second) } if long { c.handleInput(hardware.RotaryLongPressEvent{}) } else { c.handleInput(hardware.RotaryPressEvent{}) } if first.presses != 1 || second.presses != 0 { t.Fatalf("event was redispatched: first=%d second=%d", first.presses, second.presses) } } }