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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
import QtQuick 2.0
import QtTest 1.1
Canvas {
id: canvas
width: 1
height: 1
contextType: "2d"
property var contextInPaint
SignalSpy {
id: paintedSpy
target: canvas
signalName: "paint"
}
SignalSpy {
id: contextSpy
target: canvas
signalName: "contextChanged"
}
onPaint: {
contextInPaint = context;
}
TestCase {
name: "ContextTypeStored"
when: windowShown
function test_contextType() {
compare(canvas.contextType, "2d");
}
}
TestCase {
name: "ContextValidWhenTypePredefined"
when: canvas.available
function test_context() {
// Wait for the context to become active
wait(100);
compare(contextSpy.count, 1);
// Context is available
verify(canvas.context)
}
function test_contextIsConsistent() {
// Wait for the context to become active
wait(100);
compare(contextSpy.count, 1);
// getContext("2d") is the same as the context property
compare(canvas.getContext("2d"), canvas.context);
}
function test_paintHadContext() {
// Make there was a paint signal
wait(100);
verify(paintedSpy.count, 1)
// Paint was called with a valid context when contextType is
// specified
verify(canvas.contextInPaint)
// paints context was the correct one
compare(canvas.contextInPaint, canvas.getContext("2d"));
}
}
// See: http://www.w3.org/TR/css3-fonts/#font-prop
TestCase {
name: "ContextFontValidation"
when: canvas.available
function test_pixelSize() {
wait(100);
compare(contextSpy.count, 1);
var ctx = canvas.getContext("2d");
compare(ctx.font, "sans-serif,-1,10,5,50,0,0,0,0,0");
ctx.font = "80.1px cursive";
// Can't verify the chosen font family since it's different for each platform.
compare(ctx.font.substr(ctx.font.indexOf(",") + 1), "-1,80,5,50,0,0,0,0,0");
}
function test_valid() {
wait(100);
compare(contextSpy.count, 1);
var ctx = canvas.getContext("2d");
var validFonts = [
{ string: "12px sans-serif", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "12px serif", expected: "serif,-1,12,5,50,0,0,0,0,0" },
{ string: "12pt sans-serif", expected: "sans-serif,12,-1,5,50,0,0,0,0,0" },
{ string: "12pt serif", expected: "serif,12,-1,5,50,0,0,0,0,0" },
{ string: "normal 12px sans-serif", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "normal normal 12px sans-serif", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "normal normal normal 12px sans-serif", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "italic 12px sans-serif", expected: "sans-serif,-1,12,5,50,1,0,0,0,0" },
{ string: "italic normal 12px sans-serif", expected: "sans-serif,-1,12,5,50,1,0,0,0,0" },
{ string: "italic normal normal 12px sans-serif", expected: "sans-serif,-1,12,5,50,1,0,0,0,0" },
{ string: "oblique 12px sans-serif", expected: "sans-serif,-1,12,5,50,2,0,0,0,0" },
{ string: "oblique normal 12px sans-serif", expected: "sans-serif,-1,12,5,50,2,0,0,0,0" },
{ string: "oblique normal normal 12px sans-serif", expected: "sans-serif,-1,12,5,50,2,0,0,0,0" },
{ string: "bold 12px sans-serif", expected: "sans-serif,-1,12,5,75,0,0,0,0,0" },
{ string: "0 12px sans-serif", expected: "sans-serif,-1,12,5,0,0,0,0,0,0" },
{ string: "small-caps 12px sans-serif", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "12px \"sans-serif\"", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "12px 'sans-serif'", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
// sans-serif will always be chosen, but this still tests that multiple families can be read.
{ string: "12px 'sans-serif' 'cursive'", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "12px sans-serif 'cursive' monospace", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: "12px sans-serif 'cursive' monospace ", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: " 12px sans-serif 'cursive' monospace ", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" },
{ string: " 12px sans-serif 'cursive' monospace ", expected: "sans-serif,-1,12,5,50,0,0,0,0,0" }
];
for (var i = 0; i < validFonts.length; ++i) {
ctx.font = validFonts[i].string;
compare(ctx.font.substr(ctx.font.indexOf(",") + 1),
validFonts[i].expected.substr(validFonts[i].expected.indexOf(",") + 1));
}
}
function test_invalid() {
wait(100);
compare(contextSpy.count, 1);
var ctx = canvas.getContext("2d");
var originalFont = ctx.font;
var fontStrings = [
"",
"12px",
"sans-serif",
"z12px sans-serif",
"1z2px sans-serif",
"12zpx sans-serif",
"12pxz sans-serif",
"sans-serif 12px",
"12px !@weeeeeeee!@!@Don'tNameYourFontThis",
"12px )(&*^^^%#$@*!!@#$JSPOR)",
"normal normal normal normal 12px sans-serif",
"normal normal bold bold 12px sans-serif",
"bold bold 12px sans-serif",
"12px 'cursive\"",
"12px 'cursive\" sans-serif",
"12px 'cursive"
];
var ignoredWarnings = [
"Context2D: Font string is empty.",
"Context2D: Missing or misplaced font family in font string (it must come after the font size).",
"Context2D: Invalid font size unit in font string.",
"Context2D: A font size of \"z12\" is invalid.",
"Context2D: A font size of \"1z2\" is invalid.",
"Context2D: A font size of \"12z\" is invalid.",
"Context2D: Invalid font size unit in font string.",
"Context2D: Missing or misplaced font family in font string (it must come after the font size).",
"Context2D: Unclosed quote in font string.",
"Context2D: The font families specified are invalid: )(&*^^^%#$@*!!@#$JSPOR)",
"Context2D: Duplicate token \"normal\" found in font string.",
"Context2D: Duplicate token \"bold\" found in font string.",
"Context2D: Duplicate token \"bold\" found in font string.",
"Context2D: Mismatched quote in font string.",
"Context2D: Mismatched quote in font string.",
"Context2D: Unclosed quote in font string."
];
// Sanity check...
compare(ignoredWarnings.length, fontStrings.length);
for (var i = 0; i < fontStrings.length; ++i) {
ignoreWarning(ignoredWarnings[i]);
ctx.font = fontStrings[i];
compare(ctx.font, originalFont);
}
}
}
TestCase {
name: "Colors"
when: canvas.available
function test_colors() {
wait(100);
compare(contextSpy.count, 1);
var ctx = canvas.getContext("2d");
// QTBUG-47894
ctx.strokeStyle = 'hsl(255, 100%, 50%)';
var c1 = ctx.strokeStyle.toString();
ctx.strokeStyle = 'hsl(320, 100%, 50%)';
var c2 = ctx.strokeStyle.toString();
verify(c1 !== c2);
}
}
}
|