Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / native_client_sdk / src / examples / api / file_io / test.js
blob7ec3662e25e006217bd2055f7f5f47e1014745f0
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.
5 function addTests() {
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) {
23 if (!selector) {
24 selector = 'input';
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);
59 if (onError) {
60 currentTest.log('Got error message, continuing.');
61 onError();
62 } else {
63 currentTest.fail('Unexpected failure waiting for log change.');
66 return;
69 if (logMessage !== lastLogMessage)
70 return;
72 currentTest.log('Got log message, continuing.');
73 window.clearInterval(intervalId);
74 onLogChanged();
75 }, 100);
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) {
93 return true;
97 return false;
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) {
141 var message;
142 if (expected !== actual) {
143 if (additionalInfo)
144 message = additionalInfo + ': ';
145 message += 'Expected "' + expected + '", got "' + actual + '".';
146 currentTest.fail(message);
147 } else {
148 message = 'OK, "' + expected + '" === "' + actual + '".';
149 currentTest.log(message);
153 function expectContains(needle, haystack, additionalInfo) {
154 if (haystack.indexOf(needle) === -1) {
155 if (additionalInfo)
156 message = additionalInfo + ': ';
157 message += 'Expected to find "' + needle + '" in "' + haystack + '".';
158 currentTest.fail(message);
159 } else {
160 message = 'OK, "' + needle + '" in "' + haystack + '".';
161 currentTest.log(message);
165 function expectFilenameInDirectoryList(filename, additionalInfo) {
166 if (!isFilenameInDirectoryList(filename)) {
167 if (additionalInfo)
168 message = additionalInfo + ': ';
169 message += 'Expected to find "' + filename + '" in directory list.';
170 currentTest.fail(message);
171 } else {
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.
180 currentTest = test;
181 var message = 'Filesystem ready!';
183 // This message may already be logged.
184 if (getLastLogMessage() == message) {
185 test.pass();
186 return;
189 waitForLog('Filesystem ready!', function() {
190 test.pass();
191 }, function() {
192 test.fail('Got unexpected error waiting for filesystem: ');
196 common.tester.addAsyncTest('save_and_load', function(test) {
197 currentTest = test;
198 var filename = '/save_and_load.txt';
199 var fileText = 'A penny saved is a penny earned.';
201 // Save the file.
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');
207 test.pass();
212 common.tester.addAsyncTest('delete_file', function(test) {
213 currentTest = 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.');
221 function() {
222 expectEq('', getTextareaValue(), 'Unexpected data in file');
223 expectContains('File not found', getLastLogMessage(),
224 'Unexpected log message');
225 test.pass();
231 common.tester.addAsyncTest('list_directory', function(test) {
232 currentTest = 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
238 // slash.
239 var relativeFilename = filename.slice(1);
240 expectFilenameInDirectoryList(relativeFilename);
241 test.pass();
246 common.tester.addAsyncTest('make_directory', function(test) {
247 currentTest = 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
253 // slash.
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() {
261 test.pass();
267 common.tester.addAsyncTest('rename_file', function(test) {
268 currentTest = 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;';
274 // Save the file.
275 saveFile(filename, fileText, function() {
276 // Now rename it.
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.');
287 function() {
288 expectEq('', getTextareaValue(), 'Unexpected data in file');
289 expectContains('File not found', getLastLogMessage(),
290 'Unexpected log message');
291 test.pass();