Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions .github/workflows/text-input.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,52 @@ jobs:
with:
go-version-file: go.mod
cache: true
- name: Windows IME event dispatch cost
if: runner.os == 'Windows'
env:
IME_COST_BASE: ${{ github.event.pull_request.base.sha || github.event.before || github.sha }}
GOWORK: 'off'
run: |
$reference = $env:IME_COST_BASE
if (-not $reference -or $reference -match '^0+$') { $reference = $env:GITHUB_SHA }
$workspace = $PWD.Path
$directory = Join-Path $env:RUNNER_TEMP ("viva-ime-cost-" + $env:GITHUB_RUN_ID + "-" + $env:GITHUB_RUN_ATTEMPT)
$benchmark = 'internal/platform/windows/composition_cost_windows_test.go'
if (Test-Path $directory) { throw 'cost worktree already exists' }
git fetch --no-tags --depth=1 origin $reference
if ($LASTEXITCODE -ne 0) { throw 'could not fetch cost baseline' }
git worktree add --detach $directory $reference
if ($LASTEXITCODE -ne 0) { throw 'could not create cost worktree' }
try {
Copy-Item $benchmark (Join-Path $directory $benchmark) -Force
$sourceHash = (Get-FileHash -Algorithm SHA256 $benchmark).Hash
$baseHash = (Get-FileHash -Algorithm SHA256 (Join-Path $directory $benchmark)).Hash
if ($sourceHash -ne $baseHash) { throw 'benchmark source changed' }
$benchmarkBlob = git rev-parse ("HEAD:" + $benchmark)
if ($LASTEXITCODE -ne 0) { throw 'benchmark blob is unavailable' }
Write-Output "IME cost benchmark sha256=$sourceHash"
Write-Output "IME cost benchmark blob=$benchmarkBlob"
Write-Output "IME cost OS=$([Environment]::OSVersion.VersionString) arch=$env:PROCESSOR_ARCHITECTURE cpu=$env:PROCESSOR_IDENTIFIER"
go version
for ($pair = 1; $pair -le 5; $pair++) {
Push-Location $directory
try {
Write-Output "IME cost variant=baseline pair=$pair commit=$(git rev-parse HEAD)"
go test ./internal/platform/windows -run '^$' -bench '^BenchmarkWindowsIME(Acknowledgment|StateAllocation)$' -benchmem -benchtime=200ms -count=1 -timeout=90s
if ($LASTEXITCODE -ne 0) { throw 'baseline cost benchmark failed' }
} finally { Pop-Location }
Write-Output "IME cost variant=candidate pair=$pair commit=$(git rev-parse HEAD)"
go test ./internal/platform/windows -run '^$' -bench '^BenchmarkWindowsIME(Acknowledgment|StateAllocation)$' -benchmem -benchtime=200ms -count=1 -timeout=90s
if ($LASTEXITCODE -ne 0) { throw 'candidate cost benchmark failed' }
}
Write-Output "IME cost variant=dispatch commit=$(git rev-parse HEAD)"
go test ./internal/platform/windows -run '^$' -bench '^BenchmarkCompositionMessageDispatch$' -benchmem -benchtime=200ms -count=5 -timeout=90s
if ($LASTEXITCODE -ne 0) { throw 'message dispatch benchmark failed' }
} finally {
git -C $workspace worktree remove --force $directory
if ($LASTEXITCODE -ne 0) { throw 'cost worktree cleanup failed' }
Write-Output 'IME cost worktree removed'
}
- name: Application input lifecycle
run: go test ./app -count=1
- name: GTK native probe dependencies
Expand Down Expand Up @@ -80,7 +126,7 @@ jobs:
if: runner.os == 'Linux'
run: |
go test -c -tags vivagtk -o "$RUNNER_TEMP/viva-ibus-frontend.test" ./internal/platform/linux
xvfb-run -a env VIVA_IBUS_TEST_BINARY="$RUNNER_TEMP/viva-ibus-frontend.test" VIVA_IBUS_TEST_RUN='^TestNativeIBus(Frontend|OwnerReactivation|FactoryDeclinedShortcut)$' sh internal/ibus/testdata/native-service.sh libpinyin hangul
xvfb-run -a env VIVA_IBUS_TEST_BINARY="$RUNNER_TEMP/viva-ibus-frontend.test" VIVA_IBUS_TEST_RUN='^TestNativeIBus(Frontend|OwnerReactivation|FactoryDeclinedShortcut|UnsupportedModeRetirement)$' sh internal/ibus/testdata/native-service.sh libpinyin hangul
- name: Fcitx native context retirement
if: runner.os == 'Linux'
run: |
Expand All @@ -94,7 +140,7 @@ jobs:
run: |
for backend in x11 wayland; do
for engine in pinyin hangul; do
xvfb-run -a env VIVA_FCITX_ENGINE="$engine" VIVA_FCITX_REFERENCE=1 VIVA_FCITX_GTK_REFERENCE=1 VIVA_GTK_IME_NATIVE=1 G_DEBUG=fatal-criticals VIVA_FCITX_TEST_BINARY="$RUNNER_TEMP/viva-ibus-frontend.test" VIVA_IBUS_TEST_RUN='^Test(NativeIBus(Frontend|OwnerReactivation|FactoryDeclinedShortcut)|GTKNativeFcitx(Composition|WindowClose))$' sh internal/ibus/testdata/native-fcitx-service.sh "$backend"
xvfb-run -a env VIVA_FCITX_ENGINE="$engine" VIVA_FCITX_REFERENCE=1 VIVA_FCITX_GTK_REFERENCE=1 VIVA_GTK_IME_NATIVE=1 G_DEBUG=fatal-criticals VIVA_FCITX_TEST_BINARY="$RUNNER_TEMP/viva-ibus-frontend.test" VIVA_IBUS_TEST_RUN='^Test(NativeIBus(Frontend|OwnerReactivation|FactoryDeclinedShortcut|UnsupportedModeRetirement)|GTKNativeFcitx(Composition|WindowClose))$' sh internal/ibus/testdata/native-fcitx-service.sh "$backend"
done
done
- name: Linux text callback ordering
Expand All @@ -105,7 +151,10 @@ jobs:
run: go test -tags vivagtk ./internal/platform/linux -run '^TestLinuxTextCallback' -count=1 -timeout=90s
- name: Windows IME lifecycle
if: runner.os == 'Windows'
run: go test ./internal/platform/windows -run 'TestComposition|TestDynamicTextInput' -count=1
run: |
go test ./internal/platform/windows -list 'TestComposition|TestDynamicTextInput'
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
go test ./internal/platform/windows -run 'TestComposition|TestDynamicTextInput' -count=1 -v -timeout=90s
- name: Windows aligned text input
if: runner.os == 'Windows'
run: go test ./internal/platform/windows -run '^TestWindowsTextInputAlignmentMatchesGlyphCaretUnderlineAndPointer$' -count=1
Expand Down
180 changes: 180 additions & 0 deletions app/composition_handoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package app

