Add emit-on-change tracker for the Windows polling sensor
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package evidence
|
||||||
|
|
||||||
|
// foregroundTracker remembers the last observed foreground window so the
|
||||||
|
// Windows polling Source emits only on change. hwnd is held as uintptr so this
|
||||||
|
// file stays platform-neutral (it must build and test on Linux).
|
||||||
|
type foregroundTracker struct {
|
||||||
|
primed bool
|
||||||
|
available bool
|
||||||
|
hwnd uintptr
|
||||||
|
title string
|
||||||
|
}
|
||||||
|
|
||||||
|
// changed reports whether (available, hwnd, title) differs from the last
|
||||||
|
// observation and records the new values. The first call always returns true.
|
||||||
|
func (t *foregroundTracker) changed(available bool, hwnd uintptr, title string) bool {
|
||||||
|
if t.primed && available == t.available && hwnd == t.hwnd && title == t.title {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
t.primed, t.available, t.hwnd, t.title = true, available, hwnd, title
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package evidence
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestForegroundTrackerChanged(t *testing.T) {
|
||||||
|
var tr foregroundTracker
|
||||||
|
|
||||||
|
if !tr.changed(true, 100, "A") {
|
||||||
|
t.Fatal("first observation should always report changed")
|
||||||
|
}
|
||||||
|
if tr.changed(true, 100, "A") {
|
||||||
|
t.Error("identical observation should not report changed")
|
||||||
|
}
|
||||||
|
if !tr.changed(true, 100, "B") {
|
||||||
|
t.Error("title change on same hwnd should report changed")
|
||||||
|
}
|
||||||
|
if !tr.changed(true, 200, "B") {
|
||||||
|
t.Error("hwnd change should report changed")
|
||||||
|
}
|
||||||
|
if !tr.changed(false, 0, "") {
|
||||||
|
t.Error("transition to unavailable should report changed")
|
||||||
|
}
|
||||||
|
if tr.changed(false, 0, "") {
|
||||||
|
t.Error("repeated unavailable should not report changed")
|
||||||
|
}
|
||||||
|
if !tr.changed(true, 200, "B") {
|
||||||
|
t.Error("transition back to available should report changed")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user