- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system / retain_entry / test_util.js
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.
4
5 // This is a duplicate of the file test_util in
6 // chrome/test/data/extensions/api_test/file_system
7
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) {
14       if (isNew)
15         chrome.test.assertEq(reader.result, "");
16       else
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());
26             } else {
27               chrome.test.succeed();
28             }
29           } else {
30             if (shouldBeWritable) {
31               // Get a new entry and check the data got to disk.
32               chrome.fileSystem.chooseEntry(chrome.test.callbackPass(
33                   function(readEntry) {
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!"),
38                                          0);
39                     chrome.test.succeed();
40                   };
41                   readReader.onerror = function(e) {
42                     chrome.test.fail("Failed to read file after write.");
43                   };
44                   readReader.readAsText(readFile);
45                 }));
46               }));
47             } else {
48               chrome.test.fail(
49                  "'Could write to file that should not be writable.");
50             }
51           }
52         });
53         var blob = new Blob(["HoHoHo!"], {type: "text/plain"});
54         fileWriter.write(blob);
55       });
56     });
57     reader.onerror = chrome.test.callback(function(e) {
58       chrome.test.fail("Error reading file contents.");
59     });
60     reader.readAsText(file);
61   }));
62 }