1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 var currentTest
= null;
8 function dispatchClick(element
) {
9 element
.dispatchEvent(new MouseEvent('click'));
12 function getVisibleElementByTagName(tag
) {
13 var selector
= '.function:not([hidden]) ' + tag
;
14 return document
.querySelector(selector
);
17 function clickRadio(name
) {
18 currentTest
.log('Clicking ' + name
+ ' radio button.');
19 dispatchClick(document
.getElementById('radio_' + name
));
22 function setInputValue(value
, selector
) {
27 currentTest
.log('Setting input box to "' + value
+ '".');
28 getVisibleElementByTagName(selector
).value
= value
;
31 function setTextareaValue(value
) {
32 currentTest
.log('Setting textarea to "' + value
+ '".');
33 getVisibleElementByTagName('textarea').value
= value
;
36 function getTextareaValue() {
37 return getVisibleElementByTagName('textarea').value
;
40 function getLastLogMessage() {
41 var logEl
= document
.getElementById('log');
42 var logLines
= logEl
.textContent
.split('\n');
43 return logLines
[logLines
.length
- 1];
46 function waitForLog(logMessage
, onLogChanged
, onError
) {
47 // Clear the log. This prevents a previous failure from propagating to the
48 // current check. (NOTE: the log is backed by an array, so as soon as a
49 // message is logged it will be refilled with its previous data in addition
50 // to the new message.)
51 document
.getElementById('log').textContent
= '';
53 // Poll for log changes.
54 var intervalId
= window
.setInterval(function() {
55 var lastLogMessage
= getLastLogMessage();
57 if (lastLogMessage
.lastIndexOf('Error:', 0) === 0) {
58 window
.clearInterval(intervalId
);
60 currentTest
.log('Got error message, continuing.');
63 currentTest
.fail('Unexpected failure waiting for log change.');
69 if (logMessage
!== lastLogMessage
)
72 currentTest
.log('Got log message, continuing.');
73 window
.clearInterval(intervalId
);
78 function clickExecuteButtonAndWaitForLog(logMessage
, onLogChanged
, onError
) {
79 waitForLog(logMessage
, onLogChanged
, onError
);
80 currentTest
.log('Clicking button.');
81 dispatchClick(getVisibleElementByTagName('button'));
82 currentTest
.log('Waiting for log message: "' + logMessage
+ '".');
85 function isFilenameInDirectoryList(filename
) {
86 var listItemEls
= document
.querySelectorAll('#listDirOutput li');
88 currentTest
.log('Looking for ' + filename
);
89 for (var i
= 0; i
< listItemEls
.length
; ++i
) {
90 var itemText
= listItemEls
[i
].textContent
;
91 currentTest
.log('Found ' + itemText
);
92 if (itemText
=== filename
) {
100 function saveFile(filename
, fileText
, onFileSaved
, onError
) {
101 clickRadio('saveFile');
102 setInputValue(filename
);
103 setTextareaValue(fileText
);
104 clickExecuteButtonAndWaitForLog('Save success', onFileSaved
, onError
);
107 function loadFile(filename
, onFileLoaded
, onError
) {
108 clickRadio('loadFile');
109 setInputValue(filename
);
110 setTextareaValue(''); // Clear the textarea.
111 clickExecuteButtonAndWaitForLog('Load success', onFileLoaded
, onError
);
114 function deleteFile(filename
, onFileDeleted
, onError
) {
115 clickRadio('delete');
116 setInputValue(filename
);
117 clickExecuteButtonAndWaitForLog('Delete success', onFileDeleted
, onError
);
120 function listDir(dirname
, onDirectoryListed
, onError
) {
121 clickRadio('listDir');
122 setInputValue(dirname
);
123 clickExecuteButtonAndWaitForLog('List success', onDirectoryListed
, onError
);
126 function makeDir(dirname
, onDirectoryMade
, onError
) {
127 clickRadio('makeDir');
128 setInputValue(dirname
);
129 clickExecuteButtonAndWaitForLog('Make directory success',
130 onDirectoryMade
, onError
);
133 function rename(oldname
, newname
, onRenamed
, onError
) {
134 clickRadio('rename');
135 setInputValue(oldname
, '#renameOld');
136 setInputValue(newname
, '#renameNew');
137 clickExecuteButtonAndWaitForLog('Rename success', onRenamed
, onError
);
140 function expectEq(expected
, actual
, additionalInfo
) {
142 if (expected
!== actual
) {
144 message
= additionalInfo
+ ': ';
145 message
+= 'Expected "' + expected
+ '", got "' + actual
+ '".';
146 currentTest
.fail(message
);
148 message
= 'OK, "' + expected
+ '" === "' + actual
+ '".';
149 currentTest
.log(message
);
153 function expectContains(needle
, haystack
, additionalInfo
) {
154 if (haystack
.indexOf(needle
) === -1) {
156 message
= additionalInfo
+ ': ';
157 message
+= 'Expected to find "' + needle
+ '" in "' + haystack
+ '".';
158 currentTest
.fail(message
);
160 message
= 'OK, "' + needle
+ '" in "' + haystack
+ '".';
161 currentTest
.log(message
);
165 function expectFilenameInDirectoryList(filename
, additionalInfo
) {
166 if (!isFilenameInDirectoryList(filename
)) {
168 message
= additionalInfo
+ ': ';
169 message
+= 'Expected to find "' + filename
+ '" in directory list.';
170 currentTest
.fail(message
);
172 message
= 'OK, found "' + filename
+ ' in the directory list.';
173 currentTest
.log(message
);
177 common
.tester
.addAsyncTest('filesystem_ready', function(test
) {
178 // This is a bit fragile; we rely on this test being run first (and
179 // completing) before we can run any of the other tests.
181 var message
= 'Filesystem ready!';
183 // This message may already be logged.
184 if (getLastLogMessage() == message
) {
189 waitForLog('Filesystem ready!', function() {
192 test
.fail('Got unexpected error waiting for filesystem: ');
196 common
.tester
.addAsyncTest('save_and_load', function(test
) {
198 var filename
= '/save_and_load.txt';
199 var fileText
= 'A penny saved is a penny earned.';
202 saveFile(filename
, fileText
, function() {
203 // Now try to load it.
204 loadFile(filename
, function() {
205 // Make sure the text matches.
206 expectEq(fileText
, getTextareaValue(), 'Incorrect textarea');
212 common
.tester
.addAsyncTest('delete_file', function(test
) {
214 var filename
= '/delete_file.txt';
216 saveFile(filename
, 'Here today, gone tomorrow.', function() {
217 deleteFile(filename
, function() {
218 loadFile(filename
, function() {
219 test
.fail('Unexpected load success.');
222 expectEq('', getTextareaValue(), 'Unexpected data in file');
223 expectContains('File not found', getLastLogMessage(),
224 'Unexpected log message');
231 common
.tester
.addAsyncTest('list_directory', function(test
) {
233 var filename
= '/list_directory.txt';
235 saveFile(filename
, 'I\'ve got a little list...', function() {
236 listDir('/', function() {
237 // Directory listings are relative, so it will not have the leading
239 var relativeFilename
= filename
.slice(1);
240 expectFilenameInDirectoryList(relativeFilename
);
246 common
.tester
.addAsyncTest('make_directory', function(test
) {
248 var dirname
= '/new_directory';
250 makeDir(dirname
, function() {
251 listDir('/', function() {
252 // Directory listings are relative, so it will not have the leading
254 var relativeDirname
= dirname
.slice(1);
255 expectFilenameInDirectoryList(relativeDirname
);
257 // Let's see if the file can be written to this directory.
258 var filename
= dirname
+ '/file.txt';
259 var fileText
= 'A file within a directory.';
260 saveFile(filename
, fileText
, function() {
267 common
.tester
.addAsyncTest('rename_file', function(test
) {
269 var filename
= '/rename_old.txt';
270 var newFilename
= '/rename_new.txt';
271 var fileText
= 'What\'s in a name? that which we call a rose ' +
272 'by any other name would smell as sweet;';
275 saveFile(filename
, fileText
, function() {
277 rename(filename
, newFilename
, function() {
278 // Now try to load it.
279 loadFile(newFilename
, function() {
280 // Make sure the text matches.
281 expectEq(fileText
, getTextareaValue(), 'Incorrect textarea');
283 // Make sure the old file no longer exists.
284 loadFile(filename
, function() {
285 test
.fail('Unexpected load success.');
288 expectEq('', getTextareaValue(), 'Unexpected data in file');
289 expectContains('File not found', getLastLogMessage(),
290 'Unexpected log message');