1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/devicestorage/test/test_fs_createFile.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,132 @@ 1.4 +<!-- 1.5 + Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ 1.7 +--> 1.8 +<!DOCTYPE HTML> 1.9 +<html> <!-- 1.10 +https://bugzilla.mozilla.org/show_bug.cgi?id=910412 1.11 +--> 1.12 +<head> 1.13 + <title>Test createDirectory of the FileSystem API for device storage</title> 1.14 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.15 + <script type="text/javascript" src="devicestorage_common.js"></script> 1.16 + 1.17 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.18 +</head> 1.19 +<body> 1.20 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=910412">Mozilla Bug 910412</a> 1.21 +<p id="display"></p> 1.22 +<div id="content" style="display: none"> 1.23 +</div> 1.24 +<pre id="test"> 1.25 +<script class="testbody" type="application/javascript;version=1.7"> 1.26 + 1.27 +devicestorage_setup(); 1.28 + 1.29 +let gTestCount = 0; 1.30 +let gFileReader = new FileReader(); 1.31 +let gRoot; 1.32 + 1.33 +function str2array(str) { 1.34 + let strlen = str.length; 1.35 + let buf = new ArrayBuffer(strlen); 1.36 + let bufView = new Uint8Array(buf); 1.37 + for (let i=0; i < strlen; i++) { 1.38 + bufView[i] = str.charCodeAt(i); 1.39 + } 1.40 + return buf; 1.41 +} 1.42 + 1.43 +function array2str(data) { 1.44 + return String.fromCharCode.apply(String, new Uint8Array(data)); 1.45 +} 1.46 + 1.47 +let gTestCases = [ 1.48 + // Create with string data. 1.49 + { 1.50 + text: "My name is Yuan.", 1.51 + get data() { return this.text; }, 1.52 + shouldPass: true, 1.53 + mode: "replace" 1.54 + }, 1.55 + 1.56 + // Create with array buffer data. 1.57 + { 1.58 + text: "I'm from Kunming.", 1.59 + get data() { return str2array(this.text); }, 1.60 + shouldPass: true, 1.61 + mode: "replace" 1.62 + }, 1.63 + 1.64 + // Create with array buffer view data. 1.65 + { 1.66 + text: "Kunming is in Yunnan province of China.", 1.67 + get data() { return Uint8Array(str2array(this.text)); }, 1.68 + shouldPass: true, 1.69 + mode: "replace" 1.70 + }, 1.71 + 1.72 + // Create with blob data. 1.73 + { 1.74 + text: "Kunming is in Yunnan province of China.", 1.75 + get data() { return new Blob([this.text], {type: 'image/png'}); }, 1.76 + shouldPass: true, 1.77 + mode: "replace" 1.78 + }, 1.79 + 1.80 + // Don't overwrite existing file. 1.81 + { 1.82 + data: null, 1.83 + shouldPass: false, 1.84 + mode: "fail" 1.85 + } 1.86 +]; 1.87 + 1.88 +function next() { 1.89 + if (gTestCount >= gTestCases.length) { 1.90 + devicestorage_cleanup(); 1.91 + return; 1.92 + } 1.93 + let c = gTestCases[gTestCount++]; 1.94 + gRoot.createFile("text.png", { 1.95 + data: c.data, 1.96 + ifExists: c.mode 1.97 + }).then(function(file) { 1.98 + is(c.shouldPass, true, "[" + gTestCount + "] Success callback was called for createFile."); 1.99 + if (!c.shouldPass) { 1.100 + SimpleTest.executeSoon(next); 1.101 + return; 1.102 + } 1.103 + // Check the file content. 1.104 + gFileReader.readAsArrayBuffer(file); 1.105 + gFileReader.onload = function(e) { 1.106 + ab = e.target.result; 1.107 + is(array2str(e.target.result), c.text, "[" + gTestCount + "] Wrong values."); 1.108 + SimpleTest.executeSoon(next); 1.109 + }; 1.110 + }, function(e) { 1.111 + is(c.shouldPass, false, "[" + gTestCount + "] Error callback was called for createFile."); 1.112 + SimpleTest.executeSoon(next); 1.113 + }); 1.114 +} 1.115 + 1.116 +ok(navigator.getDeviceStorage, "Should have getDeviceStorage."); 1.117 + 1.118 +let storage = navigator.getDeviceStorage("pictures"); 1.119 +ok(storage, "Should have gotten a storage."); 1.120 + 1.121 +// Get the root directory 1.122 +storage.getRoot().then(function(dir) { 1.123 + ok(dir, "Should have gotten the root directory."); 1.124 + gRoot = dir; 1.125 + next(); 1.126 +}, function(e) { 1.127 + ok(false, e.name + " error should not arrive here!"); 1.128 + devicestorage_cleanup(); 1.129 +}); 1.130 + 1.131 +</script> 1.132 +</pre> 1.133 +</body> 1.134 +</html> 1.135 +