utils.spec.tsx 20 KB

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