utils.spec.tsx 17 KB

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