1 // Copyright (c) 2012 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 // This is a duplicate of the file test_util in
6 // chrome/test/data/extensions/api_test/file_system
8 function checkEntry(entry, expectedName, isNew, shouldBeWritable) {
9 chrome.test.assertEq(expectedName, entry.name);
10 // Test that the file can be read.
11 entry.file(chrome.test.callback(function(file) {
12 var reader = new FileReader();
13 reader.onloadend = chrome.test.callbackPass(function(e) {
15 chrome.test.assertEq(reader.result, "");
17 chrome.test.assertEq(reader.result.indexOf("Can you see me?"), 0);
18 // Test that we can write to the file, or not, depending on
19 // |shouldBeWritable|.
20 entry.createWriter(function(fileWriter) {
21 fileWriter.onwriteend = chrome.test.callback(function(e) {
22 if (fileWriter.error) {
23 if (shouldBeWritable) {
24 chrome.test.fail("Error writing to file: " +
25 fileWriter.error.toString());
27 chrome.test.succeed();
30 if (shouldBeWritable) {
31 // Get a new entry and check the data got to disk.
32 chrome.fileSystem.chooseEntry(chrome.test.callbackPass(
34 readEntry.file(chrome.test.callback(function(readFile) {
35 var readReader = new FileReader();
36 readReader.onloadend = function(e) {
37 chrome.test.assertEq(readReader.result.indexOf("HoHoHo!"),
39 chrome.test.succeed();
41 readReader.onerror = function(e) {
42 chrome.test.fail("Failed to read file after write.");
44 readReader.readAsText(readFile);
49 "'Could write to file that should not be writable.");
53 var blob = new Blob(["HoHoHo!"], {type: "text/plain"});
54 fileWriter.write(blob);
57 reader.onerror = chrome.test.callback(function(e) {
58 chrome.test.fail("Error reading file contents.");
60 reader.readAsText(file);