@ -15,8 +15,11 @@
@@ -15,8 +15,11 @@
'use strict' ;
var webUiUtils = require ( '../../web/ui_utils.js' ) ;
var sharedUtil = require ( '../../shared/util.js' ) ;
var binarySearchFirstItem = webUiUtils . binarySearchFirstItem ;
var getPDFFileNameFromURL = webUiUtils . getPDFFileNameFromURL ;
var EventBus = webUiUtils . EventBus ;
var createObjectURL = sharedUtil . createObjectURL ;
describe ( 'ui_utils' , function ( ) {
describe ( 'binary search' , function ( ) {
function isTrue ( boolean ) {
@ -44,6 +47,66 @@ describe('ui_utils', function () {
@@ -44,6 +47,66 @@ describe('ui_utils', function () {
expect ( binarySearchFirstItem ( [ 4 , 5 , 6 ] , isGreater3 ) ) . toEqual ( 0 ) ;
} ) ;
} ) ;
describe ( 'getPDFFileNameFromURL' , function ( ) {
it ( 'gets PDF filename' , function ( ) {
expect ( getPDFFileNameFromURL ( '/pdfs/file1.pdf' ) ) . toEqual ( 'file1.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'http://www.example.com/pdfs/file2.pdf' ) ) . toEqual ( 'file2.pdf' ) ;
} ) ;
it ( 'gets fallback filename' , function ( ) {
expect ( getPDFFileNameFromURL ( '/pdfs/file1.txt' ) ) . toEqual ( 'document.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'http://www.example.com/pdfs/file2.txt' ) ) . toEqual ( 'document.pdf' ) ;
} ) ;
it ( 'gets custom fallback filename' , function ( ) {
expect ( getPDFFileNameFromURL ( '/pdfs/file1.txt' , 'qwerty1.pdf' ) ) . toEqual ( 'qwerty1.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'http://www.example.com/pdfs/file2.txt' , 'qwerty2.pdf' ) ) . toEqual ( 'qwerty2.pdf' ) ;
expect ( getPDFFileNameFromURL ( '/pdfs/file3.txt' , '' ) ) . toEqual ( '' ) ;
} ) ;
it ( 'gets PDF filename from URL containing leading/trailing whitespace' , function ( ) {
expect ( getPDFFileNameFromURL ( ' /pdfs/file1.pdf ' ) ) . toEqual ( 'file1.pdf' ) ;
expect ( getPDFFileNameFromURL ( ' http://www.example.com/pdfs/file2.pdf ' ) ) . toEqual ( 'file2.pdf' ) ;
} ) ;
it ( 'gets PDF filename from query string' , function ( ) {
expect ( getPDFFileNameFromURL ( '/pdfs/pdfs.html?name=file1.pdf' ) ) . toEqual ( 'file1.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'http://www.example.com/pdfs/pdf.html?file2.pdf' ) ) . toEqual ( 'file2.pdf' ) ;
} ) ;
it ( 'gets PDF filename from hash string' , function ( ) {
expect ( getPDFFileNameFromURL ( '/pdfs/pdfs.html#name=file1.pdf' ) ) . toEqual ( 'file1.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'http://www.example.com/pdfs/pdf.html#file2.pdf' ) ) . toEqual ( 'file2.pdf' ) ;
} ) ;
it ( 'gets correct PDF filename when multiple ones are present' , function ( ) {
expect ( getPDFFileNameFromURL ( '/pdfs/file1.pdf?name=file.pdf' ) ) . toEqual ( 'file1.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'http://www.example.com/pdfs/file2.pdf#file.pdf' ) ) . toEqual ( 'file2.pdf' ) ;
} ) ;
it ( 'gets PDF filename from URI-encoded data' , function ( ) {
var encodedUrl = encodeURIComponent ( 'http://www.example.com/pdfs/file1.pdf' ) ;
expect ( getPDFFileNameFromURL ( encodedUrl ) ) . toEqual ( 'file1.pdf' ) ;
var encodedUrlWithQuery = encodeURIComponent ( 'http://www.example.com/pdfs/file.txt?file2.pdf' ) ;
expect ( getPDFFileNameFromURL ( encodedUrlWithQuery ) ) . toEqual ( 'file2.pdf' ) ;
} ) ;
it ( 'gets PDF filename from data mistaken for URI-encoded' , function ( ) {
expect ( getPDFFileNameFromURL ( '/pdfs/%AA.pdf' ) ) . toEqual ( '%AA.pdf' ) ;
expect ( getPDFFileNameFromURL ( '/pdfs/%2F.pdf' ) ) . toEqual ( '%2F.pdf' ) ;
} ) ;
it ( 'gets PDF filename from (some) standard protocols' , function ( ) {
expect ( getPDFFileNameFromURL ( 'http://www.example.com/file1.pdf' ) ) . toEqual ( 'file1.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'https://www.example.com/file2.pdf' ) ) . toEqual ( 'file2.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'file:///path/to/files/file3.pdf' ) ) . toEqual ( 'file3.pdf' ) ;
expect ( getPDFFileNameFromURL ( 'ftp://www.example.com/file4.pdf' ) ) . toEqual ( 'file4.pdf' ) ;
} ) ;
it ( 'gets PDF filename from query string appended to "blob:" URL' , function ( ) {
var typedArray = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
var blobUrl = createObjectURL ( typedArray , 'application/pdf' ) ;
expect ( blobUrl . indexOf ( 'blob:' ) === 0 ) . toEqual ( true ) ;
expect ( getPDFFileNameFromURL ( blobUrl + '?file.pdf' ) ) . toEqual ( 'file.pdf' ) ;
} ) ;
it ( 'gets fallback filename from query string appended to "data:" URL' , function ( ) {
var typedArray = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
var dataUrl = createObjectURL ( typedArray , 'application/pdf' , true ) ;
expect ( dataUrl . indexOf ( 'data:' ) === 0 ) . toEqual ( true ) ;
expect ( getPDFFileNameFromURL ( dataUrl + '?file1.pdf' ) ) . toEqual ( 'document.pdf' ) ;
expect ( getPDFFileNameFromURL ( ' ' + dataUrl + '?file2.pdf' ) ) . toEqual ( 'document.pdf' ) ;
} ) ;
} ) ;
describe ( 'EventBus' , function ( ) {
it ( 'dispatch event' , function ( ) {
var eventBus = new EventBus ( ) ;