| 1 | package appidentity |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // A single literal, so an adjacent string cannot be mistaken for the ID. |
| 12 | var jsAppUserModelIDRe = regexp.MustCompile(`APP_USER_MODEL_ID\s*=\s*"([^"]*)"`) |
| 13 | |
| 14 | // The Electron shell is the process that owns the taskbar window, so the |
| 15 | // launcher's explicit ID reaches Windows only through the JS port below. |
| 16 | func TestElectronShellAdoptsAppUserModelID(t *testing.T) { |
| 17 | identity, main := electronShellIdentitySources(t) |
| 18 | |
| 19 | declaration, err := os.ReadFile(identity) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | match := jsAppUserModelIDRe.FindSubmatch(declaration) |
| 24 | if match == nil { |
| 25 | t.Fatalf("no APP_USER_MODEL_ID literal in %s", identity) |
| 26 | } |
| 27 | if got := string(match[1]); got != AppUserModelID { |
| 28 | t.Errorf("Electron APP_USER_MODEL_ID = %q, want %q (internal/appidentity/identity.go)", got, AppUserModelID) |
| 29 | } |
| 30 | |
| 31 | entry, err := os.ReadFile(main) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | // The call has to run at module load: a window created earlier captures the |
| 36 | // implicit executable-path identity and the pinned icon splits off again. |
| 37 | call := strings.Index(string(entry), "applyAppUserModelId(app, process.platform)") |
| 38 | if call < 0 { |
| 39 | t.Fatalf("%s does not apply the Windows app identity", main) |
| 40 | } |
| 41 | for _, later := range []string{"app.whenReady()", "bootstrap(home)"} { |
| 42 | if at := strings.Index(string(entry), later); at >= 0 && call > at { |
| 43 | t.Errorf("%s applies the app identity after %q, too late for the window's taskbar group", main, later) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func electronShellIdentitySources(t *testing.T) (identity, main string) { |
| 49 | t.Helper() |
| 50 | packageDir, err := os.Getwd() |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | // internal/appidentity -> repo root |
| 55 | dir := filepath.Join(packageDir, "..", "..", "desktop", "electron", "src", "main") |
| 56 | return filepath.Join(dir, "appIdentity.ts"), filepath.Join(dir, "index.ts") |
| 57 | } |
| 58 |