12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842 |
- /*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
- /* jshint jasmine: true */
- /* global WebKitBlobBuilder */
- exports.defineAutoTests = function () {
- var isBrowser = (cordova.platformId === "browser");
- // Use feature detection to determine current browser instead of checking user-agent
- var isChrome = isBrowser && window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
- var isIE = isBrowser && (window.msIndexedDB);
- var isIndexedDBShim = isBrowser && !isChrome; // Firefox and IE for example
- var isWindows = (cordova.platformId === "windows" || cordova.platformId === "windows8");
- var MEDIUM_TIMEOUT = 15000;
- describe('File API', function () {
- // Adding a Jasmine helper matcher, to report errors when comparing to FileError better.
- var fileErrorMap = {
- 1 : 'NOT_FOUND_ERR',
- 2 : 'SECURITY_ERR',
- 3 : 'ABORT_ERR',
- 4 : 'NOT_READABLE_ERR',
- 5 : 'ENCODING_ERR',
- 6 : 'NO_MODIFICATION_ALLOWED_ERR',
- 7 : 'INVALID_STATE_ERR',
- 8 : 'SYNTAX_ERR',
- 9 : 'INVALID_MODIFICATION_ERR',
- 10 : 'QUOTA_EXCEEDED_ERR',
- 11 : 'TYPE_MISMATCH_ERR',
- 12 : 'PATH_EXISTS_ERR'
- },
- root,
- temp_root,
- persistent_root;
- beforeEach(function (done) {
- // Custom Matchers
- jasmine.Expectation.addMatchers({
- toBeFileError : function () {
- return {
- compare : function (error, code) {
- var pass = error.code === code;
- return {
- pass : pass,
- message : 'Expected FileError with code ' + fileErrorMap[error.code] + ' (' + error.code + ') to be ' + fileErrorMap[code] + '(' + code + ')'
- };
- }
- };
- },
- toCanonicallyMatch : function () {
- return {
- compare : function (currentPath, path) {
- var a = path.split("/").join("").split("\\").join(""),
- b = currentPath.split("/").join("").split("\\").join(""),
- pass = a === b;
- return {
- pass : pass,
- message : 'Expected paths to match : ' + path + ' should be ' + currentPath
- };
- }
- };
- },
- toFailWithMessage : function () {
- return {
- compare : function (error, message) {
- var pass = false;
- return {
- pass : pass,
- message : message
- };
- }
- };
- },
- toBeDataUrl: function () {
- return {
- compare : function (url) {
- var pass = false;
- // "data:application/octet-stream;base64,"
- var header = url.substr(0, url.indexOf(','));
- var headerParts = header.split(/[:;]/);
- if (headerParts.length === 3 &&
- headerParts[0] === 'data' &&
- headerParts[2] === 'base64') {
- pass = true;
- }
- var message = 'Expected ' + url + ' to be a valid data url. ' + header + ' is not valid header for data uris';
- return {
- pass : pass,
- message : message
- };
- }
- };
- }
- });
- //Define global variables
- var onError = function (e) {
- console.log('[ERROR] Problem setting up root filesystem for test running! Error to follow.');
- console.log(JSON.stringify(e));
- };
- window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
- root = fileSystem.root;
- // set in file.tests.js
- persistent_root = root;
- window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
- temp_root = fileSystem.root;
- // set in file.tests.js
- done();
- }, onError);
- }, onError);
- });
- // HELPER FUNCTIONS
- // deletes specified file or directory
- var deleteEntry = function (name, success, error) {
- // deletes entry, if it exists
- // entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
- success = success || function() {};
- error = error || failed.bind(null, success, 'deleteEntry failed.');
- window.resolveLocalFileSystemURL(root.toURL() + '/' + name, function (entry) {
- if (entry.isDirectory === true) {
- entry.removeRecursively(success, error);
- } else {
- entry.remove(success, error);
- }
- }, success);
- };
- // deletes file, if it exists, then invokes callback
- var deleteFile = function (fileName, callback) {
- // entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
- callback = callback || function() {};
- root.getFile(fileName, null, // remove file system entry
- function (entry) {
- entry.remove(callback, function () {
- console.log('[ERROR] deleteFile cleanup method invoked fail callback.');
- });
- }, // doesn't exist
- callback);
- };
- // deletes and re-creates the specified file
- var createFile = function (fileName, success, error) {
- deleteEntry(fileName, function () {
- root.getFile(fileName, {
- create : true
- }, success, error);
- }, error);
- };
- // deletes and re-creates the specified directory
- var createDirectory = function (dirName, success, error) {
- deleteEntry(dirName, function () {
- root.getDirectory(dirName, {
- create : true
- }, success, error);
- }, error);
- };
- function failed(done, msg, error) {
- var info = typeof msg == 'undefined' ? 'Unexpected error callback' : msg;
- var codeMsg = (error && error.code) ? (': ' + fileErrorMap[error.code]) : '';
- expect(true).toFailWithMessage(info + '\n' + JSON.stringify(error) + codeMsg);
- done();
- }
- var succeed = function (done, msg) {
- var info = typeof msg == 'undefined' ? 'Unexpected success callback' : msg;
- expect(true).toFailWithMessage(info);
- done();
- };
- var joinURL = function (base, extension) {
- if (base.charAt(base.length - 1) !== '/' && extension.charAt(0) !== '/') {
- return base + '/' + extension;
- }
- if (base.charAt(base.length - 1) === '/' && extension.charAt(0) === '/') {
- return base + extension.substring(1);
- }
- return base + extension;
- };
- describe('FileError object', function () {
- it("file.spec.1 should define FileError constants", function () {
- expect(FileError.NOT_FOUND_ERR).toBe(1);
- expect(FileError.SECURITY_ERR).toBe(2);
- expect(FileError.ABORT_ERR).toBe(3);
- expect(FileError.NOT_READABLE_ERR).toBe(4);
- expect(FileError.ENCODING_ERR).toBe(5);
- expect(FileError.NO_MODIFICATION_ALLOWED_ERR).toBe(6);
- expect(FileError.INVALID_STATE_ERR).toBe(7);
- expect(FileError.SYNTAX_ERR).toBe(8);
- expect(FileError.INVALID_MODIFICATION_ERR).toBe(9);
- expect(FileError.QUOTA_EXCEEDED_ERR).toBe(10);
- expect(FileError.TYPE_MISMATCH_ERR).toBe(11);
- expect(FileError.PATH_EXISTS_ERR).toBe(12);
- });
- });
- describe('LocalFileSystem', function () {
- it("file.spec.2 should define LocalFileSystem constants", function () {
- expect(LocalFileSystem.TEMPORARY).toBe(0);
- expect(LocalFileSystem.PERSISTENT).toBe(1);
- });
- describe('window.requestFileSystem', function () {
- it("file.spec.3 should be defined", function () {
- expect(window.requestFileSystem).toBeDefined();
- });
- it("file.spec.4 should be able to retrieve a PERSISTENT file system", function (done) {
- var win = function (fileSystem) {
- expect(fileSystem).toBeDefined();
- expect(fileSystem.name).toBeDefined();
- if (isChrome) {
- expect(fileSystem.name).toContain("Persistent");
- } else {
- expect(fileSystem.name).toBe("persistent");
- }
- expect(fileSystem.root).toBeDefined();
- expect(fileSystem.root.filesystem).toBeDefined();
- // Shouldn't use cdvfile by default.
- expect(fileSystem.root.toURL()).not.toMatch(/^cdvfile:/);
- // All DirectoryEntry URLs should always have a trailing slash.
- expect(fileSystem.root.toURL()).toMatch(/\/$/);
- done();
- };
- // Request a little bit of space on the filesystem, unless we're running in a browser where that could cause a prompt.
- var spaceRequired = isBrowser ? 0 : 1024;
- // retrieve PERSISTENT file system
- window.requestFileSystem(LocalFileSystem.PERSISTENT, spaceRequired, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving PERSISTENT file system'));
- });
- it("file.spec.5 should be able to retrieve a TEMPORARY file system", function (done) {
- var win = function (fileSystem) {
- expect(fileSystem).toBeDefined();
- if (isChrome) {
- expect(fileSystem.name).toContain("Temporary");
- } else {
- expect(fileSystem.name).toBe("temporary");
- }
- expect(fileSystem.root).toBeDefined();
- expect(fileSystem.root.filesystem).toBeDefined();
- expect(fileSystem.root.filesystem).toBe(fileSystem);
- done();
- };
- //retrieve TEMPORARY file system
- window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system'));
- });
- it("file.spec.6 should error if you request a file system that is too large", function (done) {
- if (isBrowser) {
- /*window.requestFileSystem TEMPORARY and PERSISTENT filesystem quota is not limited in Chrome.
- Firefox filesystem size is not limited but every 50MB request user permission.
- IE10 allows up to 10mb of combined AppCache and IndexedDB used in implementation
- of filesystem without prompting, once you hit that level you will be asked if you
- want to allow it to be increased up to a max of 250mb per site.
- So `size` parameter for `requestFileSystem` function does not affect on filesystem in Firefox and IE.*/
- pending();
- }
- var fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.QUOTA_EXCEEDED_ERR);
- done();
- };
- //win = createWin('window.requestFileSystem');
- // Request the file system
- window.requestFileSystem(LocalFileSystem.TEMPORARY, 1000000000000000, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system'), fail);
- });
- it("file.spec.7 should error out if you request a file system that does not exist", function (done) {
- var fail = function (error) {
- expect(error).toBeDefined();
- if (isChrome) {
- /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of SYNTAX_ERR(code: 8)
- on requesting of a non-existant filesystem.*/
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- } else {
- expect(error).toBeFileError(FileError.SYNTAX_ERR);
- }
- done();
- };
- // Request the file system
- window.requestFileSystem(-1, 0, succeed.bind(null, done, 'window.requestFileSystem'), fail);
- });
- });
- describe('window.resolveLocalFileSystemURL', function () {
- it("file.spec.8 should be defined", function () {
- expect(window.resolveLocalFileSystemURL).toBeDefined();
- });
- it("file.spec.9 should resolve a valid file name", function (done) {
- var fileName = 'file.spec.9';
- var win = function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(true);
- expect(fileEntry.isDirectory).toBe(false);
- expect(fileEntry.name).toCanonicallyMatch(fileName);
- expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
- expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
- // Clean-up
- deleteEntry(fileName, done);
- };
- createFile(fileName, function (entry) {
- window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.9.1 should resolve a file even with a terminating slash", function (done) {
- var fileName = 'file.spec.9.1';
- var win = function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(true);
- expect(fileEntry.isDirectory).toBe(false);
- expect(fileEntry.name).toCanonicallyMatch(fileName);
- expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
- expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
- // Clean-up
- deleteEntry(fileName, done);
- };
- createFile(fileName, function (entry) {
- var entryURL = entry.toURL() + '/';
- window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entryURL));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.9.5 should resolve a directory", function (done) {
- var fileName = 'file.spec.9.5';
- var win = function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(false);
- expect(fileEntry.isDirectory).toBe(true);
- expect(fileEntry.name).toCanonicallyMatch(fileName);
- expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
- expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
- // cleanup
- deleteEntry(fileName, done);
- };
- function gotDirectory(entry) {
- // lookup file system entry
- window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entry.toURL()));
- }
- createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
- });
- it("file.spec.9.6 should resolve a directory even without a terminating slash", function (done) {
- var fileName = 'file.spec.9.6';
- var win = function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(false);
- expect(fileEntry.isDirectory).toBe(true);
- expect(fileEntry.name).toCanonicallyMatch(fileName);
- expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
- expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
- // cleanup
- deleteEntry(fileName, done);
- };
- function gotDirectory(entry) {
- // lookup file system entry
- var entryURL = entry.toURL();
- entryURL = entryURL.substring(0, entryURL.length - 1);
- window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entryURL));
- }
- createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
- });
- it("file.spec.10 resolve valid file name with parameters", function (done) {
- var fileName = "resolve.file.uri.params",
- win = function (fileEntry) {
- expect(fileEntry).toBeDefined();
- if (fileEntry.toURL().toLowerCase().substring(0, 10) === "cdvfile://") {
- expect(fileEntry.fullPath).toBe("/" + fileName + "?1234567890");
- }
- expect(fileEntry.name).toBe(fileName);
- // cleanup
- deleteEntry(fileName, done);
- };
- // create a new file entry
- createFile(fileName, function (entry) {
- window.resolveLocalFileSystemURL(entry.toURL() + "?1234567890", win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + entry.toURL()));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.11 should error (NOT_FOUND_ERR) when resolving (non-existent) invalid file name", function (done) {
- var fileName = cordova.platformId === 'windowsphone' ? root.toURL() + "/" + "this.is.not.a.valid.file.txt" : joinURL(root.toURL(), "this.is.not.a.valid.file.txt"),
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- };
- // lookup file system entry
- window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
- });
- it("file.spec.12 should error (ENCODING_ERR) when resolving invalid URI with leading /", function (done) {
- var fileName = "/this.is.not.a.valid.url",
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.ENCODING_ERR);
- done();
- };
- // lookup file system entry
- window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
- });
- });
- });
- //LocalFileSystem
- describe('Metadata interface', function () {
- it("file.spec.13 should exist and have the right properties", function () {
- var metadata = new Metadata();
- expect(metadata).toBeDefined();
- expect(metadata.modificationTime).toBeDefined();
- });
- });
- describe('Flags interface', function () {
- it("file.spec.14 should exist and have the right properties", function () {
- var flags = new Flags(false, true);
- expect(flags).toBeDefined();
- expect(flags.create).toBeDefined();
- expect(flags.create).toBe(false);
- expect(flags.exclusive).toBeDefined();
- expect(flags.exclusive).toBe(true);
- });
- });
- describe('FileSystem interface', function () {
- it("file.spec.15 should have a root that is a DirectoryEntry", function (done) {
- var win = function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(false);
- expect(entry.isDirectory).toBe(true);
- expect(entry.name).toBeDefined();
- expect(entry.fullPath).toBeDefined();
- expect(entry.getMetadata).toBeDefined();
- expect(entry.moveTo).toBeDefined();
- expect(entry.copyTo).toBeDefined();
- expect(entry.toURL).toBeDefined();
- expect(entry.remove).toBeDefined();
- expect(entry.getParent).toBeDefined();
- expect(entry.createReader).toBeDefined();
- expect(entry.getFile).toBeDefined();
- expect(entry.getDirectory).toBeDefined();
- expect(entry.removeRecursively).toBeDefined();
- done();
- };
- window.resolveLocalFileSystemURL(root.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + root.toURL()));
- });
- });
- describe('DirectoryEntry', function () {
- it("file.spec.16 getFile: get Entry for file that does not exist", function (done) {
- var fileName = "de.no.file",
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- };
- // create:false, exclusive:false, file does not exist
- root.getFile(fileName, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
- });
- it("file.spec.17 getFile: create new file", function (done) {
- var fileName = "de.create.file",
- filePath = joinURL(root.fullPath, fileName),
- win = function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(fileName);
- expect(entry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(entry.name, done);
- };
- // create:true, exclusive:false, file does not exist
- root.getFile(fileName, {
- create : true
- }, win, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName));
- });
- it("file.spec.18 getFile: create new file (exclusive)", function (done) {
- var fileName = "de.create.exclusive.file",
- filePath = joinURL(root.fullPath, fileName),
- win = function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toBe(fileName);
- expect(entry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(entry.name, done);
- };
- // create:true, exclusive:true, file does not exist
- root.getFile(fileName, {
- create : true,
- exclusive : true
- }, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
- });
- it("file.spec.19 getFile: create file that already exists", function (done) {
- var fileName = "de.create.existing.file",
- filePath = joinURL(root.fullPath, fileName);
- function win(entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(fileName);
- expect(entry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(entry.name, done);
- }
- function getFile(file) {
- // create:true, exclusive:false, file exists
- root.getFile(fileName, {
- create : true
- }, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
- }
- // create file to kick off it
- root.getFile(fileName, {
- create : true
- }, getFile, failed.bind(null, done, 'root.getFile - Error on initial creating file: ' + fileName));
- });
- it("file.spec.20 getFile: create file that already exists (exclusive)", function (done) {
- var fileName = "de.create.exclusive.existing.file",
- existingFile;
- function fail(error) {
- expect(error).toBeDefined();
- if (isChrome) {
- /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of PATH_EXISTS_ERR(code: 12)
- on trying to exclusively create a file, which already exists in Chrome.*/
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- } else {
- expect(error).toBeFileError(FileError.PATH_EXISTS_ERR);
- }
- // cleanup
- deleteEntry(existingFile.name, done);
- }
- function getFile(file) {
- existingFile = file;
- // create:true, exclusive:true, file exists
- root.getFile(fileName, {
- create : true,
- exclusive : true
- }, succeed.bind(null, done, 'root.getFile - getFile function - Error unexpected callback, file should exists: ' + fileName), fail);
- }
- // create file to kick off it
- root.getFile(fileName, {
- create : true
- }, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
- });
- it("file.spec.21 DirectoryEntry.getFile: get Entry for existing file", function (done) {
- var fileName = "de.get.file",
- filePath = joinURL(root.fullPath, fileName),
- win = function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(fileName);
- expect(entry.fullPath).toCanonicallyMatch(filePath);
- expect(entry.filesystem).toBeDefined();
- expect(entry.filesystem).toBe(root.filesystem);
- //clean up
- deleteEntry(entry.name, done);
- },
- getFile = function (file) {
- // create:false, exclusive:false, file exists
- root.getFile(fileName, {
- create : false
- }, win, failed.bind(null, done, 'root.getFile - Error getting file entry: ' + fileName));
- };
- // create file to kick off it
- root.getFile(fileName, {
- create : true
- }, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
- });
- it("file.spec.22 DirectoryEntry.getFile: get FileEntry for invalid path", function (done) {
- if (isBrowser) {
- /*The plugin does not follow to ["8.3 Naming restrictions"]
- (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions).*/
- pending();
- }
- var fileName = "de:invalid:path",
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.ENCODING_ERR);
- done();
- };
- // create:false, exclusive:false, invalid path
- root.getFile(fileName, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
- });
- it("file.spec.23 DirectoryEntry.getDirectory: get Entry for directory that does not exist", function (done) {
- var dirName = "de.no.dir",
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- };
- // create:false, exclusive:false, directory does not exist
- root.getDirectory(dirName, {
- create : false
- }, succeed.bind(null, done, 'root.getDirectory - Error unexpected callback, directory should not exists: ' + dirName), fail);
- });
- it("file.spec.24 DirectoryEntry.getDirectory: create new dir with space then resolveLocalFileSystemURL", function (done) {
- var dirName = "de create dir";
- function win(directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(dirName);
- expect(directory.fullPath).toCanonicallyMatch(joinURL(root.fullPath, dirName));
- // cleanup
- deleteEntry(directory.name, done);
- }
- function getDir(dirEntry) {
- expect(dirEntry.filesystem).toBeDefined();
- expect(dirEntry.filesystem).toBe(root.filesystem);
- var dirURI = dirEntry.toURL();
- // now encode URI and try to resolve
- window.resolveLocalFileSystemURL(dirURI, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
- }
- // create:true, exclusive:false, directory does not exist
- root.getDirectory(dirName, {
- create : true
- }, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- // This test is excluded, and should probably be removed. Filesystem
- // should always be properly encoded URLs, and *not* raw paths, and it
- // doesn't make sense to double-encode the URLs and expect that to be
- // handled by the implementation.
- // If a particular platform uses paths internally rather than URLs, // then that platform should careful to pass them correctly to its
- // backend.
- xit("file.spec.25 DirectoryEntry.getDirectory: create new dir with space resolveLocalFileSystemURL with encoded URI", function (done) {
- var dirName = "de create dir2",
- dirPath = joinURL(root.fullPath, dirName);
- function win(directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(dirName);
- expect(directory.fullPath).toCanonicallyMatch(dirPath);
- // cleanup
- deleteEntry(directory.name, done);
- }
- function getDir(dirEntry) {
- var dirURI = dirEntry.toURL();
- // now encode URI and try to resolve
- window.resolveLocalFileSystemURL(encodeURI(dirURI), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
- }
- // create:true, exclusive:false, directory does not exist
- root.getDirectory(dirName, {
- create : true
- }, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.26 DirectoryEntry.getDirectory: create new directory", function (done) {
- var dirName = "de.create.dir",
- dirPath = joinURL(root.fullPath, dirName),
- win = function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(dirName);
- expect(directory.fullPath).toCanonicallyMatch(dirPath);
- expect(directory.filesystem).toBeDefined();
- expect(directory.filesystem).toBe(root.filesystem);
- // cleanup
- deleteEntry(directory.name, done);
- };
- // create:true, exclusive:false, directory does not exist
- root.getDirectory(dirName, {
- create : true
- }, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.27 DirectoryEntry.getDirectory: create new directory (exclusive)", function (done) {
- var dirName = "de.create.exclusive.dir",
- dirPath = joinURL(root.fullPath, dirName),
- win = function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(dirName);
- expect(directory.fullPath).toCanonicallyMatch(dirPath);
- expect(directory.filesystem).toBeDefined();
- expect(directory.filesystem).toBe(root.filesystem);
- // cleanup
- deleteEntry(directory.name, done);
- };
- // create:true, exclusive:true, directory does not exist
- root.getDirectory(dirName, {
- create : true,
- exclusive : true
- }, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.28 DirectoryEntry.getDirectory: create directory that already exists", function (done) {
- var dirName = "de.create.existing.dir",
- dirPath = joinURL(root.fullPath, dirName),
- win = function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(dirName);
- expect(directory.fullPath).toCanonicallyMatch(dirPath);
- // cleanup
- deleteEntry(directory.name, done);
- };
- // create directory to kick off it
- root.getDirectory(dirName, {
- create : true
- }, function () {
- root.getDirectory(dirName, {
- create : true
- }, win, failed.bind(null, done, 'root.getDirectory - Error creating existent second directory : ' + dirName));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.29 DirectoryEntry.getDirectory: create directory that already exists (exclusive)", function (done) {
- var dirName = "de.create.exclusive.existing.dir",
- existingDir,
- fail = function (error) {
- expect(error).toBeDefined();
- if (isChrome) {
- /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of PATH_EXISTS_ERR(code: 12)
- on trying to exclusively create a file or directory, which already exists (Chrome).*/
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- } else {
- expect(error).toBeFileError(FileError.PATH_EXISTS_ERR);
- }
- // cleanup
- deleteEntry(existingDir.name, done);
- };
- // create directory to kick off it
- root.getDirectory(dirName, {
- create : true
- }, function (directory) {
- existingDir = directory;
- // create:true, exclusive:true, directory exists
- root.getDirectory(dirName, {
- create : true,
- exclusive : true
- }, failed.bind(null, done, 'root.getDirectory - Unexpected success callback, second directory should not be created : ' + dirName), fail);
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.30 DirectoryEntry.getDirectory: get Entry for existing directory", function (done) {
- var dirName = "de.get.dir",
- dirPath = joinURL(root.fullPath, dirName),
- win = function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(dirName);
- expect(directory.fullPath).toCanonicallyMatch(dirPath);
- // cleanup
- deleteEntry(directory.name, done);
- };
- // create directory to kick it off
- root.getDirectory(dirName, {
- create : true
- }, function () {
- root.getDirectory(dirName, {
- create : false
- }, win, failed.bind(null, done, 'root.getDirectory - Error getting directory entry : ' + dirName));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.31 DirectoryEntry.getDirectory: get DirectoryEntry for invalid path", function (done) {
- if (isBrowser) {
- /*The plugin does not follow to ["8.3 Naming restrictions"]
- (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions).*/
- pending();
- }
- var dirName = "de:invalid:path",
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.ENCODING_ERR);
- done();
- };
- // create:false, exclusive:false, invalid path
- root.getDirectory(dirName, {
- create : false
- }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), fail);
- });
- it("file.spec.32 DirectoryEntry.getDirectory: get DirectoryEntry for existing file", function (done) {
- var fileName = "de.existing.file",
- existingFile,
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR);
- // cleanup
- deleteEntry(existingFile.name, done);
- };
- // create file to kick off it
- root.getFile(fileName, {
- create : true
- }, function (file) {
- existingFile = file;
- root.getDirectory(fileName, {
- create : false
- }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + fileName), fail);
- }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
- });
- it("file.spec.33 DirectoryEntry.getFile: get FileEntry for existing directory", function (done) {
- var dirName = "de.existing.dir",
- existingDir,
- fail = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR);
- // cleanup
- deleteEntry(existingDir.name, done);
- };
- // create directory to kick off it
- root.getDirectory(dirName, {
- create : true
- }, function (directory) {
- existingDir = directory;
- root.getFile(dirName, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, file should not exists: ' + dirName), fail);
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.34 DirectoryEntry.removeRecursively on directory", function (done) {
- var dirName = "de.removeRecursively",
- subDirName = "dir",
- dirExists = function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- };
- // create a new directory entry to kick off it
- root.getDirectory(dirName, {
- create : true
- }, function (entry) {
- entry.getDirectory(subDirName, {
- create : true
- }, function (dir) {
- entry.removeRecursively(function () {
- root.getDirectory(dirName, {
- create : false
- }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), dirExists);
- }, failed.bind(null, done, 'entry.removeRecursively - Error removing directory recursively : ' + dirName));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + subDirName));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.35 createReader: create reader on existing directory", function () {
- // create reader for root directory
- var reader = root.createReader();
- expect(reader).toBeDefined();
- expect(typeof reader.readEntries).toBe('function');
- });
- it("file.spec.36 removeRecursively on root file system", function (done) {
- var remove = function (error) {
- expect(error).toBeDefined();
- if (isChrome) {
- /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of
- NO_MODIFICATION_ALLOWED_ERR(code: 6) on trying to call removeRecursively
- on the root file system (Chrome).*/
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- } else {
- expect(error).toBeFileError(FileError.NO_MODIFICATION_ALLOWED_ERR);
- }
- done();
- };
- // remove root file system
- root.removeRecursively(succeed.bind(null, done, 'root.removeRecursively - Unexpected success callback, root cannot be removed'), remove);
- });
- });
- describe('DirectoryReader interface', function () {
- describe("readEntries", function () {
- it("file.spec.37 should read contents of existing directory", function (done) {
- var reader,
- win = function (entries) {
- expect(entries).toBeDefined();
- expect(entries instanceof Array).toBe(true);
- done();
- };
- // create reader for root directory
- reader = root.createReader();
- // read entries
- reader.readEntries(win, failed.bind(null, done, 'reader.readEntries - Error reading entries'));
- });
- it("file.spec.37.1 should read contents of existing directory", function (done) {
- var dirName = 'readEntries.dir',
- fileName = 'readeEntries.file';
- root.getDirectory(dirName, {
- create : true
- }, function (directory) {
- directory.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- var reader = directory.createReader();
- reader.readEntries(function (entries) {
- expect(entries).toBeDefined();
- expect(entries instanceof Array).toBe(true);
- expect(entries.length).toBe(1);
- expect(entries[0].fullPath).toCanonicallyMatch(fileEntry.fullPath);
- expect(entries[0].filesystem).not.toBe(null);
- if (isChrome) {
- // Slicing '[object {type}]' -> '{type}'
- expect(entries[0].filesystem.toString().slice(8, -1)).toEqual("DOMFileSystem");
- }
- else {
- expect(entries[0].filesystem instanceof FileSystem).toBe(true);
- }
- // cleanup
- deleteEntry(directory.name, done);
- }, failed.bind(null, done, 'reader.readEntries - Error reading entries from directory: ' + dirName));
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + fileName));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.109 should return an empty entry list on the second call", function (done) {
- var reader,
- fileName = 'test109.txt';
- // Add a file to ensure the root directory is non-empty and then read the contents of the directory.
- root.getFile(fileName, {
- create : true
- }, function (entry) {
- reader = root.createReader();
- //First read
- reader.readEntries(function (entries) {
- expect(entries).toBeDefined();
- expect(entries instanceof Array).toBe(true);
- expect(entries.length).not.toBe(0);
- //Second read
- reader.readEntries(function (entries_) {
- expect(entries_).toBeDefined();
- expect(entries_ instanceof Array).toBe(true);
- expect(entries_.length).toBe(0);
- //Clean up
- deleteEntry(entry.name, done);
- }, failed.bind(null, done, 'reader.readEntries - Error during SECOND reading of entries from [root] directory'));
- }, failed.bind(null, done, 'reader.readEntries - Error during FIRST reading of entries from [root] directory'));
- }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
- });
- });
- it("file.spec.38 should read contents of directory that has been removed", function (done) {
- var dirName = "de.createReader.notfound";
- // create a new directory entry to kick off it
- root.getDirectory(dirName, {
- create : true
- }, function (directory) {
- directory.removeRecursively(function () {
- var reader = directory.createReader();
- reader.readEntries(succeed.bind(null, done, 'reader.readEntries - Unexpected success callback, it should not read entries from deleted dir: ' + dirName), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- root.getDirectory(dirName, {
- create : false
- }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, it should not get deleted directory: ' + dirName), function (err) {
- expect(err).toBeDefined();
- expect(err).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- });
- });
- }, failed.bind(null, done, 'directory.removeRecursively - Error removing directory recursively : ' + dirName));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- });
- //DirectoryReader interface
- describe('File', function () {
- it("file.spec.39 constructor should be defined", function () {
- expect(File).toBeDefined();
- expect(typeof File).toBe('function');
- });
- it("file.spec.40 should be define File attributes", function () {
- var file = new File();
- expect(file.name).toBeDefined();
- expect(file.type).toBeDefined();
- expect(file.lastModifiedDate).toBeDefined();
- expect(file.size).toBeDefined();
- });
- });
- //File
- describe('FileEntry', function () {
- it("file.spec.41 should be define FileEntry methods", function (done) {
- var fileName = "fe.methods",
- testFileEntry = function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(typeof fileEntry.createWriter).toBe('function');
- expect(typeof fileEntry.file).toBe('function');
- // cleanup
- deleteEntry(fileEntry.name, done);
- };
- // create a new file entry to kick off it
- root.getFile(fileName, {
- create : true
- }, testFileEntry, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
- });
- it("file.spec.42 createWriter should return a FileWriter object", function (done) {
- var fileName = "fe.createWriter",
- testFile,
- testWriter = function (writer) {
- expect(writer).toBeDefined();
- if (isChrome) {
- // Slicing '[object {type}]' -> '{type}'
- expect(writer.toString().slice(8, -1)).toEqual("FileWriter");
- }
- else {
- expect(writer instanceof FileWriter).toBe(true);
- }
- // cleanup
- deleteEntry(testFile.name, done);
- };
- // create a new file entry to kick off it
- root.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- testFile = fileEntry;
- fileEntry.createWriter(testWriter, failed.bind(null, done, 'fileEntry.createWriter - Error creating Writer from entry'));
- }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
- });
- it("file.spec.43 file should return a File object", function (done) {
- var fileName = "fe.file",
- newFile,
- testFile = function (file) {
- expect(file).toBeDefined();
- if (isChrome) {
- // Slicing '[object {type}]' -> '{type}'
- expect(file.toString().slice(8, -1)).toEqual("File");
- }
- else {
- expect(file instanceof File).toBe(true);
- }
- // cleanup
- deleteEntry(newFile.name, done);
- };
- // create a new file entry to kick off it
- root.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- newFile = fileEntry;
- fileEntry.file(testFile, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
- });
- it("file.spec.44 file: on File that has been removed", function (done) {
- var fileName = "fe.no.file";
- // create a new file entry to kick off it
- root.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- fileEntry.remove(function () {
- fileEntry.file(succeed.bind(null, done, 'fileEntry.file - Unexpected success callback, file it should not be created from removed entry'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- });
- }, failed.bind(null, done, 'fileEntry.remove - Error removing entry : ' + fileName));
- }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
- });
- });
- //FileEntry
- describe('Entry', function () {
- it("file.spec.45 Entry object", function (done) {
- var fileName = "entry",
- fullPath = joinURL(root.fullPath, fileName),
- winEntry = function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(fileName);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- expect(typeof entry.getMetadata).toBe('function');
- expect(typeof entry.setMetadata).toBe('function');
- expect(typeof entry.moveTo).toBe('function');
- expect(typeof entry.copyTo).toBe('function');
- expect(typeof entry.toURL).toBe('function');
- expect(typeof entry.remove).toBe('function');
- expect(typeof entry.getParent).toBe('function');
- expect(typeof entry.createWriter).toBe('function');
- expect(typeof entry.file).toBe('function');
- // Clean up
- deleteEntry(fileName, done);
- };
- // create a new file entry
- createFile(fileName, winEntry, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
- });
- it("file.spec.46 Entry.getMetadata on file", function (done) {
- var fileName = "entry.metadata.file";
- // create a new file entry
- createFile(fileName, function (entry) {
- entry.getMetadata(function (metadata) {
- expect(metadata).toBeDefined();
- expect(metadata.modificationTime instanceof Date).toBe(true);
- expect(typeof metadata.size).toBe("number");
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'entry.getMetadata - Error getting metadata from entry : ' + fileName));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
- });
- it("file.spec.47 Entry.getMetadata on directory", function (done) {
- if (isIndexedDBShim) {
- /* Does not support metadata for directories (Firefox, IE) */
- pending();
- }
- var dirName = "entry.metadata.dir";
- // create a new directory entry
- createDirectory(dirName, function (entry) {
- entry.getMetadata(function (metadata) {
- expect(metadata).toBeDefined();
- expect(metadata.modificationTime instanceof Date).toBe(true);
- expect(typeof metadata.size).toBe("number");
- expect(metadata.size).toBe(0);
- // cleanup
- deleteEntry(dirName, done);
- }, failed.bind(null, done, 'entry.getMetadata - Error getting metadata from entry : ' + dirName));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.48 Entry.getParent on file in root file system", function (done) {
- var fileName = "entry.parent.file",
- rootPath = root.fullPath;
- // create a new file entry
- createFile(fileName, function (entry) {
- entry.getParent(function (parent) {
- expect(parent).toBeDefined();
- expect(parent.fullPath).toCanonicallyMatch(rootPath);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'entry.getParent - Error getting parent directory of file : ' + fileName));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
- });
- it("file.spec.49 Entry.getParent on directory in root file system", function (done) {
- var dirName = "entry.parent.dir",
- rootPath = root.fullPath;
- // create a new directory entry
- createDirectory(dirName, function (entry) {
- entry.getParent(function (parent) {
- expect(parent).toBeDefined();
- expect(parent.fullPath).toCanonicallyMatch(rootPath);
- // cleanup
- deleteEntry(dirName, done);
- }, failed.bind(null, done, 'entry.getParent - Error getting parent directory of directory : ' + dirName));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.50 Entry.getParent on root file system", function (done) {
- var rootPath = root.fullPath,
- winParent = function (parent) {
- expect(parent).toBeDefined();
- expect(parent.fullPath).toCanonicallyMatch(rootPath);
- done();
- };
- // create a new directory entry
- root.getParent(winParent, failed.bind(null, done, 'root.getParent - Error getting parent directory of root'));
- });
- it("file.spec.51 Entry.toURL on file", function (done) {
- var fileName = "entry.uri.file",
- rootPath = root.fullPath,
- winURI = function (entry) {
- var uri = entry.toURL();
- expect(uri).toBeDefined();
- expect(uri.indexOf(rootPath)).not.toBe(-1);
- // cleanup
- deleteEntry(fileName, done);
- };
- // create a new file entry
- createFile(fileName, winURI, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
- });
- it("file.spec.52 Entry.toURL on directory", function (done) {
- var dirName_1 = "num 1",
- dirName_2 = "num 2",
- rootPath = root.fullPath;
- createDirectory(dirName_1, function (entry) {
- entry.getDirectory(dirName_2, {
- create : true
- }, function (entryFile) {
- var uri = entryFile.toURL();
- expect(uri).toBeDefined();
- expect(uri).toContain('/num%201/num%202/');
- expect(uri.indexOf(rootPath)).not.toBe(-1);
- // cleanup
- deleteEntry(dirName_1, done);
- }, failed.bind(null, done, 'entry.getDirectory - Error creating directory : ' + dirName_2));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName_1));
- });
- it("file.spec.53 Entry.remove on file", function (done) {
- var fileName = "entr .rm.file";
- // create a new file entry
- createFile(fileName, function (entry) {
- expect(entry).toBeDefined();
- entry.remove(function () {
- root.getFile(fileName, null, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get deleted file : ' + fileName), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(fileName, done);
- });
- }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + fileName));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
- });
- it("file.spec.53.1 Entry.remove on filename with #s", function (done) {
- var fileName = "entry.#rm#.file";
- // create a new file entry
- createFile(fileName, function (entry) {
- expect(entry).toBeDefined();
- entry.remove(function () {
- root.getFile(fileName, null, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get deleted file : ' + fileName), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(fileName, done);
- });
- }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + fileName));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
- });
- it("file.spec.54 remove on empty directory", function (done) {
- var dirName = "entry.rm.dir";
- // create a new directory entry
- createDirectory(dirName, function (entry) {
- expect(entry).toBeDefined();
- entry.remove(function () {
- root.getDirectory(dirName, null, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, it should not get deleted directory : ' + dirName), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(dirName, done);
- });
- }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + dirName));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.55 remove on non-empty directory", function (done) {
- if (isIndexedDBShim) {
- /* Both Entry.remove and directoryEntry.removeRecursively don't fail when removing
- non-empty directories - directories being removed are cleaned
- along with contents instead (Firefox, IE)*/
- pending();
- }
- var dirName = "ent y.rm.dir.not.empty",
- fileName = "re ove.txt",
- fullPath = joinURL(root.fullPath, dirName);
- // create a new directory entry
- createDirectory(dirName, function (entry) {
- entry.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- entry.remove(succeed.bind(null, done, 'entry.remove - Unexpected success callback, it should not remove a directory that contains files : ' + dirName), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- root.getDirectory(dirName, null, function (entry) {
- expect(entry).toBeDefined();
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- // cleanup
- deleteEntry(dirName, done);
- }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + dirName));
- });
- }, failed.bind(null, done, 'entry.getFile - Error creating file : ' + fileName + ' inside of ' + dirName));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
- });
- it("file.spec.56 remove on root file system", function (done) {
- // remove entry that doesn't exist
- root.remove(succeed.bind(null, done, 'entry.remove - Unexpected success callback, it should not remove entry that it does not exists'), function (error) {
- expect(error).toBeDefined();
- if (isChrome) {
- /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of
- NO_MODIFICATION_ALLOWED_ERR(code: 6) on trying to call removeRecursively
- on the root file system.*/
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- } else {
- expect(error).toBeFileError(FileError.NO_MODIFICATION_ALLOWED_ERR);
- }
- done();
- });
- });
- it("file.spec.57 copyTo: file", function (done) {
- var file1 = "entry copy.file1",
- file2 = "entry copy.file2",
- fullPath = joinURL(root.fullPath, file2);
- // create a new file entry to kick off it
- deleteEntry(file2, function () {
- createFile(file1, function (fileEntry) {
- // copy file1 to file2
- fileEntry.copyTo(root, file2, function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- expect(entry.name).toCanonicallyMatch(file2);
- root.getFile(file2, {
- create : false
- }, function (entry2) {
- expect(entry2).toBeDefined();
- expect(entry2.isFile).toBe(true);
- expect(entry2.isDirectory).toBe(false);
- expect(entry2.fullPath).toCanonicallyMatch(fullPath);
- expect(entry2.name).toCanonicallyMatch(file2);
- // cleanup
- deleteEntry(file1, function () {
- deleteEntry(file2, done);
- });
- }, failed.bind(null, done, 'root.getFile - Error getting copied file : ' + file2));
- }, failed.bind(null, done, 'fileEntry.copyTo - Error copying file : ' + file2));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'deleteEntry - Error removing file : ' + file2));
- });
- it("file.spec.58 copyTo: file onto itself", function (done) {
- var file1 = "entry.copy.fos.file1";
- // create a new file entry to kick off it
- createFile(file1, function (entry) {
- // copy file1 onto itself
- entry.copyTo(root, null, succeed.bind(null, done, 'entry.copyTo - Unexpected success callback, it should not copy a null file'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- // cleanup
- deleteEntry(file1, done);
- });
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- });
- it("file.spec.59 copyTo: directory", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.copy.srcDir",
- dstDir = "entry.copy.dstDir",
- dstPath = joinURL(root.fullPath, dstDir),
- filePath = joinURL(dstPath, file1);
- // create a new directory entry to kick off it
- deleteEntry(dstDir, function () {
- createDirectory(srcDir, function (directory) {
- // create a file within new directory
- directory.getFile(file1, {
- create : true
- }, function () {
- directory.copyTo(root, dstDir, function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.fullPath).toCanonicallyMatch(dstPath);
- expect(directory.name).toCanonicallyMatch(dstDir);
- root.getDirectory(dstDir, {
- create : false
- }, function (dirEntry) {
- expect(dirEntry).toBeDefined();
- expect(dirEntry.isFile).toBe(false);
- expect(dirEntry.isDirectory).toBe(true);
- expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
- expect(dirEntry.name).toCanonicallyMatch(dstDir);
- dirEntry.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(true);
- expect(fileEntry.isDirectory).toBe(false);
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- expect(fileEntry.name).toCanonicallyMatch(file1);
- // cleanup
- deleteEntry(srcDir, function () {
- deleteEntry(dstDir, done);
- });
- }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
- }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory : ' + dstDir));
- }, failed.bind(null, done, 'directory.copyTo - Error copying directory : ' + srcDir + ' to :' + dstDir));
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.60 copyTo: directory to backup at same root directory", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.copy srcDirSame",
- dstDir = "entry.copy srcDirSame-backup",
- dstPath = joinURL(root.fullPath, dstDir),
- filePath = joinURL(dstPath, file1);
- // create a new directory entry to kick off it
- deleteEntry(dstDir, function () {
- createDirectory(srcDir, function (directory) {
- directory.getFile(file1, {
- create : true
- }, function () {
- directory.copyTo(root, dstDir, function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.fullPath).toCanonicallyMatch(dstPath);
- expect(directory.name).toCanonicallyMatch(dstDir);
- root.getDirectory(dstDir, {
- create : false
- }, function (dirEntry) {
- expect(dirEntry).toBeDefined();
- expect(dirEntry.isFile).toBe(false);
- expect(dirEntry.isDirectory).toBe(true);
- expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
- expect(dirEntry.name).toCanonicallyMatch(dstDir);
- dirEntry.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(true);
- expect(fileEntry.isDirectory).toBe(false);
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- expect(fileEntry.name).toCanonicallyMatch(file1);
- // cleanup
- deleteEntry(srcDir, function () {
- deleteEntry(dstDir, done);
- });
- }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
- }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory : ' + dstDir));
- }, failed.bind(null, done, 'directory.copyTo - Error copying directory : ' + srcDir + ' to :' + dstDir));
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.61 copyTo: directory onto itself", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.copy.dos.srcDir",
- srcPath = joinURL(root.fullPath, srcDir),
- filePath = joinURL(srcPath, file1);
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // create a file within new directory
- directory.getFile(file1, {
- create : true
- }, function (fileEntry) {
- // copy srcDir onto itself
- directory.copyTo(root, null, succeed.bind(null, done, 'directory.copyTo - Unexpected success callback, it should not copy file: ' + srcDir + ' to a null destination'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- root.getDirectory(srcDir, {
- create : false
- }, function (dirEntry) {
- expect(dirEntry).toBeDefined();
- expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
- dirEntry.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(srcDir, done);
- }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
- }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
- });
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- });
- it("file.spec.62 copyTo: directory into itself", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var srcDir = "entry.copy.dis.srcDir",
- dstDir = "entry.copy.dis.dstDir",
- srcPath = joinURL(root.fullPath, srcDir);
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // copy source directory into itself
- directory.copyTo(directory, dstDir, succeed.bind(null, done, 'directory.copyTo - Unexpected success callback, it should not copy a directory ' + srcDir + ' into itself'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- root.getDirectory(srcDir, {
- create : false
- }, function (dirEntry) {
- // returning confirms existence so just check fullPath entry
- expect(dirEntry).toBeDefined();
- expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
- // cleanup
- deleteEntry(srcDir, done);
- }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
- });
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- });
- it("file.spec.63 copyTo: directory that does not exist", function (done) {
- var file1 = "entry.copy.dnf.file1",
- dirName = 'dir-foo';
- createFile(file1, function (fileEntry) {
- createDirectory(dirName, function (dirEntry) {
- dirEntry.remove(function () {
- fileEntry.copyTo(dirEntry, null, succeed.bind(null, done, 'fileEntry.copyTo - Unexpected success callback, it should not copy a file ' + file1 + ' into a removed directory'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- });
- }, failed.bind(null, done, 'dirEntry.remove - Error removing directory : ' + dirName));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- });
- it("file.spec.64 copyTo: invalid target name", function (done) {
- if (isBrowser) {
- /*The plugin does not follow ["8.3 Naming restrictions"]
- (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions*/
- pending();
- }
- var file1 = "entry.copy.itn.file1",
- file2 = "bad:file:name";
- // create a new file entry
- createFile(file1, function (entry) {
- // copy file1 to file2
- entry.copyTo(root, file2, succeed.bind(null, done, 'entry.copyTo - Unexpected success callback, it should not copy a file ' + file1 + ' to an invalid file name: ' + file2), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.ENCODING_ERR);
- // cleanup
- deleteEntry(file1, done);
- });
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- });
- it("file.spec.65 moveTo: file to same parent", function (done) {
- var file1 = "entry.move.fsp.file1",
- file2 = "entry.move.fsp.file2",
- dstPath = joinURL(root.fullPath, file2);
- // create a new file entry to kick off it
- createFile(file1, function (entry) {
- // move file1 to file2
- entry.moveTo(root, file2, function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.fullPath).toCanonicallyMatch(dstPath);
- expect(entry.name).toCanonicallyMatch(file2);
- root.getFile(file2, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(dstPath);
- root.getFile(file1, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
- //expect(navigator.fileMgr.testFileExists(srcPath) === false, "original file should not exist.");
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(file1, function () {
- deleteEntry(file2, done);
- });
- });
- }, failed.bind(null, done, 'root.getFile - Error getting file : ' + file2));
- }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to root as: ' + file2));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- });
- it("file.spec.66 moveTo: file to new parent", function (done) {
- var file1 = "entry.move.fnp.file1",
- dir = "entry.move.fnp.dir",
- dstPath = joinURL(joinURL(root.fullPath, dir), file1);
- // ensure destination directory is cleaned up first
- deleteEntry(dir, function () {
- // create a new file entry to kick off it
- createFile(file1, function (entry) {
- // create a parent directory to move file to
- root.getDirectory(dir, {
- create : true
- }, function (directory) {
- // move file1 to new directory
- // move the file
- entry.moveTo(directory, null, function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.fullPath).toCanonicallyMatch(dstPath);
- expect(entry.name).toCanonicallyMatch(file1);
- // test the moved file exists
- directory.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(dstPath);
- root.getFile(file1, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(file1, function () {
- deleteEntry(dir, done);
- });
- });
- }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + dir));
- }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to: ' + dir + ' with the same name'));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dir));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dir));
- });
- it("file.spec.67 moveTo: directory to same parent", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.move.dsp.srcDir",
- dstDir = "entry.move.dsp.dstDir",
- dstPath = joinURL(root.fullPath, dstDir),
- filePath = joinURL(dstPath, file1);
- // ensure destination directory is cleaned up before it
- deleteEntry(dstDir, function () {
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // create a file within directory
- directory.getFile(file1, {
- create : true
- }, function () {
- // move srcDir to dstDir
- directory.moveTo(root, dstDir, function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.fullPath).toCanonicallyMatch(dstPath);
- expect(directory.name).toCanonicallyMatch(dstDir);
- // test that moved file exists in destination dir
- directory.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // check that the moved file no longer exists in original dir
- root.getFile(file1, {
- create : false
- }, succeed.bind(null, done, 'directory.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(srcDir, function() {
- deleteEntry(dstDir, done);
- });
- });
- }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
- }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.68 moveTo: directory to same parent with same name", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.move.dsp.srcDir",
- dstDir = "entry.move.dsp.srcDir-backup",
- dstPath = joinURL(root.fullPath, dstDir),
- filePath = joinURL(dstPath, file1);
- // ensure destination directory is cleaned up before it
- deleteEntry(dstDir, function () {
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // create a file within directory
- directory.getFile(file1, {
- create : true
- }, function () {
- // move srcDir to dstDir
- directory.moveTo(root, dstDir, function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.fullPath).toCanonicallyMatch(dstPath);
- expect(directory.name).toCanonicallyMatch(dstDir);
- // check that moved file exists in destination dir
- directory.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // check that the moved file no longer exists in original dir
- root.getFile(file1, {
- create : false
- }, succeed.bind(null, done, 'directory.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(srcDir, function() {
- deleteEntry(dstDir, done);
- });
- });
- }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
- }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.69 moveTo: directory to new parent", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.move.dnp.srcDir",
- dstDir = "entry.move.dnp.dstDir",
- dstPath = joinURL(root.fullPath, dstDir),
- filePath = joinURL(dstPath, file1);
- // ensure destination directory is cleaned up before it
- deleteEntry(dstDir, function () {
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // create a file within directory
- directory.getFile(file1, {
- create : true
- }, function () {
- // move srcDir to dstDir
- directory.moveTo(root, dstDir, function (dirEntry) {
- expect(dirEntry).toBeDefined();
- expect(dirEntry.isFile).toBe(false);
- expect(dirEntry.isDirectory).toBe(true);
- expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
- expect(dirEntry.name).toCanonicallyMatch(dstDir);
- // test that moved file exists in destination dir
- dirEntry.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // test that the moved file no longer exists in original dir
- root.getFile(file1, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(srcDir, function() {
- deleteEntry(dstDir, done);
- });
- });
- }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + dstDir));
- }, failed.bind(null, done, 'directory.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.70 moveTo: directory onto itself", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.move.dos.srcDir",
- srcPath = joinURL(root.fullPath, srcDir),
- filePath = joinURL(srcPath, file1);
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // create a file within new directory
- directory.getFile(file1, {
- create : true
- }, function () {
- // move srcDir onto itself
- directory.moveTo(root, null, succeed.bind(null, done, 'directory.moveTo - Unexpected success callback, it should not move directory to invalid path'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- // test that original dir still exists
- root.getDirectory(srcDir, {
- create : false
- }, function (dirEntry) {
- // returning confirms existence so just check fullPath entry
- expect(dirEntry).toBeDefined();
- expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
- dirEntry.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(srcDir, done);
- }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
- }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
- });
- }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- });
- it("file.spec.71 moveTo: directory into itself", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var srcDir = "entry.move.dis.srcDir",
- dstDir = "entry.move.dis.dstDir",
- srcPath = joinURL(root.fullPath, srcDir);
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // move source directory into itself
- directory.moveTo(directory, dstDir, succeed.bind(null, done, 'directory.moveTo - Unexpected success callback, it should not move a directory into itself: ' + srcDir), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- // make sure original directory still exists
- root.getDirectory(srcDir, {
- create : false
- }, function (entry) {
- expect(entry).toBeDefined();
- expect(entry.fullPath).toCanonicallyMatch(srcPath);
- // cleanup
- deleteEntry(srcDir, done);
- }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
- });
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- });
- it("file.spec.130 moveTo: directory into similar directory", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var srcDir = "entry.move.dis.srcDir",
- dstDir = "entry.move.dis.srcDir-backup";
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (srcDirEntry) {
- deleteEntry(dstDir, function () {
- createDirectory(dstDir, function (dstDirEntry) {
- // move source directory into itself
- srcDirEntry.moveTo(dstDirEntry, 'file', function (newDirEntry) {
- expect(newDirEntry).toBeDefined();
- deleteEntry(dstDir, done);
- }, failed.bind(null, done, 'directory.moveTo - Error moving a directory into a similarly-named directory: ' + srcDir));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dstDir));
- }, failed.bind(null, done, 'deleteEntry - Error deleting directory : ' + dstDir));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- });
- it("file.spec.72 moveTo: file onto itself", function (done) {
- var file1 = "entry.move.fos.file1",
- filePath = joinURL(root.fullPath, file1);
- // create a new file entry to kick off it
- createFile(file1, function (entry) {
- // move file1 onto itself
- entry.moveTo(root, null, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not move a file: ' + file1 + ' into the same parent'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- //test that original file still exists
- root.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(file1, done);
- }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
- });
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- });
- it("file.spec.73 moveTo: file onto existing directory", function (done) {
- var file1 = "entry.move.fod.file1",
- dstDir = "entry.move.fod.dstDir",
- subDir = "subDir",
- dirPath = joinURL(joinURL(root.fullPath, dstDir), subDir),
- filePath = joinURL(root.fullPath, file1);
- // ensure destination directory is cleaned up before it
- deleteEntry(dstDir, function () {
- // create a new file entry to kick off it
- createFile(file1, function (entry) {
- // create top level directory
- root.getDirectory(dstDir, {
- create : true
- }, function (directory) {
- // create sub-directory
- directory.getDirectory(subDir, {
- create : true
- }, function (subDirectory) {
- // move file1 onto sub-directory
- entry.moveTo(directory, subDir, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not move a file: ' + file1 + ' into directory: ' + dstDir + '\n' + subDir + ' directory already exists'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- // check that original dir still exists
- directory.getDirectory(subDir, {
- create : false
- }, function (dirEntry) {
- expect(dirEntry).toBeDefined();
- expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
- // check that original file still exists
- root.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(file1, function () {
- deleteEntry(dstDir, done);
- });
- }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
- }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + subDir));
- });
- }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dstDir));
- }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.74 moveTo: directory onto existing file", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "entry.move.dof.file1",
- srcDir = "entry.move.dof.srcDir",
- dirPath = joinURL(root.fullPath, srcDir),
- filePath = joinURL(root.fullPath, file1);
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (entry) {
- // create file
- root.getFile(file1, {
- create : true
- }, function (fileEntry) {
- // move directory onto file
- entry.moveTo(root, file1, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not move : \n' + srcDir + ' into root directory renamed as ' + file1 + '\n' + file1 + ' file already exists'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- // test that original directory exists
- root.getDirectory(srcDir, {
- create : false
- }, function (dirEntry) {
- // returning confirms existence so just check fullPath entry
- expect(dirEntry).toBeDefined();
- expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
- // test that original file exists
- root.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(file1, function () {
- deleteEntry(srcDir, done);
- });
- }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
- }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
- });
- }, failed.bind(null, done, 'root.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- });
- it("file.spec.75 copyTo: directory onto existing file", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "entry.copy.dof.file1",
- srcDir = "entry.copy.dof.srcDir",
- dirPath = joinURL(root.fullPath, srcDir),
- filePath = joinURL(root.fullPath, file1);
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (entry) {
- // create file
- root.getFile(file1, {
- create : true
- }, function () {
- // copy directory onto file
- entry.copyTo(root, file1, succeed.bind(null, done, 'entry.copyTo - Unexpected success callback, it should not copy : \n' + srcDir + ' into root directory renamed as ' + file1 + '\n' + file1 + ' file already exists'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- //check that original dir still exists
- root.getDirectory(srcDir, {
- create : false
- }, function (dirEntry) {
- // returning confirms existence so just check fullPath entry
- expect(dirEntry).toBeDefined();
- expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
- // test that original file still exists
- root.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // cleanup
- deleteEntry(file1, function () {
- deleteEntry(srcDir, done);
- });
- }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
- }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
- });
- }, failed.bind(null, done, 'root.getFile - Error creating file : ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- });
- it("file.spec.76 moveTo: directory onto directory that is not empty", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var srcDir = "entry.move.dod.srcDir",
- dstDir = "entry.move.dod.dstDir",
- subDir = "subDir",
- srcPath = joinURL(root.fullPath, srcDir),
- dstPath = joinURL(joinURL(root.fullPath, dstDir), subDir);
- // ensure destination directory is cleaned up before it
- deleteEntry(dstDir, function () {
- // create a new file entry to kick off it
- createDirectory(srcDir, function (entry) {
- // create top level directory
- root.getDirectory(dstDir, {
- create : true
- }, function (directory) {
- // create sub-directory
- directory.getDirectory(subDir, {
- create : true
- }, function () {
- // move srcDir onto dstDir (not empty)
- entry.moveTo(root, dstDir, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not copy : \n' + srcDir + ' into root directory renamed as ' + dstDir + '\n' + dstDir + ' directory already exists'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- // making sure destination directory still exists
- directory.getDirectory(subDir, {
- create : false
- }, function (dirEntry) {
- // returning confirms existence so just check fullPath entry
- expect(dirEntry).toBeDefined();
- expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
- // making sure source directory exists
- root.getDirectory(srcDir, {
- create : false
- }, function (srcEntry) {
- expect(srcEntry).toBeDefined();
- expect(srcEntry.fullPath).toCanonicallyMatch(srcPath);
- // cleanup
- deleteEntry(srcDir, function () {
- deleteEntry(dstDir, done);
- });
- }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
- }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + subDir));
- });
- }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
- }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
- }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.77 moveTo: file replace existing file", function (done) {
- var file1 = "entry.move.frf.file1",
- file2 = "entry.move.frf.file2",
- file2Path = joinURL(root.fullPath, file2);
- // create a new directory entry to kick off it
- createFile(file1, function (entry) {
- // create file
- root.getFile(file2, {
- create : true
- }, function () {
- // replace file2 with file1
- entry.moveTo(root, file2, function (entry2) {
- expect(entry2).toBeDefined();
- expect(entry2.isFile).toBe(true);
- expect(entry2.isDirectory).toBe(false);
- expect(entry2.fullPath).toCanonicallyMatch(file2Path);
- expect(entry2.name).toCanonicallyMatch(file2);
- // old file should not exists
- root.getFile(file1, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, file: ' + file1 + ' should not exists'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // test that new file exists
- root.getFile(file2, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(file2Path);
- // cleanup
- deleteEntry(file1, function () {
- deleteEntry(file2, done);
- });
- }, failed.bind(null, done, 'root.getFile - Error getting moved file: ' + file2));
- });
- }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to root as: ' + file2));
- }, failed.bind(null, done, 'root.getFile - Error creating file: ' + file2));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
- });
- it("file.spec.78 moveTo: directory replace empty directory", function (done) {
- if (isIndexedDBShim) {
- /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
- pending();
- }
- var file1 = "file1",
- srcDir = "entry.move.drd.srcDir",
- dstDir = "entry.move.drd.dstDir",
- dstPath = joinURL(root.fullPath, dstDir),
- filePath = dstPath + '/' + file1;
- // ensure destination directory is cleaned up before it
- deleteEntry(dstDir, function () {
- // create a new directory entry to kick off it
- createDirectory(srcDir, function (directory) {
- // create a file within source directory
- directory.getFile(file1, {
- create : true
- }, function () {
- // create destination directory
- root.getDirectory(dstDir, {
- create : true
- }, function () {
- // move srcDir to dstDir
- directory.moveTo(root, dstDir, function (dirEntry) {
- expect(dirEntry).toBeDefined();
- expect(dirEntry.isFile).toBe(false);
- expect(dirEntry.isDirectory).toBe(true);
- expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
- expect(dirEntry.name).toCanonicallyMatch(dstDir);
- // check that old directory contents have been moved
- dirEntry.getFile(file1, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
- // check that old directory no longer exists
- root.getDirectory(srcDir, {
- create : false
- }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory: ' + srcDir + ' should not exists'), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- // cleanup
- deleteEntry(srcDir, function () {
- deleteEntry(dstDir, done);
- });
- });
- }, failed.bind(null, done, 'dirEntry.getFile - Error getting moved file: ' + file1));
- }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory: ' + dstDir));
- }, failed.bind(null, done, 'root.getFile - Error creating file: ' + file1));
- }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + srcDir));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- });
- it("file.spec.79 moveTo: directory that does not exist", function (done) {
- var file1 = "entry.move.dnf.file1",
- dstDir = "entry.move.dnf.dstDir",
- dstPath = joinURL(root.fullPath, dstDir);
- // create a new file entry to kick off it
- createFile(file1, function (entry) {
- // move file to directory that does not exist
- var directory = new DirectoryEntry();
- directory.filesystem = root.filesystem;
- directory.fullPath = dstPath;
- entry.moveTo(directory, null, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, parent directory: ' + dstPath + ' should not exists'), function (error) {
- expect(error).toBeDefined();
- if (isChrome) {
- /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of NOT_FOUND_ERR(code: 1)
- on trying to moveTo directory that does not exist.*/
- expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
- } else {
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- }
- // cleanup
- deleteEntry(file1, done);
- });
- }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
- });
- it("file.spec.80 moveTo: invalid target name", function (done) {
- if (isBrowser) {
- /*The plugin does not follow ["8.3 Naming restrictions"]
- (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions*/
- pending();
- }
- var file1 = "entry.move.itn.file1",
- file2 = "bad:file:name";
- // create a new file entry to kick off it
- createFile(file1, function (entry) {
- // move file1 to file2
- entry.moveTo(root, file2, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, : ' + file1 + ' to root as: ' + file2), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.ENCODING_ERR);
- // cleanup
- deleteEntry(file1, done);
- });
- }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
- });
- });
- //Entry
- describe('FileReader', function () {
- it("file.spec.81 should have correct methods", function () {
- var reader = new FileReader();
- expect(reader).toBeDefined();
- expect(typeof reader.readAsBinaryString).toBe('function');
- expect(typeof reader.readAsDataURL).toBe('function');
- expect(typeof reader.readAsText).toBe('function');
- expect(typeof reader.readAsArrayBuffer).toBe('function');
- expect(typeof reader.abort).toBe('function');
- expect(reader.result).toBe(null);
- });
- });
- //FileReader
- describe('Read method', function () {
- it("file.spec.82 should error out on non-existent file", function (done) {
- var fileName = cordova.platformId === 'windowsphone' ? root.toURL() + "/" + "somefile.txt" : "somefile.txt",
- verifier = function (evt) {
- expect(evt).toBeDefined();
- expect(evt.target.error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- };
- root.getFile(fileName, {
- create : true
- }, function (entry) {
- entry.file(function (file) {
- deleteEntry(fileName, function () {
- //Create FileReader
- var reader = new FileReader();
- reader.onerror = verifier;
- reader.onload = succeed.bind(null, done, 'reader.onload - Unexpected success callback, file: ' + fileName + ' it should not exists');
- reader.readAsText(file);
- }, failed.bind(null, done, 'deleteEntry - Error removing file: ' + fileName));
- }, failed.bind(null, done, 'entry.file - Error reading file: ' + fileName));
- }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
- });
- it("file.spec.83 should be able to read native blob objects", function (done) {
- // Skip test if blobs are not supported (e.g.: Android 2.3).
- if (typeof window.Blob == 'undefined' || typeof window.Uint8Array == 'undefined') {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- }
- var contents = 'asdf';
- var uint8Array = new Uint8Array(contents.length);
- for (var i = 0; i < contents.length; ++i) {
- uint8Array[i] = contents.charCodeAt(i);
- }
- var Builder = window.BlobBuilder || window.WebKitBlobBuilder;
- var blob;
- if (Builder) {
- var builder = new Builder();
- builder.append(uint8Array.buffer);
- builder.append(contents);
- blob = builder.getBlob("text/plain");
- } else {
- try {
- // iOS 6 does not support Views, so pass in the buffer.
- blob = new Blob([uint8Array.buffer, contents]);
- } catch (e) {
- // Skip the test if we can't create a blob (e.g.: iOS 5).
- if (e instanceof TypeError) {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- }
- throw e;
- }
- }
- var verifier = function (evt) {
- expect(evt).toBeDefined();
- expect(evt.target.result).toBe('asdfasdf');
- done();
- };
- var reader = new FileReader();
- reader.onloadend = verifier;
- reader.readAsText(blob);
- });
- function writeDummyFile(writeBinary, callback, done, fileContents) {
- var fileName = "dummy.txt",
- fileEntry = null,
- // use default string if file data is not provided
- fileData = fileContents !== undefined ? fileContents :
- '\u20AC\xEB - There is an exception to every rule. Except this one.',
- fileDataAsBinaryString = fileContents !== undefined ? fileContents :
- '\xe2\x82\xac\xc3\xab - There is an exception to every rule. Except this one.';
- function createWriter(fe) {
- fileEntry = fe;
- fileEntry.createWriter(writeFile, failed.bind(null, done, 'fileEntry.createWriter - Error reading file: ' + fileName));
- }
- // writes file and reads it back in
- function writeFile(writer) {
- writer.onwriteend = function () {
- fileEntry.file(function (f) {
- callback(fileEntry, f, fileData, fileDataAsBinaryString);
- }, failed.bind(null, done, 'writer.onwriteend - Error writing data on file: ' + fileName));
- };
- writer.write(fileData);
- }
- fileData += writeBinary ? 'bin:\x01\x00' : '';
- fileDataAsBinaryString += writeBinary ? 'bin:\x01\x00' : '';
- // create a file, write to it, and read it in again
- createFile(fileName, createWriter, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- }
- function runReaderTest(funcName, writeBinary, done, progressFunc, verifierFunc, sliceStart, sliceEnd, fileContents) {
- writeDummyFile(writeBinary, function (fileEntry, file, fileData, fileDataAsBinaryString) {
- var verifier = function (evt) {
- expect(evt).toBeDefined();
- verifierFunc(evt, fileData, fileDataAsBinaryString);
- };
- var reader = new FileReader();
- reader.onprogress = progressFunc;
- reader.onload = verifier;
- reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + file + ' using function: ' + funcName);
- if (sliceEnd !== undefined) {
- // 'type' is specified so that is will be preserved in the resulting file:
- // http://www.w3.org/TR/FileAPI/#slice-method-algo -> "6.4.1. The slice method" -> 4. A), 6. c)
- file = file.slice(sliceStart, sliceEnd, file.type);
- } else if (sliceStart !== undefined) {
- file = file.slice(sliceStart, file.size, file.type);
- }
- reader[funcName](file);
- }, done, fileContents);
- }
- function arrayBufferEqualsString(ab, str) {
- var buf = new Uint8Array(ab);
- var match = buf.length == str.length;
- for (var i = 0; match && i < buf.length; i++) {
- match = buf[i] == str.charCodeAt(i);
- }
- return match;
- }
- it("file.spec.84 should read file properly, readAsText", function (done) {
- runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toBe(fileData);
- done();
- });
- });
- it("file.spec.84.1 should read JSON file properly, readAsText", function (done) {
- var testObject = {key1: "value1", key2: 2};
- runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toEqual(JSON.stringify(testObject));
- done();
- }, undefined, undefined, JSON.stringify(testObject));
- });
- it("file.spec.85 should read file properly, Data URI", function (done) {
- runReaderTest('readAsDataURL', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
- /* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,
- mediatype in IE is always empty (which is the same as `text-plain` according the specification),
- the mediatype in Firefox is always `application/octet-stream`.
- For example, if the content is `abcdefg` then Firefox returns `data:application/octet-stream;base64,YWJjZGVmZw==`,
- IE returns `data:;base64,YWJjZGVmZw==`, Chrome returns `data:<mediatype depending on extension of entry name>;base64,YWJjZGVmZw==`. */
- expect(evt.target.result).toBeDataUrl();
- //The atob function it is completely ignored during mobilespec execution, besides the returned object: evt
- //it is encoded and the atob function is aimed to decode a string. Even with btoa (encode) the function it gets stucked
- //because of the Unicode characters that contains the fileData object.
- //Issue reported at JIRA with all the details: CB-7095
- //expect(evt.target.result.slice(23)).toBe(atob(fileData));
- done();
- });
- });
- it("file.spec.86 should read file properly, readAsBinaryString", function (done) {
- if (isIE) {
- /*`readAsBinaryString` function is not supported by IE and has not the stub.*/
- pending();
- }
- runReaderTest('readAsBinaryString', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toBe(fileDataAsBinaryString);
- done();
- });
- });
- it("file.spec.87 should read file properly, readAsArrayBuffer", function (done) {
- // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
- if (typeof window.ArrayBuffer == 'undefined') {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- }
- runReaderTest('readAsArrayBuffer', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString)).toBe(true);
- done();
- });
- });
- it("file.spec.88 should read sliced file: readAsText", function (done) {
- runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toBe(fileDataAsBinaryString.slice(10, 40));
- done();
- }, 10, 40);
- });
- it("file.spec.89 should read sliced file: slice past eof", function (done) {
- runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toBe(fileData.slice(-5, 9999));
- done();
- }, -5, 9999);
- });
- it("file.spec.90 should read sliced file: slice to eof", function (done) {
- runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toBe(fileData.slice(-5));
- done();
- }, -5);
- });
- it("file.spec.91 should read empty slice", function (done) {
- runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toBe('');
- done();
- }, 0, 0);
- });
- it("file.spec.92 should read sliced file properly, readAsDataURL", function (done) {
- runReaderTest('readAsDataURL', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
- /* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,
- mediatype in IE is always empty (which is the same as `text-plain` according the specification),
- the mediatype in Firefox is always `application/octet-stream`.
- For example, if the content is `abcdefg` then Firefox returns `data:application/octet-stream;base64,YWJjZGVmZw==`,
- IE returns `data:;base64,YWJjZGVmZw==`, Chrome returns `data:<mediatype depending on extension of entry name>;base64,YWJjZGVmZw==`. */
- expect(evt.target.result).toBeDataUrl();
- //The atob function it is completely ignored during mobilespec execution, besides the returned object: evt
- //it is encoded and the atob function is aimed to decode a string. Even with btoa (encode) the function it gets stucked
- //because of the Unicode characters that contains the fileData object.
- //Issue reported at JIRA with all the details: CB-7095
- //expect(evt.target.result.slice(23)).toBe(atob(fileDataAsBinaryString.slice(10, -3)));
- done();
- }, 10, -3);
- });
- it("file.spec.93 should read sliced file properly, readAsBinaryString", function (done) {
- if (isIE) {
- /*`readAsBinaryString` function is not supported by IE and has not the stub.*/
- pending();
- }
- runReaderTest('readAsBinaryString', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(evt.target.result).toBe(fileDataAsBinaryString.slice(-10, -5));
- done();
- }, -10, -5);
- });
- it("file.spec.94 should read sliced file properly, readAsArrayBuffer", function (done) {
- // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
- if (typeof window.ArrayBuffer == 'undefined') {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- }
- runReaderTest('readAsArrayBuffer', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
- expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString.slice(0, -1))).toBe(true);
- done();
- }, 0, -1);
- });
- it("file.spec.94.5 should read large file in multiple chunks, readAsArrayBuffer", function (done) {
- // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
- if (typeof window.ArrayBuffer == 'undefined') {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- }
- var largeText = "";
- for (var i = 0; i < 1000; i++) {
- largeText += "Test " + i + "\n";
- }
- // Set the chunk size so that the read will take 5 chunks
- FileReader.READ_CHUNK_SIZE = Math.floor(largeText.length / 4) + 1;
- var chunkCount = 0;
- var lastProgressValue = -1;
- var progressFunc = function (evt) {
- expect(evt.loaded).toBeDefined();
- expect(evt.total).toBeDefined();
- expect(evt.total >= largeText.length).toBe(true);
- expect(evt.total <= largeText.length + 5).toBe(true);
- expect(evt.loaded > lastProgressValue).toBe(true);
- expect(evt.loaded <= evt.total).toBe(true);
- lastProgressValue = evt.loaded;
- chunkCount++;
- };
- runReaderTest(
- 'readAsArrayBuffer', true, done, progressFunc,
- function (evt, fileData, fileDataAsBinaryString) {
- expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString.slice(0, -1))).toBe(true);
- expect(lastProgressValue >= largeText.length).toBe(true);
- expect(lastProgressValue <= largeText.length + 5).toBe(true);
- expect(chunkCount).toBe(5);
- done();
- },
- 0, -1, largeText);
- });
- });
- //Read method
- describe('FileWriter', function () {
- it("file.spec.95 should have correct methods", function (done) {
- // retrieve a FileWriter object
- var fileName = "writer.methods";
- // FileWriter
- root.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- expect(writer).toBeDefined();
- expect(typeof writer.write).toBe('function');
- expect(typeof writer.seek).toBe('function');
- expect(typeof writer.truncate).toBe('function');
- expect(typeof writer.abort).toBe('function');
- // cleanup
- deleteFile(fileName, done);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
- });
- it("file.spec.96 should be able to write and append to file, createWriter", function (done) {
- var fileName = "writer.append.createWriter", // file content
- content = "There is an exception to every rule.", // for checkin file length
- exception = " Except this one.",
- length = content.length;
- // create file, then write and append to it
- createFile(fileName, function (fileEntry) {
- // writes initial file content
- fileEntry.createWriter(function (writer) {
- //Verifiers declaration
- function verifier(evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // Append some more data
- writer.onwriteend = secondVerifier;
- length += exception.length;
- writer.seek(writer.length);
- writer.write(exception);
- }
- function secondVerifier(evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- var reader = new FileReader();
- reader.onloadend = thirdVerifier;
- reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
- fileEntry.file(function(f){reader.readAsText(f);});
- }
- function thirdVerifier(evt) {
- expect(evt.target.result).toBe(content+exception);
- // cleanup
- deleteFile(fileName, done);
- }
- //Write process
- writer.onwriteend = verifier;
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.97 should be able to write and append to file, File object", function (done) {
- var fileName = "writer.append.File", // file content
- content = "There is an exception to every rule.", // for checkin file length
- exception = " Except this one.",
- length = content.length;
- root.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- //Verifiers declaration
- function verifier() {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // Append some more data
- writer.onwriteend = secondVerifier;
- length += exception.length;
- writer.seek(writer.length);
- writer.write(exception);
- }
- function secondVerifier() {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- var reader = new FileReader();
- reader.onloadend = thirdVerifier;
- reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
- fileEntry.file(function(f){reader.readAsText(f);});
- }
- function thirdVerifier(evt) {
- expect(evt.target.result).toBe(content+exception);
- // cleanup
- deleteFile(fileName, done);
- }
- //Write process
- writer.onwriteend = verifier;
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
- });
- it("file.spec.98 should be able to seek to the middle of the file and write more data than file.length", function (done) {
- var fileName = "writer.seek.write", // file content
- content = "This is our sentence.", // for checking file length
- exception = "newer sentence.",
- length = content.length;
- // create file, then write and append to it
- createFile(fileName, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- //Verifiers declaration
- function verifier(evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // Append some more data
- writer.onwriteend = secondVerifier;
- length = 12 + exception.length;
- writer.seek(12);
- writer.write(exception);
- }
- function secondVerifier(evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- var reader = new FileReader();
- reader.onloadend = thirdVerifier;
- reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
- fileEntry.file(function(f){reader.readAsText(f);});
- }
- function thirdVerifier(evt) {
- expect(evt.target.result).toBe(content.substr(0,12)+exception);
- // cleanup
- deleteFile(fileName, done);
- }
- //Write process
- writer.onwriteend = verifier;
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.99 should be able to seek to the middle of the file and write less data than file.length", function (done) {
- if (isChrome) {
- /* Chrome (re)writes as follows: "This is our sentence." -> "This is new.sentence.",
- i.e. the length is not being changed from content.length and writer length will be equal 21 */
- pending();
- }
- var fileName = "writer.seek.write2", // file content
- content = "This is our sentence.", // for checking file length
- exception = "new.",
- length = content.length;
- // create file, then write and append to it
- createFile(fileName, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- // Verifiers declaration
- function verifier(evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // Append some more data
- writer.onwriteend = secondVerifier;
- length = 8 + exception.length;
- writer.seek(8);
- writer.write(exception);
- }
- function secondVerifier(evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- var reader = new FileReader();
- reader.onloadend = thirdVerifier;
- reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
- fileEntry.file(function(f){reader.readAsText(f);});
- }
- function thirdVerifier(evt) {
- expect(evt.target.result).toBe(content.substr(0,8)+exception);
- // cleanup
- deleteFile(fileName, done);
- }
- //Write process
- writer.onwriteend = verifier;
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.100 should be able to write XML data", function (done) {
- var fileName = "writer.xml", // file content
- content = '<?xml version="1.0" encoding="UTF-8"?>\n<test prop="ack">\nData\n</test>\n', // for testing file length
- length = content.length;
- // creates file, then write XML data
- createFile(fileName, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- //Verifier content
- var verifier = function (evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // cleanup
- deleteFile(fileName, done);
- };
- //Write process
- writer.onwriteend = verifier;
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.101 should be able to write JSON data", function (done) {
- var fileName = "writer.json", // file content
- content = '{ "name": "Guy Incognito", "email": "here@there.com" }', // for testing file length
- length = content.length;
- // creates file, then write JSON content
- createFile(fileName, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- //Verifier declaration
- var verifier = function (evt) {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // cleanup
- deleteFile(fileName, done);
- };
- //Write process
- writer.onwriteend = verifier;
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.102 should be able to seek", function (done) {
- var fileName = "writer.seek", // file content
- content = "There is an exception to every rule. Except this one.", // for testing file length
- length = content.length;
- // creates file, then write JSON content
- createFile(fileName, function (fileEntry) {
- // writes file content and tests writer.seek
- fileEntry.createWriter(function (writer) {
- //Verifier declaration
- var verifier = function () {
- expect(writer.position).toBe(length);
- writer.seek(-5);
- expect(writer.position).toBe(length - 5);
- writer.seek(length + 100);
- expect(writer.position).toBe(length);
- writer.seek(10);
- expect(writer.position).toBe(10);
- // cleanup
- deleteFile(fileName, done);
- };
- //Write process
- writer.onwriteend = verifier;
- writer.seek(-100);
- expect(writer.position).toBe(0);
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.103 should be able to truncate", function (done) {
- if (isIndexedDBShim) {
- /* `abort` and `truncate` functions are not supported (Firefox, IE) */
- pending();
- }
- var fileName = "writer.truncate",
- content = "There is an exception to every rule. Except this one.";
- // creates file, writes to it, then truncates it
- createFile(fileName, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- // Verifier declaration
- var verifier = function () {
- expect(writer.length).toBe(36);
- expect(writer.position).toBe(36);
- // cleanup
- deleteFile(fileName, done);
- };
- //Write process
- writer.onwriteend = function () {
- //Truncate process after write
- writer.onwriteend = verifier;
- writer.truncate(36);
- };
- writer.write(content);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.104 should be able to write binary data from an ArrayBuffer", function (done) {
- // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
- if (typeof window.ArrayBuffer == 'undefined') {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- return;
- }
- var fileName = "bufferwriter.bin", // file content
- data = new ArrayBuffer(32),
- dataView = new Int8Array(data), // for verifying file length
- length = 32;
- for (var i = 0; i < dataView.length; i++) {
- dataView[i] = i;
- }
- // creates file, then write content
- createFile(fileName, function (fileEntry) {
- // writes file content
- fileEntry.createWriter(function (writer) {
- //Verifier declaration
- var verifier = function () {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // cleanup
- deleteFile(fileName, done);
- };
- //Write process
- writer.onwriteend = verifier;
- writer.write(data);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.105 should be able to write binary data from a Blob", function (done) {
- // Skip test if Blobs are not supported (e.g.: Android 2.3).
- if ((typeof window.Blob == 'undefined' && typeof window.WebKitBlobBuilder == 'undefined') || typeof window.ArrayBuffer == 'undefined') {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- return;
- }
- var fileName = "blobwriter.bin", // file content
- data = new ArrayBuffer(32),
- dataView = new Int8Array(data),
- blob, // for verifying file length
- length = 32;
- for (var i = 0; i < dataView.length; i++) {
- dataView[i] = i;
- }
- try {
- // Mobile Safari: Use Blob constructor
- blob = new Blob([data], {
- "type" : "application/octet-stream"
- });
- } catch (e) {
- if (window.WebKitBlobBuilder) {
- // Android Browser: Use deprecated BlobBuilder
- var builder = new WebKitBlobBuilder();
- builder.append(data);
- blob = builder.getBlob('application/octet-stream');
- } else {
- // We have no way defined to create a Blob, so fail
- fail();
- }
- }
- if (typeof blob !== 'undefined') {
- // creates file, then write content
- createFile(fileName, function (fileEntry) {
- fileEntry.createWriter(function (writer) {
- //Verifier declaration
- var verifier = function () {
- expect(writer.length).toBe(length);
- expect(writer.position).toBe(length);
- // cleanup
- deleteFile(fileName, done);
- };
- //Write process
- writer.onwriteend = verifier;
- writer.write(blob);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- }
- });
- it("file.spec.106 should be able to write a File to a FileWriter", function (done) {
- var dummyFileName = 'dummy.txt',
- outputFileName = 'verify.txt',
- dummyFileText = 'This text should be written to two files',
- verifier = function (outputFileWriter) {
- expect(outputFileWriter.length).toBe(dummyFileText.length);
- expect(outputFileWriter.position).toBe(dummyFileText.length);
- deleteFile(outputFileName, done);
- },
- writeFile = function (fileName, fileData, win) {
- var theWriter,
- write_file = function (fileEntry) {
- // writes file content to new file
- fileEntry.createWriter(function (writer) {
- theWriter = writer;
- writer.onwriteend = function (ev) {
- if (typeof fileData.length !== "undefined") {
- expect(theWriter.length).toBe(fileData.length);
- expect(theWriter.position).toBe(fileData.length);
- }
- win(theWriter);
- };
- writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
- writer.write(fileData);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- };
- createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- },
- openFile = function (fileName, callback) {
- root.getFile(fileName, {
- create : false
- }, function (fileEntry) {
- fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
- };
- writeFile(dummyFileName, dummyFileText, function (dummyFileWriter) {
- openFile(dummyFileName, function (file) {
- writeFile(outputFileName, file, verifier);
- });
- });
- });
- it("file.spec.107 should be able to write a sliced File to a FileWriter", function (done) {
- var dummyFileName = 'dummy2.txt',
- outputFileName = 'verify2.txt',
- dummyFileText = 'This text should be written to two files',
- verifier = function (outputFileWriter) {
- expect(outputFileWriter.length).toBe(10);
- expect(outputFileWriter.position).toBe(10);
- deleteFile(outputFileName, done);
- },
- writeFile = function (fileName, fileData, win) {
- var theWriter,
- write_file = function (fileEntry) {
- // writes file content to new file
- fileEntry.createWriter(function (writer) {
- theWriter = writer;
- writer.onwriteend = function (ev) {
- if (typeof fileData.length !== "undefined") {
- expect(theWriter.length).toBe(fileData.length);
- expect(theWriter.position).toBe(fileData.length);
- }
- win(theWriter);
- };
- writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
- writer.write(fileData);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- };
- createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- },
- openFile = function (fileName, callback) {
- root.getFile(fileName, {
- create : false
- }, function (fileEntry) {
- fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
- };
- writeFile(dummyFileName, dummyFileText, function (dummyFileWriter) {
- openFile(dummyFileName, function (file) {
- writeFile(outputFileName, file.slice(10, 20), verifier);
- });
- });
- });
- it("file.spec.108 should be able to write binary data from a File", function (done) {
- // Skip test if Blobs are not supported (e.g.: Android 2.3).
- if (typeof window.Blob == 'undefined' && typeof window.WebKitBlobBuilder == 'undefined') {
- expect(true).toFailWithMessage('Platform does not supported this feature');
- done();
- }
- var dummyFileName = "blobwriter.bin",
- outputFileName = 'verify.bin', // file content
- data = new ArrayBuffer(32),
- dataView = new Int8Array(data),
- blob, // for verifying file length
- length = 32,
- verifier = function (outputFileWriter) {
- expect(outputFileWriter.length).toBe(length);
- expect(outputFileWriter.position).toBe(length);
- // cleanup
- deleteFile(outputFileName);
- done();
- },
- writeFile = function (fileName, fileData, win) {
- var theWriter,
- write_file = function (fileEntry) {
- // writes file content to new file
- fileEntry.createWriter(function (writer) {
- theWriter = writer;
- writer.onwriteend = function (ev) {
- if (typeof fileData.length !== "undefined") {
- expect(theWriter.length).toBe(fileData.length);
- expect(theWriter.position).toBe(fileData.length);
- }
- win(theWriter);
- };
- writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
- writer.write(fileData);
- }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
- };
- createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- },
- openFile = function (fileName, callback) {
- root.getFile(fileName, {
- create : false
- }, function (fileEntry) {
- fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
- }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
- };
- for (var i = 0; i < dataView.length; i++) {
- dataView[i] = i;
- }
- try {
- // Mobile Safari: Use Blob constructor
- blob = new Blob([data], {
- "type" : "application/octet-stream"
- });
- } catch (e) {
- if (window.WebKitBlobBuilder) {
- // Android Browser: Use deprecated BlobBuilder
- var builder = new WebKitBlobBuilder();
- builder.append(data);
- blob = builder.getBlob('application/octet-stream');
- } else {
- // We have no way defined to create a Blob, so fail
- fail();
- }
- }
- if (typeof blob !== 'undefined') {
- // creates file, then write content
- writeFile(dummyFileName, blob, function (dummyFileWriter) {
- openFile(dummyFileName, function (file) {
- writeFile(outputFileName, file, verifier);
- });
- });
- }
- });
- });
- //FileWritter
- describe('Backwards compatibility', function () {
- /* These specs exist to test that the File plugin can still recognize file:///
- * URLs, and can resolve them to FileEntry and DirectoryEntry objects.
- * They rely on an undocumented interface to File which provides absolute file
- * paths, which are not used internally anymore.
- * If that interface is not present, then these tests will silently succeed.
- */
- it("file.spec.109 should be able to resolve a file:/// URL", function (done) {
- var localFilename = 'file.txt';
- var originalEntry;
- root.getFile(localFilename, {
- create : true
- }, function (entry) {
- originalEntry = entry;
- /* This is an undocumented interface to File which exists only for testing
- * backwards compatibilty. By obtaining the raw filesystem path of the download
- * location, we can pass that to ft.download() to make sure that previously-stored
- * paths are still valid.
- */
- cordova.exec(function (localPath) {
- window.resolveLocalFileSystemURL("file://" + encodeURI(localPath), function (fileEntry) {
- expect(fileEntry.toURL()).toEqual(originalEntry.toURL());
- // cleanup
- deleteFile(localFilename);
- done();
- }, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving URI: file://' + encodeURI(localPath)));
- }, done, 'File', '_getLocalFilesystemPath', [entry.toURL()]);
- }, failed.bind(null, done, 'root.getFile - Error creating file: ' + localFilename));
- });
- });
- //Backwards Compatibility
- describe('Parent References', function () {
- /* These specs verify that paths with parent references i("..") in them
- * work correctly, and do not cause the application to crash.
- */
- it("file.spec.110 should not throw exception resolving parent refefences", function (done) {
- /* This is a direct copy of file.spec.9, with the filename changed, * as reported in CB-5721.
- */
- var fileName = "resolve.file.uri";
- var dirName = "resolve.dir.uri";
- // create a new file entry
- createDirectory(dirName, function () {
- createFile(dirName+"/../" + fileName, function (entry) {
- // lookup file system entry
- window.resolveLocalFileSystemURL(entry.toURL(), function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.name).toCanonicallyMatch(fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving URI: ' + entry.toURL()));
- }, failed.bind(null, done, 'createFile - Error creating file: ../' + fileName));
- }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + dirName));
- });
- it("file.spec.111 should not traverse above above the root directory", function (done) {
- var fileName = "traverse.file.uri";
- // create a new file entry
- createFile(fileName, function (entry) {
- // lookup file system entry
- root.getFile('../' + fileName, {
- create : false
- }, function (fileEntry) {
- // Note: we expect this to still resolve, as the correct behaviour is to ignore the ../, not to fail out.
- expect(fileEntry).toBeDefined();
- expect(fileEntry.name).toBe(fileName);
- expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath +'/' + fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'root.getFile - Error getting file: ../' + fileName));
- }, failed.bind(null, done, 'createFile - Error creating file: ../' + fileName));
- });
- it("file.spec.112 should traverse above above the current directory", function (done) {
- var fileName = "traverse2.file.uri",
- dirName = "traverse2.subdir";
- // create a new directory and a file entry
- createFile(fileName, function () {
- createDirectory(dirName, function (entry) {
- // lookup file system entry
- entry.getFile('../' + fileName, {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.name).toBe(fileName);
- expect(fileEntry.fullPath).toCanonicallyMatch('/' + fileName);
- // cleanup
- deleteEntry(fileName, function () {
- deleteEntry(dirName, done);
- });
- }, failed.bind(null, done, 'entry.getFile - Error getting file: ' + fileName + ' recently created above: ' + dirName));
- }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + dirName));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.113 getFile: get Entry should error for missing file above root directory", function (done) {
- var fileName = "../missing.file";
- // create:false, exclusive:false, file does not exist
- root.getFile(fileName, {
- create : false
- }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not locate nonexistent file: ' + fileName), function (error) {
- expect(error).toBeDefined();
- expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
- done();
- });
- });
- });
- //Parent References
- describe('toNativeURL interface', function () {
- /* These specs verify that FileEntries have a toNativeURL method
- * which appears to be sane.
- */
- var pathExpect = cordova.platformId === 'windowsphone' ? "//nativ" : "file://";
- if (isChrome) {
- pathExpect = 'filesystem:file://';
- }
- it("file.spec.114 fileEntry should have a toNativeURL method", function (done) {
- var fileName = "native.file.uri";
- if (isWindows) {
- var rootPath = root.fullPath;
- pathExpect = rootPath.substr(0, rootPath.indexOf(":"));
- }
- // create a new file entry
- createFile(fileName, function (entry) {
- expect(entry.toNativeURL).toBeDefined();
- expect(entry.name).toCanonicallyMatch(fileName);
- expect(typeof entry.toNativeURL).toBe('function');
- var nativeURL = entry.toNativeURL();
- expect(typeof nativeURL).toBe("string");
- expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
- expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.115 DirectoryReader should return entries with toNativeURL method", function (done) {
- var dirName = 'nativeEntries.dir',
- fileName = 'nativeEntries.file',
- directory,
- checkEntries = function (entries) {
- expect(entries).toBeDefined();
- expect(entries instanceof Array).toBe(true);
- expect(entries.length).toBe(1);
- expect(entries[0].toNativeURL).toBeDefined();
- expect(typeof entries[0].toNativeURL).toBe('function');
- var nativeURL = entries[0].toNativeURL();
- expect(typeof nativeURL).toBe("string");
- expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
- expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
- // cleanup
- directory.removeRecursively(null, null);
- done();
- };
- // create a new file entry
- root.getDirectory(dirName, {
- create : true
- }, function (dir) {
- directory = dir;
- directory.getFile(fileName, {
- create : true
- }, function (fileEntry) {
- var reader = directory.createReader();
- reader.readEntries(checkEntries, failed.bind(null, done, 'reader.readEntries - Error reading entries from directory: ' + dirName));
- }, failed.bind(null, done, 'directory.getFile - Error creating file: ' + fileName));
- }, failed.bind(null, done, 'root.getDirectory - Error creating directory: ' + dirName));
- });
- it("file.spec.116 resolveLocalFileSystemURL should return entries with toNativeURL method", function (done) {
- var fileName = "native.resolve.uri";
- // create a new file entry
- createFile(fileName, function (entry) {
- resolveLocalFileSystemURL(entry.toURL(), function (entry) {
- expect(entry.toNativeURL).toBeDefined();
- expect(entry.name).toCanonicallyMatch(fileName);
- expect(typeof entry.toNativeURL).toBe('function');
- var nativeURL = entry.toNativeURL();
- expect(typeof nativeURL).toBe("string");
- expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
- expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- });
- //toNativeURL interface
- describe('resolveLocalFileSystemURL on file://', function () {
- /* These specs verify that window.resolveLocalFileSystemURL works correctly on file:// URLs
- */
- it("file.spec.117 should not resolve native URLs outside of FS roots", function (done) {
- // lookup file system entry
- window.resolveLocalFileSystemURL("file:///this.is.an.invalid.url", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file:///this.is.an.invalid.url'), function (error) {
- expect(error).toBeDefined();
- done();
- });
- });
- it("file.spec.118 should not resolve native URLs outside of FS roots", function (done) {
- // lookup file system entry
- window.resolveLocalFileSystemURL("file://localhost/this.is.an.invalid.url", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file://localhost/this.is.an.invalid.url'), function (error) {
- expect(error).toBeDefined();
- done();
- });
- });
- it("file.spec.119 should not resolve invalid native URLs", function (done) {
- // lookup file system entry
- window.resolveLocalFileSystemURL("file://localhost", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file://localhost'), function (error) {
- expect(error).toBeDefined();
- done();
- });
- });
- it("file.spec.120 should not resolve invalid native URLs with query strings", function (done) {
- // lookup file system entry
- window.resolveLocalFileSystemURL("file://localhost?test/test", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file://localhost?test/test'), function (error) {
- expect(error).toBeDefined();
- done();
- });
- });
- it("file.spec.121 should resolve native URLs returned by API", function (done) {
- var fileName = "native.resolve.uri1";
- // create a new file entry
- createFile(fileName, function (entry) {
- resolveLocalFileSystemURL(entry.toNativeURL(), function (fileEntry) {
- expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toNativeURL()));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.122 should resolve native URLs returned by API with localhost", function (done) {
- var fileName = "native.resolve.uri2";
- // create a new file entry
- createFile(fileName, function (entry) {
- var url = entry.toNativeURL();
- url = url.replace("///", "//localhost/");
- resolveLocalFileSystemURL(url, function (fileEntry) {
- expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.123 should resolve native URLs returned by API with query string", function (done) {
- var fileName = "native.resolve.uri3";
- // create a new file entry
- createFile(fileName, function (entry) {
- var url = entry.toNativeURL();
- url = url + "?test/test";
- resolveLocalFileSystemURL(url, function (fileEntry) {
- expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- it("file.spec.124 should resolve native URLs returned by API with localhost and query string", function (done) {
- var fileName = "native.resolve.uri4";
- // create a new file entry
- createFile(fileName, function (entry) {
- var url = entry.toNativeURL();
- url = url.replace("///", "//localhost/") + "?test/test";
- resolveLocalFileSystemURL(url, function (fileEntry) {
- expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
- // cleanup
- deleteEntry(fileName, done);
- }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
- }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
- });
- });
- //resolveLocalFileSystemURL on file://
- describe('cross-file-system copy and move', function () {
- /* These specs verify that Entry.copyTo and Entry.moveTo work correctly
- * when crossing filesystem boundaries.
- */
- it("file.spec.125 copyTo: temporary -> persistent", function (done) {
- var file1 = "entry.copy.file1a",
- file2 = "entry.copy.file2a",
- sourceEntry,
- fullPath = joinURL(root.fullPath, file2),
- validateFile = function (entry) {
- // a bit redundant since copy returned this entry already
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(file2);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- expect(entry.filesystem).toBeDefined();
- if (isChrome) {
- expect(entry.filesystem.name).toContain("Persistent");
- } else {
- expect(entry.filesystem.name).toEqual("persistent");
- }
- // cleanup
- deleteEntry(entry.name);
- deleteEntry(sourceEntry.name, done);
- },
- createSourceAndTransfer = function () {
- temp_root.getFile(file1, {
- create : true
- }, function (entry) {
- expect(entry.filesystem).toBeDefined();
- if (isChrome) {
- expect(entry.filesystem.name).toContain("Temporary");
- } else {
- expect(entry.filesystem.name).toEqual("temporary");
- }
- sourceEntry = entry;
- // Save for later cleanup
- entry.copyTo(persistent_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + file1 + ' to PERSISTENT root as: ' + file2));
- }, failed.bind(null, done, 'temp_root.getFile - Error creating file: ' + file1 + 'at TEMPORAL root'));
- };
- // Delete any existing file to start things off
- persistent_root.getFile(file2, {}, function (entry) {
- entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
- }, createSourceAndTransfer);
- });
- it("file.spec.126 copyTo: persistent -> temporary", function (done) {
- var file1 = "entry.copy.file1b",
- file2 = "entry.copy.file2b",
- sourceEntry,
- fullPath = joinURL(temp_root.fullPath, file2),
- validateFile = function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(file2);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- if (isChrome) {
- expect(entry.filesystem.name).toContain("Temporary");
- } else {
- expect(entry.filesystem.name).toEqual("temporary");
- }
- // cleanup
- deleteEntry(entry.name);
- deleteEntry(sourceEntry.name, done);
- },
- createSourceAndTransfer = function () {
- persistent_root.getFile(file1, {
- create : true
- }, function (entry) {
- expect(entry).toBeDefined();
- expect(entry.filesystem).toBeDefined();
- if (isChrome) {
- expect(entry.filesystem.name).toContain("Persistent");
- } else {
- expect(entry.filesystem.name).toEqual("persistent");
- }
- sourceEntry = entry;
- // Save for later cleanup
- entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + file1 + ' to TEMPORAL root as: ' + file2));
- }, failed.bind(null, done, 'persistent_root.getFile - Error creating file: ' + file1 + 'at PERSISTENT root'));
- };
- // Delete any existing file to start things off
- temp_root.getFile(file2, {}, function (entry) {
- entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
- }, createSourceAndTransfer);
- });
- it("file.spec.127 moveTo: temporary -> persistent", function (done) {
- var file1 = "entry.copy.file1a",
- file2 = "entry.copy.file2a",
- sourceEntry,
- fullPath = joinURL(root.fullPath, file2),
- validateFile = function (entry) {
- // a bit redundant since copy returned this entry already
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(file2);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- expect(entry.filesystem).toBeDefined();
- if (isChrome) {
- expect(entry.filesystem.name).toContain("Persistent");
- } else {
- expect(entry.filesystem.name).toEqual("persistent");
- }
- // cleanup
- deleteEntry(entry.name);
- deleteEntry(sourceEntry.name, done);
- },
- createSourceAndTransfer = function () {
- temp_root.getFile(file1, {
- create : true
- }, function (entry) {
- expect(entry.filesystem).toBeDefined();
- if (isChrome) {
- expect(entry.filesystem.name).toContain("Temporary");
- } else {
- expect(entry.filesystem.name).toEqual("temporary");
- }
- sourceEntry = entry;
- // Save for later cleanup
- entry.moveTo(persistent_root, file2, validateFile, failed.bind(null, done, 'entry.moveTo - Error moving file: ' + file1 + ' to PERSISTENT root as: ' + file2));
- }, failed.bind(null, done, 'temp_root.getFile - Error creating file: ' + file1 + 'at TEMPORAL root'));
- };
- // Delete any existing file to start things off
- persistent_root.getFile(file2, {}, function (entry) {
- entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
- }, createSourceAndTransfer);
- });
- it("file.spec.128 moveTo: persistent -> temporary", function (done) {
- var file1 = "entry.copy.file1b",
- file2 = "entry.copy.file2b",
- sourceEntry,
- fullPath = joinURL(temp_root.fullPath, file2),
- validateFile = function (entry) {
- expect(entry).toBeDefined();
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(file2);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- if(isChrome) {
- expect(entry.filesystem.name).toContain("Temporary");
- } else {
- expect(entry.filesystem.name).toEqual("temporary");
- }
- // cleanup
- deleteEntry(entry.name);
- deleteEntry(sourceEntry.name, done);
- },
- createSourceAndTransfer = function () {
- persistent_root.getFile(file1, {
- create : true
- }, function (entry) {
- expect(entry).toBeDefined();
- expect(entry.filesystem).toBeDefined();
- if (isChrome) {
- expect(entry.filesystem.name).toContain("Persistent");
- } else {
- expect(entry.filesystem.name).toEqual("persistent");
- }
- sourceEntry = entry;
- // Save for later cleanup
- entry.moveTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.moveTo - Error moving file: ' + file1 + ' to TEMPORAL root as: ' + file2));
- }, failed.bind(null, done, 'persistent_root.getFile - Error creating file: ' + file1 + 'at PERSISTENT root'));
- };
- // Delete any existing file to start things off
- temp_root.getFile(file2, {}, function (entry) {
- entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
- }, createSourceAndTransfer);
- });
- it("file.spec.129 cordova.file.*Directory are set", function () {
- var expectedPaths = ['applicationDirectory', 'applicationStorageDirectory', 'dataDirectory', 'cacheDirectory'];
- if (cordova.platformId == 'android' || cordova.platformId == 'amazon-fireos') {
- if (cordova.file.externalApplicationStorageDirectory !== null) {
- // https://issues.apache.org/jira/browse/CB-10411
- // If external storage can't be mounted, the cordova.file.external* properties are null.
- expectedPaths.push('externalApplicationStorageDirectory', 'externalRootDirectory', 'externalCacheDirectory', 'externalDataDirectory');
- }
- } else if (cordova.platformId == 'blackberry10') {
- expectedPaths.push('externalRootDirectory', 'sharedDirectory');
- } else if (cordova.platformId == 'ios') {
- expectedPaths.push('syncedDataDirectory', 'documentsDirectory', 'tempDirectory');
- } else if (cordova.platformId == 'osx') {
- expectedPaths.push('documentsDirectory', 'tempDirectory', 'rootDirectory');
- } else {
- console.log('Skipping test due on unsupported platform.');
- return;
- }
- for (var i = 0; i < expectedPaths.length; ++i) {
- expect(typeof cordova.file[expectedPaths[i]]).toBe('string');
- expect(cordova.file[expectedPaths[i]]).toMatch(/\/$/, 'Path should end with a slash');
- }
- });
- });
- //cross-file-system copy and move
- describe('IndexedDB-based impl', function () {
- it("file.spec.131 Nested file or nested directory should be removed when removing a parent directory", function (done) {
- var parentDirName = 'deletedDir131',
- nestedDirName = 'nestedDir131',
- nestedFileName = 'nestedFile131.txt';
- createDirectory(parentDirName, function (parent) {
- parent.getDirectory(nestedDirName, { create: true}, function () {
- parent.getFile(nestedFileName, { create: true}, function () {
- parent.removeRecursively(function() {
- root.getDirectory(parentDirName,{ create: false}, failed.bind(this, done, 'root.getDirectory - unexpected success callback : ' + parentDirName), function(){
- parent.getFile(nestedFileName,{ create: false}, failed.bind(this, done, 'getFile - unexpected success callback : ' + nestedFileName), function(){
- parent.getDirectory(nestedDirName, { create: false}, failed.bind(this, done, 'getDirectory - unexpected success callback : ' + nestedDirName), done);
- });
- });
- }, failed.bind(this, done, 'removeRecursively - Error removing directory : ' + parentDirName));
- }, failed.bind(this, done, 'getFile - Error creating file : ' + nestedFileName));
- },failed.bind(this, done, 'getDirectory - Error creating directory : ' + nestedDirName));
- }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + parentDirName));
- });
- it("file.spec.132 Entry should be created succesfully when using relative paths if its parent directory exists", function (done) {
- /* Directory entries have to be created successively.
- For example, the call `fs.root.getDirectory('dir1/dir2', {create:true}, successCallback, errorCallback)`
- will fail if dir1 did not exist. */
- var parentName = 'parentName132';
- var nestedName = 'nestedName132';
- var path = parentName + '/' + nestedName;
- var win = function(directory){
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(nestedName);
- expect(directory.fullPath).toCanonicallyMatch('/' + path + '/');
- deleteEntry(directory.name);
- deleteEntry(parentName, done);
- };
- createDirectory(parentName, function() {
- root.getDirectory(parentName + '/' + nestedName, {create:true}, win,
- failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + path));
- }, failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + parentName));
- });
- it("file.spec.133 A file being removed should not affect another file with name being a prefix of the removed file name.", function (done) {
- // Names include special symbols so that we check the IndexedDB range used
- var deletedFileName = 'deletedFile.0',
- secondFileName = 'deletedFile.0.1';
- var win = function(fileEntry){
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(true);
- expect(fileEntry.isDirectory).toBe(false);
- expect(fileEntry.name).toCanonicallyMatch(secondFileName);
- deleteEntry(fileEntry.name, done);
- };
- createFile(deletedFileName, function (deletedFile) {
- createFile(secondFileName, function () {
- deletedFile.remove(function() {
- root.getFile(deletedFileName, {create: false}, failed.bind(this, done, 'getFile - unexpected success callback getting deleted file : ' + deletedFileName), function(){
- root.getFile(secondFileName, {create: false}, win, failed.bind(this, done, 'getFile - Error getting file after deleting deletedFile : ' + secondFileName));
- });
- }, failed.bind(this, done, 'remove - Error removing file : ' + deletedFileName));
- }, failed.bind(this, done, 'getFile - Error creating file : ' + secondFileName));
- }, failed.bind(this, done, 'getFile - Error creating file : ' + deletedFileName));
- });
- it("file.spec.134 A directory being removed should not affect another directory with name being a prefix of the removed directory name.", function (done) {
- // Names include special symbols so that we check the IndexedDB range used
- var deletedDirName = 'deletedDir.0',
- secondDirName = 'deletedDir.0.1';
- var win = function(directory){
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(secondDirName);
- deleteEntry(directory.name, done);
- };
- createDirectory(deletedDirName, function (deletedDir) {
- createDirectory(secondDirName, function () {
- deletedDir.remove(function() {
- root.getDirectory(deletedDirName, {create: false}, failed.bind(this, done, 'getDirectory - unexpected success callback getting deleted directory : ' + deletedDirName), function() {
- root.getDirectory(secondDirName, {create: false}, win, failed.bind(this, done, 'getDirectory - Error getting directory after deleting deletedDirectory : ' + secondDirName));
- });
- }, failed.bind(this, done, 'remove - Error removing directory : ' + deletedDirName));
- }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + secondDirName));
- }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + deletedDirName));
- });
- it("file.spec.135 Deletion of a child directory should not affect the parent directory.", function (done) {
- var parentName = 'parentName135';
- var childName = 'childName135';
- var win = function(directory){
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(parentName);
- deleteEntry(directory.name, done);
- };
- createDirectory(parentName, function(parent){
- parent.getDirectory(childName, {create: true}, function(child){
- child.removeRecursively(function(){
- root.getDirectory(parentName, {create: false}, win, failed.bind(this, done, 'root.getDirectory - Error getting parent directory : ' + parentName));
- },
- failed.bind(this, done, 'getDirectory - Error removing directory : ' + childName));
- }, failed.bind(this, done, 'getDirectory - Error creating directory : ' + childName));
- }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + parentName));
- });
- it("file.spec.136 Paths should support Unicode symbols.", function (done) {
- var dirName = '文件插件';
- var win = function(directory){
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.name).toCanonicallyMatch(dirName);
- deleteEntry(directory.name, done);
- };
- createDirectory(dirName, function(){
- root.getDirectory(dirName, {create: false}, win,
- failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + dirName));
- }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + dirName));
- });
- });
- // Content and Asset URLs
- if (cordova.platformId == 'android') {
- describe('content: URLs', function() {
- function testContentCopy(src, done) {
- var file2 = "entry.copy.file2b",
- fullPath = joinURL(temp_root.fullPath, file2),
- validateFile = function (entry) {
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(file2);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- expect(entry.filesystem.name).toEqual("temporary");
- // cleanup
- deleteEntry(entry.name, done);
- },
- transfer = function () {
- resolveLocalFileSystemURL(src, function(entry) {
- expect(entry).toBeDefined();
- expect(entry.filesystem.name).toEqual("content");
- entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + entry.toURL() + ' to TEMPORAL root as: ' + file2));
- }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for content provider'));
- };
- // Delete any existing file to start things off
- temp_root.getFile(file2, {}, function (entry) {
- entry.remove(transfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
- }, transfer);
- }
- it("file.spec.138 copyTo: content", function(done) {
- testContentCopy('content://org.apache.cordova.file.testprovider/www/index.html', done);
- });
- it("file.spec.139 copyTo: content /w space and query", function(done) {
- testContentCopy('content://org.apache.cordova.file.testprovider/?name=foo%20bar&realPath=%2Fwww%2Findex.html', done);
- });
- it("file.spec.140 delete: content should fail", function(done) {
- resolveLocalFileSystemURL('content://org.apache.cordova.file.testprovider/www/index.html', function(entry) {
- entry.remove(failed.bind(null, done, 'expected delete to fail'), done);
- }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for content provider'));
- });
- });
- // these tests ensure that you can read and copy from android_asset folder
- // for details see https://issues.apache.org/jira/browse/CB-6428
- // and https://mail-archives.apache.org/mod_mbox/cordova-dev/201508.mbox/%3C782154441.8406572.1440182722528.JavaMail.yahoo%40mail.yahoo.com%3E
- describe('asset: URLs', function() {
- it("file.spec.141 filePaths.applicationStorage", function() {
- expect(cordova.file.applicationDirectory).toEqual('file:///android_asset/');
- }, MEDIUM_TIMEOUT);
- it("file.spec.142 assets should be enumerable", function(done) {
- resolveLocalFileSystemURL('file:///android_asset/www/fixtures/asset-test', function(entry) {
- var reader = entry.createReader();
- reader.readEntries(function (entries) {
- expect(entries.length).not.toBe(0);
- done();
- }, failed.bind(null, done, 'reader.readEntries - Error during reading of entries from assets directory'));
- }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
- }, MEDIUM_TIMEOUT);
- it("file.spec.145 asset subdirectories should be obtainable", function(done) {
- resolveLocalFileSystemURL('file:///android_asset/www/fixtures', function(entry) {
- entry.getDirectory('asset-test', { create: false }, function (subDir) {
- expect(subDir).toBeDefined();
- expect(subDir.isFile).toBe(false);
- expect(subDir.isDirectory).toBe(true);
- expect(subDir.name).toCanonicallyMatch('asset-test');
- done();
- }, failed.bind(null, done, 'entry.getDirectory - Error getting asset subdirectory'));
- }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
- }, MEDIUM_TIMEOUT);
- it("file.spec.146 asset files should be readable", function(done) {
- resolveLocalFileSystemURL('file:///android_asset/www/fixtures/asset-test/asset-test.txt', function(entry) {
- expect(entry.isFile).toBe(true);
- entry.file(function (file) {
- expect(file).toBeDefined();
- var reader = new FileReader();
- reader.onerror = failed.bind(null, done, 'reader.readAsText - Error reading asset text file');
- reader.onloadend = function () {
- expect(this.result).toBeDefined();
- expect(this.result.length).not.toBe(0);
- done();
- };
- reader.readAsText(file);
- }, failed.bind(null, done, 'entry.file - Error reading asset file'));
- }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
- }, MEDIUM_TIMEOUT);
- it("file.spec.143 copyTo: asset -> temporary", function(done) {
- var file2 = "entry.copy.file2b",
- fullPath = joinURL(temp_root.fullPath, file2),
- validateFile = function (entry) {
- expect(entry.isFile).toBe(true);
- expect(entry.isDirectory).toBe(false);
- expect(entry.name).toCanonicallyMatch(file2);
- expect(entry.fullPath).toCanonicallyMatch(fullPath);
- expect(entry.filesystem.name).toEqual("temporary");
- // cleanup
- deleteEntry(entry.name, done);
- },
- transfer = function () {
- resolveLocalFileSystemURL('file:///android_asset/www/index.html', function(entry) {
- expect(entry.filesystem.name).toEqual('assets');
- entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + entry.toURL() + ' to TEMPORAL root as: ' + file2));
- }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
- };
- // Delete any existing file to start things off
- temp_root.getFile(file2, {}, function (entry) {
- entry.remove(transfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
- }, transfer);
- }, MEDIUM_TIMEOUT);
- });
- it("file.spec.144 copyTo: asset directory", function (done) {
- var srcUrl = 'file:///android_asset/www/fixtures/asset-test';
- var dstDir = "entry.copy.dstDir";
- var dstPath = joinURL(root.fullPath, dstDir);
- // create a new directory entry to kick off it
- deleteEntry(dstDir, function () {
- resolveLocalFileSystemURL(srcUrl, function(directory) {
- directory.copyTo(root, dstDir, function (directory) {
- expect(directory).toBeDefined();
- expect(directory.isFile).toBe(false);
- expect(directory.isDirectory).toBe(true);
- expect(directory.fullPath).toCanonicallyMatch(dstPath);
- expect(directory.name).toCanonicallyMatch(dstDir);
- root.getDirectory(dstDir, {
- create : false
- }, function (dirEntry) {
- expect(dirEntry).toBeDefined();
- expect(dirEntry.isFile).toBe(false);
- expect(dirEntry.isDirectory).toBe(true);
- expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
- expect(dirEntry.name).toCanonicallyMatch(dstDir);
- dirEntry.getFile('asset-test.txt', {
- create : false
- }, function (fileEntry) {
- expect(fileEntry).toBeDefined();
- expect(fileEntry.isFile).toBe(true);
- // cleanup
- deleteEntry(dstDir, done);
- }, failed.bind(null, done, 'dirEntry.getFile - Error getting subfile'));
- }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory'));
- }, failed.bind(null, done, 'directory.copyTo - Error copying directory'));
- }, failed.bind(null, done, 'resolving src dir'));
- }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
- }, MEDIUM_TIMEOUT);
- }
- });
- };
- //******************************************************************************************
- //***************************************Manual Tests***************************************
- //******************************************************************************************
- exports.defineManualTests = function (contentEl, createActionButton) {
- function resolveFs(fsname) {
- var fsURL = "cdvfile://localhost/" + fsname + "/";
- logMessage("Resolving URL: " + fsURL);
- resolveLocalFileSystemURL(fsURL, function (entry) {
- logMessage("Success", 'green');
- logMessage(entry.toURL(), 'blue');
- logMessage(entry.toInternalURL(), 'blue');
- logMessage("Resolving URL: " + entry.toURL());
- resolveLocalFileSystemURL(entry.toURL(), function (entry2) {
- logMessage("Success", 'green');
- logMessage(entry2.toURL(), 'blue');
- logMessage(entry2.toInternalURL(), 'blue');
- }, logError("resolveLocalFileSystemURL"));
- }, logError("resolveLocalFileSystemURL"));
- }
- function testPrivateURL() {
- requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
- logMessage("Temporary root is at " + fileSystem.root.toNativeURL());
- fileSystem.root.getFile("testfile", {
- create : true
- }, function (entry) {
- logMessage("Temporary file is at " + entry.toNativeURL());
- if (entry.toNativeURL().substring(0, 12) == "file:///var/") {
- logMessage("File starts with /var/, trying /private/var");
- var newURL = "file://localhost/private/var/" + entry.toNativeURL().substring(12) + "?and=another_thing";
- //var newURL = entry.toNativeURL();
- logMessage(newURL, 'blue');
- resolveLocalFileSystemURL(newURL, function (newEntry) {
- logMessage("Successfully resolved.", 'green');
- logMessage(newEntry.toURL(), 'blue');
- logMessage(newEntry.toNativeURL(), 'blue');
- }, logError("resolveLocalFileSystemURL"));
- }
- }, logError("getFile"));
- }, logError("requestFileSystem"));
- }
- function clearLog() {
- var log = document.getElementById("info");
- log.innerHTML = "";
- }
- function logMessage(message, color) {
- var log = document.getElementById("info");
- var logLine = document.createElement('div');
- if (color) {
- logLine.style.color = color;
- }
- logLine.innerHTML = message;
- log.appendChild(logLine);
- }
- function logError(serviceName) {
- return function (err) {
- logMessage("ERROR: " + serviceName + " " + JSON.stringify(err), "red");
- };
- }
- var fsRoots = {
- "ios" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
- "osx" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
- "android" : "files,files-external,documents,sdcard,cache,cache-external,root",
- "amazon-fireos" : "files,files-external,documents,sdcard,cache,cache-external,root",
- "windows": "temporary,persistent"
- };
- //Add title and align to content
- var div = document.createElement('h2');
- div.appendChild(document.createTextNode('File Systems'));
- div.setAttribute("align", "center");
- contentEl.appendChild(div);
- div = document.createElement('h3');
- div.appendChild(document.createTextNode('Results are displayed in yellow status box below with expected results noted under that'));
- div.setAttribute("align", "center");
- contentEl.appendChild(div);
- div = document.createElement('div');
- div.setAttribute("id", "button");
- div.setAttribute("align", "center");
- contentEl.appendChild(div);
- if (fsRoots.hasOwnProperty(cordova.platformId)) {
- (fsRoots[cordova.platformId].split(',')).forEach(function (fs) {
- if (cordova.platformId === 'ios' && fs === 'private') {
- createActionButton("Test private URL (iOS)", function () {
- clearLog();
- testPrivateURL();
- }, 'button');
- } else {
- createActionButton(fs, function () {
- clearLog();
- resolveFs(fs);
- }, 'button');
- }
- });
- }
- div = document.createElement('div');
- div.setAttribute("id", "info");
- div.setAttribute("align", "center");
- contentEl.appendChild(div);
- div = document.createElement('h3');
- div.appendChild(document.createTextNode('For each test above, file or directory should be successfully found. ' +
- 'Status box should say Resolving URL was Success. The first URL resolved is the internal URL. ' +
- 'The second URL resolved is the absolute URL. Blue URLs must match.'));
- contentEl.appendChild(div);
- div = document.createElement('h3');
- div.appendChild(document.createTextNode('For Test private URL (iOS), the private URL (first blue URL in status box) ' +
- 'should be successfully resolved. Status box should say Successfully resolved. Both blue URLs below ' +
- 'that should match.'));
- contentEl.appendChild(div);
- };
|