summaryrefslogtreecommitdiffstats
path: root/tests/auto/qtquick2/qquickcanvasitem/data/tst_canvas.qml
blob: a00ccc3c3f0307c20c7b73570d69965e02f38ff1 (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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import QtQuick 2.0
import QtTest 1.0

Rectangle {
    id:container
    width:100
    height:100
    Component {
        id:canvas
        Canvas {
            id:c
             width:100;height:100
             onPaint: {
                 var context = getContext("2d");
                 context.fillStyle = "red";
                 context.fillRect(0, 0, 100, 100);
             }
             property int paintCount:spyPaint.count
             property int paintedCount:spyPainted.count
             property int canvasSizeChangedCount:spyCanvasSizeChanged.count
             property int tileSizeChangedCount:spyTileSizeChanged.count
             property int renderStrategyChangedCount:spyRenderStrategyChanged.count
             property int canvasWindowChangedCount:spyCanvasWindowChanged.count
             property int renderTargetChangedCount:spyRenderTargetChanged.count
             property int imageLoadedCount:spyImageLoaded.count
             property int availableChangedCount:spyAvailableChanged.count

             SignalSpy {id: spyPaint;target:c;signalName: "paint"}
             SignalSpy {id: spyPainted;target:c;signalName: "painted"}
             SignalSpy {id: spyCanvasSizeChanged;target:c;signalName: "canvasSizeChanged"}
             SignalSpy {id: spyTileSizeChanged;target:c;signalName: "tileSizeChanged"}
             SignalSpy {id: spyRenderStrategyChanged;target:c;signalName: "renderStrategyChanged"}
             SignalSpy {id: spyCanvasWindowChanged;target:c;signalName: "canvasWindowChanged"}
             SignalSpy {id: spyRenderTargetChanged;target:c;signalName: "renderTargetChanged"}
             SignalSpy {id: spyImageLoaded;target:c;signalName: "imageLoaded"}
             SignalSpy {id: spyAvailableChanged;target:c;signalName: "availableChanged"}
        }
    }

   TestCase {
       name: "Canvas"; when: windowShown
       function test_canvasSize() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           //by default canvasSize is same with canvas' actual size
           // when canvas size changes, canvasSize should be changed as well.
           compare(c.canvasSize.width, c.width);
           compare(c.canvasSize.height, c.height);
           c.width = 20;
           compare(c.canvasSize.width, 20);
           compare(c.canvasSizeChangedCount, 1);
           c.height = 5;
           compare(c.canvasSizeChangedCount, 2);
           compare(c.canvasSize.height, 5);

           //change canvasSize manually, then canvasSize detaches from canvas
           //actual size.
           c.canvasSize.width = 100;
           compare(c.canvasSizeChangedCount, 3);
           compare(c.canvasSize.width, 100);
           compare(c.width, 20);
           c.canvasSize.height = 50;
           compare(c.canvasSizeChangedCount, 4);
           compare(c.canvasSize.height, 50);
           compare(c.height, 5);

           c.width = 10;
           compare(c.canvasSizeChangedCount, 4);
           compare(c.canvasSize.width, 100);
           compare(c.canvasSize.height, 50);

           c.height = 10;
           compare(c.canvasSizeChangedCount, 4);
           compare(c.canvasSize.width, 100);
           compare(c.canvasSize.height, 50);
           c.destroy();
      }
       function test_tileSize() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           compare(c.tileSize.width, c.width);
           compare(c.tileSize.height, c.height);
           c.width = 20;
           compare(c.tileSize.width, 20);
           compare(c.tileSizeChangedCount, 1);
           c.height = 5;
           compare(c.tileSizeChangedCount, 2);
           compare(c.tileSize.height, 5);

           c.tileSize.width = 100;
           compare(c.tileSizeChangedCount, 3);
           compare(c.tileSize.width, 100);
           compare(c.width, 20);
           c.tileSize.height = 50;
           compare(c.tileSizeChangedCount, 4);
           compare(c.tileSize.height, 50);
           compare(c.height, 5);

           c.width = 10;
           compare(c.tileSizeChangedCount, 4);
           compare(c.tileSize.width, 100);
           compare(c.tileSize.height, 50);

           c.height = 10;
           compare(c.tileSizeChangedCount, 4);
           compare(c.tileSize.width, 100);
           compare(c.tileSize.height, 50);
           c.destroy();

       }

       function test_canvasWindow() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);
           compare(c.canvasWindow.x, 0);
           compare(c.canvasWindow.y, 0);
           compare(c.canvasWindow.width, c.width);
           compare(c.canvasWindow.height, c.height);

           c.width = 20;
           compare(c.canvasWindow.width, 20);
           compare(c.canvasWindowChangedCount, 1);
           c.height = 5;
           compare(c.canvasWindowChangedCount, 2);
           compare(c.canvasWindow.height, 5);

           c.canvasWindow.x = 5;
           c.canvasWindow.y = 6;
           c.canvasWindow.width = 10;
           c.canvasWindow.height =20;
           compare(c.canvasWindowChangedCount, 6);
           compare(c.canvasWindow.width, 10);
           compare(c.canvasWindow.height, 20);
           compare(c.canvasWindow.x, 5);
           compare(c.canvasWindow.y, 6);
           c.destroy();

      }
       function test_renderTargetAndStrategy() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           compare(c.renderTarget, Canvas.Image);
           compare(c.renderStrategy, Canvas.Threaded);
           c.destroy();

      }
       function test_save() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           c.renderTarget = Canvas.Image;
           c.requestPaint();
           wait(100);
           verify(c.save("c.png"));
           c.loadImage("c.png");
           wait(200);
           compare(c.imageLoadedCount, 1);
           verify(c.isImageLoaded("c.png"));
           verify(!c.isImageLoading("c.png"));
           verify(!c.isImageError("c.png"));
           c.destroy();

      }
       function test_toDataURL_data() {
           return [{mimeType:"image/png"},
                   {mimeType:"image/bmp"},
                   {mimeType:"image/jpeg"},
                   {mimeType:"image/x-portable-pixmap"},
                   //{mimeType:"image/tiff"}, QTBUG-23980
                   {mimeType:"image/xpm"},
                   ];
       }

       function test_toDataURL(data) {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           var ctx = c.getContext("2d");
           ctx.fillStyle = "red";
           ctx.fillRect(0, 0, c.width, c.height);
           wait(100);

           var dataUrl = c.toDataURL();
           verify(dataUrl != "data:,");
           dataUrl = c.toDataURL("image/invalid");
           verify(dataUrl == "data:,");

           dataUrl = c.toDataURL(data.mimeType);
           verify(dataUrl != "data:,");

           ctx.save();
           ctx.fillStyle = "blue";
           ctx.fillRect(0, 0, c.width, c.height);
           ctx.restore();
           wait(100);

           var dataUrl2 = c.toDataURL(data.mimeType);
           verify (dataUrl2 != "data:,");
           verify (dataUrl2 != dataUrl);
           c.destroy();

      }
       function test_paint() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           c.renderTarget = Canvas.Image;
           c.renderStrategy = Canvas.Immediate;
           var ctx = c.getContext("2d");
           ctx.fillRect(0, 0, c.width, c.height);
           c.toDataURL();
           wait(100);

           compare(c.paintedCount, 1);
           compare(c.paintCount, 1);
           c.destroy();

      }
       function test_loadImage() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           c.loadImage("red.png");
           wait(200);
           compare(c.imageLoadedCount, 1);
           verify(c.isImageLoaded("red.png"));
           verify(!c.isImageLoading("red.png"));
           verify(!c.isImageError("red.png"));

           c.unloadImage("red.png");
           verify(!c.isImageLoaded("red.png"));
           verify(!c.isImageLoading("red.png"));
           verify(!c.isImageError("red.png"));
           c.destroy();

      }

       function test_getContext() {
           var c = canvas.createObject(container);
           verify(c);
           wait(100);
           verify(c.availableChangedCount, 1);

           var ctx = c.getContext("2d");
           verify(ctx);
           compare(ctx.canvas, c);
           ctx = c.getContext('2d');
           verify(ctx);
           compare(ctx.canvas, c);
           ctx = c.getContext('2D');
           verify(ctx);
           compare(ctx.canvas, c);
           ctx = c.getContext('invalid');
           verify(!ctx);
           c.destroy();

      }
   }
}