utils.spec.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. import Fuse from 'fuse.js';
  2. import {mat3, vec2} from 'gl-matrix';
  3. import {
  4. computeConfigViewWithStategy,
  5. computeHighlightedBounds,
  6. createProgram,
  7. createShader,
  8. ELLIPSIS,
  9. findRangeBinarySearch,
  10. getContext,
  11. makeProjectionMatrix,
  12. Rect,
  13. Transform,
  14. trimTextCenter,
  15. } from 'sentry/utils/profiling/gl/utils';
  16. describe('makeProjectionMatrix', () => {
  17. it('should return a projection matrix', () => {
  18. // prettier-ignore
  19. expect(makeProjectionMatrix(1024, 768)).toEqual(mat3.fromValues(
  20. 2/1024, 0, 0,
  21. -0, -2/768, -0,
  22. -1,1,1
  23. ));
  24. });
  25. });
  26. describe('getContext', () => {
  27. it('throws if it cannot retrieve context', () => {
  28. expect(() =>
  29. // @ts-ignore partial canvas mock
  30. getContext({getContext: jest.fn().mockImplementationOnce(() => null)}, 'webgl')
  31. ).toThrow();
  32. expect(() =>
  33. // @ts-ignore partial canvas mock
  34. getContext({getContext: jest.fn().mockImplementationOnce(() => null)}, '2d')
  35. ).toThrow();
  36. });
  37. it('returns ctx', () => {
  38. const ctx = {};
  39. expect(
  40. // @ts-ignore partial canvas mock
  41. getContext({getContext: jest.fn().mockImplementationOnce(() => ctx)}, 'webgl')
  42. ).toBe(ctx);
  43. });
  44. });
  45. describe('createProgram', () => {
  46. it('throws if it fails to create a program', () => {
  47. const ctx: Partial<WebGLRenderingContext> = {
  48. createProgram: jest.fn().mockImplementation(() => {
  49. return null;
  50. }),
  51. };
  52. // @ts-ignore this is a partial mock
  53. expect(() => createProgram(ctx, {}, {})).toThrow('Could not create program');
  54. });
  55. it('attaches both shaders and links program', () => {
  56. const program = {};
  57. const ctx: Partial<WebGLRenderingContext> = {
  58. createProgram: jest.fn().mockImplementation(() => {
  59. return program;
  60. }),
  61. getProgramParameter: jest.fn().mockImplementation(() => program),
  62. linkProgram: jest.fn(),
  63. attachShader: jest.fn(),
  64. };
  65. const vertexShader = {};
  66. const fragmentShader = {};
  67. // @ts-ignore this is a partial mock
  68. createProgram(ctx, vertexShader, fragmentShader);
  69. expect(ctx.createProgram).toHaveBeenCalled();
  70. expect(ctx.linkProgram).toHaveBeenCalled();
  71. expect(ctx.attachShader).toHaveBeenCalledWith(program, vertexShader);
  72. expect(ctx.attachShader).toHaveBeenCalledWith(program, fragmentShader);
  73. });
  74. it('deletes the program if compiling fails', () => {
  75. const program = {};
  76. const ctx: Partial<WebGLRenderingContext> = {
  77. createProgram: jest.fn().mockImplementation(() => {
  78. return program;
  79. }),
  80. deleteProgram: jest.fn(),
  81. getProgramParameter: jest.fn().mockImplementation(() => 0),
  82. linkProgram: jest.fn(),
  83. attachShader: jest.fn(),
  84. };
  85. const vertexShader = {};
  86. const fragmentShader = {};
  87. // @ts-ignore this is a partial mock
  88. expect(() => createProgram(ctx, vertexShader, fragmentShader)).toThrow();
  89. expect(ctx.createProgram).toHaveBeenCalled();
  90. expect(ctx.linkProgram).toHaveBeenCalled();
  91. expect(ctx.attachShader).toHaveBeenCalledWith(program, vertexShader);
  92. expect(ctx.attachShader).toHaveBeenCalledWith(program, fragmentShader);
  93. expect(ctx.deleteProgram).toHaveBeenCalledWith(program);
  94. });
  95. });
  96. describe('createShader', () => {
  97. it('fails to create', () => {
  98. const ctx: Partial<WebGLRenderingContext> = {
  99. createShader: jest.fn().mockImplementationOnce(() => null),
  100. };
  101. const type = 0;
  102. // @ts-ignore this is a partial mock
  103. expect(() => createShader(ctx, type, '')).toThrow();
  104. expect(ctx.createShader).toHaveBeenLastCalledWith(type);
  105. });
  106. it('successfully compiles', () => {
  107. const shader: WebGLShader = {};
  108. const type = 0;
  109. const shaderSource = `vec4(1.0, 0.0, 0.0, 1.0)`;
  110. const ctx: Partial<WebGLRenderingContext> = {
  111. createShader: jest.fn().mockImplementation(() => shader),
  112. shaderSource: jest.fn(),
  113. compileShader: jest.fn(),
  114. getShaderParameter: jest.fn().mockImplementation(() => 1),
  115. COMPILE_STATUS: 1,
  116. };
  117. // @ts-ignore this is a partial mock
  118. expect(() => createShader(ctx, type, shaderSource)).not.toThrow();
  119. // @ts-ignore this is a partial mock
  120. expect(createShader(ctx, type, shaderSource)).toBe(shader);
  121. expect(ctx.shaderSource).toHaveBeenLastCalledWith(shader, shaderSource);
  122. expect(ctx.getShaderParameter).toHaveBeenLastCalledWith(shader, ctx.COMPILE_STATUS);
  123. });
  124. it('deletes shader if compilation fails', () => {
  125. const shader: WebGLShader = {};
  126. const type = 0;
  127. const shaderSource = `vec4(1.0, 0.0, 0.0, 1.0)`;
  128. const ctx: Partial<WebGLRenderingContext> = {
  129. createShader: jest.fn().mockImplementation(() => shader),
  130. shaderSource: jest.fn(),
  131. compileShader: jest.fn(),
  132. getShaderParameter: jest.fn().mockImplementation(() => 0),
  133. deleteShader: jest.fn(),
  134. COMPILE_STATUS: 0,
  135. };
  136. // @ts-ignore this is a partial mock
  137. expect(() => createShader(ctx, type, shaderSource)).toThrow(
  138. 'Failed to compile shader'
  139. );
  140. });
  141. });
  142. describe('Transform', () => {
  143. it('betweenRect', () => {
  144. expect(Transform.betweenRect(new Rect(2, 3, 4, 5), new Rect(1, 2, 10, 15))).toEqual(
  145. new Rect(1, 2, 2.5, 3)
  146. );
  147. });
  148. });
  149. describe('Rect', () => {
  150. it('initializes an empty rect as 0 width and height rect at 0,0 origin', () => {
  151. expect(Rect.Empty()).toEqual(new Rect(0, 0, 0, 0));
  152. expect(Rect.Empty().isEmpty()).toBe(true);
  153. });
  154. it('clones rect', () => {
  155. const a = new Rect(1, 2, 3, 4);
  156. const b = Rect.From(a);
  157. expect(b.equals(a)).toBe(true);
  158. });
  159. it('getters return correct values', () => {
  160. const rect = new Rect(1, 2, 3, 4);
  161. expect(rect.x).toBe(1);
  162. expect(rect.y).toBe(2);
  163. expect(rect.width).toBe(3);
  164. expect(rect.height).toBe(4);
  165. expect(rect.left).toBe(rect.x);
  166. expect(rect.right).toBe(rect.left + rect.width);
  167. expect(rect.top).toBe(rect.y);
  168. expect(rect.bottom).toBe(rect.y + rect.height);
  169. });
  170. describe('collision', () => {
  171. it('containsX', () => {
  172. expect(new Rect(0, 0, 1, 1).containsX(vec2.fromValues(0.5, 0))).toBe(true);
  173. // when we are exactly on the edge
  174. expect(new Rect(0, 0, 1, 1).containsX(vec2.fromValues(0, 0))).toBe(true);
  175. expect(new Rect(0, 0, 1, 1).containsX(vec2.fromValues(1, 0))).toBe(true);
  176. // when we are outside the rect
  177. expect(new Rect(0, 0, 1, 1).containsX(vec2.fromValues(-0.5, 0))).toBe(false);
  178. expect(new Rect(0, 0, 1, 1).containsX(vec2.fromValues(1.5, 0))).toBe(false);
  179. });
  180. it('containsY', () => {
  181. expect(new Rect(0, 0, 1, 1).containsY(vec2.fromValues(0, 0.5))).toBe(true);
  182. // when we are exactly on the edge
  183. expect(new Rect(0, 0, 1, 1).containsY(vec2.fromValues(0, 0))).toBe(true);
  184. expect(new Rect(0, 0, 1, 1).containsY(vec2.fromValues(0, 1))).toBe(true);
  185. // when we are outside the rect
  186. expect(new Rect(0, 0, 1, 1).containsY(vec2.fromValues(0, -0.5))).toBe(false);
  187. expect(new Rect(0, 0, 1, 1).containsY(vec2.fromValues(0, 1.5))).toBe(false);
  188. });
  189. it('contains', () => {
  190. expect(new Rect(0, 0, 1, 1).contains(vec2.fromValues(0.5, 0.5))).toBe(true);
  191. expect(new Rect(0, 0, 1, 1).contains(vec2.fromValues(1.5, 1.5))).toBe(false);
  192. expect(new Rect(0, 0, 1, 1).contains(vec2.fromValues(-0.5, -0.5))).toBe(false);
  193. });
  194. it('containsRect', () => {
  195. expect(new Rect(0, 0, 1, 1).containsRect(new Rect(0.1, 0.1, 0.1, 0.1))).toBe(true);
  196. });
  197. it('overlapsLeft', () => {
  198. expect(new Rect(0, 0, 1, 1).leftOverlapsWith(new Rect(-0.5, 0, 1, 1))).toBe(true);
  199. expect(new Rect(0, 0, 1, 1).leftOverlapsWith(new Rect(1, 0, 1, 1))).toBe(false);
  200. });
  201. it('overlapsRight', () => {
  202. expect(new Rect(0, 0, 1, 1).rightOverlapsWith(new Rect(0.5, 0, 1, 1))).toBe(true);
  203. expect(new Rect(0, 0, 1, 1).rightOverlapsWith(new Rect(1.5, 0, 1, 1))).toBe(false);
  204. });
  205. it('overlaps', () => {
  206. expect(new Rect(0, 0, 1, 1).overlaps(new Rect(-1, -1, 2, 2))).toBe(true);
  207. // we are exactly on the edge
  208. expect(new Rect(0, 0, 1, 1).overlaps(new Rect(1, 1, 1, 1))).toBe(true);
  209. expect(new Rect(0, 0, 1, 1).overlaps(new Rect(2, 1, 1, 1))).toBe(false);
  210. expect(new Rect(0, 0, 1, 1).overlaps(new Rect(-1, -1, 1, 1))).toBe(true);
  211. });
  212. it('hasIntersectionWidth', () => {
  213. expect(new Rect(0, 0, 1, 1).hasIntersectionWith(new Rect(1, 1, 2, 2))).toBe(false);
  214. expect(new Rect(0, 0, 1, 1).hasIntersectionWith(new Rect(-1, -1, 2, 2))).toBe(true);
  215. });
  216. });
  217. it('withHeight', () => {
  218. expect(new Rect(0, 0, 1, 1).withHeight(2).height).toBe(2);
  219. });
  220. it('withWidth', () => {
  221. expect(new Rect(0, 0, 1, 1).withWidth(2).width).toBe(2);
  222. });
  223. it('toBounds', () => {
  224. expect(new Rect(1, 0, 2, 2).toBounds()).toEqual([1, 0, 3, 2]);
  225. });
  226. it('toArray', () => {
  227. expect(new Rect(0, 0, 1, 1).toArray()).toEqual([0, 0, 1, 1]);
  228. });
  229. it('between', () => {
  230. expect(new Rect(1, 1, 2, 4).between(new Rect(2, 2, 4, 10))).toEqual(
  231. new Rect(2, 2, 2, 2.5)
  232. );
  233. });
  234. it('toMatrix', () => {
  235. expect(new Rect(0.5, 1, 2, 3).toMatrix()).toEqual(
  236. mat3.fromValues(2, 0, 0, 0, 3, 0, 0.5, 1, 1)
  237. );
  238. });
  239. it('notEqualTo', () => {
  240. expect(new Rect(0, 0, 1, 1).notEqualTo(new Rect(0, 0, 1, 1))).toBe(false);
  241. expect(new Rect(0, 0, 1, 1).notEqualTo(new Rect(0, 0, 1, 2))).toBe(true);
  242. });
  243. describe('transforms', () => {
  244. it('transformRect', () => {
  245. // prettier-ignore
  246. // Scale (10,20),translate by (3, 4)
  247. const matrix = mat3.fromValues(
  248. 10,0,0,
  249. 0,20,0,
  250. 3,4,0,
  251. )
  252. expect(new Rect(1, 1, 1, 1).transformRect(matrix)).toEqual(
  253. new Rect(13, 24, 10, 20)
  254. );
  255. });
  256. it('translateX', () => {
  257. expect(new Rect(0, 0, 1, 1).translateX(1).x).toBe(1);
  258. });
  259. it('translateY', () => {
  260. expect(new Rect(0, 0, 1, 1).translateY(1).y).toBe(1);
  261. });
  262. it('translate', () => {
  263. expect(new Rect(0, 0, 1, 1).translate(1, 1).origin).toEqual(vec2.fromValues(1, 1));
  264. });
  265. it('scaleX', () => {
  266. expect(new Rect(0, 0, 1, 1).scaleX(2).size).toEqual(vec2.fromValues(2, 1));
  267. });
  268. it('scaleY', () => {
  269. expect(new Rect(0, 0, 1, 1).scaleY(2).size).toEqual(vec2.fromValues(1, 2));
  270. });
  271. it('scale', () => {
  272. expect(new Rect(0, 0, 1, 1).scale(2, 2).size).toEqual(vec2.fromValues(2, 2));
  273. });
  274. it('equals', () => {
  275. expect(new Rect(1, 0, 0, 0).equals(new Rect(0, 0, 0, 0))).toBe(false);
  276. expect(new Rect(0, 1, 0, 0).equals(new Rect(0, 0, 0, 0))).toBe(false);
  277. expect(new Rect(0, 0, 1, 0).equals(new Rect(0, 0, 0, 0))).toBe(false);
  278. expect(new Rect(0, 0, 0, 1).equals(new Rect(0, 0, 0, 0))).toBe(false);
  279. });
  280. it('scaledBy', () => {
  281. expect(new Rect(0, 0, 1, 1).scale(3, 4).equals(new Rect(0, 0, 3, 4))).toBe(true);
  282. });
  283. it('scaleOriginBy', () => {
  284. expect(new Rect(1, 1, 1, 1).scaleOriginBy(2, 2).origin).toEqual(
  285. vec2.fromValues(2, 2)
  286. );
  287. });
  288. });
  289. });
  290. describe('findRangeBinarySearch', () => {
  291. it('finds in single iteration', () => {
  292. const text = new Array(10)
  293. .fill(0)
  294. .map((_, i) => String.fromCharCode(i + 97))
  295. .join('');
  296. const fn = jest.fn().mockImplementation(n => {
  297. return text.substring(0, n).length;
  298. });
  299. const target = 2;
  300. const precision = 1;
  301. // First iteration will halve 1+3, next iteration will compare 2-1 <= 1 and return [1,2]
  302. const [low, high] = findRangeBinarySearch({low: 1, high: 3}, fn, target, precision);
  303. expect([low, high]).toEqual([1, 2]);
  304. expect(fn).toHaveBeenCalledTimes(1);
  305. expect(text.substring(0, low)).toBe('a');
  306. });
  307. it('finds closest range', () => {
  308. const text = new Array(10)
  309. .fill(0)
  310. .map((_, i) => String.fromCharCode(i + 97))
  311. .join('');
  312. const fn = jest.fn().mockImplementation(n => {
  313. return text.substring(0, n).length;
  314. });
  315. const target = 4;
  316. const precision = 1;
  317. const [low, high] = findRangeBinarySearch({low: 0, high: 10}, fn, target, precision);
  318. expect([low, high]).toEqual([3.75, 4.375]);
  319. expect(fn).toHaveBeenCalledTimes(4);
  320. expect(text.substring(0, low)).toBe('abc');
  321. });
  322. });
  323. describe('trimTextCenter', () => {
  324. it('trims nothing if low > length', () => {
  325. expect(trimTextCenter('abc', 4)).toMatchObject({
  326. end: 0,
  327. length: 0,
  328. start: 0,
  329. text: 'abc',
  330. });
  331. });
  332. it('trims center perfectly', () => {
  333. expect(trimTextCenter('abcdef', 5.5)).toMatchObject({
  334. end: 4,
  335. length: 2,
  336. start: 2,
  337. text: `ab${ELLIPSIS}ef`,
  338. });
  339. });
  340. it('favors prefix length', () => {
  341. expect(trimTextCenter('abcdef', 5)).toMatchObject({
  342. end: 5,
  343. length: 3,
  344. start: 2,
  345. text: `ab${ELLIPSIS}f`,
  346. });
  347. });
  348. });
  349. describe('computeHighlightedBounds', () => {
  350. const testTable = [
  351. {
  352. name: 'reduces bounds[1] if tail is truncated',
  353. text: 'CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long)',
  354. args: {
  355. bounds: [4, 11],
  356. trim: {
  357. // match tail truncated
  358. text: 'CA::Dis…long)',
  359. start: 7,
  360. end: 95,
  361. length: 88,
  362. },
  363. },
  364. expected: [4, 8], // Dis...
  365. },
  366. {
  367. name: 'shifts bounds if truncated before bounds',
  368. text: '-[UIScrollView _smoothScrollDisplayLink:]',
  369. args: {
  370. bounds: [28, 35],
  371. trim: {
  372. text: '-[UIScrollView…playLink:]',
  373. start: 14,
  374. end: 31,
  375. length: 17,
  376. },
  377. },
  378. expected: [14, 19], // ...play
  379. },
  380. {
  381. name: 'shifts bounds if truncated before bounds',
  382. text: '-[UIScrollView _smoothScrollDisplayLink:]',
  383. args: {
  384. bounds: [28, 35],
  385. trim: {
  386. // match bounds are shifted after truncate
  387. text: '-[UIScrollView _sm…rollDisplayLink:]',
  388. start: 18,
  389. end: 24,
  390. length: 6,
  391. },
  392. },
  393. expected: [23, 30], // Display
  394. },
  395. {
  396. name: 'reduces bounds if fully truncated',
  397. text: '-[UIScrollView _smoothScrollDisplayLink:]',
  398. args: {
  399. bounds: [28, 35],
  400. trim: {
  401. // matched text is within truncated ellipsis ,
  402. text: '-[UIScr…Link:]',
  403. start: 7,
  404. end: 35,
  405. length: 28,
  406. },
  407. },
  408. expected: [7, 8], // …
  409. },
  410. {
  411. name: 'matched bounds fall before and after truncate',
  412. text: '-[UIScrollView _smoothScrollDisplayLink:]',
  413. args: {
  414. bounds: [16, 28],
  415. trim: {
  416. // match bounds are shifted after truncate
  417. text: '-[UIScrollView _sm…rollDisplayLink:]',
  418. start: 18,
  419. end: 24,
  420. length: 6,
  421. },
  422. },
  423. expected: [16, 23], // smoothScroll
  424. },
  425. {
  426. name: 'matched bounds fall before truncate',
  427. text: '-[UIScrollView _smoothScrollDisplayLink:]',
  428. args: {
  429. bounds: [4, 14],
  430. trim: {
  431. // match bounds are shifted after truncate
  432. text: '-[UIScrollView _sm…rollDisplayLink:]',
  433. start: 18,
  434. end: 24,
  435. length: 6,
  436. },
  437. },
  438. expected: [4, 14], // smoothScroll
  439. },
  440. ];
  441. it.each(testTable)(`$name`, ({args, expected}) => {
  442. const value = computeHighlightedBounds(args.bounds as Fuse.RangeTuple, args.trim);
  443. expect(value).toEqual(expected);
  444. });
  445. });
  446. describe('computeConfigViewWithStategy', () => {
  447. it('exact (preserves view height)', () => {
  448. const view = new Rect(0, 0, 1, 1);
  449. const frame = new Rect(0, 0, 0.5, 0.5);
  450. expect(
  451. computeConfigViewWithStategy('exact', view, frame).equals(new Rect(0, 0, 0.5, 1))
  452. ).toBe(true);
  453. });
  454. it('min (when view is too small to fit frame)', () => {
  455. const view = new Rect(0, 0, 1, 1);
  456. const frame = new Rect(2, 2, 5, 1);
  457. expect(
  458. computeConfigViewWithStategy('min', view, frame).equals(new Rect(2, 2, 5, 1))
  459. ).toBe(true);
  460. });
  461. it('min (frame is outside of view on the left)', () => {
  462. const view = new Rect(5, 0, 10, 1);
  463. const frame = new Rect(1, 0, 1, 1);
  464. expect(
  465. computeConfigViewWithStategy('min', view, frame).equals(new Rect(1, 0, 10, 1))
  466. ).toBe(true);
  467. });
  468. it('min (frame overlaps with view on the left)', () => {
  469. const view = new Rect(5, 0, 10, 1);
  470. const frame = new Rect(4, 0, 2, 1);
  471. expect(
  472. computeConfigViewWithStategy('min', view, frame).equals(new Rect(4, 0, 10, 1))
  473. ).toBe(true);
  474. });
  475. it('min (frame overlaps with view on the right)', () => {
  476. const view = new Rect(0, 0, 10, 1);
  477. const frame = new Rect(9, 0, 5, 1);
  478. expect(
  479. computeConfigViewWithStategy('min', view, frame).equals(new Rect(4, 0, 10, 1))
  480. ).toBe(true);
  481. });
  482. it('min (frame is outside of view on the right)', () => {
  483. const view = new Rect(0, 0, 10, 1);
  484. const frame = new Rect(12, 0, 5, 1);
  485. expect(
  486. computeConfigViewWithStategy('min', view, frame).equals(new Rect(7, 0, 10, 1))
  487. ).toBe(true);
  488. });
  489. it('min (frame is above the view)', () => {
  490. const view = new Rect(0, 1, 10, 1);
  491. const frame = new Rect(0, 0, 10, 1);
  492. expect(
  493. computeConfigViewWithStategy('min', view, frame).equals(new Rect(0, 0, 10, 1))
  494. ).toBe(true);
  495. });
  496. it('min (frame is below the view)', () => {
  497. const view = new Rect(0, 0, 10, 1);
  498. const frame = new Rect(0, 2, 10, 1);
  499. expect(
  500. computeConfigViewWithStategy('min', view, frame).equals(new Rect(0, 2, 10, 1))
  501. ).toBe(true);
  502. });
  503. });