-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathentry_cursor_anim_test.go
75 lines (63 loc) · 2.05 KB
/
entry_cursor_anim_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package widget
import (
"image/color"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"fyne.io/fyne/v2/canvas"
col "fyne.io/fyne/v2/internal/color"
_ "fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
)
func TestEntryCursorAnim(t *testing.T) {
cursorOpaque := theme.Color(theme.ColorNamePrimary)
r, g, b, _ := col.ToNRGBA(cursorOpaque)
cursorDim := color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: 0x16}
alphaEquals := func(color1, color2 color.Color) bool {
_, _, _, a1 := col.ToNRGBA(color1)
_, _, _, a2 := col.ToNRGBA(color2)
return uint8(a1>>8) == uint8(a2>>8) // only check 8bit colour channels
}
cursor := canvas.NewRectangle(color.Black)
a := newEntryCursorAnimation(cursor)
a.start()
a.anim.Tick(0.0)
assert.True(t, alphaEquals(cursorDim, a.cursor.FillColor))
a.anim.Tick(1.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))
a.interrupt()
a.anim.Tick(0.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))
a.anim.Tick(0.5)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))
a.anim.Tick(1.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))
a.timeNow = func() time.Time {
return time.Now().Add(cursorInterruptTime)
}
// animation should be restarted inverting the colors
a.anim.Tick(0.0)
runtime.Gosched()
time.Sleep(10 * time.Millisecond) // ensure go routine for restart animation is executed
a.anim.Tick(0.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))
a.anim.Tick(1.0)
assert.True(t, alphaEquals(cursorDim, a.cursor.FillColor))
a.timeNow = time.Now
a.interrupt()
a.anim.Tick(0.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))
a.timeNow = func() time.Time {
return time.Now().Add(cursorInterruptTime)
}
a.anim.Tick(0.0)
runtime.Gosched()
time.Sleep(10 * time.Millisecond) // ensure go routine for restart animation is executed
a.anim.Tick(0.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))
a.anim.Tick(1.0)
assert.True(t, alphaEquals(cursorDim, a.cursor.FillColor))
a.stop()
assert.Nil(t, a.anim)
}