1 // Copyright (c) 2011 Google, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * @fileoverview Assertions and other code used to test a canvas proxy.
24 * @author konigsberg@google.com (Robert Konigsberg)
27 var CanvasAssertions
= {};
30 * Assert that a line is drawn between the two points
32 * This merely looks for one of these four possibilities:
33 * moveTo(p1) -> lineTo(p2)
34 * moveTo(p2) -> lineTo(p1)
35 * lineTo(p1) -> lineTo(p2)
36 * lineTo(p2) -> lineTo(p1)
38 * attrs is meant to be used when you want to track things like
39 * color and stroke width.
41 CanvasAssertions
.assertLineDrawn
= function(proxy
, p1
, p2
, attrs
) {
42 // found = 1 when prior loop found p1.
43 // found = 2 when prior loop found p2.
45 for (var i
= 0; i
< proxy
.calls__
.length
; i
++) {
46 var call
= proxy
.calls__
[i
];
48 // This disables lineTo -> moveTo pairs.
49 if (call
.name
== "moveTo" && priorFound
> 0) {
54 if (call
.name
== "moveTo" || call
.name
== "lineTo") {
55 var matchp1
= CanvasAssertions
.matchPixels(p1
, call
.args
);
56 var matchp2
= CanvasAssertions
.matchPixels(p2
, call
.args
);
57 if (matchp1
|| matchp2
) {
58 if (priorFound
== 1 && matchp2
) {
59 // TODO -- add property test here CanvasAssertions.matchAttributes(attrs, call.properties)
62 if (priorFound
== 2 && matchp1
) {
63 // TODO -- add property test here CanvasAssertions.matchAttributes(attrs, call.properties)
66 found
= matchp1
? 1 : 2;
72 var toString
= function(x
) {
75 if (x
.hasOwnProperty(prop
)) {
79 s
= s
+ prop
+ ": " + x
[prop
];
84 fail("Can't find a line drawn between " + p1
+
85 " and " + p2
+ " with attributes " + toString(attrs
));
89 * Checks how many lines of the given color have been drawn.
90 * @return {Integer} The number of lines of the given color.
92 CanvasAssertions
.numLinesDrawn
= function(proxy
, color
) {
94 for (var i
= 0; i
< proxy
.calls__
.length
; i
++) {
95 var call
= proxy
.calls__
[i
];
96 if (call
.name
== "lineTo" && call
.properties
.strokeStyle
== color
) {
103 CanvasAssertions
.matchPixels
= function(expected
, actual
) {
104 // Expect array of two integers. Assuming the values are within one
105 // integer unit of each other. This should be tightened down by someone
106 // who knows what pixel a value of 5.8888 results in.
107 return Math
.abs(expected
[0] - actual
[0]) < 1 &&
108 Math
.abs(expected
[1] - actual
[1]) < 1;
111 CanvasAssertions
.matchAttributes
= function(expected
, actual
) {
112 for (var attr
in expected
) {
113 if (expected
.hasOwnProperty(attr
) && expected
[attr
] != actual
[attr
]) {