Home | History | Annotate | Download | only in tests
      1 jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
      2 describe('PathKit\'s SVG Behavior', function() {
      3     // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
      4     var PathKit = null;
      5     const LoadPathKit = new Promise(function(resolve, reject) {
      6         if (PathKit) {
      7             resolve();
      8         } else {
      9             PathKitInit({
     10                 locateFile: (file) => '/pathkit/'+file,
     11             }).ready().then((_PathKit) => {
     12                 PathKit = _PathKit;
     13                 resolve();
     14             });
     15         }
     16     });
     17 
     18     it('can create a path from an SVG string', function(done) {
     19         LoadPathKit.then(catchException(done, () => {
     20             //.This is a parallelagram from
     21             // https://upload.wikimedia.org/wikipedia/commons/e/e7/Simple_parallelogram.svg
     22             let path = PathKit.FromSVGString('M 205,5 L 795,5 L 595,295 L 5,295 L 205,5 z');
     23 
     24             let cmds = path.toCmds();
     25             expect(cmds).toBeTruthy();
     26             // 1 move, 4 lines, 1 close
     27             // each element in cmds is an array, with index 0 being the verb, and the rest being args
     28             expect(cmds.length).toBe(6);
     29             expect(cmds).toEqual([[PathKit.MOVE_VERB, 205, 5],
     30                                   [PathKit.LINE_VERB, 795, 5],
     31                                   [PathKit.LINE_VERB, 595, 295],
     32                                   [PathKit.LINE_VERB, 5, 295],
     33                                   [PathKit.LINE_VERB, 205, 5],
     34                                   [PathKit.CLOSE_VERB]]);
     35             path.delete();
     36             done();
     37         }));
     38     });
     39 
     40     it('can create an SVG string from a path', function(done) {
     41         LoadPathKit.then(catchException(done, () => {
     42             let cmds = [[PathKit.MOVE_VERB, 205, 5],
     43                        [PathKit.LINE_VERB, 795, 5],
     44                        [PathKit.LINE_VERB, 595, 295],
     45                        [PathKit.LINE_VERB, 5, 295],
     46                        [PathKit.LINE_VERB, 205, 5],
     47                        [PathKit.CLOSE_VERB]];
     48             let path = PathKit.FromCmds(cmds);
     49 
     50             let svgStr = path.toSVGString();
     51             // We output it in terse form, which is different than Wikipedia's version
     52             expect(svgStr).toEqual('M205 5L795 5L595 295L5 295L205 5Z');
     53             path.delete();
     54             done();
     55         }));
     56     });
     57 
     58     it('can create an SVG string from hex values', function(done) {
     59         LoadPathKit.then(catchException(done, () => {
     60             let cmds = [[PathKit.MOVE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
     61                        [PathKit.LINE_VERB, 795, 5],
     62                        [PathKit.LINE_VERB, 595, 295],
     63                        [PathKit.LINE_VERB, 5, 295],
     64                        [PathKit.LINE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
     65                        [PathKit.CLOSE_VERB]];
     66             let path = PathKit.FromCmds(cmds);
     67 
     68             let svgStr = path.toSVGString();
     69             expect(svgStr).toEqual('M9.37088e-26 2.0003L795 5L595 295L5 295L9.37088e-26 2.0003Z');
     70             path.delete();
     71             done();
     72         }));
     73     });
     74 
     75     it('should have input and the output be the same', function(done) {
     76         LoadPathKit.then(catchException(done, () => {
     77             let testCases = [
     78                 'M0 0L1075 0L1075 242L0 242L0 0Z'
     79             ];
     80 
     81             for(let svg of testCases) {
     82                 let path = PathKit.FromSVGString(svg);
     83                 let output = path.toSVGString();
     84 
     85                 expect(svg).toEqual(output);
     86 
     87                 path.delete();
     88             }
     89             done();
     90         }));
     91     });
     92 
     93     it('approximates arcs (conics) with quads', function(done) {
     94         LoadPathKit.then(catchException(done, () => {
     95             let path = PathKit.NewPath();
     96             path.moveTo(50, 120);
     97             path.arc(50, 120, 45, 0, 1.75 * Math.PI);
     98             path.lineTo(50, 120);
     99             let svgStr = path.toSVGString();
    100             // Q stands for quad.  No need to check the whole path, as that's more
    101             // what the gold correctness tests are for (can account for changes we make
    102             // to the approximation algorithms).
    103             expect(svgStr).toContain('Q');
    104             path.delete();
    105 
    106              reportSVGString(svgStr, 'conics_quads_approx').then(() => {
    107                 done();
    108             }).catch(reportError(done));
    109         }));
    110     });
    111 
    112 });
    113