desktopTrayBadge.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. const state = vi.hoisted(() => {
  3. const trayInstances: Array<{
  4. imageAtCreate: string;
  5. images: unknown[];
  6. tooltips: string[];
  7. }> = [];
  8. const createFromBitmapCalls: Array<{ buffer: Buffer; width: number; height: number }> = [];
  9. let baseImageCounter = 0;
  10. // 32×32 不透明蓝底(预乘 BGRA),模拟托盘底图
  11. const baseBuffer = Buffer.alloc(32 * 32 * 4);
  12. for (let offset = 0; offset < baseBuffer.length; offset += 4) {
  13. baseBuffer[offset] = 200;
  14. baseBuffer[offset + 1] = 40;
  15. baseBuffer[offset + 2] = 10;
  16. baseBuffer[offset + 3] = 255;
  17. }
  18. const fakeBaseImage = () => ({
  19. __baseImage: ++baseImageCounter,
  20. isEmpty: () => false,
  21. resize: () => ({ toBitmap: () => baseBuffer }),
  22. });
  23. return {
  24. trayInstances,
  25. createFromBitmapCalls,
  26. baseBuffer,
  27. fakeBaseImage,
  28. resetBaseImageCounter: () => {
  29. baseImageCounter = 0;
  30. },
  31. };
  32. });
  33. vi.mock("electron", () => ({
  34. app: { isPackaged: false },
  35. Menu: { buildFromTemplate: (template: unknown) => ({ template }) },
  36. Tray: class {
  37. images: unknown[] = [];
  38. tooltips: string[] = [];
  39. constructor(imagePath: string) {
  40. state.trayInstances.push({ imageAtCreate: imagePath, images: this.images, tooltips: this.tooltips });
  41. }
  42. setImage(image: unknown) {
  43. this.images.push(image);
  44. }
  45. setToolTip(tooltip: string) {
  46. this.tooltips.push(tooltip);
  47. }
  48. setContextMenu() {}
  49. on() {}
  50. },
  51. nativeImage: {
  52. createFromPath: () => state.fakeBaseImage(),
  53. createFromBitmap: (buffer: Buffer, options: { width: number; height: number }) => {
  54. state.createFromBitmapCalls.push({ buffer, width: options.width, height: options.height });
  55. return { __composed: state.createFromBitmapCalls.length, buffer };
  56. },
  57. },
  58. }));
  59. describe("desktop tray unread badge", () => {
  60. beforeEach(() => {
  61. vi.resetModules();
  62. state.trayInstances.length = 0;
  63. state.createFromBitmapCalls.length = 0;
  64. state.resetBaseImageCounter();
  65. });
  66. async function loadModule() {
  67. return import("../src/main/desktopTray.js");
  68. }
  69. const trayOptions = {
  70. getLocale: () => "zh-CN" as const,
  71. showCurrentWindow: () => {},
  72. executeDesktopCommand: async () => {},
  73. quitApp: () => {},
  74. logger: { warn: vi.fn() },
  75. };
  76. function pixelOf(buffer: Buffer, x: number, y: number): number[] {
  77. const offset = (y * 32 + x) * 4;
  78. return [buffer[offset]!, buffer[offset + 1]!, buffer[offset + 2]!, buffer[offset + 3]!];
  79. }
  80. it("没有托盘时 no-op 不抛错", async () => {
  81. const { applyDesktopTrayUnreadBadge } = await loadModule();
  82. expect(() => applyDesktopTrayUnreadBadge(3)).not.toThrow();
  83. expect(state.trayInstances).toHaveLength(0);
  84. });
  85. it("计数非零时在 Logo 圆角透明缺口合成红点,按 label 缓存", async () => {
  86. const mod = await loadModule();
  87. mod.createDesktopTray({ ...trayOptions, platform: "win32" });
  88. expect(state.trayInstances).toHaveLength(1);
  89. mod.applyDesktopTrayUnreadBadge(3);
  90. mod.applyDesktopTrayUnreadBadge(3);
  91. // 同一 label 只合成一次
  92. expect(state.createFromBitmapCalls).toHaveLength(1);
  93. // 创建时 + 两次换图
  94. const images = state.trayInstances[0]!.images;
  95. expect(images).toHaveLength(2);
  96. const { buffer } = state.createFromBitmapCalls[0]!;
  97. expect(state.createFromBitmapCalls[0]!).toMatchObject({ width: 32, height: 32 });
  98. expect(buffer.length).toBe(32 * 32 * 4);
  99. // 画布 32×32、Logo 满幅;红点 9×9(画布 10、radius 4)圆心 (28.5, 3.5),
  100. // 嵌进 Logo 圆角的透明缺口,与图片上边缘、右边缘正切。
  101. expect(pixelOf(buffer, 26, 4)).toEqual([53, 57, 229, 255]);
  102. expect(pixelOf(buffer, 25, 3)).toEqual([53, 57, 229, 255]);
  103. // 正切:红点像素触达图片第 0 行(上边缘)与第 31 列(右边缘)
  104. expect(pixelOf(buffer, 28, 0)).toEqual([53, 57, 229, 255]);
  105. expect(pixelOf(buffer, 31, 3)).toEqual([53, 57, 229, 255]);
  106. // Logo 本体与远离红点的底图保持蓝色
  107. expect(pixelOf(buffer, 16, 16)).toEqual([200, 40, 10, 255]);
  108. expect(pixelOf(buffer, 22, 0)).toEqual([200, 40, 10, 255]);
  109. // 整图不应出现白色笔画像素
  110. let hasWhitePixel = false;
  111. for (let offset = 0; offset + 3 < buffer.length; offset += 4) {
  112. if (
  113. buffer[offset] === 255 &&
  114. buffer[offset + 1] === 255 &&
  115. buffer[offset + 2] === 255 &&
  116. buffer[offset + 3] === 255
  117. ) {
  118. hasWhitePixel = true;
  119. break;
  120. }
  121. }
  122. expect(hasWhitePixel).toBe(false);
  123. });
  124. it("计数归零时恢复原始托盘底图", async () => {
  125. const mod = await loadModule();
  126. mod.createDesktopTray({ ...trayOptions, platform: "win32" });
  127. mod.applyDesktopTrayUnreadBadge(3);
  128. mod.applyDesktopTrayUnreadBadge(0);
  129. const images = state.trayInstances[0]!.images;
  130. expect(images).toHaveLength(2);
  131. // 恢复的图就是 createFromPath 返回的底图对象本身
  132. expect(images[1]).toMatchObject({ __baseImage: 1 });
  133. });
  134. it("linux 平台托盘同样生效", async () => {
  135. const mod = await loadModule();
  136. mod.createDesktopTray({ ...trayOptions, platform: "linux" });
  137. mod.applyDesktopTrayUnreadBadge(1);
  138. expect(state.createFromBitmapCalls).toHaveLength(1);
  139. });
  140. });