import (
"testing"
"unsafe"

"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/internal/platform"
"github.com/vivaui/viva/text"
)

func TestPainterTentativeHandoffRetainsInstalledOwner(t *testing.T) {
for _, policy := range []string{"document", "terminal", "secure"} {
for _, mode := range []string{"editor replacement", "cached provider", "cached external switch", "provider disabled", "target replacement", "subpixel disabled", "provider removed", "same owner", "geometry only"} {
t.Run(policy+"/"+mode, func(t *testing.T) {
first, second := text.NewEditor("abc"), text.NewEditor("other")
if !first.EnableTentativeComposition() || !second.EnableTentativeComposition() {
t.Fatal("capability refused")
}
first.Insert("!")
first.SetSelection(text.Range{Start: 1, End: 2})
second.ApplyComposition(text.CompositionEvent{Phase: text.CompositionUpdate, Text: "incoming"})
incoming := second.String()
current := first
var extraWidth float32
var canvas gfx.Canvas
cached := &TextInput{Bounds: gfx.Rect{Width: 120, Height: 24}, FontSize: 13, Editor: first}
role := gfx.RoleTextField
if policy == "terminal" {
role = gfx.RoleTerminal
}
config := Config{Title: "composition handoff", Width: 200, Height: 80}
config.Paint = func(canvas *gfx.Canvas, size gfx.Size) {
canvas.HitWithOptions(gfx.Item(11), gfx.Rect{Width: 120, Height: 24}, "First", gfx.HitOptions{Role: role, Autofocus: true, Secure: policy == "secure"})
canvas.HitWithOptions(gfx.Item(12), gfx.Rect{Y: 30, Width: 120, Height: 24}, "Second", gfx.HitOptions{Role: role})
}
config.TextInput = func(context TextInputContext) *TextInput {
if mode == "cached external switch" {
return cached
}
if current == nil {
return nil
}
bounds := context.Bounds
bounds.Width += extraWidth
if mode == "cached provider" {
*cached = TextInput{Bounds: bounds, FontSize: 13, Editor: current}
return cached
}
return &TextInput{Bounds: bounds, FontSize: 13, Editor: current}
}
owner := newTestPainter(t, &canvas, config)
owner.syncTextInput()
if _, err := owner.applyTextEvent(platform.TextEvent{Kind: platform.TextSetMarked, Text: "ni", CompositionPhase: text.CompositionUpdate}); err != nil {
t.Fatal(err)
}
if first.String() != "anic!" || !first.TentativeCompositionActive() {
t.Fatal("preedit did not start")
}
switch mode {
case "editor replacement", "cached provider":
current = second
case "cached external switch":
current, cached.Editor = second, second
case "provider disabled":
current = nil
case "target replacement":
owner.focus.Apply(gfx.EventResult{Focus: gfx.Item(12)})
case "subpixel disabled":
extraWidth = -119.7
case "provider removed":
owner.textInput = nil
case "geometry only":
extraWidth = 10
}
update := owner.syncTextInput()
if mode == "same owner" || mode == "geometry only" {
if !first.TentativeCompositionActive() || first.String() != "anic!" {
t.Fatal("unchanged owner lost its live composition")
}
return
}
want := "anic!"
if policy == "terminal" {
want = "ac!"
}
if first.CompositionActive() || first.TentativeCompositionActive() || first.String() != want || first.CommittedString() != want {
t.Fatalf("outgoing owner not settled: text=%q committed=%q active=%t, want %q", first.String(), first.CommittedString(), first.CompositionActive(), want)
}
if mode == "provider disabled" || mode == "provider removed" || mode == "subpixel disabled" {
if !update.Changed || update.Input != nil || owner.textEnabled || owner.activeText != nil {
t.Fatal("disabled input did not release the native owner")
}
}
if mode == "target replacement" && owner.nativeText.State.Composing {
t.Fatal("same editor's new target published a stale composing snapshot")
}
if second.String() != incoming || !second.TentativeCompositionActive() {
t.Fatal("handoff settled the incoming editor")
}
if policy == "secure" {
if first.CanUndo() || first.CanRedo() {
t.Fatal("secure handoff retained committed history")
}
return
}
undo, redo := []string{"abc!", "abc"}, []string{"abc!", want}
if policy == "document" {
undo, redo = []string{"ac!", "abc!", "abc"}, []string{"abc!", "ac!", want}
}
for _, value := range undo {
if !first.Undo() || first.String() != value {
t.Fatalf("handoff Undo = %q, want %q", first.String(), value)
}
}
for _, value := range redo {
if !first.Redo() || first.String() != value {
t.Fatalf("handoff Redo = %q, want %q", first.String(), value)
}
}
})
}
}
}

