Files
openchamber-android-native/third_party/mygo/internal/e2e/icon_test.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

97 lines
2.9 KiB
Go

//go:build !js
package e2e
import (
"bytes"
"fmt"
"image/png"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/egoist/mygo"
)
// TestFileIcon draws the icons of an application, a folder and a file at
// the size asked for, and fails for a path that does not exist.
func TestFileIcon(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "notes.txt")
os.WriteFile(file, []byte("hello"), 0o644)
paths := []string{dir, file}
switch runtime.GOOS {
case "darwin":
paths = append(paths, "/System/Applications/Calculator.app")
case "windows":
paths = append(paths, filepath.Join(os.Getenv("SystemRoot"), "System32", "notepad.exe"))
case "linux":
// A desktop entry's icon is its application's, from the theme.
entry := filepath.Join(dir, "editor.desktop")
os.WriteFile(entry, []byte("[Desktop Entry]\nType=Application\nName=Editor\nExec=true\nIcon=folder\n"), 0o644)
// One whose icon the theme lacks, as Byobu's on Ubuntu, gets the
// icon of desktop entries.
missing := filepath.Join(dir, "unknown.desktop")
os.WriteFile(missing, []byte("[Desktop Entry]\nType=Application\nName=Unknown\nExec=true\nIcon=mygo-no-such-icon\n"), 0o644)
paths = append(paths, entry, missing)
}
for _, p := range paths {
for _, size := range []int{16, 64} {
data, err := mygo.Shell.FileIcon(p, size)
checkIcon(t, fmt.Sprintf("FileIcon(%q, %d)", p, size), data, err, size)
}
}
if runtime.GOOS == "linux" {
// The entry above names the icon of folders.
entry, _ := mygo.Shell.FileIcon(filepath.Join(dir, "editor.desktop"), 32)
folder, _ := mygo.Shell.FileIcon(dir, 32)
if !bytes.Equal(entry, folder) {
t.Error("a desktop entry's icon is not the one it names")
}
}
if _, err := mygo.Shell.FileIcon(filepath.Join(dir, "missing"), 32); err == nil {
t.Error("FileIcon of a missing file did not fail")
}
}
// TestTypeIcon draws the icons of file types no file has, and of a type
// no application knows, at the size asked for.
func TestTypeIcon(t *testing.T) {
for _, ext := range []string{"pdf", ".txt", "zip", "mygo-unknown", ""} {
for _, size := range []int{16, 64} {
data, err := mygo.Shell.TypeIcon(ext, size)
checkIcon(t, fmt.Sprintf("TypeIcon(%q, %d)", ext, size), data, err, size)
}
}
}
// checkIcon checks that an icon is a PNG of size by size pixels that
// draws at least an eighth of them.
func checkIcon(t *testing.T, call string, data []byte, err error, size int) {
t.Helper()
if err != nil {
t.Errorf("%s: %v", call, err)
return
}
img, err := png.Decode(bytes.NewReader(data))
if err != nil {
t.Errorf("%s: not a PNG: %v", call, err)
return
}
if b := img.Bounds(); b.Dx() != size || b.Dy() != size {
t.Errorf("%s is %dx%d", call, b.Dx(), b.Dy())
}
opaque := 0
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
if _, _, _, a := img.At(x, y).RGBA(); a > 0 {
opaque++
}
}
}
if opaque < size*size/8 {
t.Errorf("%s draws %d of %d pixels", call, opaque, size*size)
}
}