utils.spec.tsx 20 KB

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