windows_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //go:build windows && (amd64 || arm64)
  2. package nativehost
  3. import (
  4. "github.com/wailsapp/go-webview2/pkg/combridge"
  5. "golang.org/x/sys/windows"
  6. "syscall"
  7. "testing"
  8. "unsafe"
  9. )
  10. func TestWindowsCallbackABI(t *testing.T) {
  11. if unsafe.Sizeof(winRect{}) != 16 || unsafe.Sizeof(winMessage{}) != 48 || unsafe.Sizeof(winClass{}) != 80 {
  12. t.Fatal("Win32 layout mismatch")
  13. }
  14. called := false
  15. callback := combridge.New[protocolCallback](&cdpDone{fn: func(hr uintptr, value *uint16) {
  16. called = true
  17. if hr != 0 {
  18. t.Fatal(hr)
  19. }
  20. s, e := boundedUTF16(value, 16)
  21. if e != nil || s != "WebView2 世界" {
  22. t.Fatal(s, e)
  23. }
  24. }})
  25. value, _ := windows.UTF16PtrFromString("WebView2 世界")
  26. object := (*winObject)(unsafe.Pointer(combridge.IUnknownFromUintptr(callback.Ref())))
  27. hr, _, _ := syscall.SyscallN(object.table[3], callback.Ref(), 0, uintptr(unsafe.Pointer(value)))
  28. if hr != 0 || !called {
  29. t.Fatal("COM callback not invoked", hr)
  30. }
  31. if e := callback.Close(); e != nil {
  32. t.Fatal(e)
  33. }
  34. if hrError(0) != nil || hrError(1) != nil || hrError(0x80004005) == nil {
  35. t.Fatal("HRESULT sign decoding failed")
  36. }
  37. if s, e := boundedUTF16(nil, 1); e != nil || s != "" {
  38. t.Fatal(s, e)
  39. }
  40. if _, e := boundedUTF16(value, 1); e == nil {
  41. t.Fatal("unbounded UTF-16 accepted")
  42. }
  43. }