package ui import ( "math" "testing" "github.com/egoist/mygo/internal/platform" ) func TestField(t *testing.T) { name, email, font, size := "", "", "Arial", "Small" notify, express := false, false emailError := "Enter an email address." tt := coreNewTester(func(c *context) { coreColumn(c).Gap(12).Children(func() { coreField(c, "Name", func() { coreTextInput(c, &name) }).Description("As on your passport.") coreField(c, "Email", func() { coreTextInput(c, &email) }).Description("For receipts.").Error(emailError) coreField(c, "Notifications", func() { coreCheckbox(c, ¬ify, "Send emails") }) coreField(c, "Delivery", func() { coreRadioGroup(c, func() { coreRadio(c, &size, "Small", "Small") coreRadio(c, &size, "Large", "Large") }) }) coreField(c, "Font", func() { coreCombobox(c, &font, fonts) }) coreField(c, "Express", func() { coreSwitch(c, &express) }) coreButton(c, "Cut").Tooltip("Cut the selection") }) }, 500, 700) tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) tree := tt.h.access // The label names the control, and the texts below describe it, the // error first. if n := accessNode(t, tree, platform.RoleTextField, "Name"); n.Description != "As on your passport." || n.States&platform.AccessInvalid != 0 { t.Errorf("Name: %+v", n) } if n := accessNode(t, tree, platform.RoleTextField, "Email"); n.Description != emailError+"\nFor receipts." || n.States&platform.AccessInvalid == 0 { t.Errorf("Email: %+v", n) } if !tt.HasText(emailError) || !tt.HasText("For receipts.") { t.Errorf("the texts below do not show: %q", tt.Texts()) } // A check box names itself; the field names the group around it. accessNode(t, tree, platform.RoleCheckBox, "Send emails") accessNode(t, tree, platform.RoleGroup, "Notifications") accessNode(t, tree, platform.RoleRadioGroup, "Delivery") accessNode(t, tree, platform.RoleComboBox, "Font") accessNode(t, tree, platform.RoleSwitch, "Express") if n := accessNode(t, tree, platform.RoleButton, "Cut"); n.Description != "Cut the selection" { t.Errorf("the tooltip does not describe the button: %+v", n) } // A click on the label focuses the control, or clicks it. tt.Click("Name") if !tt.Focused("Name") { t.Error("a click on the label did not focus the input") } tt.Type("Ada") if name != "Ada" { t.Errorf("typed %q", name) } // The text, not the group it names. var label Rect for _, n := range tt.rt.labels { if n.text == "Notifications" { label = n.r } } tt.ClickAt(label.X+label.W/2, label.Y+label.H/2) if !notify { t.Error("a click on the label did not check the check box") } tt.Click("Express") if !express { t.Error("a click on the label did not turn the switch on") } // In a group, the one chosen. tt.Click("Delivery") if !tt.Focused("Small") || size != "Small" { t.Errorf("a click on the label of a radio group: focused %v, chose %q", tt.Focused("Small"), size) } } func TestFieldDisabled(t *testing.T) { name, saved := "", 0 tt := coreNewTester(func(c *context) { coreFieldset(c, "Account", func() { coreField(c, "Name", func() { coreTextInput(c, &name) }) if coreButton(c, "Save").Clicked() { saved++ } }).Disabled(true) }, 400, 300) // The fieldset disables what is inside it, though it does so after // building it. tt.Click("Name") if tt.Focused("Name") { t.Error("a click on the label focused a disabled input") } tt.Click("Save") if saved != 0 || tt.Focused("Save") { t.Errorf("a disabled button: %d clicks, focused %v", saved, tt.Focused("Save")) } r, _ := tt.Find("Name") tt.ClickAt(r.X+r.W/2, r.Y+r.H+20) // the input if tt.Focused("Name") { t.Error("a click focused a disabled input") } tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) if n := accessNode(t, tt.h.access, platform.RoleGroup, "Account"); n.States&platform.AccessDisabled == 0 { t.Errorf("the fieldset: %+v", n) } } // TestLabelledBy checks that a text the view draws, given to a control // with LabelledBy, names the control and takes a click to it, as HTML's //