summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/unit/test_bezierCanvas.js
blob: 3b0b4628bbb99d0ddef2ddd816e8d95af21607fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests the BezierCanvas API in the CubicBezierWidget module

var Cu = Components.utils;
var {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
var {CubicBezier, BezierCanvas} = require("devtools/client/shared/widgets/CubicBezierWidget");

function run_test() {
  offsetsGetterReturnsData();
  convertsOffsetsToCoordinates();
  plotsCanvas();
}

function offsetsGetterReturnsData() {
  do_print("offsets getter returns an array of 2 offset objects");

  let b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [.25, 0]);
  let offsets = b.offsets;

  do_check_eq(offsets.length, 2);

  do_check_true("top" in offsets[0]);
  do_check_true("left" in offsets[0]);
  do_check_true("top" in offsets[1]);
  do_check_true("left" in offsets[1]);

  do_check_eq(offsets[0].top, "300px");
  do_check_eq(offsets[0].left, "0px");
  do_check_eq(offsets[1].top, "100px");
  do_check_eq(offsets[1].left, "200px");

  do_print("offsets getter returns data according to current padding");

  b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [0, 0]);
  offsets = b.offsets;

  do_check_eq(offsets[0].top, "400px");
  do_check_eq(offsets[0].left, "0px");
  do_check_eq(offsets[1].top, "0px");
  do_check_eq(offsets[1].left, "200px");
}

function convertsOffsetsToCoordinates() {
  do_print("Converts offsets to coordinates");

  let b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [.25, 0]);

  let coordinates = b.offsetsToCoordinates({style: {
    left: "0px",
    top: "0px"
  }});
  do_check_eq(coordinates.length, 2);
  do_check_eq(coordinates[0], 0);
  do_check_eq(coordinates[1], 1.5);

  coordinates = b.offsetsToCoordinates({style: {
    left: "0px",
    top: "300px"
  }});
  do_check_eq(coordinates[0], 0);
  do_check_eq(coordinates[1], 0);

  coordinates = b.offsetsToCoordinates({style: {
    left: "200px",
    top: "100px"
  }});
  do_check_eq(coordinates[0], 1);
  do_check_eq(coordinates[1], 1);
}

function plotsCanvas() {
  do_print("Plots the curve to the canvas");

  let hasDrawnCurve = false;
  let b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [.25, 0]);
  b.ctx.bezierCurveTo = () => {
    hasDrawnCurve = true;
  };
  b.plot();

  do_check_true(hasDrawnCurve);
}

function getCubicBezier() {
  return new CubicBezier([0, 0, 1, 1]);
}

function getCanvasMock(w = 200, h = 400) {
  return {
    getContext: function () {
      return {
        scale: () => {},
        translate: () => {},
        clearRect: () => {},
        beginPath: () => {},
        closePath: () => {},
        moveTo: () => {},
        lineTo: () => {},
        stroke: () => {},
        arc: () => {},
        fill: () => {},
        bezierCurveTo: () => {},
        save: () => {},
        restore: () => {},
        setTransform: () => {}
      };
    },
    width: w,
    height: h
  };
}