5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 20:21:01 +08:00
wails/v3/pkg/application/events_test.go
Lea Anthony 5dbda4aead
Feature: AssetServer Runtime (#2335)
* Tidy up runtime JS

* Initial implementation of runtime over http

* Update runtime deps. Fix test task.

* Support Clipboard.
Message Processor refactor.

* Add `Window.Screen()`
Clipboard `GetText` -> `Text`

* Support most dialogs
Better JS->Go object mapping
Implement Go->JS callback mechanism
Rename `window.runtime` -> `window.wails` to better reflect the Go API

* Support SaveFile dialog

* Remove go.work

* Tidy up

* Event->CustomEvent to prevent potential clash with native JS Event object
Support Eventing

* Support application calls

* Support logging

* Support named windows
Remove debug info

* Update v3 changes
2023-02-06 20:50:11 +11:00

136 lines
2.7 KiB
Go

package application_test
import (
"sync"
"testing"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/matryer/is"
)
type mockNotifier struct {
Events []*application.CustomEvent
}
func (m *mockNotifier) dispatchEventToWindows(event *application.CustomEvent) {
m.Events = append(m.Events, event)
}
func (m *mockNotifier) Reset() {
m.Events = []*application.CustomEvent{}
}
func Test_EventsOn(t *testing.T) {
i := is.New(t)
notifier := &mockNotifier{}
eventProcessor := application.NewCustomEventProcessor(notifier.dispatchEventToWindows)
// Test On
eventName := "test"
counter := 0
var wg sync.WaitGroup
wg.Add(1)
unregisterFn := eventProcessor.On(eventName, func(event *application.CustomEvent) {
// This is called in a goroutine
counter++
wg.Done()
})
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
wg.Wait()
i.Equal(1, counter)
// Unregister
notifier.Reset()
unregisterFn()
counter = 0
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
i.Equal(0, counter)
}
func Test_EventsOnce(t *testing.T) {
i := is.New(t)
notifier := &mockNotifier{}
eventProcessor := application.NewCustomEventProcessor(notifier.dispatchEventToWindows)
// Test On
eventName := "test"
counter := 0
var wg sync.WaitGroup
wg.Add(1)
unregisterFn := eventProcessor.Once(eventName, func(event *application.CustomEvent) {
// This is called in a goroutine
counter++
wg.Done()
})
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
wg.Wait()
i.Equal(1, counter)
// Unregister
notifier.Reset()
unregisterFn()
counter = 0
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
i.Equal(0, counter)
}
func Test_EventsOnMultiple(t *testing.T) {
i := is.New(t)
notifier := &mockNotifier{}
eventProcessor := application.NewCustomEventProcessor(notifier.dispatchEventToWindows)
// Test On
eventName := "test"
counter := 0
var wg sync.WaitGroup
wg.Add(2)
unregisterFn := eventProcessor.OnMultiple(eventName, func(event *application.CustomEvent) {
// This is called in a goroutine
counter++
wg.Done()
}, 2)
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
wg.Wait()
i.Equal(2, counter)
// Unregister
notifier.Reset()
unregisterFn()
counter = 0
eventProcessor.Emit(&application.CustomEvent{
Name: "test",
Data: "test payload",
})
i.Equal(0, counter)
}