166 lines
3.2 KiB
Go
166 lines
3.2 KiB
Go
package gel
|
|
|
|
import (
|
|
"image"
|
|
"testing"
|
|
)
|
|
|
|
func TestClickableCreation(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
if click == nil {
|
|
t.Fatal("Clickable() returned nil")
|
|
}
|
|
if click.Window != w {
|
|
t.Error("Clickable Window reference incorrect")
|
|
}
|
|
}
|
|
|
|
func TestClickableEventCallbacks(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
// Default callbacks should exist
|
|
if click.Events.Click == nil {
|
|
t.Error("Events.Click should not be nil")
|
|
}
|
|
if click.Events.Cancel == nil {
|
|
t.Error("Events.Cancel should not be nil")
|
|
}
|
|
if click.Events.Press == nil {
|
|
t.Error("Events.Press should not be nil")
|
|
}
|
|
}
|
|
|
|
func TestClickableSetClick(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
called := false
|
|
click.SetClick(func() {
|
|
called = true
|
|
})
|
|
|
|
// Invoke the callback
|
|
click.Events.Click()
|
|
if !called {
|
|
t.Error("SetClick callback was not set correctly")
|
|
}
|
|
}
|
|
|
|
func TestClickableSetCancel(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
called := false
|
|
click.SetCancel(func() {
|
|
called = true
|
|
})
|
|
|
|
click.Events.Cancel()
|
|
if !called {
|
|
t.Error("SetCancel callback was not set correctly")
|
|
}
|
|
}
|
|
|
|
func TestClickableSetPress(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
called := false
|
|
click.SetPress(func() {
|
|
called = true
|
|
})
|
|
|
|
click.Events.Press()
|
|
if !called {
|
|
t.Error("SetPress callback was not set correctly")
|
|
}
|
|
}
|
|
|
|
func TestClickableFn(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
gtx := testContext(t, image.Pt(100, 100))
|
|
|
|
// Fn should not panic and should return dimensions
|
|
dims := click.Fn(gtx)
|
|
|
|
// Clickable by itself doesn't render anything visible, so zero dimensions are expected
|
|
// but it should not panic
|
|
_ = dims
|
|
}
|
|
|
|
func TestClickableHistory(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
// History should start empty
|
|
history := click.History()
|
|
if len(history) != 0 {
|
|
t.Errorf("initial history length = %d, want 0", len(history))
|
|
}
|
|
}
|
|
|
|
func TestClickableClicked(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
// Without any events, Clicked should return false
|
|
if click.Clicked() {
|
|
t.Error("Clicked() should return false without click events")
|
|
}
|
|
}
|
|
|
|
func TestClickableClicks(t *testing.T) {
|
|
w := testWindow(t)
|
|
click := w.Clickable()
|
|
|
|
// Without any events, Clicks should return empty slice
|
|
clicks := click.Clicks()
|
|
if len(clicks) != 0 {
|
|
t.Errorf("Clicks() returned %d clicks, want 0", len(clicks))
|
|
}
|
|
}
|
|
|
|
func TestClickableChainedMethods(t *testing.T) {
|
|
w := testWindow(t)
|
|
|
|
clickCalled := false
|
|
pressCalled := false
|
|
cancelCalled := false
|
|
|
|
// Test fluent API
|
|
click := w.Clickable().
|
|
SetClick(func() { clickCalled = true }).
|
|
SetPress(func() { pressCalled = true }).
|
|
SetCancel(func() { cancelCalled = true })
|
|
|
|
// Verify all callbacks were set
|
|
click.Events.Click()
|
|
click.Events.Press()
|
|
click.Events.Cancel()
|
|
|
|
if !clickCalled {
|
|
t.Error("Click callback not set in chain")
|
|
}
|
|
if !pressCalled {
|
|
t.Error("Press callback not set in chain")
|
|
}
|
|
if !cancelCalled {
|
|
t.Error("Cancel callback not set in chain")
|
|
}
|
|
}
|
|
|
|
func BenchmarkClickableFn(b *testing.B) {
|
|
w := testWindow(&testing.T{})
|
|
click := w.Clickable()
|
|
gtx := testContext(&testing.T{}, image.Pt(100, 100))
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
click.Fn(gtx)
|
|
}
|
|
}
|