func TestPainterCachedCustomHandoffPreservesProviderOwnership(t *testing.T) {
first, second := text.NewEditor("first"), text.NewEditor("second")
for _, editor := range []*text.Editor{first, second} {
editor.EnableTentativeComposition()
editor.ApplyComposition(text.CompositionEvent{Phase: text.CompositionUpdate, Text: "ni"})
}
cached := &TextInput{State: TextStateOf(first), OnEvent: func(event TextEvent) (TextState, error) {
t.Fatal("custom owner received an unsolicited handoff event")
return TextState{}, nil
}}
var canvas gfx.Canvas
config := Config{Title: "custom cached handoff", Width: 200, Height: 80}
config.Paint = func(canvas *gfx.Canvas, size gfx.Size) {
canvas.HitWithOptions(gfx.Item(11), gfx.Rect{Width: 120, Height: 24}, "First", gfx.HitOptions{Role: gfx.RoleTextField, Autofocus: true})
canvas.HitWithOptions(gfx.Item(12), gfx.Rect{Y: 30, Width: 120, Height: 24}, "Second", gfx.HitOptions{Role: gfx.RoleTextField})
}
config.TextInput = func(TextInputContext) *TextInput { return cached }
owner := newTestPainter(t, &canvas, config)
owner.syncTextInput()
cached.Editor = second
owner.focus.Apply(gfx.EventResult{Focus: gfx.Item(12)})
owner.syncTextInput()
if !first.TentativeCompositionActive() || !second.TentativeCompositionActive() {
t.Fatal("cached custom handoff settled a provider-owned or incoming transaction")
}
}

func BenchmarkPainterTextInputSync(b *testing.B) {
for _, enabled := range []bool{false, true} {
name := "legacy"
if enabled {
name = "tentative"
}
b.Run(name, func(b *testing.B) {
editor := text.NewEditor("steady input")
if enabled {
editor.EnableTentativeComposition()
}
cached := &TextInput{Editor: editor, Bounds: gfx.Rect{Width: 120, Height: 24}, FontSize: 13}
var canvas gfx.Canvas
canvas.HitWithOptions(gfx.Item(11), cached.Bounds, "Field", gfx.HitOptions{Role: gfx.RoleTextField, Autofocus: true})
owner := &painter{canvas: &canvas, textInput: func(TextInputContext) *TextInput { return cached }}
owner.focus.Sync(&canvas)
owner.syncTextInput()
if !owner.textEnabled {
b.Fatal("input not enabled")
}
b.ReportAllocs()
for b.Loop() {
owner.syncTextInput()
}
b.ReportMetric(float64(unsafe.Sizeof(painter{})), "painter-B")
})
}
}
Loading
Loading