desktopTrayBadge.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 representations: Array<{
  9. scaleFactor: number;
  10. width: number;
  11. height: number;
  12. buffer: Buffer;
  13. }> = [];
  14. let baseImageCounter = 0;
  15. const createBaseBuffer = (size: number) => {
  16. // 不透明蓝底(预乘 BGRA),模拟托盘底图的 1x / 2x 表示
  17. const buffer = Buffer.alloc(size * size * 4);
  18. for (let offset = 0; offset < buffer.length; offset += 4) {
  19. buffer[offset] = 200;
  20. buffer[offset + 1] = 40;
  21. buffer[offset + 2] = 10;
  22. buffer[offset + 3] = 255;
  23. }
  24. return buffer;
  25. };
  26. const baseBuffer16 = createBaseBuffer(16);
  27. const baseBuffer32 = createBaseBuffer(32);
  28. const fakeBaseImage = () => ({
  29. __baseImage: ++baseImageCounter,
  30. isEmpty: () => false,
  31. resize: ({ width }: { width: number }) => ({
  32. toBitmap: () => (width === 16 ? baseBuffer16 : baseBuffer32),
  33. }),
  34. });
  35. return {
  36. trayInstances,
  37. representations,
  38. baseBuffer16,
  39. baseBuffer32,
  40. fakeBaseImage,
  41. resetBaseImageCounter: () => {
  42. baseImageCounter = 0;
  43. },
  44. };
  45. });
  46. vi.mock("electron", () => ({
  47. app: { isPackaged: false },
  48. Menu: { buildFromTemplate: (template: unknown) => ({ template }) },
  49. Tray: class {
  50. images: unknown[] = [];
  51. tooltips: string[] = [];
  52. constructor(imagePath: string) {
  53. state.trayInstances.push({ imageAtCreate: imagePath, images: this.images, tooltips: this.tooltips });
  54. }
  55. setImage(image: unknown) {
  56. this.images.push(image);
  57. }
  58. setToolTip(tooltip: string) {
  59. this.tooltips.push(tooltip);
  60. }
  61. setContextMenu() {}
  62. on() {}
  63. },
  64. nativeImage: {
  65. createFromPath: () => state.fakeBaseImage(),
  66. createEmpty: () => ({
  67. __composed: true,
  68. addRepresentation: (representation: {
  69. scaleFactor: number;
  70. width: number;
  71. height: number;
  72. buffer: Buffer;
  73. }) => state.representations.push(representation),
  74. }),
  75. },
  76. }));
  77. describe("desktop tray unread badge", () => {
  78. beforeEach(() => {
  79. vi.resetModules();
  80. state.trayInstances.length = 0;
  81. state.representations.length = 0;
  82. state.resetBaseImageCounter();
  83. });
  84. async function loadModule() {
  85. return import("../src/main/desktopTray.js");
  86. }
  87. const trayOptions = {
  88. getLocale: () => "zh-CN" as const,
  89. showCurrentWindow: () => {},
  90. executeDesktopCommand: async () => {},
  91. quitApp: () => {},
  92. logger: { warn: vi.fn() },
  93. };
  94. function pixelOf(buffer: Buffer, size: number, x: number, y: number): number[] {
  95. const offset = (y * size + x) * 4;
  96. return [buffer[offset]!, buffer[offset + 1]!, buffer[offset + 2]!, buffer[offset + 3]!];
  97. }
  98. it("没有托盘时 no-op 不抛错", async () => {
  99. const { applyDesktopTrayUnreadBadge } = await loadModule();
  100. expect(() => applyDesktopTrayUnreadBadge(3)).not.toThrow();
  101. expect(state.trayInstances).toHaveLength(0);
  102. });
  103. it("计数非零时在 Logo 圆角透明缺口合成红点,按 label 缓存", async () => {
  104. const mod = await loadModule();
  105. mod.createDesktopTray({ ...trayOptions, platform: "win32" });
  106. expect(state.trayInstances).toHaveLength(1);
  107. mod.applyDesktopTrayUnreadBadge(3);
  108. mod.applyDesktopTrayUnreadBadge(3);
  109. // 同一 label 只合成一次(缓存的 NativeImage 含 1x/2x 两份表示)
  110. expect(state.representations).toHaveLength(2);
  111. expect(state.representations.map(({ scaleFactor, width, height }) => ({
  112. scaleFactor,
  113. width,
  114. height,
  115. }))).toEqual([
  116. { scaleFactor: 1, width: 16, height: 16 },
  117. { scaleFactor: 2, width: 32, height: 32 },
  118. ]);
  119. // 创建时 + 两次换图
  120. const images = state.trayInstances[0]!.images;
  121. expect(images).toHaveLength(2);
  122. const oneX = state.representations[0]!.buffer;
  123. const twoX = state.representations[1]!.buffer;
  124. // 1x / 2x 都有纯红芯,且抗锯齿边缘与底图混合(不是硬边纯红或原底图)
  125. expect(pixelOf(oneX, 16, 13, 2)).toEqual([53, 57, 229, 255]);
  126. expect(pixelOf(twoX, 32, 27, 4)).toEqual([53, 57, 229, 255]);
  127. expect(pixelOf(twoX, 32, 23, 4)).not.toEqual([200, 40, 10, 255]);
  128. expect(pixelOf(twoX, 32, 23, 4)).not.toEqual([53, 57, 229, 255]);
  129. // Logo 本体与远离红点的底图保持不变
  130. expect(pixelOf(oneX, 16, 8, 8)).toEqual([200, 40, 10, 255]);
  131. expect(pixelOf(twoX, 32, 16, 16)).toEqual([200, 40, 10, 255]);
  132. // 整图不应出现白色笔画像素
  133. for (const buffer of [oneX, twoX]) {
  134. let hasWhitePixel = false;
  135. for (let offset = 0; offset + 3 < buffer.length; offset += 4) {
  136. if (
  137. buffer[offset] === 255 &&
  138. buffer[offset + 1] === 255 &&
  139. buffer[offset + 2] === 255 &&
  140. buffer[offset + 3] === 255
  141. ) {
  142. hasWhitePixel = true;
  143. break;
  144. }
  145. }
  146. expect(hasWhitePixel).toBe(false);
  147. }
  148. });
  149. it("计数归零时恢复原始托盘底图", async () => {
  150. const mod = await loadModule();
  151. mod.createDesktopTray({ ...trayOptions, platform: "win32" });
  152. mod.applyDesktopTrayUnreadBadge(3);
  153. mod.applyDesktopTrayUnreadBadge(0);
  154. const images = state.trayInstances[0]!.images;
  155. expect(images).toHaveLength(2);
  156. // 恢复的图就是 createFromPath 返回的底图对象本身
  157. expect(images[1]).toMatchObject({ __baseImage: 1 });
  158. });
  159. it("linux 平台托盘同样生效", async () => {
  160. const mod = await loadModule();
  161. mod.createDesktopTray({ ...trayOptions, platform: "linux" });
  162. mod.applyDesktopTrayUnreadBadge(1);
  163. expect(state.representations).toHaveLength(2);
  164. });
  165. });