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
);
11 // Test that we are writable (or not), as expected.
12 chrome
.fileSystem
.isWritableEntry(entry
, chrome
.test
.callbackPass(
13 function(isWritable
) {
14 chrome
.test
.assertEq(isWritable
, shouldBeWritable
);
17 // Test that the file can be read.
18 entry
.file(chrome
.test
.callback(function(file
) {
19 var reader
= new FileReader();
20 reader
.onloadend
= chrome
.test
.callbackPass(function(e
) {
22 chrome
.test
.assertEq(reader
.result
, "");
24 chrome
.test
.assertEq(reader
.result
.indexOf("Can you see me?"), 0);
25 // Test that we can write to the file, or not, depending on
26 // |shouldBeWritable|.
27 entry
.createWriter(function(fileWriter
) {
28 fileWriter
.onwriteend
= chrome
.test
.callback(function(e
) {
29 if (fileWriter
.error
) {
30 if (shouldBeWritable
) {
31 chrome
.test
.fail("Error writing to file: " +
32 fileWriter
.error
.toString());
34 chrome
.test
.succeed();
37 if (shouldBeWritable
) {
38 // Get a new entry and check the data got to disk.
39 chrome
.fileSystem
.chooseEntry(chrome
.test
.callbackPass(
41 readEntry
.file(chrome
.test
.callback(function(readFile
) {
42 var readReader
= new FileReader();
43 readReader
.onloadend = function(e
) {
44 chrome
.test
.assertEq(readReader
.result
.indexOf("HoHoHo!"),
46 chrome
.test
.succeed();
48 readReader
.onerror = function(e
) {
49 chrome
.test
.fail("Failed to read file after write.");
51 readReader
.readAsText(readFile
);
56 "'Could write to file that should not be writable.");
60 var blob
= new Blob(["HoHoHo!"], {type
: "text/plain"});
61 fileWriter
.write(blob
);
64 reader
.onerror
= chrome
.test
.callback(function(e
) {
65 chrome
.test
.fail("Error reading file contents.");
67 reader
.readAsText(file
);