Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/ui/ve.ui.DataTransferHandlerFactory.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface DataTransferHandlerFactory tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.ui.DataTransferHandlerFactory' );
8
9 /* Stubs */
10
11 ve.test.utils.makeStubTransferHandler = function ( name, handlesPaste, types, kinds, extensions ) {
12 function StubHandler() {
13 StubHandler.super.apply( this, arguments );
14 }
15 OO.inheritClass( StubHandler, extensions ? ve.ui.FileTransferHandler : ve.ui.DataTransferHandler );
16 StubHandler.static.name = name;
17 StubHandler.static.handlesPaste = !!handlesPaste;
18 StubHandler.static.kinds = kinds;
19 StubHandler.static.types = types || [];
20 StubHandler.static.extensions = extensions;
21 return StubHandler;
22 };
23
24 ve.test.utils.makeStubTransferItem = function ( type, kind, extension ) {
25 return {
26 type: type,
27 kind: kind,
28 getExtension: function () {
29 return extension;
30 }
31 };
32 };
33
34 /* Tests */
35 QUnit.test( 'getHandlerNameForItem', ( assert ) => {
36 const makeStubTransferHandler = ve.test.utils.makeStubTransferHandler,
37 makeStubTransferItem = ve.test.utils.makeStubTransferItem,
38 factory = new ve.ui.DataTransferHandlerFactory(),
39 stubItemTypeHtml = makeStubTransferItem( 'text/html' ),
40 stubItemFileHtml = makeStubTransferItem( 'text/html', 'file', 'html' ),
41 stubItemStringHtml = makeStubTransferItem( 'text/html', 'string', 'html' ),
42 stubItemExtHtml = makeStubTransferItem( null, null, 'html' ),
43 stubItemProto = makeStubTransferItem( '__proto__', '__proto__', '__proto__' );
44
45 const StubHandlerFileHtml1 = makeStubTransferHandler( 'filehtml1', true, [ 'text/html' ], [ 'file' ], [ 'html' ] );
46 const StubHandlerFileHtml2 = makeStubTransferHandler( 'filehtml2', false, [ 'text/html' ], [ 'file' ], [ 'html' ] );
47 const StubHandlerStringHtml = makeStubTransferHandler( 'stringhtml', false, [ 'text/html' ], [ 'string' ] );
48 const StubHandlerHtml1 = makeStubTransferHandler( 'html1', true, [ 'text/html' ] );
49 const StubHandlerHtml2 = makeStubTransferHandler( 'html2', false, [ 'text/html' ] );
50 // The `html3` handler should never show up
51 const StubHandlerHtml3 = makeStubTransferHandler( 'html3', true, [ 'text/html' ] );
52 StubHandlerHtml3.static.matchFunction = function () {
53 return false;
54 };
55
56 // The factory should start out empty and __proto__ shouldn't cause a crash
57 assert.strictEqual( factory.getHandlerNameForItem( stubItemTypeHtml, false ), undefined, 'Empty factory shouldn\'t match by type' );
58 assert.strictEqual( factory.getHandlerNameForItem( stubItemFileHtml, false ), undefined, 'Empty factory shouldn\'t match by kind' );
59 assert.strictEqual( factory.getHandlerNameForItem( stubItemStringHtml, false ), undefined, 'Empty factory shouldn\'t match by kind (2)' );
60 assert.strictEqual( factory.getHandlerNameForItem( stubItemExtHtml, false ), undefined, 'Empty factory shouldn\'t match by extension' );
61 assert.strictEqual( factory.getHandlerNameForItem( stubItemProto, false ), undefined, 'Empty factory shouldn\'t crash on __proto__' );
62
63 factory.register( StubHandlerFileHtml1 );
64 factory.register( StubHandlerFileHtml2 );
65 factory.register( StubHandlerStringHtml );
66 factory.register( StubHandlerHtml1 );
67 factory.register( StubHandlerHtml2 );
68 factory.register( StubHandlerHtml3 );
69
70 // Ensure that __proto__ doesn't cause a crash
71 assert.strictEqual( factory.getHandlerNameForItem( stubItemProto, false ), undefined, 'Ensure that __proto__ doesn\'t cause a crash' );
72
73 // 1. Match by kind + type
74 assert.strictEqual( factory.getHandlerNameForItem( stubItemFileHtml, false ), 'filehtml2', 'Match by kind and type (unfiltered)' );
75 assert.strictEqual( factory.getHandlerNameForItem( stubItemFileHtml, true ), 'filehtml1', 'Match by kind a type (filtered for paste)' );
76 // 2. Match by just type (note that html3 doesn't show up)
77 assert.strictEqual( factory.getHandlerNameForItem( stubItemTypeHtml, false ), 'html2', 'Match by type (unfiltered)' );
78 assert.strictEqual( factory.getHandlerNameForItem( stubItemTypeHtml, true ), 'html1', 'Match by type (filtered for paste)' );
79 // 3. Match by file extension
80 assert.strictEqual( factory.getHandlerNameForItem( stubItemExtHtml, false ), 'filehtml2', 'Match by extension (unfiltered)' );
81 assert.strictEqual( factory.getHandlerNameForItem( stubItemExtHtml, true ), 'filehtml1', 'Match by extension (filtered for paste)' );
82
83 // Match by (1) kind and type, then fall through & match by (2) just type.
84 assert.strictEqual( factory.getHandlerNameForItem( stubItemStringHtml, false ), 'stringhtml', 'Match by kind and type (unfiltered, take 2)' );
85 assert.strictEqual( factory.getHandlerNameForItem( stubItemStringHtml, true ), 'html1', 'Fall through kind and type match after filter, match by just type' );
86 } );