OLD | NEW |
---|---|
(Empty) | |
1 <html> | |
2 <head> | |
3 <script src="../../resources/js-test.js"></script> | |
4 <script src="resources/shared.js"></script> | |
5 </head> | |
6 <body> | |
7 <input type="file" id="fileInput" multiple></input> | |
8 <script> | |
9 | |
10 description("Confirm basic Blob/File/FileList functionality."); | |
11 | |
12 fileInput = document.getElementById("fileInput"); | |
jsbell
2014/06/02 23:05:06
Note to self: we should wrap this into a helper th
ericu
2014/06/02 23:53:07
That would be much nicer.
| |
13 if (window.eventSender) { | |
14 var fileRect = fileInput.getClientRects()[0]; | |
15 var targetX = fileRect.left + fileRect.width / 2; | |
16 var targetY = fileRect.top + fileRect.height / 2; | |
17 eventSender.beginDragWithFiles(['resources/test-data.html', 'resources/test- data.txt']); | |
18 eventSender.mouseMoveTo(targetX, targetY); | |
19 eventSender.mouseUp(); | |
20 } | |
21 | |
22 function prepareDatabase() | |
23 { | |
24 db = event.target.result; | |
25 var trans = event.target.transaction; | |
26 evalAndLog("store = db.createObjectStore('storeName')"); | |
27 evalAndLog("store.put('value', 'key')"); | |
28 trans.onerror = unexpectedErrorCallback; | |
29 trans.onabort = unexpectedAbortCallback; | |
30 } | |
31 | |
32 var blobValidation = ".size == test_content.length"; | |
33 function testBlob() | |
34 { | |
35 debug(""); | |
36 debug("testBlob():"); | |
37 | |
38 shouldBeTrue("FileReader != null"); | |
39 evalAndLog("test_content = 'This is a test. This is only a test.'"); | |
40 evalAndLog("blob = new Blob([test_content])"); | |
41 validateResult("blob", blobValidation, testFile); | |
42 } | |
43 | |
44 var fileValidation = ".name == fileInput.files[0].name"; | |
45 function testFile() | |
46 { | |
47 debug(""); | |
48 debug("testFile():"); | |
49 evalAndLog("file = fileInput.files[0]"); | |
50 validateResult("file", fileValidation, testFileList); | |
51 } | |
52 | |
53 var fileListValidation = "[1].name == fileInput.files[1].name"; | |
54 function testFileList() | |
55 { | |
56 debug(""); | |
57 debug("testFileList():"); | |
58 evalAndLog("filelist = fileInput.files"); | |
59 validateResult("filelist", | |
60 fileListValidation, testCursor); | |
61 } | |
62 | |
63 function testCursor() | |
64 { | |
65 debug(""); | |
66 debug("testCursor():"); | |
67 evalAndLog("transaction = db.transaction('storeName', 'readonly')"); | |
68 transaction.onerror = unexpectedErrorCallback; | |
69 evalAndLog("store = transaction.objectStore('storeName')"); | |
70 evalAndLog("request = store.openCursor()"); | |
71 | |
72 var count = 0; | |
73 request.onsuccess = continueToTest; | |
74 | |
75 function continueToTest(event) | |
76 { | |
77 cursor = event.target.result; | |
78 switch (count++) { | |
79 case 0: | |
80 shouldBeTrue("cursor.value" + blobValidation); | |
81 break; | |
82 case 1: | |
83 shouldBeTrue("cursor.value" + fileValidation); | |
84 break; | |
85 case 2: | |
86 shouldBeTrue("cursor.value" + fileListValidation); | |
87 break; | |
88 case 3: | |
89 shouldBeTrue("cursor.value == 'value'"); | |
90 break; | |
91 case 4: | |
92 shouldBeNull("cursor"); | |
93 finishJSTest(); | |
94 return; | |
95 default: | |
96 testFailed("count was unexpectedly " + count); | |
97 } | |
98 evalAndLog("cursor.continue();"); | |
99 } | |
100 } | |
101 | |
102 function validateResult(variable, validation, onComplete) | |
103 { | |
104 var keyName = variable + "key"; | |
105 debug(""); | |
106 debug("validateResult(" + variable + "):"); | |
107 evalAndLog("transaction = db.transaction('storeName', 'readwrite')"); | |
108 evalAndLog("store = transaction.objectStore('storeName')"); | |
109 evalAndLog("store.put(" + variable + ", '" + keyName + "')"); | |
110 var readTransactionOnComplete = function (e) { | |
111 shouldBeTrue("event.target.result" + validation); | |
112 onComplete(); | |
113 } | |
114 transaction.oncomplete = function () { | |
115 doRead(keyName, readTransactionOnComplete); | |
116 } | |
117 } | |
118 | |
119 function doRead(keyName, onComplete) | |
120 { | |
121 evalAndLog("transaction = db.transaction('storeName', 'readwrite')"); | |
122 evalAndLog("store = transaction.objectStore('storeName')"); | |
123 evalAndLog("request = store.get('" + keyName + "')"); | |
124 request.onsuccess = onComplete; | |
125 transaction.onerror = unexpectedErrorCallback; | |
126 } | |
127 | |
128 if (window.eventSender) { | |
129 indexedDBTest(prepareDatabase, testBlob); | |
130 } else { | |
131 alert("Select at least 2 files using the input control above to initiate the test"); | |
132 document.getElementById("fileInput").onchange = function() { indexedDBTest(p repareDatabase, testBlob); }; | |
133 } | |
134 | |
135 </script> | |
136 </body> | |
137 </html> | |
OLD | NEW |