github.com/Seikaijyu/gio@v0.0.1/widget/button_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package widget_test
     4  
     5  import (
     6  	"image"
     7  	"testing"
     8  
     9  	"github.com/Seikaijyu/gio/io/key"
    10  	"github.com/Seikaijyu/gio/io/router"
    11  	"github.com/Seikaijyu/gio/io/system"
    12  	"github.com/Seikaijyu/gio/layout"
    13  	"github.com/Seikaijyu/gio/op"
    14  	"github.com/Seikaijyu/gio/widget"
    15  )
    16  
    17  func TestClickable(t *testing.T) {
    18  	var (
    19  		ops op.Ops
    20  		r   router.Router
    21  		b1  widget.Clickable
    22  		b2  widget.Clickable
    23  	)
    24  	gtx := layout.NewContext(&ops, system.FrameEvent{Queue: &r})
    25  	layout := func() {
    26  		b1.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
    27  			return layout.Dimensions{Size: image.Pt(100, 100)}
    28  		})
    29  		// buttons are on top of each other but we only use focus and keyevents, so this is fine
    30  		b2.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
    31  			return layout.Dimensions{Size: image.Pt(100, 100)}
    32  		})
    33  	}
    34  	frame := func() {
    35  		ops.Reset()
    36  		layout()
    37  		r.Frame(gtx.Ops)
    38  	}
    39  	// frame: request focus for button 1
    40  	b1.Focus()
    41  	frame()
    42  	// frame: gain focus for button 1
    43  	frame()
    44  	if !b1.Focused() {
    45  		t.Error("button 1 did not gain focus")
    46  	}
    47  	if b2.Focused() {
    48  		t.Error("button 2 should not have focus")
    49  	}
    50  	// frame: press & release return
    51  	frame()
    52  	r.Queue(
    53  		key.Event{
    54  			Name:  key.NameReturn,
    55  			State: key.Press,
    56  		},
    57  		key.Event{
    58  			Name:  key.NameReturn,
    59  			State: key.Release,
    60  		},
    61  	)
    62  	if !b1.Clicked(gtx) {
    63  		t.Error("button 1 did not get clicked when it got return press & release")
    64  	}
    65  	if b2.Clicked(gtx) {
    66  		t.Error("button 2 got clicked when it did not have focus")
    67  	}
    68  	// frame: press return down
    69  	r.Queue(
    70  		key.Event{
    71  			Name:  key.NameReturn,
    72  			State: key.Press,
    73  		},
    74  	)
    75  	frame()
    76  	if b1.Clicked(gtx) {
    77  		t.Error("button 1 got clicked, even if it only got return press")
    78  	}
    79  	// frame: request focus for button 2
    80  	b2.Focus()
    81  	frame()
    82  	// frame: gain focus for button 2
    83  	frame()
    84  	if b1.Focused() {
    85  		t.Error("button 1 should not have focus")
    86  	}
    87  	if !b2.Focused() {
    88  		t.Error("button 2 did not gain focus")
    89  	}
    90  	// frame: release return
    91  	r.Queue(
    92  		key.Event{
    93  			Name:  key.NameReturn,
    94  			State: key.Release,
    95  		},
    96  	)
    97  	frame()
    98  	if b1.Clicked(gtx) {
    99  		t.Error("button 1 got clicked, even if it had lost focus")
   100  	}
   101  	if b2.Clicked(gtx) {
   102  		t.Error("button 2 should not have been clicked, as it only got return release")
   103  	}
   104  }