tests.js 225 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. */
  20. /* jshint jasmine: true */
  21. /* global WebKitBlobBuilder */
  22. exports.defineAutoTests = function () {
  23. var isBrowser = (cordova.platformId === "browser");
  24. // Use feature detection to determine current browser instead of checking user-agent
  25. var isChrome = isBrowser && window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
  26. var isIE = isBrowser && (window.msIndexedDB);
  27. var isIndexedDBShim = isBrowser && !isChrome; // Firefox and IE for example
  28. var isWindows = (cordova.platformId === "windows" || cordova.platformId === "windows8");
  29. var MEDIUM_TIMEOUT = 15000;
  30. describe('File API', function () {
  31. // Adding a Jasmine helper matcher, to report errors when comparing to FileError better.
  32. var fileErrorMap = {
  33. 1 : 'NOT_FOUND_ERR',
  34. 2 : 'SECURITY_ERR',
  35. 3 : 'ABORT_ERR',
  36. 4 : 'NOT_READABLE_ERR',
  37. 5 : 'ENCODING_ERR',
  38. 6 : 'NO_MODIFICATION_ALLOWED_ERR',
  39. 7 : 'INVALID_STATE_ERR',
  40. 8 : 'SYNTAX_ERR',
  41. 9 : 'INVALID_MODIFICATION_ERR',
  42. 10 : 'QUOTA_EXCEEDED_ERR',
  43. 11 : 'TYPE_MISMATCH_ERR',
  44. 12 : 'PATH_EXISTS_ERR'
  45. },
  46. root,
  47. temp_root,
  48. persistent_root;
  49. beforeEach(function (done) {
  50. // Custom Matchers
  51. jasmine.Expectation.addMatchers({
  52. toBeFileError : function () {
  53. return {
  54. compare : function (error, code) {
  55. var pass = error.code === code;
  56. return {
  57. pass : pass,
  58. message : 'Expected FileError with code ' + fileErrorMap[error.code] + ' (' + error.code + ') to be ' + fileErrorMap[code] + '(' + code + ')'
  59. };
  60. }
  61. };
  62. },
  63. toCanonicallyMatch : function () {
  64. return {
  65. compare : function (currentPath, path) {
  66. var a = path.split("/").join("").split("\\").join(""),
  67. b = currentPath.split("/").join("").split("\\").join(""),
  68. pass = a === b;
  69. return {
  70. pass : pass,
  71. message : 'Expected paths to match : ' + path + ' should be ' + currentPath
  72. };
  73. }
  74. };
  75. },
  76. toFailWithMessage : function () {
  77. return {
  78. compare : function (error, message) {
  79. var pass = false;
  80. return {
  81. pass : pass,
  82. message : message
  83. };
  84. }
  85. };
  86. },
  87. toBeDataUrl: function () {
  88. return {
  89. compare : function (url) {
  90. var pass = false;
  91. // "data:application/octet-stream;base64,"
  92. var header = url.substr(0, url.indexOf(','));
  93. var headerParts = header.split(/[:;]/);
  94. if (headerParts.length === 3 &&
  95. headerParts[0] === 'data' &&
  96. headerParts[2] === 'base64') {
  97. pass = true;
  98. }
  99. var message = 'Expected ' + url + ' to be a valid data url. ' + header + ' is not valid header for data uris';
  100. return {
  101. pass : pass,
  102. message : message
  103. };
  104. }
  105. };
  106. }
  107. });
  108. //Define global variables
  109. var onError = function (e) {
  110. console.log('[ERROR] Problem setting up root filesystem for test running! Error to follow.');
  111. console.log(JSON.stringify(e));
  112. };
  113. window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
  114. root = fileSystem.root;
  115. // set in file.tests.js
  116. persistent_root = root;
  117. window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
  118. temp_root = fileSystem.root;
  119. // set in file.tests.js
  120. done();
  121. }, onError);
  122. }, onError);
  123. });
  124. // HELPER FUNCTIONS
  125. // deletes specified file or directory
  126. var deleteEntry = function (name, success, error) {
  127. // deletes entry, if it exists
  128. // entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
  129. success = success || function() {};
  130. error = error || failed.bind(null, success, 'deleteEntry failed.');
  131. window.resolveLocalFileSystemURL(root.toURL() + '/' + name, function (entry) {
  132. if (entry.isDirectory === true) {
  133. entry.removeRecursively(success, error);
  134. } else {
  135. entry.remove(success, error);
  136. }
  137. }, success);
  138. };
  139. // deletes file, if it exists, then invokes callback
  140. var deleteFile = function (fileName, callback) {
  141. // entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
  142. callback = callback || function() {};
  143. root.getFile(fileName, null, // remove file system entry
  144. function (entry) {
  145. entry.remove(callback, function () {
  146. console.log('[ERROR] deleteFile cleanup method invoked fail callback.');
  147. });
  148. }, // doesn't exist
  149. callback);
  150. };
  151. // deletes and re-creates the specified file
  152. var createFile = function (fileName, success, error) {
  153. deleteEntry(fileName, function () {
  154. root.getFile(fileName, {
  155. create : true
  156. }, success, error);
  157. }, error);
  158. };
  159. // deletes and re-creates the specified directory
  160. var createDirectory = function (dirName, success, error) {
  161. deleteEntry(dirName, function () {
  162. root.getDirectory(dirName, {
  163. create : true
  164. }, success, error);
  165. }, error);
  166. };
  167. function failed(done, msg, error) {
  168. var info = typeof msg == 'undefined' ? 'Unexpected error callback' : msg;
  169. var codeMsg = (error && error.code) ? (': ' + fileErrorMap[error.code]) : '';
  170. expect(true).toFailWithMessage(info + '\n' + JSON.stringify(error) + codeMsg);
  171. done();
  172. }
  173. var succeed = function (done, msg) {
  174. var info = typeof msg == 'undefined' ? 'Unexpected success callback' : msg;
  175. expect(true).toFailWithMessage(info);
  176. done();
  177. };
  178. var joinURL = function (base, extension) {
  179. if (base.charAt(base.length - 1) !== '/' && extension.charAt(0) !== '/') {
  180. return base + '/' + extension;
  181. }
  182. if (base.charAt(base.length - 1) === '/' && extension.charAt(0) === '/') {
  183. return base + extension.substring(1);
  184. }
  185. return base + extension;
  186. };
  187. describe('FileError object', function () {
  188. it("file.spec.1 should define FileError constants", function () {
  189. expect(FileError.NOT_FOUND_ERR).toBe(1);
  190. expect(FileError.SECURITY_ERR).toBe(2);
  191. expect(FileError.ABORT_ERR).toBe(3);
  192. expect(FileError.NOT_READABLE_ERR).toBe(4);
  193. expect(FileError.ENCODING_ERR).toBe(5);
  194. expect(FileError.NO_MODIFICATION_ALLOWED_ERR).toBe(6);
  195. expect(FileError.INVALID_STATE_ERR).toBe(7);
  196. expect(FileError.SYNTAX_ERR).toBe(8);
  197. expect(FileError.INVALID_MODIFICATION_ERR).toBe(9);
  198. expect(FileError.QUOTA_EXCEEDED_ERR).toBe(10);
  199. expect(FileError.TYPE_MISMATCH_ERR).toBe(11);
  200. expect(FileError.PATH_EXISTS_ERR).toBe(12);
  201. });
  202. });
  203. describe('LocalFileSystem', function () {
  204. it("file.spec.2 should define LocalFileSystem constants", function () {
  205. expect(LocalFileSystem.TEMPORARY).toBe(0);
  206. expect(LocalFileSystem.PERSISTENT).toBe(1);
  207. });
  208. describe('window.requestFileSystem', function () {
  209. it("file.spec.3 should be defined", function () {
  210. expect(window.requestFileSystem).toBeDefined();
  211. });
  212. it("file.spec.4 should be able to retrieve a PERSISTENT file system", function (done) {
  213. var win = function (fileSystem) {
  214. expect(fileSystem).toBeDefined();
  215. expect(fileSystem.name).toBeDefined();
  216. if (isChrome) {
  217. expect(fileSystem.name).toContain("Persistent");
  218. } else {
  219. expect(fileSystem.name).toBe("persistent");
  220. }
  221. expect(fileSystem.root).toBeDefined();
  222. expect(fileSystem.root.filesystem).toBeDefined();
  223. // Shouldn't use cdvfile by default.
  224. expect(fileSystem.root.toURL()).not.toMatch(/^cdvfile:/);
  225. // All DirectoryEntry URLs should always have a trailing slash.
  226. expect(fileSystem.root.toURL()).toMatch(/\/$/);
  227. done();
  228. };
  229. // Request a little bit of space on the filesystem, unless we're running in a browser where that could cause a prompt.
  230. var spaceRequired = isBrowser ? 0 : 1024;
  231. // retrieve PERSISTENT file system
  232. window.requestFileSystem(LocalFileSystem.PERSISTENT, spaceRequired, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving PERSISTENT file system'));
  233. });
  234. it("file.spec.5 should be able to retrieve a TEMPORARY file system", function (done) {
  235. var win = function (fileSystem) {
  236. expect(fileSystem).toBeDefined();
  237. if (isChrome) {
  238. expect(fileSystem.name).toContain("Temporary");
  239. } else {
  240. expect(fileSystem.name).toBe("temporary");
  241. }
  242. expect(fileSystem.root).toBeDefined();
  243. expect(fileSystem.root.filesystem).toBeDefined();
  244. expect(fileSystem.root.filesystem).toBe(fileSystem);
  245. done();
  246. };
  247. //retrieve TEMPORARY file system
  248. window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system'));
  249. });
  250. it("file.spec.6 should error if you request a file system that is too large", function (done) {
  251. if (isBrowser) {
  252. /*window.requestFileSystem TEMPORARY and PERSISTENT filesystem quota is not limited in Chrome.
  253. Firefox filesystem size is not limited but every 50MB request user permission.
  254. IE10 allows up to 10mb of combined AppCache and IndexedDB used in implementation
  255. of filesystem without prompting, once you hit that level you will be asked if you
  256. want to allow it to be increased up to a max of 250mb per site.
  257. So `size` parameter for `requestFileSystem` function does not affect on filesystem in Firefox and IE.*/
  258. pending();
  259. }
  260. var fail = function (error) {
  261. expect(error).toBeDefined();
  262. expect(error).toBeFileError(FileError.QUOTA_EXCEEDED_ERR);
  263. done();
  264. };
  265. //win = createWin('window.requestFileSystem');
  266. // Request the file system
  267. window.requestFileSystem(LocalFileSystem.TEMPORARY, 1000000000000000, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system'), fail);
  268. });
  269. it("file.spec.7 should error out if you request a file system that does not exist", function (done) {
  270. var fail = function (error) {
  271. expect(error).toBeDefined();
  272. if (isChrome) {
  273. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of SYNTAX_ERR(code: 8)
  274. on requesting of a non-existant filesystem.*/
  275. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  276. } else {
  277. expect(error).toBeFileError(FileError.SYNTAX_ERR);
  278. }
  279. done();
  280. };
  281. // Request the file system
  282. window.requestFileSystem(-1, 0, succeed.bind(null, done, 'window.requestFileSystem'), fail);
  283. });
  284. });
  285. describe('window.resolveLocalFileSystemURL', function () {
  286. it("file.spec.8 should be defined", function () {
  287. expect(window.resolveLocalFileSystemURL).toBeDefined();
  288. });
  289. it("file.spec.9 should resolve a valid file name", function (done) {
  290. var fileName = 'file.spec.9';
  291. var win = function (fileEntry) {
  292. expect(fileEntry).toBeDefined();
  293. expect(fileEntry.isFile).toBe(true);
  294. expect(fileEntry.isDirectory).toBe(false);
  295. expect(fileEntry.name).toCanonicallyMatch(fileName);
  296. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  297. expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
  298. // Clean-up
  299. deleteEntry(fileName, done);
  300. };
  301. createFile(fileName, function (entry) {
  302. window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
  303. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  304. });
  305. it("file.spec.9.1 should resolve a file even with a terminating slash", function (done) {
  306. var fileName = 'file.spec.9.1';
  307. var win = function (fileEntry) {
  308. expect(fileEntry).toBeDefined();
  309. expect(fileEntry.isFile).toBe(true);
  310. expect(fileEntry.isDirectory).toBe(false);
  311. expect(fileEntry.name).toCanonicallyMatch(fileName);
  312. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  313. expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
  314. // Clean-up
  315. deleteEntry(fileName, done);
  316. };
  317. createFile(fileName, function (entry) {
  318. var entryURL = entry.toURL() + '/';
  319. window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entryURL));
  320. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  321. });
  322. it("file.spec.9.5 should resolve a directory", function (done) {
  323. var fileName = 'file.spec.9.5';
  324. var win = function (fileEntry) {
  325. expect(fileEntry).toBeDefined();
  326. expect(fileEntry.isFile).toBe(false);
  327. expect(fileEntry.isDirectory).toBe(true);
  328. expect(fileEntry.name).toCanonicallyMatch(fileName);
  329. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  330. expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
  331. // cleanup
  332. deleteEntry(fileName, done);
  333. };
  334. function gotDirectory(entry) {
  335. // lookup file system entry
  336. window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entry.toURL()));
  337. }
  338. createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
  339. });
  340. it("file.spec.9.6 should resolve a directory even without a terminating slash", function (done) {
  341. var fileName = 'file.spec.9.6';
  342. var win = function (fileEntry) {
  343. expect(fileEntry).toBeDefined();
  344. expect(fileEntry.isFile).toBe(false);
  345. expect(fileEntry.isDirectory).toBe(true);
  346. expect(fileEntry.name).toCanonicallyMatch(fileName);
  347. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  348. expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
  349. // cleanup
  350. deleteEntry(fileName, done);
  351. };
  352. function gotDirectory(entry) {
  353. // lookup file system entry
  354. var entryURL = entry.toURL();
  355. entryURL = entryURL.substring(0, entryURL.length - 1);
  356. window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entryURL));
  357. }
  358. createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
  359. });
  360. it("file.spec.10 resolve valid file name with parameters", function (done) {
  361. var fileName = "resolve.file.uri.params",
  362. win = function (fileEntry) {
  363. expect(fileEntry).toBeDefined();
  364. if (fileEntry.toURL().toLowerCase().substring(0, 10) === "cdvfile://") {
  365. expect(fileEntry.fullPath).toBe("/" + fileName + "?1234567890");
  366. }
  367. expect(fileEntry.name).toBe(fileName);
  368. // cleanup
  369. deleteEntry(fileName, done);
  370. };
  371. // create a new file entry
  372. createFile(fileName, function (entry) {
  373. window.resolveLocalFileSystemURL(entry.toURL() + "?1234567890", win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + entry.toURL()));
  374. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  375. });
  376. it("file.spec.11 should error (NOT_FOUND_ERR) when resolving (non-existent) invalid file name", function (done) {
  377. var fileName = cordova.platformId === 'windowsphone' ? root.toURL() + "/" + "this.is.not.a.valid.file.txt" : joinURL(root.toURL(), "this.is.not.a.valid.file.txt"),
  378. fail = function (error) {
  379. expect(error).toBeDefined();
  380. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  381. done();
  382. };
  383. // lookup file system entry
  384. window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
  385. });
  386. it("file.spec.12 should error (ENCODING_ERR) when resolving invalid URI with leading /", function (done) {
  387. var fileName = "/this.is.not.a.valid.url",
  388. fail = function (error) {
  389. expect(error).toBeDefined();
  390. expect(error).toBeFileError(FileError.ENCODING_ERR);
  391. done();
  392. };
  393. // lookup file system entry
  394. window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
  395. });
  396. });
  397. });
  398. //LocalFileSystem
  399. describe('Metadata interface', function () {
  400. it("file.spec.13 should exist and have the right properties", function () {
  401. var metadata = new Metadata();
  402. expect(metadata).toBeDefined();
  403. expect(metadata.modificationTime).toBeDefined();
  404. });
  405. });
  406. describe('Flags interface', function () {
  407. it("file.spec.14 should exist and have the right properties", function () {
  408. var flags = new Flags(false, true);
  409. expect(flags).toBeDefined();
  410. expect(flags.create).toBeDefined();
  411. expect(flags.create).toBe(false);
  412. expect(flags.exclusive).toBeDefined();
  413. expect(flags.exclusive).toBe(true);
  414. });
  415. });
  416. describe('FileSystem interface', function () {
  417. it("file.spec.15 should have a root that is a DirectoryEntry", function (done) {
  418. var win = function (entry) {
  419. expect(entry).toBeDefined();
  420. expect(entry.isFile).toBe(false);
  421. expect(entry.isDirectory).toBe(true);
  422. expect(entry.name).toBeDefined();
  423. expect(entry.fullPath).toBeDefined();
  424. expect(entry.getMetadata).toBeDefined();
  425. expect(entry.moveTo).toBeDefined();
  426. expect(entry.copyTo).toBeDefined();
  427. expect(entry.toURL).toBeDefined();
  428. expect(entry.remove).toBeDefined();
  429. expect(entry.getParent).toBeDefined();
  430. expect(entry.createReader).toBeDefined();
  431. expect(entry.getFile).toBeDefined();
  432. expect(entry.getDirectory).toBeDefined();
  433. expect(entry.removeRecursively).toBeDefined();
  434. done();
  435. };
  436. window.resolveLocalFileSystemURL(root.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + root.toURL()));
  437. });
  438. });
  439. describe('DirectoryEntry', function () {
  440. it("file.spec.16 getFile: get Entry for file that does not exist", function (done) {
  441. var fileName = "de.no.file",
  442. fail = function (error) {
  443. expect(error).toBeDefined();
  444. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  445. done();
  446. };
  447. // create:false, exclusive:false, file does not exist
  448. root.getFile(fileName, {
  449. create : false
  450. }, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
  451. });
  452. it("file.spec.17 getFile: create new file", function (done) {
  453. var fileName = "de.create.file",
  454. filePath = joinURL(root.fullPath, fileName),
  455. win = function (entry) {
  456. expect(entry).toBeDefined();
  457. expect(entry.isFile).toBe(true);
  458. expect(entry.isDirectory).toBe(false);
  459. expect(entry.name).toCanonicallyMatch(fileName);
  460. expect(entry.fullPath).toCanonicallyMatch(filePath);
  461. // cleanup
  462. deleteEntry(entry.name, done);
  463. };
  464. // create:true, exclusive:false, file does not exist
  465. root.getFile(fileName, {
  466. create : true
  467. }, win, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName));
  468. });
  469. it("file.spec.18 getFile: create new file (exclusive)", function (done) {
  470. var fileName = "de.create.exclusive.file",
  471. filePath = joinURL(root.fullPath, fileName),
  472. win = function (entry) {
  473. expect(entry).toBeDefined();
  474. expect(entry.isFile).toBe(true);
  475. expect(entry.isDirectory).toBe(false);
  476. expect(entry.name).toBe(fileName);
  477. expect(entry.fullPath).toCanonicallyMatch(filePath);
  478. // cleanup
  479. deleteEntry(entry.name, done);
  480. };
  481. // create:true, exclusive:true, file does not exist
  482. root.getFile(fileName, {
  483. create : true,
  484. exclusive : true
  485. }, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  486. });
  487. it("file.spec.19 getFile: create file that already exists", function (done) {
  488. var fileName = "de.create.existing.file",
  489. filePath = joinURL(root.fullPath, fileName);
  490. function win(entry) {
  491. expect(entry).toBeDefined();
  492. expect(entry.isFile).toBe(true);
  493. expect(entry.isDirectory).toBe(false);
  494. expect(entry.name).toCanonicallyMatch(fileName);
  495. expect(entry.fullPath).toCanonicallyMatch(filePath);
  496. // cleanup
  497. deleteEntry(entry.name, done);
  498. }
  499. function getFile(file) {
  500. // create:true, exclusive:false, file exists
  501. root.getFile(fileName, {
  502. create : true
  503. }, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  504. }
  505. // create file to kick off it
  506. root.getFile(fileName, {
  507. create : true
  508. }, getFile, failed.bind(null, done, 'root.getFile - Error on initial creating file: ' + fileName));
  509. });
  510. it("file.spec.20 getFile: create file that already exists (exclusive)", function (done) {
  511. var fileName = "de.create.exclusive.existing.file",
  512. existingFile;
  513. function fail(error) {
  514. expect(error).toBeDefined();
  515. if (isChrome) {
  516. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of PATH_EXISTS_ERR(code: 12)
  517. on trying to exclusively create a file, which already exists in Chrome.*/
  518. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  519. } else {
  520. expect(error).toBeFileError(FileError.PATH_EXISTS_ERR);
  521. }
  522. // cleanup
  523. deleteEntry(existingFile.name, done);
  524. }
  525. function getFile(file) {
  526. existingFile = file;
  527. // create:true, exclusive:true, file exists
  528. root.getFile(fileName, {
  529. create : true,
  530. exclusive : true
  531. }, succeed.bind(null, done, 'root.getFile - getFile function - Error unexpected callback, file should exists: ' + fileName), fail);
  532. }
  533. // create file to kick off it
  534. root.getFile(fileName, {
  535. create : true
  536. }, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  537. });
  538. it("file.spec.21 DirectoryEntry.getFile: get Entry for existing file", function (done) {
  539. var fileName = "de.get.file",
  540. filePath = joinURL(root.fullPath, fileName),
  541. win = function (entry) {
  542. expect(entry).toBeDefined();
  543. expect(entry.isFile).toBe(true);
  544. expect(entry.isDirectory).toBe(false);
  545. expect(entry.name).toCanonicallyMatch(fileName);
  546. expect(entry.fullPath).toCanonicallyMatch(filePath);
  547. expect(entry.filesystem).toBeDefined();
  548. expect(entry.filesystem).toBe(root.filesystem);
  549. //clean up
  550. deleteEntry(entry.name, done);
  551. },
  552. getFile = function (file) {
  553. // create:false, exclusive:false, file exists
  554. root.getFile(fileName, {
  555. create : false
  556. }, win, failed.bind(null, done, 'root.getFile - Error getting file entry: ' + fileName));
  557. };
  558. // create file to kick off it
  559. root.getFile(fileName, {
  560. create : true
  561. }, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  562. });
  563. it("file.spec.22 DirectoryEntry.getFile: get FileEntry for invalid path", function (done) {
  564. if (isBrowser) {
  565. /*The plugin does not follow to ["8.3 Naming restrictions"]
  566. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions).*/
  567. pending();
  568. }
  569. var fileName = "de:invalid:path",
  570. fail = function (error) {
  571. expect(error).toBeDefined();
  572. expect(error).toBeFileError(FileError.ENCODING_ERR);
  573. done();
  574. };
  575. // create:false, exclusive:false, invalid path
  576. root.getFile(fileName, {
  577. create : false
  578. }, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
  579. });
  580. it("file.spec.23 DirectoryEntry.getDirectory: get Entry for directory that does not exist", function (done) {
  581. var dirName = "de.no.dir",
  582. fail = function (error) {
  583. expect(error).toBeDefined();
  584. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  585. done();
  586. };
  587. // create:false, exclusive:false, directory does not exist
  588. root.getDirectory(dirName, {
  589. create : false
  590. }, succeed.bind(null, done, 'root.getDirectory - Error unexpected callback, directory should not exists: ' + dirName), fail);
  591. });
  592. it("file.spec.24 DirectoryEntry.getDirectory: create new dir with space then resolveLocalFileSystemURL", function (done) {
  593. var dirName = "de create dir";
  594. function win(directory) {
  595. expect(directory).toBeDefined();
  596. expect(directory.isFile).toBe(false);
  597. expect(directory.isDirectory).toBe(true);
  598. expect(directory.name).toCanonicallyMatch(dirName);
  599. expect(directory.fullPath).toCanonicallyMatch(joinURL(root.fullPath, dirName));
  600. // cleanup
  601. deleteEntry(directory.name, done);
  602. }
  603. function getDir(dirEntry) {
  604. expect(dirEntry.filesystem).toBeDefined();
  605. expect(dirEntry.filesystem).toBe(root.filesystem);
  606. var dirURI = dirEntry.toURL();
  607. // now encode URI and try to resolve
  608. window.resolveLocalFileSystemURL(dirURI, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
  609. }
  610. // create:true, exclusive:false, directory does not exist
  611. root.getDirectory(dirName, {
  612. create : true
  613. }, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  614. });
  615. // This test is excluded, and should probably be removed. Filesystem
  616. // should always be properly encoded URLs, and *not* raw paths, and it
  617. // doesn't make sense to double-encode the URLs and expect that to be
  618. // handled by the implementation.
  619. // If a particular platform uses paths internally rather than URLs, // then that platform should careful to pass them correctly to its
  620. // backend.
  621. xit("file.spec.25 DirectoryEntry.getDirectory: create new dir with space resolveLocalFileSystemURL with encoded URI", function (done) {
  622. var dirName = "de create dir2",
  623. dirPath = joinURL(root.fullPath, dirName);
  624. function win(directory) {
  625. expect(directory).toBeDefined();
  626. expect(directory.isFile).toBe(false);
  627. expect(directory.isDirectory).toBe(true);
  628. expect(directory.name).toCanonicallyMatch(dirName);
  629. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  630. // cleanup
  631. deleteEntry(directory.name, done);
  632. }
  633. function getDir(dirEntry) {
  634. var dirURI = dirEntry.toURL();
  635. // now encode URI and try to resolve
  636. window.resolveLocalFileSystemURL(encodeURI(dirURI), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
  637. }
  638. // create:true, exclusive:false, directory does not exist
  639. root.getDirectory(dirName, {
  640. create : true
  641. }, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  642. });
  643. it("file.spec.26 DirectoryEntry.getDirectory: create new directory", function (done) {
  644. var dirName = "de.create.dir",
  645. dirPath = joinURL(root.fullPath, dirName),
  646. win = function (directory) {
  647. expect(directory).toBeDefined();
  648. expect(directory.isFile).toBe(false);
  649. expect(directory.isDirectory).toBe(true);
  650. expect(directory.name).toCanonicallyMatch(dirName);
  651. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  652. expect(directory.filesystem).toBeDefined();
  653. expect(directory.filesystem).toBe(root.filesystem);
  654. // cleanup
  655. deleteEntry(directory.name, done);
  656. };
  657. // create:true, exclusive:false, directory does not exist
  658. root.getDirectory(dirName, {
  659. create : true
  660. }, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  661. });
  662. it("file.spec.27 DirectoryEntry.getDirectory: create new directory (exclusive)", function (done) {
  663. var dirName = "de.create.exclusive.dir",
  664. dirPath = joinURL(root.fullPath, dirName),
  665. win = function (directory) {
  666. expect(directory).toBeDefined();
  667. expect(directory.isFile).toBe(false);
  668. expect(directory.isDirectory).toBe(true);
  669. expect(directory.name).toCanonicallyMatch(dirName);
  670. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  671. expect(directory.filesystem).toBeDefined();
  672. expect(directory.filesystem).toBe(root.filesystem);
  673. // cleanup
  674. deleteEntry(directory.name, done);
  675. };
  676. // create:true, exclusive:true, directory does not exist
  677. root.getDirectory(dirName, {
  678. create : true,
  679. exclusive : true
  680. }, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  681. });
  682. it("file.spec.28 DirectoryEntry.getDirectory: create directory that already exists", function (done) {
  683. var dirName = "de.create.existing.dir",
  684. dirPath = joinURL(root.fullPath, dirName),
  685. win = function (directory) {
  686. expect(directory).toBeDefined();
  687. expect(directory.isFile).toBe(false);
  688. expect(directory.isDirectory).toBe(true);
  689. expect(directory.name).toCanonicallyMatch(dirName);
  690. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  691. // cleanup
  692. deleteEntry(directory.name, done);
  693. };
  694. // create directory to kick off it
  695. root.getDirectory(dirName, {
  696. create : true
  697. }, function () {
  698. root.getDirectory(dirName, {
  699. create : true
  700. }, win, failed.bind(null, done, 'root.getDirectory - Error creating existent second directory : ' + dirName));
  701. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  702. });
  703. it("file.spec.29 DirectoryEntry.getDirectory: create directory that already exists (exclusive)", function (done) {
  704. var dirName = "de.create.exclusive.existing.dir",
  705. existingDir,
  706. fail = function (error) {
  707. expect(error).toBeDefined();
  708. if (isChrome) {
  709. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of PATH_EXISTS_ERR(code: 12)
  710. on trying to exclusively create a file or directory, which already exists (Chrome).*/
  711. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  712. } else {
  713. expect(error).toBeFileError(FileError.PATH_EXISTS_ERR);
  714. }
  715. // cleanup
  716. deleteEntry(existingDir.name, done);
  717. };
  718. // create directory to kick off it
  719. root.getDirectory(dirName, {
  720. create : true
  721. }, function (directory) {
  722. existingDir = directory;
  723. // create:true, exclusive:true, directory exists
  724. root.getDirectory(dirName, {
  725. create : true,
  726. exclusive : true
  727. }, failed.bind(null, done, 'root.getDirectory - Unexpected success callback, second directory should not be created : ' + dirName), fail);
  728. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  729. });
  730. it("file.spec.30 DirectoryEntry.getDirectory: get Entry for existing directory", function (done) {
  731. var dirName = "de.get.dir",
  732. dirPath = joinURL(root.fullPath, dirName),
  733. win = function (directory) {
  734. expect(directory).toBeDefined();
  735. expect(directory.isFile).toBe(false);
  736. expect(directory.isDirectory).toBe(true);
  737. expect(directory.name).toCanonicallyMatch(dirName);
  738. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  739. // cleanup
  740. deleteEntry(directory.name, done);
  741. };
  742. // create directory to kick it off
  743. root.getDirectory(dirName, {
  744. create : true
  745. }, function () {
  746. root.getDirectory(dirName, {
  747. create : false
  748. }, win, failed.bind(null, done, 'root.getDirectory - Error getting directory entry : ' + dirName));
  749. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  750. });
  751. it("file.spec.31 DirectoryEntry.getDirectory: get DirectoryEntry for invalid path", function (done) {
  752. if (isBrowser) {
  753. /*The plugin does not follow to ["8.3 Naming restrictions"]
  754. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions).*/
  755. pending();
  756. }
  757. var dirName = "de:invalid:path",
  758. fail = function (error) {
  759. expect(error).toBeDefined();
  760. expect(error).toBeFileError(FileError.ENCODING_ERR);
  761. done();
  762. };
  763. // create:false, exclusive:false, invalid path
  764. root.getDirectory(dirName, {
  765. create : false
  766. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), fail);
  767. });
  768. it("file.spec.32 DirectoryEntry.getDirectory: get DirectoryEntry for existing file", function (done) {
  769. var fileName = "de.existing.file",
  770. existingFile,
  771. fail = function (error) {
  772. expect(error).toBeDefined();
  773. expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR);
  774. // cleanup
  775. deleteEntry(existingFile.name, done);
  776. };
  777. // create file to kick off it
  778. root.getFile(fileName, {
  779. create : true
  780. }, function (file) {
  781. existingFile = file;
  782. root.getDirectory(fileName, {
  783. create : false
  784. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + fileName), fail);
  785. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  786. });
  787. it("file.spec.33 DirectoryEntry.getFile: get FileEntry for existing directory", function (done) {
  788. var dirName = "de.existing.dir",
  789. existingDir,
  790. fail = function (error) {
  791. expect(error).toBeDefined();
  792. expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR);
  793. // cleanup
  794. deleteEntry(existingDir.name, done);
  795. };
  796. // create directory to kick off it
  797. root.getDirectory(dirName, {
  798. create : true
  799. }, function (directory) {
  800. existingDir = directory;
  801. root.getFile(dirName, {
  802. create : false
  803. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, file should not exists: ' + dirName), fail);
  804. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  805. });
  806. it("file.spec.34 DirectoryEntry.removeRecursively on directory", function (done) {
  807. var dirName = "de.removeRecursively",
  808. subDirName = "dir",
  809. dirExists = function (error) {
  810. expect(error).toBeDefined();
  811. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  812. done();
  813. };
  814. // create a new directory entry to kick off it
  815. root.getDirectory(dirName, {
  816. create : true
  817. }, function (entry) {
  818. entry.getDirectory(subDirName, {
  819. create : true
  820. }, function (dir) {
  821. entry.removeRecursively(function () {
  822. root.getDirectory(dirName, {
  823. create : false
  824. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), dirExists);
  825. }, failed.bind(null, done, 'entry.removeRecursively - Error removing directory recursively : ' + dirName));
  826. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + subDirName));
  827. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  828. });
  829. it("file.spec.35 createReader: create reader on existing directory", function () {
  830. // create reader for root directory
  831. var reader = root.createReader();
  832. expect(reader).toBeDefined();
  833. expect(typeof reader.readEntries).toBe('function');
  834. });
  835. it("file.spec.36 removeRecursively on root file system", function (done) {
  836. var remove = function (error) {
  837. expect(error).toBeDefined();
  838. if (isChrome) {
  839. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of
  840. NO_MODIFICATION_ALLOWED_ERR(code: 6) on trying to call removeRecursively
  841. on the root file system (Chrome).*/
  842. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  843. } else {
  844. expect(error).toBeFileError(FileError.NO_MODIFICATION_ALLOWED_ERR);
  845. }
  846. done();
  847. };
  848. // remove root file system
  849. root.removeRecursively(succeed.bind(null, done, 'root.removeRecursively - Unexpected success callback, root cannot be removed'), remove);
  850. });
  851. });
  852. describe('DirectoryReader interface', function () {
  853. describe("readEntries", function () {
  854. it("file.spec.37 should read contents of existing directory", function (done) {
  855. var reader,
  856. win = function (entries) {
  857. expect(entries).toBeDefined();
  858. expect(entries instanceof Array).toBe(true);
  859. done();
  860. };
  861. // create reader for root directory
  862. reader = root.createReader();
  863. // read entries
  864. reader.readEntries(win, failed.bind(null, done, 'reader.readEntries - Error reading entries'));
  865. });
  866. it("file.spec.37.1 should read contents of existing directory", function (done) {
  867. var dirName = 'readEntries.dir',
  868. fileName = 'readeEntries.file';
  869. root.getDirectory(dirName, {
  870. create : true
  871. }, function (directory) {
  872. directory.getFile(fileName, {
  873. create : true
  874. }, function (fileEntry) {
  875. var reader = directory.createReader();
  876. reader.readEntries(function (entries) {
  877. expect(entries).toBeDefined();
  878. expect(entries instanceof Array).toBe(true);
  879. expect(entries.length).toBe(1);
  880. expect(entries[0].fullPath).toCanonicallyMatch(fileEntry.fullPath);
  881. expect(entries[0].filesystem).not.toBe(null);
  882. if (isChrome) {
  883. // Slicing '[object {type}]' -> '{type}'
  884. expect(entries[0].filesystem.toString().slice(8, -1)).toEqual("DOMFileSystem");
  885. }
  886. else {
  887. expect(entries[0].filesystem instanceof FileSystem).toBe(true);
  888. }
  889. // cleanup
  890. deleteEntry(directory.name, done);
  891. }, failed.bind(null, done, 'reader.readEntries - Error reading entries from directory: ' + dirName));
  892. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + fileName));
  893. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  894. });
  895. it("file.spec.109 should return an empty entry list on the second call", function (done) {
  896. var reader,
  897. fileName = 'test109.txt';
  898. // Add a file to ensure the root directory is non-empty and then read the contents of the directory.
  899. root.getFile(fileName, {
  900. create : true
  901. }, function (entry) {
  902. reader = root.createReader();
  903. //First read
  904. reader.readEntries(function (entries) {
  905. expect(entries).toBeDefined();
  906. expect(entries instanceof Array).toBe(true);
  907. expect(entries.length).not.toBe(0);
  908. //Second read
  909. reader.readEntries(function (entries_) {
  910. expect(entries_).toBeDefined();
  911. expect(entries_ instanceof Array).toBe(true);
  912. expect(entries_.length).toBe(0);
  913. //Clean up
  914. deleteEntry(entry.name, done);
  915. }, failed.bind(null, done, 'reader.readEntries - Error during SECOND reading of entries from [root] directory'));
  916. }, failed.bind(null, done, 'reader.readEntries - Error during FIRST reading of entries from [root] directory'));
  917. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  918. });
  919. });
  920. it("file.spec.38 should read contents of directory that has been removed", function (done) {
  921. var dirName = "de.createReader.notfound";
  922. // create a new directory entry to kick off it
  923. root.getDirectory(dirName, {
  924. create : true
  925. }, function (directory) {
  926. directory.removeRecursively(function () {
  927. var reader = directory.createReader();
  928. reader.readEntries(succeed.bind(null, done, 'reader.readEntries - Unexpected success callback, it should not read entries from deleted dir: ' + dirName), function (error) {
  929. expect(error).toBeDefined();
  930. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  931. root.getDirectory(dirName, {
  932. create : false
  933. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, it should not get deleted directory: ' + dirName), function (err) {
  934. expect(err).toBeDefined();
  935. expect(err).toBeFileError(FileError.NOT_FOUND_ERR);
  936. done();
  937. });
  938. });
  939. }, failed.bind(null, done, 'directory.removeRecursively - Error removing directory recursively : ' + dirName));
  940. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  941. });
  942. });
  943. //DirectoryReader interface
  944. describe('File', function () {
  945. it("file.spec.39 constructor should be defined", function () {
  946. expect(File).toBeDefined();
  947. expect(typeof File).toBe('function');
  948. });
  949. it("file.spec.40 should be define File attributes", function () {
  950. var file = new File();
  951. expect(file.name).toBeDefined();
  952. expect(file.type).toBeDefined();
  953. expect(file.lastModifiedDate).toBeDefined();
  954. expect(file.size).toBeDefined();
  955. });
  956. });
  957. //File
  958. describe('FileEntry', function () {
  959. it("file.spec.41 should be define FileEntry methods", function (done) {
  960. var fileName = "fe.methods",
  961. testFileEntry = function (fileEntry) {
  962. expect(fileEntry).toBeDefined();
  963. expect(typeof fileEntry.createWriter).toBe('function');
  964. expect(typeof fileEntry.file).toBe('function');
  965. // cleanup
  966. deleteEntry(fileEntry.name, done);
  967. };
  968. // create a new file entry to kick off it
  969. root.getFile(fileName, {
  970. create : true
  971. }, testFileEntry, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  972. });
  973. it("file.spec.42 createWriter should return a FileWriter object", function (done) {
  974. var fileName = "fe.createWriter",
  975. testFile,
  976. testWriter = function (writer) {
  977. expect(writer).toBeDefined();
  978. if (isChrome) {
  979. // Slicing '[object {type}]' -> '{type}'
  980. expect(writer.toString().slice(8, -1)).toEqual("FileWriter");
  981. }
  982. else {
  983. expect(writer instanceof FileWriter).toBe(true);
  984. }
  985. // cleanup
  986. deleteEntry(testFile.name, done);
  987. };
  988. // create a new file entry to kick off it
  989. root.getFile(fileName, {
  990. create : true
  991. }, function (fileEntry) {
  992. testFile = fileEntry;
  993. fileEntry.createWriter(testWriter, failed.bind(null, done, 'fileEntry.createWriter - Error creating Writer from entry'));
  994. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  995. });
  996. it("file.spec.43 file should return a File object", function (done) {
  997. var fileName = "fe.file",
  998. newFile,
  999. testFile = function (file) {
  1000. expect(file).toBeDefined();
  1001. if (isChrome) {
  1002. // Slicing '[object {type}]' -> '{type}'
  1003. expect(file.toString().slice(8, -1)).toEqual("File");
  1004. }
  1005. else {
  1006. expect(file instanceof File).toBe(true);
  1007. }
  1008. // cleanup
  1009. deleteEntry(newFile.name, done);
  1010. };
  1011. // create a new file entry to kick off it
  1012. root.getFile(fileName, {
  1013. create : true
  1014. }, function (fileEntry) {
  1015. newFile = fileEntry;
  1016. fileEntry.file(testFile, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  1017. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  1018. });
  1019. it("file.spec.44 file: on File that has been removed", function (done) {
  1020. var fileName = "fe.no.file";
  1021. // create a new file entry to kick off it
  1022. root.getFile(fileName, {
  1023. create : true
  1024. }, function (fileEntry) {
  1025. fileEntry.remove(function () {
  1026. fileEntry.file(succeed.bind(null, done, 'fileEntry.file - Unexpected success callback, file it should not be created from removed entry'), function (error) {
  1027. expect(error).toBeDefined();
  1028. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1029. done();
  1030. });
  1031. }, failed.bind(null, done, 'fileEntry.remove - Error removing entry : ' + fileName));
  1032. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  1033. });
  1034. });
  1035. //FileEntry
  1036. describe('Entry', function () {
  1037. it("file.spec.45 Entry object", function (done) {
  1038. var fileName = "entry",
  1039. fullPath = joinURL(root.fullPath, fileName),
  1040. winEntry = function (entry) {
  1041. expect(entry).toBeDefined();
  1042. expect(entry.isFile).toBe(true);
  1043. expect(entry.isDirectory).toBe(false);
  1044. expect(entry.name).toCanonicallyMatch(fileName);
  1045. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  1046. expect(typeof entry.getMetadata).toBe('function');
  1047. expect(typeof entry.setMetadata).toBe('function');
  1048. expect(typeof entry.moveTo).toBe('function');
  1049. expect(typeof entry.copyTo).toBe('function');
  1050. expect(typeof entry.toURL).toBe('function');
  1051. expect(typeof entry.remove).toBe('function');
  1052. expect(typeof entry.getParent).toBe('function');
  1053. expect(typeof entry.createWriter).toBe('function');
  1054. expect(typeof entry.file).toBe('function');
  1055. // Clean up
  1056. deleteEntry(fileName, done);
  1057. };
  1058. // create a new file entry
  1059. createFile(fileName, winEntry, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1060. });
  1061. it("file.spec.46 Entry.getMetadata on file", function (done) {
  1062. var fileName = "entry.metadata.file";
  1063. // create a new file entry
  1064. createFile(fileName, function (entry) {
  1065. entry.getMetadata(function (metadata) {
  1066. expect(metadata).toBeDefined();
  1067. expect(metadata.modificationTime instanceof Date).toBe(true);
  1068. expect(typeof metadata.size).toBe("number");
  1069. // cleanup
  1070. deleteEntry(fileName, done);
  1071. }, failed.bind(null, done, 'entry.getMetadata - Error getting metadata from entry : ' + fileName));
  1072. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1073. });
  1074. it("file.spec.47 Entry.getMetadata on directory", function (done) {
  1075. if (isIndexedDBShim) {
  1076. /* Does not support metadata for directories (Firefox, IE) */
  1077. pending();
  1078. }
  1079. var dirName = "entry.metadata.dir";
  1080. // create a new directory entry
  1081. createDirectory(dirName, function (entry) {
  1082. entry.getMetadata(function (metadata) {
  1083. expect(metadata).toBeDefined();
  1084. expect(metadata.modificationTime instanceof Date).toBe(true);
  1085. expect(typeof metadata.size).toBe("number");
  1086. expect(metadata.size).toBe(0);
  1087. // cleanup
  1088. deleteEntry(dirName, done);
  1089. }, failed.bind(null, done, 'entry.getMetadata - Error getting metadata from entry : ' + dirName));
  1090. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1091. });
  1092. it("file.spec.48 Entry.getParent on file in root file system", function (done) {
  1093. var fileName = "entry.parent.file",
  1094. rootPath = root.fullPath;
  1095. // create a new file entry
  1096. createFile(fileName, function (entry) {
  1097. entry.getParent(function (parent) {
  1098. expect(parent).toBeDefined();
  1099. expect(parent.fullPath).toCanonicallyMatch(rootPath);
  1100. // cleanup
  1101. deleteEntry(fileName, done);
  1102. }, failed.bind(null, done, 'entry.getParent - Error getting parent directory of file : ' + fileName));
  1103. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1104. });
  1105. it("file.spec.49 Entry.getParent on directory in root file system", function (done) {
  1106. var dirName = "entry.parent.dir",
  1107. rootPath = root.fullPath;
  1108. // create a new directory entry
  1109. createDirectory(dirName, function (entry) {
  1110. entry.getParent(function (parent) {
  1111. expect(parent).toBeDefined();
  1112. expect(parent.fullPath).toCanonicallyMatch(rootPath);
  1113. // cleanup
  1114. deleteEntry(dirName, done);
  1115. }, failed.bind(null, done, 'entry.getParent - Error getting parent directory of directory : ' + dirName));
  1116. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1117. });
  1118. it("file.spec.50 Entry.getParent on root file system", function (done) {
  1119. var rootPath = root.fullPath,
  1120. winParent = function (parent) {
  1121. expect(parent).toBeDefined();
  1122. expect(parent.fullPath).toCanonicallyMatch(rootPath);
  1123. done();
  1124. };
  1125. // create a new directory entry
  1126. root.getParent(winParent, failed.bind(null, done, 'root.getParent - Error getting parent directory of root'));
  1127. });
  1128. it("file.spec.51 Entry.toURL on file", function (done) {
  1129. var fileName = "entry.uri.file",
  1130. rootPath = root.fullPath,
  1131. winURI = function (entry) {
  1132. var uri = entry.toURL();
  1133. expect(uri).toBeDefined();
  1134. expect(uri.indexOf(rootPath)).not.toBe(-1);
  1135. // cleanup
  1136. deleteEntry(fileName, done);
  1137. };
  1138. // create a new file entry
  1139. createFile(fileName, winURI, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1140. });
  1141. it("file.spec.52 Entry.toURL on directory", function (done) {
  1142. var dirName_1 = "num 1",
  1143. dirName_2 = "num 2",
  1144. rootPath = root.fullPath;
  1145. createDirectory(dirName_1, function (entry) {
  1146. entry.getDirectory(dirName_2, {
  1147. create : true
  1148. }, function (entryFile) {
  1149. var uri = entryFile.toURL();
  1150. expect(uri).toBeDefined();
  1151. expect(uri).toContain('/num%201/num%202/');
  1152. expect(uri.indexOf(rootPath)).not.toBe(-1);
  1153. // cleanup
  1154. deleteEntry(dirName_1, done);
  1155. }, failed.bind(null, done, 'entry.getDirectory - Error creating directory : ' + dirName_2));
  1156. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName_1));
  1157. });
  1158. it("file.spec.53 Entry.remove on file", function (done) {
  1159. var fileName = "entr .rm.file";
  1160. // create a new file entry
  1161. createFile(fileName, function (entry) {
  1162. expect(entry).toBeDefined();
  1163. entry.remove(function () {
  1164. root.getFile(fileName, null, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get deleted file : ' + fileName), function (error) {
  1165. expect(error).toBeDefined();
  1166. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1167. // cleanup
  1168. deleteEntry(fileName, done);
  1169. });
  1170. }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + fileName));
  1171. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1172. });
  1173. it("file.spec.53.1 Entry.remove on filename with #s", function (done) {
  1174. var fileName = "entry.#rm#.file";
  1175. // create a new file entry
  1176. createFile(fileName, function (entry) {
  1177. expect(entry).toBeDefined();
  1178. entry.remove(function () {
  1179. root.getFile(fileName, null, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get deleted file : ' + fileName), function (error) {
  1180. expect(error).toBeDefined();
  1181. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1182. // cleanup
  1183. deleteEntry(fileName, done);
  1184. });
  1185. }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + fileName));
  1186. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1187. });
  1188. it("file.spec.54 remove on empty directory", function (done) {
  1189. var dirName = "entry.rm.dir";
  1190. // create a new directory entry
  1191. createDirectory(dirName, function (entry) {
  1192. expect(entry).toBeDefined();
  1193. entry.remove(function () {
  1194. root.getDirectory(dirName, null, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, it should not get deleted directory : ' + dirName), function (error) {
  1195. expect(error).toBeDefined();
  1196. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1197. // cleanup
  1198. deleteEntry(dirName, done);
  1199. });
  1200. }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + dirName));
  1201. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1202. });
  1203. it("file.spec.55 remove on non-empty directory", function (done) {
  1204. if (isIndexedDBShim) {
  1205. /* Both Entry.remove and directoryEntry.removeRecursively don't fail when removing
  1206. non-empty directories - directories being removed are cleaned
  1207. along with contents instead (Firefox, IE)*/
  1208. pending();
  1209. }
  1210. var dirName = "ent y.rm.dir.not.empty",
  1211. fileName = "re ove.txt",
  1212. fullPath = joinURL(root.fullPath, dirName);
  1213. // create a new directory entry
  1214. createDirectory(dirName, function (entry) {
  1215. entry.getFile(fileName, {
  1216. create : true
  1217. }, function (fileEntry) {
  1218. entry.remove(succeed.bind(null, done, 'entry.remove - Unexpected success callback, it should not remove a directory that contains files : ' + dirName), function (error) {
  1219. expect(error).toBeDefined();
  1220. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1221. root.getDirectory(dirName, null, function (entry) {
  1222. expect(entry).toBeDefined();
  1223. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  1224. // cleanup
  1225. deleteEntry(dirName, done);
  1226. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + dirName));
  1227. });
  1228. }, failed.bind(null, done, 'entry.getFile - Error creating file : ' + fileName + ' inside of ' + dirName));
  1229. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1230. });
  1231. it("file.spec.56 remove on root file system", function (done) {
  1232. // remove entry that doesn't exist
  1233. root.remove(succeed.bind(null, done, 'entry.remove - Unexpected success callback, it should not remove entry that it does not exists'), function (error) {
  1234. expect(error).toBeDefined();
  1235. if (isChrome) {
  1236. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of
  1237. NO_MODIFICATION_ALLOWED_ERR(code: 6) on trying to call removeRecursively
  1238. on the root file system.*/
  1239. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1240. } else {
  1241. expect(error).toBeFileError(FileError.NO_MODIFICATION_ALLOWED_ERR);
  1242. }
  1243. done();
  1244. });
  1245. });
  1246. it("file.spec.57 copyTo: file", function (done) {
  1247. var file1 = "entry copy.file1",
  1248. file2 = "entry copy.file2",
  1249. fullPath = joinURL(root.fullPath, file2);
  1250. // create a new file entry to kick off it
  1251. deleteEntry(file2, function () {
  1252. createFile(file1, function (fileEntry) {
  1253. // copy file1 to file2
  1254. fileEntry.copyTo(root, file2, function (entry) {
  1255. expect(entry).toBeDefined();
  1256. expect(entry.isFile).toBe(true);
  1257. expect(entry.isDirectory).toBe(false);
  1258. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  1259. expect(entry.name).toCanonicallyMatch(file2);
  1260. root.getFile(file2, {
  1261. create : false
  1262. }, function (entry2) {
  1263. expect(entry2).toBeDefined();
  1264. expect(entry2.isFile).toBe(true);
  1265. expect(entry2.isDirectory).toBe(false);
  1266. expect(entry2.fullPath).toCanonicallyMatch(fullPath);
  1267. expect(entry2.name).toCanonicallyMatch(file2);
  1268. // cleanup
  1269. deleteEntry(file1, function () {
  1270. deleteEntry(file2, done);
  1271. });
  1272. }, failed.bind(null, done, 'root.getFile - Error getting copied file : ' + file2));
  1273. }, failed.bind(null, done, 'fileEntry.copyTo - Error copying file : ' + file2));
  1274. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1275. }, failed.bind(null, done, 'deleteEntry - Error removing file : ' + file2));
  1276. });
  1277. it("file.spec.58 copyTo: file onto itself", function (done) {
  1278. var file1 = "entry.copy.fos.file1";
  1279. // create a new file entry to kick off it
  1280. createFile(file1, function (entry) {
  1281. // copy file1 onto itself
  1282. entry.copyTo(root, null, succeed.bind(null, done, 'entry.copyTo - Unexpected success callback, it should not copy a null file'), function (error) {
  1283. expect(error).toBeDefined();
  1284. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1285. // cleanup
  1286. deleteEntry(file1, done);
  1287. });
  1288. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1289. });
  1290. it("file.spec.59 copyTo: directory", function (done) {
  1291. if (isIndexedDBShim) {
  1292. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1293. pending();
  1294. }
  1295. var file1 = "file1",
  1296. srcDir = "entry.copy.srcDir",
  1297. dstDir = "entry.copy.dstDir",
  1298. dstPath = joinURL(root.fullPath, dstDir),
  1299. filePath = joinURL(dstPath, file1);
  1300. // create a new directory entry to kick off it
  1301. deleteEntry(dstDir, function () {
  1302. createDirectory(srcDir, function (directory) {
  1303. // create a file within new directory
  1304. directory.getFile(file1, {
  1305. create : true
  1306. }, function () {
  1307. directory.copyTo(root, dstDir, function (directory) {
  1308. expect(directory).toBeDefined();
  1309. expect(directory.isFile).toBe(false);
  1310. expect(directory.isDirectory).toBe(true);
  1311. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1312. expect(directory.name).toCanonicallyMatch(dstDir);
  1313. root.getDirectory(dstDir, {
  1314. create : false
  1315. }, function (dirEntry) {
  1316. expect(dirEntry).toBeDefined();
  1317. expect(dirEntry.isFile).toBe(false);
  1318. expect(dirEntry.isDirectory).toBe(true);
  1319. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  1320. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  1321. dirEntry.getFile(file1, {
  1322. create : false
  1323. }, function (fileEntry) {
  1324. expect(fileEntry).toBeDefined();
  1325. expect(fileEntry.isFile).toBe(true);
  1326. expect(fileEntry.isDirectory).toBe(false);
  1327. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1328. expect(fileEntry.name).toCanonicallyMatch(file1);
  1329. // cleanup
  1330. deleteEntry(srcDir, function () {
  1331. deleteEntry(dstDir, done);
  1332. });
  1333. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
  1334. }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory : ' + dstDir));
  1335. }, failed.bind(null, done, 'directory.copyTo - Error copying directory : ' + srcDir + ' to :' + dstDir));
  1336. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1337. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1338. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1339. });
  1340. it("file.spec.60 copyTo: directory to backup at same root directory", function (done) {
  1341. if (isIndexedDBShim) {
  1342. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1343. pending();
  1344. }
  1345. var file1 = "file1",
  1346. srcDir = "entry.copy srcDirSame",
  1347. dstDir = "entry.copy srcDirSame-backup",
  1348. dstPath = joinURL(root.fullPath, dstDir),
  1349. filePath = joinURL(dstPath, file1);
  1350. // create a new directory entry to kick off it
  1351. deleteEntry(dstDir, function () {
  1352. createDirectory(srcDir, function (directory) {
  1353. directory.getFile(file1, {
  1354. create : true
  1355. }, function () {
  1356. directory.copyTo(root, dstDir, function (directory) {
  1357. expect(directory).toBeDefined();
  1358. expect(directory.isFile).toBe(false);
  1359. expect(directory.isDirectory).toBe(true);
  1360. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1361. expect(directory.name).toCanonicallyMatch(dstDir);
  1362. root.getDirectory(dstDir, {
  1363. create : false
  1364. }, function (dirEntry) {
  1365. expect(dirEntry).toBeDefined();
  1366. expect(dirEntry.isFile).toBe(false);
  1367. expect(dirEntry.isDirectory).toBe(true);
  1368. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  1369. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  1370. dirEntry.getFile(file1, {
  1371. create : false
  1372. }, function (fileEntry) {
  1373. expect(fileEntry).toBeDefined();
  1374. expect(fileEntry.isFile).toBe(true);
  1375. expect(fileEntry.isDirectory).toBe(false);
  1376. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1377. expect(fileEntry.name).toCanonicallyMatch(file1);
  1378. // cleanup
  1379. deleteEntry(srcDir, function () {
  1380. deleteEntry(dstDir, done);
  1381. });
  1382. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
  1383. }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory : ' + dstDir));
  1384. }, failed.bind(null, done, 'directory.copyTo - Error copying directory : ' + srcDir + ' to :' + dstDir));
  1385. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1386. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1387. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1388. });
  1389. it("file.spec.61 copyTo: directory onto itself", function (done) {
  1390. if (isIndexedDBShim) {
  1391. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1392. pending();
  1393. }
  1394. var file1 = "file1",
  1395. srcDir = "entry.copy.dos.srcDir",
  1396. srcPath = joinURL(root.fullPath, srcDir),
  1397. filePath = joinURL(srcPath, file1);
  1398. // create a new directory entry to kick off it
  1399. createDirectory(srcDir, function (directory) {
  1400. // create a file within new directory
  1401. directory.getFile(file1, {
  1402. create : true
  1403. }, function (fileEntry) {
  1404. // copy srcDir onto itself
  1405. 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) {
  1406. expect(error).toBeDefined();
  1407. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1408. root.getDirectory(srcDir, {
  1409. create : false
  1410. }, function (dirEntry) {
  1411. expect(dirEntry).toBeDefined();
  1412. expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
  1413. dirEntry.getFile(file1, {
  1414. create : false
  1415. }, function (fileEntry) {
  1416. expect(fileEntry).toBeDefined();
  1417. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1418. // cleanup
  1419. deleteEntry(srcDir, done);
  1420. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
  1421. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
  1422. });
  1423. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1424. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1425. });
  1426. it("file.spec.62 copyTo: directory into itself", function (done) {
  1427. if (isIndexedDBShim) {
  1428. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1429. pending();
  1430. }
  1431. var srcDir = "entry.copy.dis.srcDir",
  1432. dstDir = "entry.copy.dis.dstDir",
  1433. srcPath = joinURL(root.fullPath, srcDir);
  1434. // create a new directory entry to kick off it
  1435. createDirectory(srcDir, function (directory) {
  1436. // copy source directory into itself
  1437. directory.copyTo(directory, dstDir, succeed.bind(null, done, 'directory.copyTo - Unexpected success callback, it should not copy a directory ' + srcDir + ' into itself'), function (error) {
  1438. expect(error).toBeDefined();
  1439. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1440. root.getDirectory(srcDir, {
  1441. create : false
  1442. }, function (dirEntry) {
  1443. // returning confirms existence so just check fullPath entry
  1444. expect(dirEntry).toBeDefined();
  1445. expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
  1446. // cleanup
  1447. deleteEntry(srcDir, done);
  1448. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
  1449. });
  1450. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1451. });
  1452. it("file.spec.63 copyTo: directory that does not exist", function (done) {
  1453. var file1 = "entry.copy.dnf.file1",
  1454. dirName = 'dir-foo';
  1455. createFile(file1, function (fileEntry) {
  1456. createDirectory(dirName, function (dirEntry) {
  1457. dirEntry.remove(function () {
  1458. 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) {
  1459. expect(error).toBeDefined();
  1460. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1461. done();
  1462. });
  1463. }, failed.bind(null, done, 'dirEntry.remove - Error removing directory : ' + dirName));
  1464. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1465. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1466. });
  1467. it("file.spec.64 copyTo: invalid target name", function (done) {
  1468. if (isBrowser) {
  1469. /*The plugin does not follow ["8.3 Naming restrictions"]
  1470. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions*/
  1471. pending();
  1472. }
  1473. var file1 = "entry.copy.itn.file1",
  1474. file2 = "bad:file:name";
  1475. // create a new file entry
  1476. createFile(file1, function (entry) {
  1477. // copy file1 to file2
  1478. 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) {
  1479. expect(error).toBeDefined();
  1480. expect(error).toBeFileError(FileError.ENCODING_ERR);
  1481. // cleanup
  1482. deleteEntry(file1, done);
  1483. });
  1484. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1485. });
  1486. it("file.spec.65 moveTo: file to same parent", function (done) {
  1487. var file1 = "entry.move.fsp.file1",
  1488. file2 = "entry.move.fsp.file2",
  1489. dstPath = joinURL(root.fullPath, file2);
  1490. // create a new file entry to kick off it
  1491. createFile(file1, function (entry) {
  1492. // move file1 to file2
  1493. entry.moveTo(root, file2, function (entry) {
  1494. expect(entry).toBeDefined();
  1495. expect(entry.isFile).toBe(true);
  1496. expect(entry.isDirectory).toBe(false);
  1497. expect(entry.fullPath).toCanonicallyMatch(dstPath);
  1498. expect(entry.name).toCanonicallyMatch(file2);
  1499. root.getFile(file2, {
  1500. create : false
  1501. }, function (fileEntry) {
  1502. expect(fileEntry).toBeDefined();
  1503. expect(fileEntry.fullPath).toCanonicallyMatch(dstPath);
  1504. root.getFile(file1, {
  1505. create : false
  1506. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1507. //expect(navigator.fileMgr.testFileExists(srcPath) === false, "original file should not exist.");
  1508. expect(error).toBeDefined();
  1509. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1510. // cleanup
  1511. deleteEntry(file1, function () {
  1512. deleteEntry(file2, done);
  1513. });
  1514. });
  1515. }, failed.bind(null, done, 'root.getFile - Error getting file : ' + file2));
  1516. }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to root as: ' + file2));
  1517. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1518. });
  1519. it("file.spec.66 moveTo: file to new parent", function (done) {
  1520. var file1 = "entry.move.fnp.file1",
  1521. dir = "entry.move.fnp.dir",
  1522. dstPath = joinURL(joinURL(root.fullPath, dir), file1);
  1523. // ensure destination directory is cleaned up first
  1524. deleteEntry(dir, function () {
  1525. // create a new file entry to kick off it
  1526. createFile(file1, function (entry) {
  1527. // create a parent directory to move file to
  1528. root.getDirectory(dir, {
  1529. create : true
  1530. }, function (directory) {
  1531. // move file1 to new directory
  1532. // move the file
  1533. entry.moveTo(directory, null, function (entry) {
  1534. expect(entry).toBeDefined();
  1535. expect(entry.isFile).toBe(true);
  1536. expect(entry.isDirectory).toBe(false);
  1537. expect(entry.fullPath).toCanonicallyMatch(dstPath);
  1538. expect(entry.name).toCanonicallyMatch(file1);
  1539. // test the moved file exists
  1540. directory.getFile(file1, {
  1541. create : false
  1542. }, function (fileEntry) {
  1543. expect(fileEntry).toBeDefined();
  1544. expect(fileEntry.fullPath).toCanonicallyMatch(dstPath);
  1545. root.getFile(file1, {
  1546. create : false
  1547. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1548. expect(error).toBeDefined();
  1549. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1550. // cleanup
  1551. deleteEntry(file1, function () {
  1552. deleteEntry(dir, done);
  1553. });
  1554. });
  1555. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + dir));
  1556. }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to: ' + dir + ' with the same name'));
  1557. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dir));
  1558. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1559. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dir));
  1560. });
  1561. it("file.spec.67 moveTo: directory to same parent", function (done) {
  1562. if (isIndexedDBShim) {
  1563. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1564. pending();
  1565. }
  1566. var file1 = "file1",
  1567. srcDir = "entry.move.dsp.srcDir",
  1568. dstDir = "entry.move.dsp.dstDir",
  1569. dstPath = joinURL(root.fullPath, dstDir),
  1570. filePath = joinURL(dstPath, file1);
  1571. // ensure destination directory is cleaned up before it
  1572. deleteEntry(dstDir, function () {
  1573. // create a new directory entry to kick off it
  1574. createDirectory(srcDir, function (directory) {
  1575. // create a file within directory
  1576. directory.getFile(file1, {
  1577. create : true
  1578. }, function () {
  1579. // move srcDir to dstDir
  1580. directory.moveTo(root, dstDir, function (directory) {
  1581. expect(directory).toBeDefined();
  1582. expect(directory.isFile).toBe(false);
  1583. expect(directory.isDirectory).toBe(true);
  1584. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1585. expect(directory.name).toCanonicallyMatch(dstDir);
  1586. // test that moved file exists in destination dir
  1587. directory.getFile(file1, {
  1588. create : false
  1589. }, function (fileEntry) {
  1590. expect(fileEntry).toBeDefined();
  1591. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1592. // check that the moved file no longer exists in original dir
  1593. root.getFile(file1, {
  1594. create : false
  1595. }, succeed.bind(null, done, 'directory.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1596. expect(error).toBeDefined();
  1597. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1598. // cleanup
  1599. deleteEntry(srcDir, function() {
  1600. deleteEntry(dstDir, done);
  1601. });
  1602. });
  1603. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
  1604. }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  1605. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1606. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1607. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1608. });
  1609. it("file.spec.68 moveTo: directory to same parent with same name", function (done) {
  1610. if (isIndexedDBShim) {
  1611. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1612. pending();
  1613. }
  1614. var file1 = "file1",
  1615. srcDir = "entry.move.dsp.srcDir",
  1616. dstDir = "entry.move.dsp.srcDir-backup",
  1617. dstPath = joinURL(root.fullPath, dstDir),
  1618. filePath = joinURL(dstPath, file1);
  1619. // ensure destination directory is cleaned up before it
  1620. deleteEntry(dstDir, function () {
  1621. // create a new directory entry to kick off it
  1622. createDirectory(srcDir, function (directory) {
  1623. // create a file within directory
  1624. directory.getFile(file1, {
  1625. create : true
  1626. }, function () {
  1627. // move srcDir to dstDir
  1628. directory.moveTo(root, dstDir, function (directory) {
  1629. expect(directory).toBeDefined();
  1630. expect(directory.isFile).toBe(false);
  1631. expect(directory.isDirectory).toBe(true);
  1632. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1633. expect(directory.name).toCanonicallyMatch(dstDir);
  1634. // check that moved file exists in destination dir
  1635. directory.getFile(file1, {
  1636. create : false
  1637. }, function (fileEntry) {
  1638. expect(fileEntry).toBeDefined();
  1639. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1640. // check that the moved file no longer exists in original dir
  1641. root.getFile(file1, {
  1642. create : false
  1643. }, succeed.bind(null, done, 'directory.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1644. expect(error).toBeDefined();
  1645. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1646. // cleanup
  1647. deleteEntry(srcDir, function() {
  1648. deleteEntry(dstDir, done);
  1649. });
  1650. });
  1651. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
  1652. }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  1653. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1654. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1655. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1656. });
  1657. it("file.spec.69 moveTo: directory to new parent", function (done) {
  1658. if (isIndexedDBShim) {
  1659. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1660. pending();
  1661. }
  1662. var file1 = "file1",
  1663. srcDir = "entry.move.dnp.srcDir",
  1664. dstDir = "entry.move.dnp.dstDir",
  1665. dstPath = joinURL(root.fullPath, dstDir),
  1666. filePath = joinURL(dstPath, file1);
  1667. // ensure destination directory is cleaned up before it
  1668. deleteEntry(dstDir, function () {
  1669. // create a new directory entry to kick off it
  1670. createDirectory(srcDir, function (directory) {
  1671. // create a file within directory
  1672. directory.getFile(file1, {
  1673. create : true
  1674. }, function () {
  1675. // move srcDir to dstDir
  1676. directory.moveTo(root, dstDir, function (dirEntry) {
  1677. expect(dirEntry).toBeDefined();
  1678. expect(dirEntry.isFile).toBe(false);
  1679. expect(dirEntry.isDirectory).toBe(true);
  1680. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  1681. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  1682. // test that moved file exists in destination dir
  1683. dirEntry.getFile(file1, {
  1684. create : false
  1685. }, function (fileEntry) {
  1686. expect(fileEntry).toBeDefined();
  1687. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1688. // test that the moved file no longer exists in original dir
  1689. root.getFile(file1, {
  1690. create : false
  1691. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1692. expect(error).toBeDefined();
  1693. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1694. // cleanup
  1695. deleteEntry(srcDir, function() {
  1696. deleteEntry(dstDir, done);
  1697. });
  1698. });
  1699. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + dstDir));
  1700. }, failed.bind(null, done, 'directory.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  1701. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1702. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1703. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1704. });
  1705. it("file.spec.70 moveTo: directory onto itself", function (done) {
  1706. if (isIndexedDBShim) {
  1707. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1708. pending();
  1709. }
  1710. var file1 = "file1",
  1711. srcDir = "entry.move.dos.srcDir",
  1712. srcPath = joinURL(root.fullPath, srcDir),
  1713. filePath = joinURL(srcPath, file1);
  1714. // create a new directory entry to kick off it
  1715. createDirectory(srcDir, function (directory) {
  1716. // create a file within new directory
  1717. directory.getFile(file1, {
  1718. create : true
  1719. }, function () {
  1720. // move srcDir onto itself
  1721. directory.moveTo(root, null, succeed.bind(null, done, 'directory.moveTo - Unexpected success callback, it should not move directory to invalid path'), function (error) {
  1722. expect(error).toBeDefined();
  1723. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1724. // test that original dir still exists
  1725. root.getDirectory(srcDir, {
  1726. create : false
  1727. }, function (dirEntry) {
  1728. // returning confirms existence so just check fullPath entry
  1729. expect(dirEntry).toBeDefined();
  1730. expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
  1731. dirEntry.getFile(file1, {
  1732. create : false
  1733. }, function (fileEntry) {
  1734. expect(fileEntry).toBeDefined();
  1735. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1736. // cleanup
  1737. deleteEntry(srcDir, done);
  1738. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
  1739. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
  1740. });
  1741. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1742. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1743. });
  1744. it("file.spec.71 moveTo: directory into itself", function (done) {
  1745. if (isIndexedDBShim) {
  1746. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1747. pending();
  1748. }
  1749. var srcDir = "entry.move.dis.srcDir",
  1750. dstDir = "entry.move.dis.dstDir",
  1751. srcPath = joinURL(root.fullPath, srcDir);
  1752. // create a new directory entry to kick off it
  1753. createDirectory(srcDir, function (directory) {
  1754. // move source directory into itself
  1755. directory.moveTo(directory, dstDir, succeed.bind(null, done, 'directory.moveTo - Unexpected success callback, it should not move a directory into itself: ' + srcDir), function (error) {
  1756. expect(error).toBeDefined();
  1757. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1758. // make sure original directory still exists
  1759. root.getDirectory(srcDir, {
  1760. create : false
  1761. }, function (entry) {
  1762. expect(entry).toBeDefined();
  1763. expect(entry.fullPath).toCanonicallyMatch(srcPath);
  1764. // cleanup
  1765. deleteEntry(srcDir, done);
  1766. }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  1767. });
  1768. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1769. });
  1770. it("file.spec.130 moveTo: directory into similar directory", function (done) {
  1771. if (isIndexedDBShim) {
  1772. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1773. pending();
  1774. }
  1775. var srcDir = "entry.move.dis.srcDir",
  1776. dstDir = "entry.move.dis.srcDir-backup";
  1777. // create a new directory entry to kick off it
  1778. createDirectory(srcDir, function (srcDirEntry) {
  1779. deleteEntry(dstDir, function () {
  1780. createDirectory(dstDir, function (dstDirEntry) {
  1781. // move source directory into itself
  1782. srcDirEntry.moveTo(dstDirEntry, 'file', function (newDirEntry) {
  1783. expect(newDirEntry).toBeDefined();
  1784. deleteEntry(dstDir, done);
  1785. }, failed.bind(null, done, 'directory.moveTo - Error moving a directory into a similarly-named directory: ' + srcDir));
  1786. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dstDir));
  1787. }, failed.bind(null, done, 'deleteEntry - Error deleting directory : ' + dstDir));
  1788. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1789. });
  1790. it("file.spec.72 moveTo: file onto itself", function (done) {
  1791. var file1 = "entry.move.fos.file1",
  1792. filePath = joinURL(root.fullPath, file1);
  1793. // create a new file entry to kick off it
  1794. createFile(file1, function (entry) {
  1795. // move file1 onto itself
  1796. 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) {
  1797. expect(error).toBeDefined();
  1798. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1799. //test that original file still exists
  1800. root.getFile(file1, {
  1801. create : false
  1802. }, function (fileEntry) {
  1803. expect(fileEntry).toBeDefined();
  1804. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1805. // cleanup
  1806. deleteEntry(file1, done);
  1807. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1808. });
  1809. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1810. });
  1811. it("file.spec.73 moveTo: file onto existing directory", function (done) {
  1812. var file1 = "entry.move.fod.file1",
  1813. dstDir = "entry.move.fod.dstDir",
  1814. subDir = "subDir",
  1815. dirPath = joinURL(joinURL(root.fullPath, dstDir), subDir),
  1816. filePath = joinURL(root.fullPath, file1);
  1817. // ensure destination directory is cleaned up before it
  1818. deleteEntry(dstDir, function () {
  1819. // create a new file entry to kick off it
  1820. createFile(file1, function (entry) {
  1821. // create top level directory
  1822. root.getDirectory(dstDir, {
  1823. create : true
  1824. }, function (directory) {
  1825. // create sub-directory
  1826. directory.getDirectory(subDir, {
  1827. create : true
  1828. }, function (subDirectory) {
  1829. // move file1 onto sub-directory
  1830. 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) {
  1831. expect(error).toBeDefined();
  1832. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1833. // check that original dir still exists
  1834. directory.getDirectory(subDir, {
  1835. create : false
  1836. }, function (dirEntry) {
  1837. expect(dirEntry).toBeDefined();
  1838. expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
  1839. // check that original file still exists
  1840. root.getFile(file1, {
  1841. create : false
  1842. }, function (fileEntry) {
  1843. expect(fileEntry).toBeDefined();
  1844. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1845. // cleanup
  1846. deleteEntry(file1, function () {
  1847. deleteEntry(dstDir, done);
  1848. });
  1849. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1850. }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + subDir));
  1851. });
  1852. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
  1853. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dstDir));
  1854. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1855. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1856. });
  1857. it("file.spec.74 moveTo: directory onto existing file", function (done) {
  1858. if (isIndexedDBShim) {
  1859. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1860. pending();
  1861. }
  1862. var file1 = "entry.move.dof.file1",
  1863. srcDir = "entry.move.dof.srcDir",
  1864. dirPath = joinURL(root.fullPath, srcDir),
  1865. filePath = joinURL(root.fullPath, file1);
  1866. // create a new directory entry to kick off it
  1867. createDirectory(srcDir, function (entry) {
  1868. // create file
  1869. root.getFile(file1, {
  1870. create : true
  1871. }, function (fileEntry) {
  1872. // move directory onto file
  1873. 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) {
  1874. expect(error).toBeDefined();
  1875. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1876. // test that original directory exists
  1877. root.getDirectory(srcDir, {
  1878. create : false
  1879. }, function (dirEntry) {
  1880. // returning confirms existence so just check fullPath entry
  1881. expect(dirEntry).toBeDefined();
  1882. expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
  1883. // test that original file exists
  1884. root.getFile(file1, {
  1885. create : false
  1886. }, function (fileEntry) {
  1887. expect(fileEntry).toBeDefined();
  1888. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1889. // cleanup
  1890. deleteEntry(file1, function () {
  1891. deleteEntry(srcDir, done);
  1892. });
  1893. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1894. }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  1895. });
  1896. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + file1));
  1897. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1898. });
  1899. it("file.spec.75 copyTo: directory onto existing file", function (done) {
  1900. if (isIndexedDBShim) {
  1901. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1902. pending();
  1903. }
  1904. var file1 = "entry.copy.dof.file1",
  1905. srcDir = "entry.copy.dof.srcDir",
  1906. dirPath = joinURL(root.fullPath, srcDir),
  1907. filePath = joinURL(root.fullPath, file1);
  1908. // create a new directory entry to kick off it
  1909. createDirectory(srcDir, function (entry) {
  1910. // create file
  1911. root.getFile(file1, {
  1912. create : true
  1913. }, function () {
  1914. // copy directory onto file
  1915. 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) {
  1916. expect(error).toBeDefined();
  1917. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1918. //check that original dir still exists
  1919. root.getDirectory(srcDir, {
  1920. create : false
  1921. }, function (dirEntry) {
  1922. // returning confirms existence so just check fullPath entry
  1923. expect(dirEntry).toBeDefined();
  1924. expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
  1925. // test that original file still exists
  1926. root.getFile(file1, {
  1927. create : false
  1928. }, function (fileEntry) {
  1929. expect(fileEntry).toBeDefined();
  1930. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1931. // cleanup
  1932. deleteEntry(file1, function () {
  1933. deleteEntry(srcDir, done);
  1934. });
  1935. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1936. }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  1937. });
  1938. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + file1));
  1939. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1940. });
  1941. it("file.spec.76 moveTo: directory onto directory that is not empty", function (done) {
  1942. if (isIndexedDBShim) {
  1943. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1944. pending();
  1945. }
  1946. var srcDir = "entry.move.dod.srcDir",
  1947. dstDir = "entry.move.dod.dstDir",
  1948. subDir = "subDir",
  1949. srcPath = joinURL(root.fullPath, srcDir),
  1950. dstPath = joinURL(joinURL(root.fullPath, dstDir), subDir);
  1951. // ensure destination directory is cleaned up before it
  1952. deleteEntry(dstDir, function () {
  1953. // create a new file entry to kick off it
  1954. createDirectory(srcDir, function (entry) {
  1955. // create top level directory
  1956. root.getDirectory(dstDir, {
  1957. create : true
  1958. }, function (directory) {
  1959. // create sub-directory
  1960. directory.getDirectory(subDir, {
  1961. create : true
  1962. }, function () {
  1963. // move srcDir onto dstDir (not empty)
  1964. 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) {
  1965. expect(error).toBeDefined();
  1966. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1967. // making sure destination directory still exists
  1968. directory.getDirectory(subDir, {
  1969. create : false
  1970. }, function (dirEntry) {
  1971. // returning confirms existence so just check fullPath entry
  1972. expect(dirEntry).toBeDefined();
  1973. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  1974. // making sure source directory exists
  1975. root.getDirectory(srcDir, {
  1976. create : false
  1977. }, function (srcEntry) {
  1978. expect(srcEntry).toBeDefined();
  1979. expect(srcEntry.fullPath).toCanonicallyMatch(srcPath);
  1980. // cleanup
  1981. deleteEntry(srcDir, function () {
  1982. deleteEntry(dstDir, done);
  1983. });
  1984. }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  1985. }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + subDir));
  1986. });
  1987. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
  1988. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
  1989. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1990. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1991. });
  1992. it("file.spec.77 moveTo: file replace existing file", function (done) {
  1993. var file1 = "entry.move.frf.file1",
  1994. file2 = "entry.move.frf.file2",
  1995. file2Path = joinURL(root.fullPath, file2);
  1996. // create a new directory entry to kick off it
  1997. createFile(file1, function (entry) {
  1998. // create file
  1999. root.getFile(file2, {
  2000. create : true
  2001. }, function () {
  2002. // replace file2 with file1
  2003. entry.moveTo(root, file2, function (entry2) {
  2004. expect(entry2).toBeDefined();
  2005. expect(entry2.isFile).toBe(true);
  2006. expect(entry2.isDirectory).toBe(false);
  2007. expect(entry2.fullPath).toCanonicallyMatch(file2Path);
  2008. expect(entry2.name).toCanonicallyMatch(file2);
  2009. // old file should not exists
  2010. root.getFile(file1, {
  2011. create : false
  2012. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, file: ' + file1 + ' should not exists'), function (error) {
  2013. expect(error).toBeDefined();
  2014. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  2015. // test that new file exists
  2016. root.getFile(file2, {
  2017. create : false
  2018. }, function (fileEntry) {
  2019. expect(fileEntry).toBeDefined();
  2020. expect(fileEntry.fullPath).toCanonicallyMatch(file2Path);
  2021. // cleanup
  2022. deleteEntry(file1, function () {
  2023. deleteEntry(file2, done);
  2024. });
  2025. }, failed.bind(null, done, 'root.getFile - Error getting moved file: ' + file2));
  2026. });
  2027. }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to root as: ' + file2));
  2028. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + file2));
  2029. }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
  2030. });
  2031. it("file.spec.78 moveTo: directory replace empty directory", function (done) {
  2032. if (isIndexedDBShim) {
  2033. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  2034. pending();
  2035. }
  2036. var file1 = "file1",
  2037. srcDir = "entry.move.drd.srcDir",
  2038. dstDir = "entry.move.drd.dstDir",
  2039. dstPath = joinURL(root.fullPath, dstDir),
  2040. filePath = dstPath + '/' + file1;
  2041. // ensure destination directory is cleaned up before it
  2042. deleteEntry(dstDir, function () {
  2043. // create a new directory entry to kick off it
  2044. createDirectory(srcDir, function (directory) {
  2045. // create a file within source directory
  2046. directory.getFile(file1, {
  2047. create : true
  2048. }, function () {
  2049. // create destination directory
  2050. root.getDirectory(dstDir, {
  2051. create : true
  2052. }, function () {
  2053. // move srcDir to dstDir
  2054. directory.moveTo(root, dstDir, function (dirEntry) {
  2055. expect(dirEntry).toBeDefined();
  2056. expect(dirEntry.isFile).toBe(false);
  2057. expect(dirEntry.isDirectory).toBe(true);
  2058. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  2059. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  2060. // check that old directory contents have been moved
  2061. dirEntry.getFile(file1, {
  2062. create : false
  2063. }, function (fileEntry) {
  2064. expect(fileEntry).toBeDefined();
  2065. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  2066. // check that old directory no longer exists
  2067. root.getDirectory(srcDir, {
  2068. create : false
  2069. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory: ' + srcDir + ' should not exists'), function (error) {
  2070. expect(error).toBeDefined();
  2071. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  2072. // cleanup
  2073. deleteEntry(srcDir, function () {
  2074. deleteEntry(dstDir, done);
  2075. });
  2076. });
  2077. }, failed.bind(null, done, 'dirEntry.getFile - Error getting moved file: ' + file1));
  2078. }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  2079. }, failed.bind(null, done, 'root.getDirectory - Error creating directory: ' + dstDir));
  2080. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + file1));
  2081. }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + srcDir));
  2082. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  2083. });
  2084. it("file.spec.79 moveTo: directory that does not exist", function (done) {
  2085. var file1 = "entry.move.dnf.file1",
  2086. dstDir = "entry.move.dnf.dstDir",
  2087. dstPath = joinURL(root.fullPath, dstDir);
  2088. // create a new file entry to kick off it
  2089. createFile(file1, function (entry) {
  2090. // move file to directory that does not exist
  2091. var directory = new DirectoryEntry();
  2092. directory.filesystem = root.filesystem;
  2093. directory.fullPath = dstPath;
  2094. entry.moveTo(directory, null, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, parent directory: ' + dstPath + ' should not exists'), function (error) {
  2095. expect(error).toBeDefined();
  2096. if (isChrome) {
  2097. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of NOT_FOUND_ERR(code: 1)
  2098. on trying to moveTo directory that does not exist.*/
  2099. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  2100. } else {
  2101. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  2102. }
  2103. // cleanup
  2104. deleteEntry(file1, done);
  2105. });
  2106. }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
  2107. });
  2108. it("file.spec.80 moveTo: invalid target name", function (done) {
  2109. if (isBrowser) {
  2110. /*The plugin does not follow ["8.3 Naming restrictions"]
  2111. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions*/
  2112. pending();
  2113. }
  2114. var file1 = "entry.move.itn.file1",
  2115. file2 = "bad:file:name";
  2116. // create a new file entry to kick off it
  2117. createFile(file1, function (entry) {
  2118. // move file1 to file2
  2119. entry.moveTo(root, file2, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, : ' + file1 + ' to root as: ' + file2), function (error) {
  2120. expect(error).toBeDefined();
  2121. expect(error).toBeFileError(FileError.ENCODING_ERR);
  2122. // cleanup
  2123. deleteEntry(file1, done);
  2124. });
  2125. }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
  2126. });
  2127. });
  2128. //Entry
  2129. describe('FileReader', function () {
  2130. it("file.spec.81 should have correct methods", function () {
  2131. var reader = new FileReader();
  2132. expect(reader).toBeDefined();
  2133. expect(typeof reader.readAsBinaryString).toBe('function');
  2134. expect(typeof reader.readAsDataURL).toBe('function');
  2135. expect(typeof reader.readAsText).toBe('function');
  2136. expect(typeof reader.readAsArrayBuffer).toBe('function');
  2137. expect(typeof reader.abort).toBe('function');
  2138. expect(reader.result).toBe(null);
  2139. });
  2140. });
  2141. //FileReader
  2142. describe('Read method', function () {
  2143. it("file.spec.82 should error out on non-existent file", function (done) {
  2144. var fileName = cordova.platformId === 'windowsphone' ? root.toURL() + "/" + "somefile.txt" : "somefile.txt",
  2145. verifier = function (evt) {
  2146. expect(evt).toBeDefined();
  2147. expect(evt.target.error).toBeFileError(FileError.NOT_FOUND_ERR);
  2148. done();
  2149. };
  2150. root.getFile(fileName, {
  2151. create : true
  2152. }, function (entry) {
  2153. entry.file(function (file) {
  2154. deleteEntry(fileName, function () {
  2155. //Create FileReader
  2156. var reader = new FileReader();
  2157. reader.onerror = verifier;
  2158. reader.onload = succeed.bind(null, done, 'reader.onload - Unexpected success callback, file: ' + fileName + ' it should not exists');
  2159. reader.readAsText(file);
  2160. }, failed.bind(null, done, 'deleteEntry - Error removing file: ' + fileName));
  2161. }, failed.bind(null, done, 'entry.file - Error reading file: ' + fileName));
  2162. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  2163. });
  2164. it("file.spec.83 should be able to read native blob objects", function (done) {
  2165. // Skip test if blobs are not supported (e.g.: Android 2.3).
  2166. if (typeof window.Blob == 'undefined' || typeof window.Uint8Array == 'undefined') {
  2167. expect(true).toFailWithMessage('Platform does not supported this feature');
  2168. done();
  2169. }
  2170. var contents = 'asdf';
  2171. var uint8Array = new Uint8Array(contents.length);
  2172. for (var i = 0; i < contents.length; ++i) {
  2173. uint8Array[i] = contents.charCodeAt(i);
  2174. }
  2175. var Builder = window.BlobBuilder || window.WebKitBlobBuilder;
  2176. var blob;
  2177. if (Builder) {
  2178. var builder = new Builder();
  2179. builder.append(uint8Array.buffer);
  2180. builder.append(contents);
  2181. blob = builder.getBlob("text/plain");
  2182. } else {
  2183. try {
  2184. // iOS 6 does not support Views, so pass in the buffer.
  2185. blob = new Blob([uint8Array.buffer, contents]);
  2186. } catch (e) {
  2187. // Skip the test if we can't create a blob (e.g.: iOS 5).
  2188. if (e instanceof TypeError) {
  2189. expect(true).toFailWithMessage('Platform does not supported this feature');
  2190. done();
  2191. }
  2192. throw e;
  2193. }
  2194. }
  2195. var verifier = function (evt) {
  2196. expect(evt).toBeDefined();
  2197. expect(evt.target.result).toBe('asdfasdf');
  2198. done();
  2199. };
  2200. var reader = new FileReader();
  2201. reader.onloadend = verifier;
  2202. reader.readAsText(blob);
  2203. });
  2204. function writeDummyFile(writeBinary, callback, done, fileContents) {
  2205. var fileName = "dummy.txt",
  2206. fileEntry = null,
  2207. // use default string if file data is not provided
  2208. fileData = fileContents !== undefined ? fileContents :
  2209. '\u20AC\xEB - There is an exception to every rule. Except this one.',
  2210. fileDataAsBinaryString = fileContents !== undefined ? fileContents :
  2211. '\xe2\x82\xac\xc3\xab - There is an exception to every rule. Except this one.';
  2212. function createWriter(fe) {
  2213. fileEntry = fe;
  2214. fileEntry.createWriter(writeFile, failed.bind(null, done, 'fileEntry.createWriter - Error reading file: ' + fileName));
  2215. }
  2216. // writes file and reads it back in
  2217. function writeFile(writer) {
  2218. writer.onwriteend = function () {
  2219. fileEntry.file(function (f) {
  2220. callback(fileEntry, f, fileData, fileDataAsBinaryString);
  2221. }, failed.bind(null, done, 'writer.onwriteend - Error writing data on file: ' + fileName));
  2222. };
  2223. writer.write(fileData);
  2224. }
  2225. fileData += writeBinary ? 'bin:\x01\x00' : '';
  2226. fileDataAsBinaryString += writeBinary ? 'bin:\x01\x00' : '';
  2227. // create a file, write to it, and read it in again
  2228. createFile(fileName, createWriter, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2229. }
  2230. function runReaderTest(funcName, writeBinary, done, progressFunc, verifierFunc, sliceStart, sliceEnd, fileContents) {
  2231. writeDummyFile(writeBinary, function (fileEntry, file, fileData, fileDataAsBinaryString) {
  2232. var verifier = function (evt) {
  2233. expect(evt).toBeDefined();
  2234. verifierFunc(evt, fileData, fileDataAsBinaryString);
  2235. };
  2236. var reader = new FileReader();
  2237. reader.onprogress = progressFunc;
  2238. reader.onload = verifier;
  2239. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + file + ' using function: ' + funcName);
  2240. if (sliceEnd !== undefined) {
  2241. // 'type' is specified so that is will be preserved in the resulting file:
  2242. // http://www.w3.org/TR/FileAPI/#slice-method-algo -> "6.4.1. The slice method" -> 4. A), 6. c)
  2243. file = file.slice(sliceStart, sliceEnd, file.type);
  2244. } else if (sliceStart !== undefined) {
  2245. file = file.slice(sliceStart, file.size, file.type);
  2246. }
  2247. reader[funcName](file);
  2248. }, done, fileContents);
  2249. }
  2250. function arrayBufferEqualsString(ab, str) {
  2251. var buf = new Uint8Array(ab);
  2252. var match = buf.length == str.length;
  2253. for (var i = 0; match && i < buf.length; i++) {
  2254. match = buf[i] == str.charCodeAt(i);
  2255. }
  2256. return match;
  2257. }
  2258. it("file.spec.84 should read file properly, readAsText", function (done) {
  2259. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2260. expect(evt.target.result).toBe(fileData);
  2261. done();
  2262. });
  2263. });
  2264. it("file.spec.84.1 should read JSON file properly, readAsText", function (done) {
  2265. var testObject = {key1: "value1", key2: 2};
  2266. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2267. expect(evt.target.result).toEqual(JSON.stringify(testObject));
  2268. done();
  2269. }, undefined, undefined, JSON.stringify(testObject));
  2270. });
  2271. it("file.spec.85 should read file properly, Data URI", function (done) {
  2272. runReaderTest('readAsDataURL', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2273. /* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,
  2274. mediatype in IE is always empty (which is the same as `text-plain` according the specification),
  2275. the mediatype in Firefox is always `application/octet-stream`.
  2276. For example, if the content is `abcdefg` then Firefox returns `data:application/octet-stream;base64,YWJjZGVmZw==`,
  2277. IE returns `data:;base64,YWJjZGVmZw==`, Chrome returns `data:<mediatype depending on extension of entry name>;base64,YWJjZGVmZw==`. */
  2278. expect(evt.target.result).toBeDataUrl();
  2279. //The atob function it is completely ignored during mobilespec execution, besides the returned object: evt
  2280. //it is encoded and the atob function is aimed to decode a string. Even with btoa (encode) the function it gets stucked
  2281. //because of the Unicode characters that contains the fileData object.
  2282. //Issue reported at JIRA with all the details: CB-7095
  2283. //expect(evt.target.result.slice(23)).toBe(atob(fileData));
  2284. done();
  2285. });
  2286. });
  2287. it("file.spec.86 should read file properly, readAsBinaryString", function (done) {
  2288. if (isIE) {
  2289. /*`readAsBinaryString` function is not supported by IE and has not the stub.*/
  2290. pending();
  2291. }
  2292. runReaderTest('readAsBinaryString', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2293. expect(evt.target.result).toBe(fileDataAsBinaryString);
  2294. done();
  2295. });
  2296. });
  2297. it("file.spec.87 should read file properly, readAsArrayBuffer", function (done) {
  2298. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2299. if (typeof window.ArrayBuffer == 'undefined') {
  2300. expect(true).toFailWithMessage('Platform does not supported this feature');
  2301. done();
  2302. }
  2303. runReaderTest('readAsArrayBuffer', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2304. expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString)).toBe(true);
  2305. done();
  2306. });
  2307. });
  2308. it("file.spec.88 should read sliced file: readAsText", function (done) {
  2309. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2310. expect(evt.target.result).toBe(fileDataAsBinaryString.slice(10, 40));
  2311. done();
  2312. }, 10, 40);
  2313. });
  2314. it("file.spec.89 should read sliced file: slice past eof", function (done) {
  2315. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2316. expect(evt.target.result).toBe(fileData.slice(-5, 9999));
  2317. done();
  2318. }, -5, 9999);
  2319. });
  2320. it("file.spec.90 should read sliced file: slice to eof", function (done) {
  2321. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2322. expect(evt.target.result).toBe(fileData.slice(-5));
  2323. done();
  2324. }, -5);
  2325. });
  2326. it("file.spec.91 should read empty slice", function (done) {
  2327. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2328. expect(evt.target.result).toBe('');
  2329. done();
  2330. }, 0, 0);
  2331. });
  2332. it("file.spec.92 should read sliced file properly, readAsDataURL", function (done) {
  2333. runReaderTest('readAsDataURL', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2334. /* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,
  2335. mediatype in IE is always empty (which is the same as `text-plain` according the specification),
  2336. the mediatype in Firefox is always `application/octet-stream`.
  2337. For example, if the content is `abcdefg` then Firefox returns `data:application/octet-stream;base64,YWJjZGVmZw==`,
  2338. IE returns `data:;base64,YWJjZGVmZw==`, Chrome returns `data:<mediatype depending on extension of entry name>;base64,YWJjZGVmZw==`. */
  2339. expect(evt.target.result).toBeDataUrl();
  2340. //The atob function it is completely ignored during mobilespec execution, besides the returned object: evt
  2341. //it is encoded and the atob function is aimed to decode a string. Even with btoa (encode) the function it gets stucked
  2342. //because of the Unicode characters that contains the fileData object.
  2343. //Issue reported at JIRA with all the details: CB-7095
  2344. //expect(evt.target.result.slice(23)).toBe(atob(fileDataAsBinaryString.slice(10, -3)));
  2345. done();
  2346. }, 10, -3);
  2347. });
  2348. it("file.spec.93 should read sliced file properly, readAsBinaryString", function (done) {
  2349. if (isIE) {
  2350. /*`readAsBinaryString` function is not supported by IE and has not the stub.*/
  2351. pending();
  2352. }
  2353. runReaderTest('readAsBinaryString', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2354. expect(evt.target.result).toBe(fileDataAsBinaryString.slice(-10, -5));
  2355. done();
  2356. }, -10, -5);
  2357. });
  2358. it("file.spec.94 should read sliced file properly, readAsArrayBuffer", function (done) {
  2359. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2360. if (typeof window.ArrayBuffer == 'undefined') {
  2361. expect(true).toFailWithMessage('Platform does not supported this feature');
  2362. done();
  2363. }
  2364. runReaderTest('readAsArrayBuffer', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2365. expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString.slice(0, -1))).toBe(true);
  2366. done();
  2367. }, 0, -1);
  2368. });
  2369. it("file.spec.94.5 should read large file in multiple chunks, readAsArrayBuffer", function (done) {
  2370. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2371. if (typeof window.ArrayBuffer == 'undefined') {
  2372. expect(true).toFailWithMessage('Platform does not supported this feature');
  2373. done();
  2374. }
  2375. var largeText = "";
  2376. for (var i = 0; i < 1000; i++) {
  2377. largeText += "Test " + i + "\n";
  2378. }
  2379. // Set the chunk size so that the read will take 5 chunks
  2380. FileReader.READ_CHUNK_SIZE = Math.floor(largeText.length / 4) + 1;
  2381. var chunkCount = 0;
  2382. var lastProgressValue = -1;
  2383. var progressFunc = function (evt) {
  2384. expect(evt.loaded).toBeDefined();
  2385. expect(evt.total).toBeDefined();
  2386. expect(evt.total >= largeText.length).toBe(true);
  2387. expect(evt.total <= largeText.length + 5).toBe(true);
  2388. expect(evt.loaded > lastProgressValue).toBe(true);
  2389. expect(evt.loaded <= evt.total).toBe(true);
  2390. lastProgressValue = evt.loaded;
  2391. chunkCount++;
  2392. };
  2393. runReaderTest(
  2394. 'readAsArrayBuffer', true, done, progressFunc,
  2395. function (evt, fileData, fileDataAsBinaryString) {
  2396. expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString.slice(0, -1))).toBe(true);
  2397. expect(lastProgressValue >= largeText.length).toBe(true);
  2398. expect(lastProgressValue <= largeText.length + 5).toBe(true);
  2399. expect(chunkCount).toBe(5);
  2400. done();
  2401. },
  2402. 0, -1, largeText);
  2403. });
  2404. });
  2405. //Read method
  2406. describe('FileWriter', function () {
  2407. it("file.spec.95 should have correct methods", function (done) {
  2408. // retrieve a FileWriter object
  2409. var fileName = "writer.methods";
  2410. // FileWriter
  2411. root.getFile(fileName, {
  2412. create : true
  2413. }, function (fileEntry) {
  2414. fileEntry.createWriter(function (writer) {
  2415. expect(writer).toBeDefined();
  2416. expect(typeof writer.write).toBe('function');
  2417. expect(typeof writer.seek).toBe('function');
  2418. expect(typeof writer.truncate).toBe('function');
  2419. expect(typeof writer.abort).toBe('function');
  2420. // cleanup
  2421. deleteFile(fileName, done);
  2422. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2423. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  2424. });
  2425. it("file.spec.96 should be able to write and append to file, createWriter", function (done) {
  2426. var fileName = "writer.append.createWriter", // file content
  2427. content = "There is an exception to every rule.", // for checkin file length
  2428. exception = " Except this one.",
  2429. length = content.length;
  2430. // create file, then write and append to it
  2431. createFile(fileName, function (fileEntry) {
  2432. // writes initial file content
  2433. fileEntry.createWriter(function (writer) {
  2434. //Verifiers declaration
  2435. function verifier(evt) {
  2436. expect(writer.length).toBe(length);
  2437. expect(writer.position).toBe(length);
  2438. // Append some more data
  2439. writer.onwriteend = secondVerifier;
  2440. length += exception.length;
  2441. writer.seek(writer.length);
  2442. writer.write(exception);
  2443. }
  2444. function secondVerifier(evt) {
  2445. expect(writer.length).toBe(length);
  2446. expect(writer.position).toBe(length);
  2447. var reader = new FileReader();
  2448. reader.onloadend = thirdVerifier;
  2449. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2450. fileEntry.file(function(f){reader.readAsText(f);});
  2451. }
  2452. function thirdVerifier(evt) {
  2453. expect(evt.target.result).toBe(content+exception);
  2454. // cleanup
  2455. deleteFile(fileName, done);
  2456. }
  2457. //Write process
  2458. writer.onwriteend = verifier;
  2459. writer.write(content);
  2460. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2461. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2462. });
  2463. it("file.spec.97 should be able to write and append to file, File object", function (done) {
  2464. var fileName = "writer.append.File", // file content
  2465. content = "There is an exception to every rule.", // for checkin file length
  2466. exception = " Except this one.",
  2467. length = content.length;
  2468. root.getFile(fileName, {
  2469. create : true
  2470. }, function (fileEntry) {
  2471. fileEntry.createWriter(function (writer) {
  2472. //Verifiers declaration
  2473. function verifier() {
  2474. expect(writer.length).toBe(length);
  2475. expect(writer.position).toBe(length);
  2476. // Append some more data
  2477. writer.onwriteend = secondVerifier;
  2478. length += exception.length;
  2479. writer.seek(writer.length);
  2480. writer.write(exception);
  2481. }
  2482. function secondVerifier() {
  2483. expect(writer.length).toBe(length);
  2484. expect(writer.position).toBe(length);
  2485. var reader = new FileReader();
  2486. reader.onloadend = thirdVerifier;
  2487. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2488. fileEntry.file(function(f){reader.readAsText(f);});
  2489. }
  2490. function thirdVerifier(evt) {
  2491. expect(evt.target.result).toBe(content+exception);
  2492. // cleanup
  2493. deleteFile(fileName, done);
  2494. }
  2495. //Write process
  2496. writer.onwriteend = verifier;
  2497. writer.write(content);
  2498. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2499. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  2500. });
  2501. it("file.spec.98 should be able to seek to the middle of the file and write more data than file.length", function (done) {
  2502. var fileName = "writer.seek.write", // file content
  2503. content = "This is our sentence.", // for checking file length
  2504. exception = "newer sentence.",
  2505. length = content.length;
  2506. // create file, then write and append to it
  2507. createFile(fileName, function (fileEntry) {
  2508. fileEntry.createWriter(function (writer) {
  2509. //Verifiers declaration
  2510. function verifier(evt) {
  2511. expect(writer.length).toBe(length);
  2512. expect(writer.position).toBe(length);
  2513. // Append some more data
  2514. writer.onwriteend = secondVerifier;
  2515. length = 12 + exception.length;
  2516. writer.seek(12);
  2517. writer.write(exception);
  2518. }
  2519. function secondVerifier(evt) {
  2520. expect(writer.length).toBe(length);
  2521. expect(writer.position).toBe(length);
  2522. var reader = new FileReader();
  2523. reader.onloadend = thirdVerifier;
  2524. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2525. fileEntry.file(function(f){reader.readAsText(f);});
  2526. }
  2527. function thirdVerifier(evt) {
  2528. expect(evt.target.result).toBe(content.substr(0,12)+exception);
  2529. // cleanup
  2530. deleteFile(fileName, done);
  2531. }
  2532. //Write process
  2533. writer.onwriteend = verifier;
  2534. writer.write(content);
  2535. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2536. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2537. });
  2538. it("file.spec.99 should be able to seek to the middle of the file and write less data than file.length", function (done) {
  2539. if (isChrome) {
  2540. /* Chrome (re)writes as follows: "This is our sentence." -> "This is new.sentence.",
  2541. i.e. the length is not being changed from content.length and writer length will be equal 21 */
  2542. pending();
  2543. }
  2544. var fileName = "writer.seek.write2", // file content
  2545. content = "This is our sentence.", // for checking file length
  2546. exception = "new.",
  2547. length = content.length;
  2548. // create file, then write and append to it
  2549. createFile(fileName, function (fileEntry) {
  2550. fileEntry.createWriter(function (writer) {
  2551. // Verifiers declaration
  2552. function verifier(evt) {
  2553. expect(writer.length).toBe(length);
  2554. expect(writer.position).toBe(length);
  2555. // Append some more data
  2556. writer.onwriteend = secondVerifier;
  2557. length = 8 + exception.length;
  2558. writer.seek(8);
  2559. writer.write(exception);
  2560. }
  2561. function secondVerifier(evt) {
  2562. expect(writer.length).toBe(length);
  2563. expect(writer.position).toBe(length);
  2564. var reader = new FileReader();
  2565. reader.onloadend = thirdVerifier;
  2566. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2567. fileEntry.file(function(f){reader.readAsText(f);});
  2568. }
  2569. function thirdVerifier(evt) {
  2570. expect(evt.target.result).toBe(content.substr(0,8)+exception);
  2571. // cleanup
  2572. deleteFile(fileName, done);
  2573. }
  2574. //Write process
  2575. writer.onwriteend = verifier;
  2576. writer.write(content);
  2577. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2578. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2579. });
  2580. it("file.spec.100 should be able to write XML data", function (done) {
  2581. var fileName = "writer.xml", // file content
  2582. content = '<?xml version="1.0" encoding="UTF-8"?>\n<test prop="ack">\nData\n</test>\n', // for testing file length
  2583. length = content.length;
  2584. // creates file, then write XML data
  2585. createFile(fileName, function (fileEntry) {
  2586. fileEntry.createWriter(function (writer) {
  2587. //Verifier content
  2588. var verifier = function (evt) {
  2589. expect(writer.length).toBe(length);
  2590. expect(writer.position).toBe(length);
  2591. // cleanup
  2592. deleteFile(fileName, done);
  2593. };
  2594. //Write process
  2595. writer.onwriteend = verifier;
  2596. writer.write(content);
  2597. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2598. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2599. });
  2600. it("file.spec.101 should be able to write JSON data", function (done) {
  2601. var fileName = "writer.json", // file content
  2602. content = '{ "name": "Guy Incognito", "email": "here@there.com" }', // for testing file length
  2603. length = content.length;
  2604. // creates file, then write JSON content
  2605. createFile(fileName, function (fileEntry) {
  2606. fileEntry.createWriter(function (writer) {
  2607. //Verifier declaration
  2608. var verifier = function (evt) {
  2609. expect(writer.length).toBe(length);
  2610. expect(writer.position).toBe(length);
  2611. // cleanup
  2612. deleteFile(fileName, done);
  2613. };
  2614. //Write process
  2615. writer.onwriteend = verifier;
  2616. writer.write(content);
  2617. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2618. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2619. });
  2620. it("file.spec.102 should be able to seek", function (done) {
  2621. var fileName = "writer.seek", // file content
  2622. content = "There is an exception to every rule. Except this one.", // for testing file length
  2623. length = content.length;
  2624. // creates file, then write JSON content
  2625. createFile(fileName, function (fileEntry) {
  2626. // writes file content and tests writer.seek
  2627. fileEntry.createWriter(function (writer) {
  2628. //Verifier declaration
  2629. var verifier = function () {
  2630. expect(writer.position).toBe(length);
  2631. writer.seek(-5);
  2632. expect(writer.position).toBe(length - 5);
  2633. writer.seek(length + 100);
  2634. expect(writer.position).toBe(length);
  2635. writer.seek(10);
  2636. expect(writer.position).toBe(10);
  2637. // cleanup
  2638. deleteFile(fileName, done);
  2639. };
  2640. //Write process
  2641. writer.onwriteend = verifier;
  2642. writer.seek(-100);
  2643. expect(writer.position).toBe(0);
  2644. writer.write(content);
  2645. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2646. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2647. });
  2648. it("file.spec.103 should be able to truncate", function (done) {
  2649. if (isIndexedDBShim) {
  2650. /* `abort` and `truncate` functions are not supported (Firefox, IE) */
  2651. pending();
  2652. }
  2653. var fileName = "writer.truncate",
  2654. content = "There is an exception to every rule. Except this one.";
  2655. // creates file, writes to it, then truncates it
  2656. createFile(fileName, function (fileEntry) {
  2657. fileEntry.createWriter(function (writer) {
  2658. // Verifier declaration
  2659. var verifier = function () {
  2660. expect(writer.length).toBe(36);
  2661. expect(writer.position).toBe(36);
  2662. // cleanup
  2663. deleteFile(fileName, done);
  2664. };
  2665. //Write process
  2666. writer.onwriteend = function () {
  2667. //Truncate process after write
  2668. writer.onwriteend = verifier;
  2669. writer.truncate(36);
  2670. };
  2671. writer.write(content);
  2672. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2673. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2674. });
  2675. it("file.spec.104 should be able to write binary data from an ArrayBuffer", function (done) {
  2676. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2677. if (typeof window.ArrayBuffer == 'undefined') {
  2678. expect(true).toFailWithMessage('Platform does not supported this feature');
  2679. done();
  2680. return;
  2681. }
  2682. var fileName = "bufferwriter.bin", // file content
  2683. data = new ArrayBuffer(32),
  2684. dataView = new Int8Array(data), // for verifying file length
  2685. length = 32;
  2686. for (var i = 0; i < dataView.length; i++) {
  2687. dataView[i] = i;
  2688. }
  2689. // creates file, then write content
  2690. createFile(fileName, function (fileEntry) {
  2691. // writes file content
  2692. fileEntry.createWriter(function (writer) {
  2693. //Verifier declaration
  2694. var verifier = function () {
  2695. expect(writer.length).toBe(length);
  2696. expect(writer.position).toBe(length);
  2697. // cleanup
  2698. deleteFile(fileName, done);
  2699. };
  2700. //Write process
  2701. writer.onwriteend = verifier;
  2702. writer.write(data);
  2703. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2704. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2705. });
  2706. it("file.spec.105 should be able to write binary data from a Blob", function (done) {
  2707. // Skip test if Blobs are not supported (e.g.: Android 2.3).
  2708. if ((typeof window.Blob == 'undefined' && typeof window.WebKitBlobBuilder == 'undefined') || typeof window.ArrayBuffer == 'undefined') {
  2709. expect(true).toFailWithMessage('Platform does not supported this feature');
  2710. done();
  2711. return;
  2712. }
  2713. var fileName = "blobwriter.bin", // file content
  2714. data = new ArrayBuffer(32),
  2715. dataView = new Int8Array(data),
  2716. blob, // for verifying file length
  2717. length = 32;
  2718. for (var i = 0; i < dataView.length; i++) {
  2719. dataView[i] = i;
  2720. }
  2721. try {
  2722. // Mobile Safari: Use Blob constructor
  2723. blob = new Blob([data], {
  2724. "type" : "application/octet-stream"
  2725. });
  2726. } catch (e) {
  2727. if (window.WebKitBlobBuilder) {
  2728. // Android Browser: Use deprecated BlobBuilder
  2729. var builder = new WebKitBlobBuilder();
  2730. builder.append(data);
  2731. blob = builder.getBlob('application/octet-stream');
  2732. } else {
  2733. // We have no way defined to create a Blob, so fail
  2734. fail();
  2735. }
  2736. }
  2737. if (typeof blob !== 'undefined') {
  2738. // creates file, then write content
  2739. createFile(fileName, function (fileEntry) {
  2740. fileEntry.createWriter(function (writer) {
  2741. //Verifier declaration
  2742. var verifier = function () {
  2743. expect(writer.length).toBe(length);
  2744. expect(writer.position).toBe(length);
  2745. // cleanup
  2746. deleteFile(fileName, done);
  2747. };
  2748. //Write process
  2749. writer.onwriteend = verifier;
  2750. writer.write(blob);
  2751. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2752. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2753. }
  2754. });
  2755. it("file.spec.106 should be able to write a File to a FileWriter", function (done) {
  2756. var dummyFileName = 'dummy.txt',
  2757. outputFileName = 'verify.txt',
  2758. dummyFileText = 'This text should be written to two files',
  2759. verifier = function (outputFileWriter) {
  2760. expect(outputFileWriter.length).toBe(dummyFileText.length);
  2761. expect(outputFileWriter.position).toBe(dummyFileText.length);
  2762. deleteFile(outputFileName, done);
  2763. },
  2764. writeFile = function (fileName, fileData, win) {
  2765. var theWriter,
  2766. write_file = function (fileEntry) {
  2767. // writes file content to new file
  2768. fileEntry.createWriter(function (writer) {
  2769. theWriter = writer;
  2770. writer.onwriteend = function (ev) {
  2771. if (typeof fileData.length !== "undefined") {
  2772. expect(theWriter.length).toBe(fileData.length);
  2773. expect(theWriter.position).toBe(fileData.length);
  2774. }
  2775. win(theWriter);
  2776. };
  2777. writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
  2778. writer.write(fileData);
  2779. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2780. };
  2781. createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2782. },
  2783. openFile = function (fileName, callback) {
  2784. root.getFile(fileName, {
  2785. create : false
  2786. }, function (fileEntry) {
  2787. fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  2788. }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
  2789. };
  2790. writeFile(dummyFileName, dummyFileText, function (dummyFileWriter) {
  2791. openFile(dummyFileName, function (file) {
  2792. writeFile(outputFileName, file, verifier);
  2793. });
  2794. });
  2795. });
  2796. it("file.spec.107 should be able to write a sliced File to a FileWriter", function (done) {
  2797. var dummyFileName = 'dummy2.txt',
  2798. outputFileName = 'verify2.txt',
  2799. dummyFileText = 'This text should be written to two files',
  2800. verifier = function (outputFileWriter) {
  2801. expect(outputFileWriter.length).toBe(10);
  2802. expect(outputFileWriter.position).toBe(10);
  2803. deleteFile(outputFileName, done);
  2804. },
  2805. writeFile = function (fileName, fileData, win) {
  2806. var theWriter,
  2807. write_file = function (fileEntry) {
  2808. // writes file content to new file
  2809. fileEntry.createWriter(function (writer) {
  2810. theWriter = writer;
  2811. writer.onwriteend = function (ev) {
  2812. if (typeof fileData.length !== "undefined") {
  2813. expect(theWriter.length).toBe(fileData.length);
  2814. expect(theWriter.position).toBe(fileData.length);
  2815. }
  2816. win(theWriter);
  2817. };
  2818. writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
  2819. writer.write(fileData);
  2820. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2821. };
  2822. createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2823. },
  2824. openFile = function (fileName, callback) {
  2825. root.getFile(fileName, {
  2826. create : false
  2827. }, function (fileEntry) {
  2828. fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  2829. }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
  2830. };
  2831. writeFile(dummyFileName, dummyFileText, function (dummyFileWriter) {
  2832. openFile(dummyFileName, function (file) {
  2833. writeFile(outputFileName, file.slice(10, 20), verifier);
  2834. });
  2835. });
  2836. });
  2837. it("file.spec.108 should be able to write binary data from a File", function (done) {
  2838. // Skip test if Blobs are not supported (e.g.: Android 2.3).
  2839. if (typeof window.Blob == 'undefined' && typeof window.WebKitBlobBuilder == 'undefined') {
  2840. expect(true).toFailWithMessage('Platform does not supported this feature');
  2841. done();
  2842. }
  2843. var dummyFileName = "blobwriter.bin",
  2844. outputFileName = 'verify.bin', // file content
  2845. data = new ArrayBuffer(32),
  2846. dataView = new Int8Array(data),
  2847. blob, // for verifying file length
  2848. length = 32,
  2849. verifier = function (outputFileWriter) {
  2850. expect(outputFileWriter.length).toBe(length);
  2851. expect(outputFileWriter.position).toBe(length);
  2852. // cleanup
  2853. deleteFile(outputFileName);
  2854. done();
  2855. },
  2856. writeFile = function (fileName, fileData, win) {
  2857. var theWriter,
  2858. write_file = function (fileEntry) {
  2859. // writes file content to new file
  2860. fileEntry.createWriter(function (writer) {
  2861. theWriter = writer;
  2862. writer.onwriteend = function (ev) {
  2863. if (typeof fileData.length !== "undefined") {
  2864. expect(theWriter.length).toBe(fileData.length);
  2865. expect(theWriter.position).toBe(fileData.length);
  2866. }
  2867. win(theWriter);
  2868. };
  2869. writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
  2870. writer.write(fileData);
  2871. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2872. };
  2873. createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2874. },
  2875. openFile = function (fileName, callback) {
  2876. root.getFile(fileName, {
  2877. create : false
  2878. }, function (fileEntry) {
  2879. fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  2880. }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
  2881. };
  2882. for (var i = 0; i < dataView.length; i++) {
  2883. dataView[i] = i;
  2884. }
  2885. try {
  2886. // Mobile Safari: Use Blob constructor
  2887. blob = new Blob([data], {
  2888. "type" : "application/octet-stream"
  2889. });
  2890. } catch (e) {
  2891. if (window.WebKitBlobBuilder) {
  2892. // Android Browser: Use deprecated BlobBuilder
  2893. var builder = new WebKitBlobBuilder();
  2894. builder.append(data);
  2895. blob = builder.getBlob('application/octet-stream');
  2896. } else {
  2897. // We have no way defined to create a Blob, so fail
  2898. fail();
  2899. }
  2900. }
  2901. if (typeof blob !== 'undefined') {
  2902. // creates file, then write content
  2903. writeFile(dummyFileName, blob, function (dummyFileWriter) {
  2904. openFile(dummyFileName, function (file) {
  2905. writeFile(outputFileName, file, verifier);
  2906. });
  2907. });
  2908. }
  2909. });
  2910. });
  2911. //FileWritter
  2912. describe('Backwards compatibility', function () {
  2913. /* These specs exist to test that the File plugin can still recognize file:///
  2914. * URLs, and can resolve them to FileEntry and DirectoryEntry objects.
  2915. * They rely on an undocumented interface to File which provides absolute file
  2916. * paths, which are not used internally anymore.
  2917. * If that interface is not present, then these tests will silently succeed.
  2918. */
  2919. it("file.spec.109 should be able to resolve a file:/// URL", function (done) {
  2920. var localFilename = 'file.txt';
  2921. var originalEntry;
  2922. root.getFile(localFilename, {
  2923. create : true
  2924. }, function (entry) {
  2925. originalEntry = entry;
  2926. /* This is an undocumented interface to File which exists only for testing
  2927. * backwards compatibilty. By obtaining the raw filesystem path of the download
  2928. * location, we can pass that to ft.download() to make sure that previously-stored
  2929. * paths are still valid.
  2930. */
  2931. cordova.exec(function (localPath) {
  2932. window.resolveLocalFileSystemURL("file://" + encodeURI(localPath), function (fileEntry) {
  2933. expect(fileEntry.toURL()).toEqual(originalEntry.toURL());
  2934. // cleanup
  2935. deleteFile(localFilename);
  2936. done();
  2937. }, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving URI: file://' + encodeURI(localPath)));
  2938. }, done, 'File', '_getLocalFilesystemPath', [entry.toURL()]);
  2939. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + localFilename));
  2940. });
  2941. });
  2942. //Backwards Compatibility
  2943. describe('Parent References', function () {
  2944. /* These specs verify that paths with parent references i("..") in them
  2945. * work correctly, and do not cause the application to crash.
  2946. */
  2947. it("file.spec.110 should not throw exception resolving parent refefences", function (done) {
  2948. /* This is a direct copy of file.spec.9, with the filename changed, * as reported in CB-5721.
  2949. */
  2950. var fileName = "resolve.file.uri";
  2951. var dirName = "resolve.dir.uri";
  2952. // create a new file entry
  2953. createDirectory(dirName, function () {
  2954. createFile(dirName+"/../" + fileName, function (entry) {
  2955. // lookup file system entry
  2956. window.resolveLocalFileSystemURL(entry.toURL(), function (fileEntry) {
  2957. expect(fileEntry).toBeDefined();
  2958. expect(fileEntry.name).toCanonicallyMatch(fileName);
  2959. // cleanup
  2960. deleteEntry(fileName, done);
  2961. }, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving URI: ' + entry.toURL()));
  2962. }, failed.bind(null, done, 'createFile - Error creating file: ../' + fileName));
  2963. }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + dirName));
  2964. });
  2965. it("file.spec.111 should not traverse above above the root directory", function (done) {
  2966. var fileName = "traverse.file.uri";
  2967. // create a new file entry
  2968. createFile(fileName, function (entry) {
  2969. // lookup file system entry
  2970. root.getFile('../' + fileName, {
  2971. create : false
  2972. }, function (fileEntry) {
  2973. // Note: we expect this to still resolve, as the correct behaviour is to ignore the ../, not to fail out.
  2974. expect(fileEntry).toBeDefined();
  2975. expect(fileEntry.name).toBe(fileName);
  2976. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath +'/' + fileName);
  2977. // cleanup
  2978. deleteEntry(fileName, done);
  2979. }, failed.bind(null, done, 'root.getFile - Error getting file: ../' + fileName));
  2980. }, failed.bind(null, done, 'createFile - Error creating file: ../' + fileName));
  2981. });
  2982. it("file.spec.112 should traverse above above the current directory", function (done) {
  2983. var fileName = "traverse2.file.uri",
  2984. dirName = "traverse2.subdir";
  2985. // create a new directory and a file entry
  2986. createFile(fileName, function () {
  2987. createDirectory(dirName, function (entry) {
  2988. // lookup file system entry
  2989. entry.getFile('../' + fileName, {
  2990. create : false
  2991. }, function (fileEntry) {
  2992. expect(fileEntry).toBeDefined();
  2993. expect(fileEntry.name).toBe(fileName);
  2994. expect(fileEntry.fullPath).toCanonicallyMatch('/' + fileName);
  2995. // cleanup
  2996. deleteEntry(fileName, function () {
  2997. deleteEntry(dirName, done);
  2998. });
  2999. }, failed.bind(null, done, 'entry.getFile - Error getting file: ' + fileName + ' recently created above: ' + dirName));
  3000. }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + dirName));
  3001. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3002. });
  3003. it("file.spec.113 getFile: get Entry should error for missing file above root directory", function (done) {
  3004. var fileName = "../missing.file";
  3005. // create:false, exclusive:false, file does not exist
  3006. root.getFile(fileName, {
  3007. create : false
  3008. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not locate nonexistent file: ' + fileName), function (error) {
  3009. expect(error).toBeDefined();
  3010. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  3011. done();
  3012. });
  3013. });
  3014. });
  3015. //Parent References
  3016. describe('toNativeURL interface', function () {
  3017. /* These specs verify that FileEntries have a toNativeURL method
  3018. * which appears to be sane.
  3019. */
  3020. var pathExpect = cordova.platformId === 'windowsphone' ? "//nativ" : "file://";
  3021. if (isChrome) {
  3022. pathExpect = 'filesystem:file://';
  3023. }
  3024. it("file.spec.114 fileEntry should have a toNativeURL method", function (done) {
  3025. var fileName = "native.file.uri";
  3026. if (isWindows) {
  3027. var rootPath = root.fullPath;
  3028. pathExpect = rootPath.substr(0, rootPath.indexOf(":"));
  3029. }
  3030. // create a new file entry
  3031. createFile(fileName, function (entry) {
  3032. expect(entry.toNativeURL).toBeDefined();
  3033. expect(entry.name).toCanonicallyMatch(fileName);
  3034. expect(typeof entry.toNativeURL).toBe('function');
  3035. var nativeURL = entry.toNativeURL();
  3036. expect(typeof nativeURL).toBe("string");
  3037. expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
  3038. expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
  3039. // cleanup
  3040. deleteEntry(fileName, done);
  3041. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3042. });
  3043. it("file.spec.115 DirectoryReader should return entries with toNativeURL method", function (done) {
  3044. var dirName = 'nativeEntries.dir',
  3045. fileName = 'nativeEntries.file',
  3046. directory,
  3047. checkEntries = function (entries) {
  3048. expect(entries).toBeDefined();
  3049. expect(entries instanceof Array).toBe(true);
  3050. expect(entries.length).toBe(1);
  3051. expect(entries[0].toNativeURL).toBeDefined();
  3052. expect(typeof entries[0].toNativeURL).toBe('function');
  3053. var nativeURL = entries[0].toNativeURL();
  3054. expect(typeof nativeURL).toBe("string");
  3055. expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
  3056. expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
  3057. // cleanup
  3058. directory.removeRecursively(null, null);
  3059. done();
  3060. };
  3061. // create a new file entry
  3062. root.getDirectory(dirName, {
  3063. create : true
  3064. }, function (dir) {
  3065. directory = dir;
  3066. directory.getFile(fileName, {
  3067. create : true
  3068. }, function (fileEntry) {
  3069. var reader = directory.createReader();
  3070. reader.readEntries(checkEntries, failed.bind(null, done, 'reader.readEntries - Error reading entries from directory: ' + dirName));
  3071. }, failed.bind(null, done, 'directory.getFile - Error creating file: ' + fileName));
  3072. }, failed.bind(null, done, 'root.getDirectory - Error creating directory: ' + dirName));
  3073. });
  3074. it("file.spec.116 resolveLocalFileSystemURL should return entries with toNativeURL method", function (done) {
  3075. var fileName = "native.resolve.uri";
  3076. // create a new file entry
  3077. createFile(fileName, function (entry) {
  3078. resolveLocalFileSystemURL(entry.toURL(), function (entry) {
  3079. expect(entry.toNativeURL).toBeDefined();
  3080. expect(entry.name).toCanonicallyMatch(fileName);
  3081. expect(typeof entry.toNativeURL).toBe('function');
  3082. var nativeURL = entry.toNativeURL();
  3083. expect(typeof nativeURL).toBe("string");
  3084. expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
  3085. expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
  3086. // cleanup
  3087. deleteEntry(fileName, done);
  3088. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
  3089. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3090. });
  3091. });
  3092. //toNativeURL interface
  3093. describe('resolveLocalFileSystemURL on file://', function () {
  3094. /* These specs verify that window.resolveLocalFileSystemURL works correctly on file:// URLs
  3095. */
  3096. it("file.spec.117 should not resolve native URLs outside of FS roots", function (done) {
  3097. // lookup file system entry
  3098. 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) {
  3099. expect(error).toBeDefined();
  3100. done();
  3101. });
  3102. });
  3103. it("file.spec.118 should not resolve native URLs outside of FS roots", function (done) {
  3104. // lookup file system entry
  3105. 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) {
  3106. expect(error).toBeDefined();
  3107. done();
  3108. });
  3109. });
  3110. it("file.spec.119 should not resolve invalid native URLs", function (done) {
  3111. // lookup file system entry
  3112. window.resolveLocalFileSystemURL("file://localhost", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file://localhost'), function (error) {
  3113. expect(error).toBeDefined();
  3114. done();
  3115. });
  3116. });
  3117. it("file.spec.120 should not resolve invalid native URLs with query strings", function (done) {
  3118. // lookup file system entry
  3119. 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) {
  3120. expect(error).toBeDefined();
  3121. done();
  3122. });
  3123. });
  3124. it("file.spec.121 should resolve native URLs returned by API", function (done) {
  3125. var fileName = "native.resolve.uri1";
  3126. // create a new file entry
  3127. createFile(fileName, function (entry) {
  3128. resolveLocalFileSystemURL(entry.toNativeURL(), function (fileEntry) {
  3129. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3130. // cleanup
  3131. deleteEntry(fileName, done);
  3132. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toNativeURL()));
  3133. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3134. });
  3135. it("file.spec.122 should resolve native URLs returned by API with localhost", function (done) {
  3136. var fileName = "native.resolve.uri2";
  3137. // create a new file entry
  3138. createFile(fileName, function (entry) {
  3139. var url = entry.toNativeURL();
  3140. url = url.replace("///", "//localhost/");
  3141. resolveLocalFileSystemURL(url, function (fileEntry) {
  3142. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3143. // cleanup
  3144. deleteEntry(fileName, done);
  3145. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
  3146. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3147. });
  3148. it("file.spec.123 should resolve native URLs returned by API with query string", function (done) {
  3149. var fileName = "native.resolve.uri3";
  3150. // create a new file entry
  3151. createFile(fileName, function (entry) {
  3152. var url = entry.toNativeURL();
  3153. url = url + "?test/test";
  3154. resolveLocalFileSystemURL(url, function (fileEntry) {
  3155. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3156. // cleanup
  3157. deleteEntry(fileName, done);
  3158. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
  3159. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3160. });
  3161. it("file.spec.124 should resolve native URLs returned by API with localhost and query string", function (done) {
  3162. var fileName = "native.resolve.uri4";
  3163. // create a new file entry
  3164. createFile(fileName, function (entry) {
  3165. var url = entry.toNativeURL();
  3166. url = url.replace("///", "//localhost/") + "?test/test";
  3167. resolveLocalFileSystemURL(url, function (fileEntry) {
  3168. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3169. // cleanup
  3170. deleteEntry(fileName, done);
  3171. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
  3172. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3173. });
  3174. });
  3175. //resolveLocalFileSystemURL on file://
  3176. describe('cross-file-system copy and move', function () {
  3177. /* These specs verify that Entry.copyTo and Entry.moveTo work correctly
  3178. * when crossing filesystem boundaries.
  3179. */
  3180. it("file.spec.125 copyTo: temporary -> persistent", function (done) {
  3181. var file1 = "entry.copy.file1a",
  3182. file2 = "entry.copy.file2a",
  3183. sourceEntry,
  3184. fullPath = joinURL(root.fullPath, file2),
  3185. validateFile = function (entry) {
  3186. // a bit redundant since copy returned this entry already
  3187. expect(entry).toBeDefined();
  3188. expect(entry.isFile).toBe(true);
  3189. expect(entry.isDirectory).toBe(false);
  3190. expect(entry.name).toCanonicallyMatch(file2);
  3191. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3192. expect(entry.filesystem).toBeDefined();
  3193. if (isChrome) {
  3194. expect(entry.filesystem.name).toContain("Persistent");
  3195. } else {
  3196. expect(entry.filesystem.name).toEqual("persistent");
  3197. }
  3198. // cleanup
  3199. deleteEntry(entry.name);
  3200. deleteEntry(sourceEntry.name, done);
  3201. },
  3202. createSourceAndTransfer = function () {
  3203. temp_root.getFile(file1, {
  3204. create : true
  3205. }, function (entry) {
  3206. expect(entry.filesystem).toBeDefined();
  3207. if (isChrome) {
  3208. expect(entry.filesystem.name).toContain("Temporary");
  3209. } else {
  3210. expect(entry.filesystem.name).toEqual("temporary");
  3211. }
  3212. sourceEntry = entry;
  3213. // Save for later cleanup
  3214. entry.copyTo(persistent_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + file1 + ' to PERSISTENT root as: ' + file2));
  3215. }, failed.bind(null, done, 'temp_root.getFile - Error creating file: ' + file1 + 'at TEMPORAL root'));
  3216. };
  3217. // Delete any existing file to start things off
  3218. persistent_root.getFile(file2, {}, function (entry) {
  3219. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3220. }, createSourceAndTransfer);
  3221. });
  3222. it("file.spec.126 copyTo: persistent -> temporary", function (done) {
  3223. var file1 = "entry.copy.file1b",
  3224. file2 = "entry.copy.file2b",
  3225. sourceEntry,
  3226. fullPath = joinURL(temp_root.fullPath, file2),
  3227. validateFile = function (entry) {
  3228. expect(entry).toBeDefined();
  3229. expect(entry.isFile).toBe(true);
  3230. expect(entry.isDirectory).toBe(false);
  3231. expect(entry.name).toCanonicallyMatch(file2);
  3232. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3233. if (isChrome) {
  3234. expect(entry.filesystem.name).toContain("Temporary");
  3235. } else {
  3236. expect(entry.filesystem.name).toEqual("temporary");
  3237. }
  3238. // cleanup
  3239. deleteEntry(entry.name);
  3240. deleteEntry(sourceEntry.name, done);
  3241. },
  3242. createSourceAndTransfer = function () {
  3243. persistent_root.getFile(file1, {
  3244. create : true
  3245. }, function (entry) {
  3246. expect(entry).toBeDefined();
  3247. expect(entry.filesystem).toBeDefined();
  3248. if (isChrome) {
  3249. expect(entry.filesystem.name).toContain("Persistent");
  3250. } else {
  3251. expect(entry.filesystem.name).toEqual("persistent");
  3252. }
  3253. sourceEntry = entry;
  3254. // Save for later cleanup
  3255. entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + file1 + ' to TEMPORAL root as: ' + file2));
  3256. }, failed.bind(null, done, 'persistent_root.getFile - Error creating file: ' + file1 + 'at PERSISTENT root'));
  3257. };
  3258. // Delete any existing file to start things off
  3259. temp_root.getFile(file2, {}, function (entry) {
  3260. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3261. }, createSourceAndTransfer);
  3262. });
  3263. it("file.spec.127 moveTo: temporary -> persistent", function (done) {
  3264. var file1 = "entry.copy.file1a",
  3265. file2 = "entry.copy.file2a",
  3266. sourceEntry,
  3267. fullPath = joinURL(root.fullPath, file2),
  3268. validateFile = function (entry) {
  3269. // a bit redundant since copy returned this entry already
  3270. expect(entry).toBeDefined();
  3271. expect(entry.isFile).toBe(true);
  3272. expect(entry.isDirectory).toBe(false);
  3273. expect(entry.name).toCanonicallyMatch(file2);
  3274. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3275. expect(entry.filesystem).toBeDefined();
  3276. if (isChrome) {
  3277. expect(entry.filesystem.name).toContain("Persistent");
  3278. } else {
  3279. expect(entry.filesystem.name).toEqual("persistent");
  3280. }
  3281. // cleanup
  3282. deleteEntry(entry.name);
  3283. deleteEntry(sourceEntry.name, done);
  3284. },
  3285. createSourceAndTransfer = function () {
  3286. temp_root.getFile(file1, {
  3287. create : true
  3288. }, function (entry) {
  3289. expect(entry.filesystem).toBeDefined();
  3290. if (isChrome) {
  3291. expect(entry.filesystem.name).toContain("Temporary");
  3292. } else {
  3293. expect(entry.filesystem.name).toEqual("temporary");
  3294. }
  3295. sourceEntry = entry;
  3296. // Save for later cleanup
  3297. entry.moveTo(persistent_root, file2, validateFile, failed.bind(null, done, 'entry.moveTo - Error moving file: ' + file1 + ' to PERSISTENT root as: ' + file2));
  3298. }, failed.bind(null, done, 'temp_root.getFile - Error creating file: ' + file1 + 'at TEMPORAL root'));
  3299. };
  3300. // Delete any existing file to start things off
  3301. persistent_root.getFile(file2, {}, function (entry) {
  3302. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3303. }, createSourceAndTransfer);
  3304. });
  3305. it("file.spec.128 moveTo: persistent -> temporary", function (done) {
  3306. var file1 = "entry.copy.file1b",
  3307. file2 = "entry.copy.file2b",
  3308. sourceEntry,
  3309. fullPath = joinURL(temp_root.fullPath, file2),
  3310. validateFile = function (entry) {
  3311. expect(entry).toBeDefined();
  3312. expect(entry.isFile).toBe(true);
  3313. expect(entry.isDirectory).toBe(false);
  3314. expect(entry.name).toCanonicallyMatch(file2);
  3315. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3316. if(isChrome) {
  3317. expect(entry.filesystem.name).toContain("Temporary");
  3318. } else {
  3319. expect(entry.filesystem.name).toEqual("temporary");
  3320. }
  3321. // cleanup
  3322. deleteEntry(entry.name);
  3323. deleteEntry(sourceEntry.name, done);
  3324. },
  3325. createSourceAndTransfer = function () {
  3326. persistent_root.getFile(file1, {
  3327. create : true
  3328. }, function (entry) {
  3329. expect(entry).toBeDefined();
  3330. expect(entry.filesystem).toBeDefined();
  3331. if (isChrome) {
  3332. expect(entry.filesystem.name).toContain("Persistent");
  3333. } else {
  3334. expect(entry.filesystem.name).toEqual("persistent");
  3335. }
  3336. sourceEntry = entry;
  3337. // Save for later cleanup
  3338. entry.moveTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.moveTo - Error moving file: ' + file1 + ' to TEMPORAL root as: ' + file2));
  3339. }, failed.bind(null, done, 'persistent_root.getFile - Error creating file: ' + file1 + 'at PERSISTENT root'));
  3340. };
  3341. // Delete any existing file to start things off
  3342. temp_root.getFile(file2, {}, function (entry) {
  3343. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3344. }, createSourceAndTransfer);
  3345. });
  3346. it("file.spec.129 cordova.file.*Directory are set", function () {
  3347. var expectedPaths = ['applicationDirectory', 'applicationStorageDirectory', 'dataDirectory', 'cacheDirectory'];
  3348. if (cordova.platformId == 'android' || cordova.platformId == 'amazon-fireos') {
  3349. if (cordova.file.externalApplicationStorageDirectory !== null) {
  3350. // https://issues.apache.org/jira/browse/CB-10411
  3351. // If external storage can't be mounted, the cordova.file.external* properties are null.
  3352. expectedPaths.push('externalApplicationStorageDirectory', 'externalRootDirectory', 'externalCacheDirectory', 'externalDataDirectory');
  3353. }
  3354. } else if (cordova.platformId == 'blackberry10') {
  3355. expectedPaths.push('externalRootDirectory', 'sharedDirectory');
  3356. } else if (cordova.platformId == 'ios') {
  3357. expectedPaths.push('syncedDataDirectory', 'documentsDirectory', 'tempDirectory');
  3358. } else if (cordova.platformId == 'osx') {
  3359. expectedPaths.push('documentsDirectory', 'tempDirectory', 'rootDirectory');
  3360. } else {
  3361. console.log('Skipping test due on unsupported platform.');
  3362. return;
  3363. }
  3364. for (var i = 0; i < expectedPaths.length; ++i) {
  3365. expect(typeof cordova.file[expectedPaths[i]]).toBe('string');
  3366. expect(cordova.file[expectedPaths[i]]).toMatch(/\/$/, 'Path should end with a slash');
  3367. }
  3368. });
  3369. });
  3370. //cross-file-system copy and move
  3371. describe('IndexedDB-based impl', function () {
  3372. it("file.spec.131 Nested file or nested directory should be removed when removing a parent directory", function (done) {
  3373. var parentDirName = 'deletedDir131',
  3374. nestedDirName = 'nestedDir131',
  3375. nestedFileName = 'nestedFile131.txt';
  3376. createDirectory(parentDirName, function (parent) {
  3377. parent.getDirectory(nestedDirName, { create: true}, function () {
  3378. parent.getFile(nestedFileName, { create: true}, function () {
  3379. parent.removeRecursively(function() {
  3380. root.getDirectory(parentDirName,{ create: false}, failed.bind(this, done, 'root.getDirectory - unexpected success callback : ' + parentDirName), function(){
  3381. parent.getFile(nestedFileName,{ create: false}, failed.bind(this, done, 'getFile - unexpected success callback : ' + nestedFileName), function(){
  3382. parent.getDirectory(nestedDirName, { create: false}, failed.bind(this, done, 'getDirectory - unexpected success callback : ' + nestedDirName), done);
  3383. });
  3384. });
  3385. }, failed.bind(this, done, 'removeRecursively - Error removing directory : ' + parentDirName));
  3386. }, failed.bind(this, done, 'getFile - Error creating file : ' + nestedFileName));
  3387. },failed.bind(this, done, 'getDirectory - Error creating directory : ' + nestedDirName));
  3388. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + parentDirName));
  3389. });
  3390. it("file.spec.132 Entry should be created succesfully when using relative paths if its parent directory exists", function (done) {
  3391. /* Directory entries have to be created successively.
  3392. For example, the call `fs.root.getDirectory('dir1/dir2', {create:true}, successCallback, errorCallback)`
  3393. will fail if dir1 did not exist. */
  3394. var parentName = 'parentName132';
  3395. var nestedName = 'nestedName132';
  3396. var path = parentName + '/' + nestedName;
  3397. var win = function(directory){
  3398. expect(directory).toBeDefined();
  3399. expect(directory.isFile).toBe(false);
  3400. expect(directory.isDirectory).toBe(true);
  3401. expect(directory.name).toCanonicallyMatch(nestedName);
  3402. expect(directory.fullPath).toCanonicallyMatch('/' + path + '/');
  3403. deleteEntry(directory.name);
  3404. deleteEntry(parentName, done);
  3405. };
  3406. createDirectory(parentName, function() {
  3407. root.getDirectory(parentName + '/' + nestedName, {create:true}, win,
  3408. failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + path));
  3409. }, failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + parentName));
  3410. });
  3411. 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) {
  3412. // Names include special symbols so that we check the IndexedDB range used
  3413. var deletedFileName = 'deletedFile.0',
  3414. secondFileName = 'deletedFile.0.1';
  3415. var win = function(fileEntry){
  3416. expect(fileEntry).toBeDefined();
  3417. expect(fileEntry.isFile).toBe(true);
  3418. expect(fileEntry.isDirectory).toBe(false);
  3419. expect(fileEntry.name).toCanonicallyMatch(secondFileName);
  3420. deleteEntry(fileEntry.name, done);
  3421. };
  3422. createFile(deletedFileName, function (deletedFile) {
  3423. createFile(secondFileName, function () {
  3424. deletedFile.remove(function() {
  3425. root.getFile(deletedFileName, {create: false}, failed.bind(this, done, 'getFile - unexpected success callback getting deleted file : ' + deletedFileName), function(){
  3426. root.getFile(secondFileName, {create: false}, win, failed.bind(this, done, 'getFile - Error getting file after deleting deletedFile : ' + secondFileName));
  3427. });
  3428. }, failed.bind(this, done, 'remove - Error removing file : ' + deletedFileName));
  3429. }, failed.bind(this, done, 'getFile - Error creating file : ' + secondFileName));
  3430. }, failed.bind(this, done, 'getFile - Error creating file : ' + deletedFileName));
  3431. });
  3432. 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) {
  3433. // Names include special symbols so that we check the IndexedDB range used
  3434. var deletedDirName = 'deletedDir.0',
  3435. secondDirName = 'deletedDir.0.1';
  3436. var win = function(directory){
  3437. expect(directory).toBeDefined();
  3438. expect(directory.isFile).toBe(false);
  3439. expect(directory.isDirectory).toBe(true);
  3440. expect(directory.name).toCanonicallyMatch(secondDirName);
  3441. deleteEntry(directory.name, done);
  3442. };
  3443. createDirectory(deletedDirName, function (deletedDir) {
  3444. createDirectory(secondDirName, function () {
  3445. deletedDir.remove(function() {
  3446. root.getDirectory(deletedDirName, {create: false}, failed.bind(this, done, 'getDirectory - unexpected success callback getting deleted directory : ' + deletedDirName), function() {
  3447. root.getDirectory(secondDirName, {create: false}, win, failed.bind(this, done, 'getDirectory - Error getting directory after deleting deletedDirectory : ' + secondDirName));
  3448. });
  3449. }, failed.bind(this, done, 'remove - Error removing directory : ' + deletedDirName));
  3450. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + secondDirName));
  3451. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + deletedDirName));
  3452. });
  3453. it("file.spec.135 Deletion of a child directory should not affect the parent directory.", function (done) {
  3454. var parentName = 'parentName135';
  3455. var childName = 'childName135';
  3456. var win = function(directory){
  3457. expect(directory).toBeDefined();
  3458. expect(directory.isFile).toBe(false);
  3459. expect(directory.isDirectory).toBe(true);
  3460. expect(directory.name).toCanonicallyMatch(parentName);
  3461. deleteEntry(directory.name, done);
  3462. };
  3463. createDirectory(parentName, function(parent){
  3464. parent.getDirectory(childName, {create: true}, function(child){
  3465. child.removeRecursively(function(){
  3466. root.getDirectory(parentName, {create: false}, win, failed.bind(this, done, 'root.getDirectory - Error getting parent directory : ' + parentName));
  3467. },
  3468. failed.bind(this, done, 'getDirectory - Error removing directory : ' + childName));
  3469. }, failed.bind(this, done, 'getDirectory - Error creating directory : ' + childName));
  3470. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + parentName));
  3471. });
  3472. it("file.spec.136 Paths should support Unicode symbols.", function (done) {
  3473. var dirName = '文件插件';
  3474. var win = function(directory){
  3475. expect(directory).toBeDefined();
  3476. expect(directory.isFile).toBe(false);
  3477. expect(directory.isDirectory).toBe(true);
  3478. expect(directory.name).toCanonicallyMatch(dirName);
  3479. deleteEntry(directory.name, done);
  3480. };
  3481. createDirectory(dirName, function(){
  3482. root.getDirectory(dirName, {create: false}, win,
  3483. failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + dirName));
  3484. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + dirName));
  3485. });
  3486. });
  3487. // Content and Asset URLs
  3488. if (cordova.platformId == 'android') {
  3489. describe('content: URLs', function() {
  3490. function testContentCopy(src, done) {
  3491. var file2 = "entry.copy.file2b",
  3492. fullPath = joinURL(temp_root.fullPath, file2),
  3493. validateFile = function (entry) {
  3494. expect(entry.isFile).toBe(true);
  3495. expect(entry.isDirectory).toBe(false);
  3496. expect(entry.name).toCanonicallyMatch(file2);
  3497. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3498. expect(entry.filesystem.name).toEqual("temporary");
  3499. // cleanup
  3500. deleteEntry(entry.name, done);
  3501. },
  3502. transfer = function () {
  3503. resolveLocalFileSystemURL(src, function(entry) {
  3504. expect(entry).toBeDefined();
  3505. expect(entry.filesystem.name).toEqual("content");
  3506. entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + entry.toURL() + ' to TEMPORAL root as: ' + file2));
  3507. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for content provider'));
  3508. };
  3509. // Delete any existing file to start things off
  3510. temp_root.getFile(file2, {}, function (entry) {
  3511. entry.remove(transfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3512. }, transfer);
  3513. }
  3514. it("file.spec.138 copyTo: content", function(done) {
  3515. testContentCopy('content://org.apache.cordova.file.testprovider/www/index.html', done);
  3516. });
  3517. it("file.spec.139 copyTo: content /w space and query", function(done) {
  3518. testContentCopy('content://org.apache.cordova.file.testprovider/?name=foo%20bar&realPath=%2Fwww%2Findex.html', done);
  3519. });
  3520. it("file.spec.140 delete: content should fail", function(done) {
  3521. resolveLocalFileSystemURL('content://org.apache.cordova.file.testprovider/www/index.html', function(entry) {
  3522. entry.remove(failed.bind(null, done, 'expected delete to fail'), done);
  3523. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for content provider'));
  3524. });
  3525. });
  3526. // these tests ensure that you can read and copy from android_asset folder
  3527. // for details see https://issues.apache.org/jira/browse/CB-6428
  3528. // and https://mail-archives.apache.org/mod_mbox/cordova-dev/201508.mbox/%3C782154441.8406572.1440182722528.JavaMail.yahoo%40mail.yahoo.com%3E
  3529. describe('asset: URLs', function() {
  3530. it("file.spec.141 filePaths.applicationStorage", function() {
  3531. expect(cordova.file.applicationDirectory).toEqual('file:///android_asset/');
  3532. }, MEDIUM_TIMEOUT);
  3533. it("file.spec.142 assets should be enumerable", function(done) {
  3534. resolveLocalFileSystemURL('file:///android_asset/www/fixtures/asset-test', function(entry) {
  3535. var reader = entry.createReader();
  3536. reader.readEntries(function (entries) {
  3537. expect(entries.length).not.toBe(0);
  3538. done();
  3539. }, failed.bind(null, done, 'reader.readEntries - Error during reading of entries from assets directory'));
  3540. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3541. }, MEDIUM_TIMEOUT);
  3542. it("file.spec.145 asset subdirectories should be obtainable", function(done) {
  3543. resolveLocalFileSystemURL('file:///android_asset/www/fixtures', function(entry) {
  3544. entry.getDirectory('asset-test', { create: false }, function (subDir) {
  3545. expect(subDir).toBeDefined();
  3546. expect(subDir.isFile).toBe(false);
  3547. expect(subDir.isDirectory).toBe(true);
  3548. expect(subDir.name).toCanonicallyMatch('asset-test');
  3549. done();
  3550. }, failed.bind(null, done, 'entry.getDirectory - Error getting asset subdirectory'));
  3551. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3552. }, MEDIUM_TIMEOUT);
  3553. it("file.spec.146 asset files should be readable", function(done) {
  3554. resolveLocalFileSystemURL('file:///android_asset/www/fixtures/asset-test/asset-test.txt', function(entry) {
  3555. expect(entry.isFile).toBe(true);
  3556. entry.file(function (file) {
  3557. expect(file).toBeDefined();
  3558. var reader = new FileReader();
  3559. reader.onerror = failed.bind(null, done, 'reader.readAsText - Error reading asset text file');
  3560. reader.onloadend = function () {
  3561. expect(this.result).toBeDefined();
  3562. expect(this.result.length).not.toBe(0);
  3563. done();
  3564. };
  3565. reader.readAsText(file);
  3566. }, failed.bind(null, done, 'entry.file - Error reading asset file'));
  3567. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3568. }, MEDIUM_TIMEOUT);
  3569. it("file.spec.143 copyTo: asset -> temporary", function(done) {
  3570. var file2 = "entry.copy.file2b",
  3571. fullPath = joinURL(temp_root.fullPath, file2),
  3572. validateFile = function (entry) {
  3573. expect(entry.isFile).toBe(true);
  3574. expect(entry.isDirectory).toBe(false);
  3575. expect(entry.name).toCanonicallyMatch(file2);
  3576. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3577. expect(entry.filesystem.name).toEqual("temporary");
  3578. // cleanup
  3579. deleteEntry(entry.name, done);
  3580. },
  3581. transfer = function () {
  3582. resolveLocalFileSystemURL('file:///android_asset/www/index.html', function(entry) {
  3583. expect(entry.filesystem.name).toEqual('assets');
  3584. entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + entry.toURL() + ' to TEMPORAL root as: ' + file2));
  3585. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3586. };
  3587. // Delete any existing file to start things off
  3588. temp_root.getFile(file2, {}, function (entry) {
  3589. entry.remove(transfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3590. }, transfer);
  3591. }, MEDIUM_TIMEOUT);
  3592. });
  3593. it("file.spec.144 copyTo: asset directory", function (done) {
  3594. var srcUrl = 'file:///android_asset/www/fixtures/asset-test';
  3595. var dstDir = "entry.copy.dstDir";
  3596. var dstPath = joinURL(root.fullPath, dstDir);
  3597. // create a new directory entry to kick off it
  3598. deleteEntry(dstDir, function () {
  3599. resolveLocalFileSystemURL(srcUrl, function(directory) {
  3600. directory.copyTo(root, dstDir, function (directory) {
  3601. expect(directory).toBeDefined();
  3602. expect(directory.isFile).toBe(false);
  3603. expect(directory.isDirectory).toBe(true);
  3604. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  3605. expect(directory.name).toCanonicallyMatch(dstDir);
  3606. root.getDirectory(dstDir, {
  3607. create : false
  3608. }, function (dirEntry) {
  3609. expect(dirEntry).toBeDefined();
  3610. expect(dirEntry.isFile).toBe(false);
  3611. expect(dirEntry.isDirectory).toBe(true);
  3612. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  3613. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  3614. dirEntry.getFile('asset-test.txt', {
  3615. create : false
  3616. }, function (fileEntry) {
  3617. expect(fileEntry).toBeDefined();
  3618. expect(fileEntry.isFile).toBe(true);
  3619. // cleanup
  3620. deleteEntry(dstDir, done);
  3621. }, failed.bind(null, done, 'dirEntry.getFile - Error getting subfile'));
  3622. }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory'));
  3623. }, failed.bind(null, done, 'directory.copyTo - Error copying directory'));
  3624. }, failed.bind(null, done, 'resolving src dir'));
  3625. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  3626. }, MEDIUM_TIMEOUT);
  3627. }
  3628. });
  3629. };
  3630. //******************************************************************************************
  3631. //***************************************Manual Tests***************************************
  3632. //******************************************************************************************
  3633. exports.defineManualTests = function (contentEl, createActionButton) {
  3634. function resolveFs(fsname) {
  3635. var fsURL = "cdvfile://localhost/" + fsname + "/";
  3636. logMessage("Resolving URL: " + fsURL);
  3637. resolveLocalFileSystemURL(fsURL, function (entry) {
  3638. logMessage("Success", 'green');
  3639. logMessage(entry.toURL(), 'blue');
  3640. logMessage(entry.toInternalURL(), 'blue');
  3641. logMessage("Resolving URL: " + entry.toURL());
  3642. resolveLocalFileSystemURL(entry.toURL(), function (entry2) {
  3643. logMessage("Success", 'green');
  3644. logMessage(entry2.toURL(), 'blue');
  3645. logMessage(entry2.toInternalURL(), 'blue');
  3646. }, logError("resolveLocalFileSystemURL"));
  3647. }, logError("resolveLocalFileSystemURL"));
  3648. }
  3649. function testPrivateURL() {
  3650. requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
  3651. logMessage("Temporary root is at " + fileSystem.root.toNativeURL());
  3652. fileSystem.root.getFile("testfile", {
  3653. create : true
  3654. }, function (entry) {
  3655. logMessage("Temporary file is at " + entry.toNativeURL());
  3656. if (entry.toNativeURL().substring(0, 12) == "file:///var/") {
  3657. logMessage("File starts with /var/, trying /private/var");
  3658. var newURL = "file://localhost/private/var/" + entry.toNativeURL().substring(12) + "?and=another_thing";
  3659. //var newURL = entry.toNativeURL();
  3660. logMessage(newURL, 'blue');
  3661. resolveLocalFileSystemURL(newURL, function (newEntry) {
  3662. logMessage("Successfully resolved.", 'green');
  3663. logMessage(newEntry.toURL(), 'blue');
  3664. logMessage(newEntry.toNativeURL(), 'blue');
  3665. }, logError("resolveLocalFileSystemURL"));
  3666. }
  3667. }, logError("getFile"));
  3668. }, logError("requestFileSystem"));
  3669. }
  3670. function clearLog() {
  3671. var log = document.getElementById("info");
  3672. log.innerHTML = "";
  3673. }
  3674. function logMessage(message, color) {
  3675. var log = document.getElementById("info");
  3676. var logLine = document.createElement('div');
  3677. if (color) {
  3678. logLine.style.color = color;
  3679. }
  3680. logLine.innerHTML = message;
  3681. log.appendChild(logLine);
  3682. }
  3683. function logError(serviceName) {
  3684. return function (err) {
  3685. logMessage("ERROR: " + serviceName + " " + JSON.stringify(err), "red");
  3686. };
  3687. }
  3688. var fsRoots = {
  3689. "ios" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
  3690. "osx" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
  3691. "android" : "files,files-external,documents,sdcard,cache,cache-external,root",
  3692. "amazon-fireos" : "files,files-external,documents,sdcard,cache,cache-external,root",
  3693. "windows": "temporary,persistent"
  3694. };
  3695. //Add title and align to content
  3696. var div = document.createElement('h2');
  3697. div.appendChild(document.createTextNode('File Systems'));
  3698. div.setAttribute("align", "center");
  3699. contentEl.appendChild(div);
  3700. div = document.createElement('h3');
  3701. div.appendChild(document.createTextNode('Results are displayed in yellow status box below with expected results noted under that'));
  3702. div.setAttribute("align", "center");
  3703. contentEl.appendChild(div);
  3704. div = document.createElement('div');
  3705. div.setAttribute("id", "button");
  3706. div.setAttribute("align", "center");
  3707. contentEl.appendChild(div);
  3708. if (fsRoots.hasOwnProperty(cordova.platformId)) {
  3709. (fsRoots[cordova.platformId].split(',')).forEach(function (fs) {
  3710. if (cordova.platformId === 'ios' && fs === 'private') {
  3711. createActionButton("Test private URL (iOS)", function () {
  3712. clearLog();
  3713. testPrivateURL();
  3714. }, 'button');
  3715. } else {
  3716. createActionButton(fs, function () {
  3717. clearLog();
  3718. resolveFs(fs);
  3719. }, 'button');
  3720. }
  3721. });
  3722. }
  3723. div = document.createElement('div');
  3724. div.setAttribute("id", "info");
  3725. div.setAttribute("align", "center");
  3726. contentEl.appendChild(div);
  3727. div = document.createElement('h3');
  3728. div.appendChild(document.createTextNode('For each test above, file or directory should be successfully found. ' +
  3729. 'Status box should say Resolving URL was Success. The first URL resolved is the internal URL. ' +
  3730. 'The second URL resolved is the absolute URL. Blue URLs must match.'));
  3731. contentEl.appendChild(div);
  3732. div = document.createElement('h3');
  3733. div.appendChild(document.createTextNode('For Test private URL (iOS), the private URL (first blue URL in status box) ' +
  3734. 'should be successfully resolved. Status box should say Successfully resolved. Both blue URLs below ' +
  3735. 'that should match.'));
  3736. contentEl.appendChild(div);
  3737. };