Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/provider/mmv.provider.Image.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { ImageProvider } = require( 'mmv' );
19
20 ( function () {
21 QUnit.module( 'mmv.provider.Image', QUnit.newMwEnvironment() );
22
23 QUnit.test( 'Image constructor sense check', ( assert ) => {
24 const imageProvider = new ImageProvider();
25
26 assert.true( imageProvider instanceof ImageProvider );
27 } );
28
29 QUnit.test( 'Image load success', ( assert ) => {
30 const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0' +
31 'iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH' +
32 '8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC';
33 const imageProvider = new ImageProvider();
34
35 imageProvider.imagePreloadingSupported = () => false;
36
37 return imageProvider.get( url ).then( ( image ) => {
38 assert.true( image instanceof HTMLImageElement,
39 'success handler was called with the image element' );
40 assert.strictEqual( image.src, url, 'image src is correct' );
41 } );
42 } );
43
44 QUnit.test( 'Image caching', ( assert ) => {
45 const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0' +
46 'iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH' +
47 '8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC';
48 const url2 = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
49 let result;
50 const imageProvider = new ImageProvider();
51
52 imageProvider.imagePreloadingSupported = () => false;
53
54 return QUnit.whenPromisesComplete(
55 imageProvider.get( url ).then( ( image ) => {
56 result = image;
57 assert.true( image instanceof HTMLImageElement,
58 'success handler was called with the image element' );
59 assert.strictEqual( image.src, url, 'image src is correct' );
60 } ),
61
62 imageProvider.get( url ).then( ( image ) => {
63 assert.strictEqual( image, result, 'image element is cached and not regenerated' );
64 assert.strictEqual( image.src, url, 'image src is correct' );
65 } ),
66
67 imageProvider.get( url2 ).then( ( image ) => {
68 assert.notStrictEqual( image, result, 'image element for different url is not cached' );
69 assert.strictEqual( image.src, url2, 'image src is correct' );
70 } )
71 );
72 } );
73
74 QUnit.test( 'Image load fail', ( assert ) => {
75 const imageProvider = new ImageProvider();
76 const oldMwLog = mw.log;
77 const done = assert.async();
78 let mwLogCalled = false;
79
80 imageProvider.imagePreloadingSupported = () => false;
81 mw.log = function () {
82 mwLogCalled = true;
83 };
84
85 imageProvider.get( 'doesntexist.png' ).fail( () => {
86 assert.true( true, 'fail handler was called' );
87 assert.true( mwLogCalled, 'mw.log was called' );
88 mw.log = oldMwLog;
89 done();
90 } );
91 } );
92
93 QUnit.test( 'Image load with preloading supported', ( assert ) => {
94 const url = mw.config.get( 'wgExtensionAssetsPath' ) + '/MultimediaViewer/resources/mmv.ui.restriction/img/restrict-personality.svg';
95 const imageProvider = new ImageProvider();
96 const endsWith = function ( a, b ) {
97 return a.indexOf( b ) === a.length - b.length;
98 };
99
100 imageProvider.imagePreloadingSupported = () => true;
101 imageProvider.performance = {
102 record: function () {
103 return $.Deferred().resolve();
104 }
105 };
106
107 return imageProvider.get( url ).then( ( image ) => {
108 // can't test equality as browsers transform this to a full URL
109 assert.true( endsWith( image.src, url ), 'local image loaded with correct source' );
110 } );
111 } );
112
113 QUnit.test( 'Failed image load with preloading supported', ( assert ) => {
114 const url = 'nosuchimage.png';
115 const imageProvider = new ImageProvider();
116 const done = assert.async();
117
118 imageProvider.imagePreloadingSupported = () => true;
119 imageProvider.performance = {
120 record: function () {
121 return $.Deferred().resolve();
122 }
123 };
124
125 imageProvider.get( url ).fail( () => {
126 assert.true( true, 'Fail callback called for non-existing image' );
127 done();
128 } );
129 } );
130
131 QUnit.test( 'imageQueryParameter', ( assert ) => {
132 const imageProvider = new ImageProvider( 'foo' );
133 const done = assert.async();
134
135 imageProvider.imagePreloadingSupported = () => false;
136 imageProvider.rawGet = function ( url ) {
137 assert.strictEqual( url, 'http://www.wikipedia.org/?foo=', 'Extra parameter added' );
138
139 return $.Deferred().resolve();
140 };
141
142 imageProvider.get( 'http://www.wikipedia.org/' ).then( () => {
143 done();
144 } );
145 } );
146 }() );