Presets were flagged HiDPI by screen width alone, so a 2880x1800 preset read "HiDPI" on a 100% screen where macOS runs it at 1x, and a VM left at the 1920x1080 default ran 1x on a 200% screen. - GuestStartsHiDPI follows upstream reims-vgpu 3731afd (RgfxDisplayIdentity.hpp, reims-vgpu-window features.md): HiDPI from a 2048x1536 window above 160 ppi (150% scaling or more), or from 3840x2160 at any scale. - Presets are the 16:9 or 16:10 sizes that fit the screen's work area below a title bar, largest four; the largest HiDPI one is recommended. The display probe now reports the work area. - When no preset is HiDPI, the resolution row says how to get there: borderless full screen, 150% Windows scaling, or not on this screen. On this host (3072x1920 at 200%) the presets are 2560x1600 (HiDPI, recommended), 1920x1200, 1680x1050 and 1280x800. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
5.9 KiB
Go
192 lines
5.9 KiB
Go
package host
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestGuestStartsHiDPI(t *testing.T) {
|
|
cases := []struct {
|
|
w, h, scale int
|
|
want bool
|
|
}{
|
|
{1920, 1080, 200, false}, // too small at any scale: the case of a 1x desktop on a 200% screen
|
|
{2048, 1536, 150, true}, // smallest HiDPI window above 160 ppi
|
|
{2047, 1536, 200, false},
|
|
{2560, 1440, 200, false}, // 1440 high is under 1536
|
|
{2560, 1600, 150, true},
|
|
{2880, 1800, 145, false}, // 145% is exactly 160 ppi, not above
|
|
{2880, 1800, 125, false},
|
|
{3200, 1800, 100, false},
|
|
{3840, 2160, 100, true}, // any density from 3840 x 2160
|
|
{3840, 2152, 100, false},
|
|
{2880, 1800, 0, false}, // unknown scale counts as 1x
|
|
}
|
|
for _, c := range cases {
|
|
if got := GuestStartsHiDPI(c.w, c.h, c.scale); got != c.want {
|
|
t.Errorf("GuestStartsHiDPI(%d, %d, %d%%) = %v, want %v", c.w, c.h, c.scale, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRecommendDisplayPresets(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
primary *DisplayInfo
|
|
wantRec string
|
|
wantHi bool
|
|
wantSizes []string
|
|
advice string
|
|
}{
|
|
{
|
|
name: "3072x1920 at 200%, as a Retina remote display",
|
|
primary: &DisplayInfo{Width: 3072, Height: 1920, ScalePct: 200, WorkWidth: 3072, WorkHeight: 1824},
|
|
wantRec: "2560x1600",
|
|
wantHi: true,
|
|
wantSizes: []string{"2560x1600", "1920x1200", "1680x1050", "1280x800"},
|
|
},
|
|
{
|
|
name: "4K at 150%",
|
|
primary: &DisplayInfo{Width: 3840, Height: 2160, ScalePct: 150, WorkWidth: 3840, WorkHeight: 2088},
|
|
wantRec: "3200x1800",
|
|
wantHi: true,
|
|
wantSizes: []string{"3200x1800", "2880x1620", "2560x1440", "1920x1080"},
|
|
},
|
|
{
|
|
// No window fits 3840 x 2160, the one size HiDPI at 100%; full screen is.
|
|
name: "4K at 100%",
|
|
primary: &DisplayInfo{Width: 3840, Height: 2160, ScalePct: 100, WorkWidth: 3840, WorkHeight: 2112},
|
|
wantRec: "3200x1800",
|
|
wantHi: false,
|
|
wantSizes: []string{"3200x1800", "2880x1620", "2560x1440", "1920x1080"},
|
|
advice: HiDPIAdviceFullscreen,
|
|
},
|
|
{
|
|
name: "2.5K 16:10 laptop at 150%",
|
|
primary: &DisplayInfo{Width: 2560, Height: 1600, ScalePct: 150, WorkWidth: 2560, WorkHeight: 1528},
|
|
wantRec: "1920x1200",
|
|
wantHi: false,
|
|
wantSizes: []string{"1920x1200", "1680x1050", "1280x800"},
|
|
// The whole 2560 x 1600 screen is HiDPI at 150%.
|
|
advice: HiDPIAdviceFullscreen,
|
|
},
|
|
{
|
|
name: "1080p at 100%, work area unknown",
|
|
primary: &DisplayInfo{Width: 1920, Height: 1080, ScalePct: 100},
|
|
wantRec: "1600x900",
|
|
wantHi: false,
|
|
wantSizes: []string{"1600x900", "1280x720"},
|
|
advice: HiDPIAdviceUnavailable,
|
|
},
|
|
{
|
|
name: "2880x1800 laptop at 100%",
|
|
primary: &DisplayInfo{Width: 2880, Height: 1800, ScalePct: 100, WorkWidth: 2880, WorkHeight: 1752},
|
|
wantRec: "2560x1600",
|
|
wantHi: false,
|
|
wantSizes: []string{"2560x1600", "1920x1200", "1680x1050", "1280x800"},
|
|
advice: HiDPIAdviceScale,
|
|
},
|
|
{
|
|
name: "nil fallback",
|
|
primary: nil,
|
|
wantRec: "1920x1080",
|
|
wantHi: false,
|
|
wantSizes: []string{"1920x1080", "2560x1440", "2880x1800"},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
presets, rec := RecommendDisplayPresets(tc.primary)
|
|
if rec != tc.wantRec {
|
|
t.Fatalf("rec = %q, want %q", rec, tc.wantRec)
|
|
}
|
|
var sizes []string
|
|
recs := 0
|
|
for _, p := range presets {
|
|
sizes = append(sizes, p.Resolution)
|
|
var w, h int
|
|
_, _ = fmt.Sscanf(p.Resolution, "%dx%d", &w, &h)
|
|
scale := 0
|
|
if tc.primary != nil {
|
|
scale = tc.primary.ScalePct
|
|
}
|
|
if p.HiDPI != (tc.primary != nil && GuestStartsHiDPI(w, h, scale)) {
|
|
t.Errorf("%s HiDPI = %v disagrees with GuestStartsHiDPI", p.Resolution, p.HiDPI)
|
|
}
|
|
if p.Recommended {
|
|
recs++
|
|
if p.Resolution != tc.wantRec || p.HiDPI != tc.wantHi {
|
|
t.Errorf("recommended %s HiDPI=%v, want %s HiDPI=%v", p.Resolution, p.HiDPI, tc.wantRec, tc.wantHi)
|
|
}
|
|
}
|
|
}
|
|
if recs != 1 {
|
|
t.Errorf("%d presets recommended, want 1", recs)
|
|
}
|
|
if fmt.Sprint(sizes) != fmt.Sprint(tc.wantSizes) {
|
|
t.Errorf("sizes = %v, want %v", sizes, tc.wantSizes)
|
|
}
|
|
if got := HiDPIAdvice(tc.primary, presets); got != tc.advice {
|
|
t.Errorf("HiDPIAdvice = %q, want %q", got, tc.advice)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInspectDiskUsageQCOW2(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
qcow := filepath.Join(tmp, "disk.qcow2")
|
|
hdr := make([]byte, 1024)
|
|
copy(hdr[0:4], []byte{'Q', 'F', 'I', 0xfb})
|
|
binary.BigEndian.PutUint32(hdr[4:8], 3)
|
|
const wantVirtual uint64 = 128 << 30
|
|
binary.BigEndian.PutUint64(hdr[24:32], wantVirtual)
|
|
if err := os.WriteFile(qcow, hdr, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
usage, err := InspectDiskUsage(qcow)
|
|
if err != nil {
|
|
t.Fatalf("InspectDiskUsage: %v", err)
|
|
}
|
|
if usage.Format != "qcow2" {
|
|
t.Fatalf("Format = %q, want qcow2", usage.Format)
|
|
}
|
|
if usage.VirtualBytes != wantVirtual {
|
|
t.Fatalf("VirtualBytes = %d, want %d", usage.VirtualBytes, wantVirtual)
|
|
}
|
|
if usage.ActualBytes == 0 {
|
|
t.Fatalf("ActualBytes should be non-zero")
|
|
}
|
|
}
|
|
|
|
func TestComputeCPUPercentAndSelfSample(t *testing.T) {
|
|
t0 := time.Now()
|
|
s1 := ProcessSample{PID: 42, CPUDuration: 200 * time.Millisecond, SampledAt: t0}
|
|
s2 := ProcessSample{PID: 42, CPUDuration: 600 * time.Millisecond, SampledAt: t0.Add(time.Second)}
|
|
pct := ComputeCPUPercent(s1, s2, 8)
|
|
if pct < 4.9 || pct > 5.1 {
|
|
t.Fatalf("ComputeCPUPercent = %.2f, want ~5.0", pct)
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
self, err := SampleProcessStats(os.Getpid())
|
|
if err != nil {
|
|
t.Fatalf("SampleProcessStats(self): %v", err)
|
|
}
|
|
if self.WorkingSet == 0 {
|
|
t.Fatalf("expected non-zero WorkingSet for current process")
|
|
}
|
|
// Ensure Inspect populates displays cleanly without panicking.
|
|
rep := Inspect(t.TempDir())
|
|
if rep.RecommendedResolution == "" || len(rep.DisplayPresets) == 0 {
|
|
t.Fatalf("Inspect should always populate display presets & recommended resolution")
|
|
}
|
|
}
|
|
}
|