Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!-- |
michael@0 | 2 | Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | --> |
michael@0 | 5 | <!DOCTYPE HTML> |
michael@0 | 6 | <html> <!-- |
michael@0 | 7 | https://bugzilla.mozilla.org/show_bug.cgi?id=910412 |
michael@0 | 8 | --> |
michael@0 | 9 | <head> |
michael@0 | 10 | <title>Test createDirectory of the FileSystem API for device storage</title> |
michael@0 | 11 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 12 | <script type="text/javascript" src="devicestorage_common.js"></script> |
michael@0 | 13 | |
michael@0 | 14 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 15 | </head> |
michael@0 | 16 | <body> |
michael@0 | 17 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=910412">Mozilla Bug 910412</a> |
michael@0 | 18 | <p id="display"></p> |
michael@0 | 19 | <div id="content" style="display: none"> |
michael@0 | 20 | </div> |
michael@0 | 21 | <pre id="test"> |
michael@0 | 22 | <script class="testbody" type="application/javascript;version=1.7"> |
michael@0 | 23 | |
michael@0 | 24 | devicestorage_setup(); |
michael@0 | 25 | |
michael@0 | 26 | let gTestCount = 0; |
michael@0 | 27 | let gFileReader = new FileReader(); |
michael@0 | 28 | let gRoot; |
michael@0 | 29 | |
michael@0 | 30 | function str2array(str) { |
michael@0 | 31 | let strlen = str.length; |
michael@0 | 32 | let buf = new ArrayBuffer(strlen); |
michael@0 | 33 | let bufView = new Uint8Array(buf); |
michael@0 | 34 | for (let i=0; i < strlen; i++) { |
michael@0 | 35 | bufView[i] = str.charCodeAt(i); |
michael@0 | 36 | } |
michael@0 | 37 | return buf; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | function array2str(data) { |
michael@0 | 41 | return String.fromCharCode.apply(String, new Uint8Array(data)); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | let gTestCases = [ |
michael@0 | 45 | // Create with string data. |
michael@0 | 46 | { |
michael@0 | 47 | text: "My name is Yuan.", |
michael@0 | 48 | get data() { return this.text; }, |
michael@0 | 49 | shouldPass: true, |
michael@0 | 50 | mode: "replace" |
michael@0 | 51 | }, |
michael@0 | 52 | |
michael@0 | 53 | // Create with array buffer data. |
michael@0 | 54 | { |
michael@0 | 55 | text: "I'm from Kunming.", |
michael@0 | 56 | get data() { return str2array(this.text); }, |
michael@0 | 57 | shouldPass: true, |
michael@0 | 58 | mode: "replace" |
michael@0 | 59 | }, |
michael@0 | 60 | |
michael@0 | 61 | // Create with array buffer view data. |
michael@0 | 62 | { |
michael@0 | 63 | text: "Kunming is in Yunnan province of China.", |
michael@0 | 64 | get data() { return Uint8Array(str2array(this.text)); }, |
michael@0 | 65 | shouldPass: true, |
michael@0 | 66 | mode: "replace" |
michael@0 | 67 | }, |
michael@0 | 68 | |
michael@0 | 69 | // Create with blob data. |
michael@0 | 70 | { |
michael@0 | 71 | text: "Kunming is in Yunnan province of China.", |
michael@0 | 72 | get data() { return new Blob([this.text], {type: 'image/png'}); }, |
michael@0 | 73 | shouldPass: true, |
michael@0 | 74 | mode: "replace" |
michael@0 | 75 | }, |
michael@0 | 76 | |
michael@0 | 77 | // Don't overwrite existing file. |
michael@0 | 78 | { |
michael@0 | 79 | data: null, |
michael@0 | 80 | shouldPass: false, |
michael@0 | 81 | mode: "fail" |
michael@0 | 82 | } |
michael@0 | 83 | ]; |
michael@0 | 84 | |
michael@0 | 85 | function next() { |
michael@0 | 86 | if (gTestCount >= gTestCases.length) { |
michael@0 | 87 | devicestorage_cleanup(); |
michael@0 | 88 | return; |
michael@0 | 89 | } |
michael@0 | 90 | let c = gTestCases[gTestCount++]; |
michael@0 | 91 | gRoot.createFile("text.png", { |
michael@0 | 92 | data: c.data, |
michael@0 | 93 | ifExists: c.mode |
michael@0 | 94 | }).then(function(file) { |
michael@0 | 95 | is(c.shouldPass, true, "[" + gTestCount + "] Success callback was called for createFile."); |
michael@0 | 96 | if (!c.shouldPass) { |
michael@0 | 97 | SimpleTest.executeSoon(next); |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | // Check the file content. |
michael@0 | 101 | gFileReader.readAsArrayBuffer(file); |
michael@0 | 102 | gFileReader.onload = function(e) { |
michael@0 | 103 | ab = e.target.result; |
michael@0 | 104 | is(array2str(e.target.result), c.text, "[" + gTestCount + "] Wrong values."); |
michael@0 | 105 | SimpleTest.executeSoon(next); |
michael@0 | 106 | }; |
michael@0 | 107 | }, function(e) { |
michael@0 | 108 | is(c.shouldPass, false, "[" + gTestCount + "] Error callback was called for createFile."); |
michael@0 | 109 | SimpleTest.executeSoon(next); |
michael@0 | 110 | }); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | ok(navigator.getDeviceStorage, "Should have getDeviceStorage."); |
michael@0 | 114 | |
michael@0 | 115 | let storage = navigator.getDeviceStorage("pictures"); |
michael@0 | 116 | ok(storage, "Should have gotten a storage."); |
michael@0 | 117 | |
michael@0 | 118 | // Get the root directory |
michael@0 | 119 | storage.getRoot().then(function(dir) { |
michael@0 | 120 | ok(dir, "Should have gotten the root directory."); |
michael@0 | 121 | gRoot = dir; |
michael@0 | 122 | next(); |
michael@0 | 123 | }, function(e) { |
michael@0 | 124 | ok(false, e.name + " error should not arrive here!"); |
michael@0 | 125 | devicestorage_cleanup(); |
michael@0 | 126 | }); |
michael@0 | 127 | |
michael@0 | 128 | </script> |
michael@0 | 129 | </pre> |
michael@0 | 130 | </body> |
michael@0 | 131 | </html> |
michael@0 | 132 |