package layout import ( "image" "testing" "github.com/BeatGlow/beatglow/app" "github.com/BeatGlow/beatglow/app/menu" "github.com/BeatGlow/beatglow/app/widget" "github.com/BeatGlow/beatglow/config" ) type settingsTestContext struct { app.Context closed bool } func (c *settingsTestContext) Show(layout app.Layout) { c.closed = layout == nil } func TestSettingsBackAndInformation(t *testing.T) { data := config.DefaultSettings root, err := menu.New(&data, 4, nil) if err != nil { t.Fatal(err) } l := &settings{menu: root, menuWidget: widget.NewMenu(image.Pt(256, 64), root)} ctx := &settingsTestContext{} root.Position = 4 // System l.HandleButtonPress(ctx) if l.menu.Parent != root { t.Fatal("did not enter System submenu") } for _, position := range []int{1, 2} { l.menu.Position = position l.HandleButtonPress(ctx) if l.menu.IsEditing() { t.Fatal("system information entered edit mode") } } l.HandleRotary(ctx, -2) l.HandleButtonPress(ctx) if l.menu != root || l.menuWidget.Root != root { t.Fatal("Back did not restore the root menu") } if root.Position != 4 { t.Fatal("parent selection was lost") } root.Position = 0 l.HandleButtonPress(ctx) if !ctx.closed { t.Fatal("root Back did not close settings") } } func TestSettingsLongPressSynchronizesMenu(t *testing.T) { data := config.DefaultSettings root, err := menu.New(&data, 4, nil) if err != nil { t.Fatal(err) } root.Position = 1 // Display child := root.EnterSubMenu() child.Position = 1 child.ToggleEdit() l := &settings{menu: child, menuWidget: widget.NewMenu(image.Pt(256, 64), child)} ctx := &settingsTestContext{} l.HandleButtonLongPress(ctx) if !ctx.closed || l.menu != root || l.menuWidget.Root != root { t.Fatal("long press left the rendered menu out of sync") } if child.IsEditing() || root.IsEditing() { t.Fatal("long press left an item in edit mode") } l.HandleButtonPress(ctx) if l.menu != child || l.menu.IsEditing() { t.Fatal("reopening submenu did not restore navigation mode") } } func TestBooleanSelectionSurvivesConfirmation(t *testing.T) { data := config.DefaultSettings data.Player.ShowArt = true root, err := menu.New(&data, 4, nil) if err != nil { t.Fatal(err) } root.Position = 2 // Player l := &settings{settings: &data, menu: root, menuWidget: widget.NewMenu(image.Pt(256, 64), root)} ctx := &settingsTestContext{} l.HandleButtonPress(ctx) l.HandleRotary(ctx, 2) // Show album art l.HandleButtonPress(ctx) for _, delta := range []int{-1, -1, -3} { l.HandleRotary(ctx, delta) } if data.Player.ShowArt { t.Fatal("left did not keep checkbox unchecked") } l.HandleButtonPress(ctx) if l.menu.IsEditing() || data.Player.ShowArt { t.Fatal("press did not confirm unchecked value") } l.HandleRotary(ctx, -2) l.HandleButtonPress(ctx) // Back to root l.HandleButtonPress(ctx) // Reopen Player l.HandleRotary(ctx, 2) // Show album art l.HandleButtonPress(ctx) // Edit selected Show album art if data.Player.ShowArt { t.Fatal("reopening checkbox reset its value") } for _, delta := range []int{1, 1, 3} { l.HandleRotary(ctx, delta) } l.HandleButtonPress(ctx) if l.menu.IsEditing() || !data.Player.ShowArt { t.Fatal("press did not confirm checked value") } }