Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / file_system / test_util.js
bloba768b029fbdfbdd14813c457d1bb741f3f5993ca
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);
15   }));
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) {
21       if (isNew)
22         chrome.test.assertEq(reader.result, "");
23       else
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());
33             } else {
34               chrome.test.succeed();
35             }
36           } else {
37             if (shouldBeWritable) {
38               // Get a new entry and check the data got to disk.
39               chrome.fileSystem.chooseEntry(chrome.test.callbackPass(
40                   function(readEntry) {
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!"),
45                                          0);
46                     chrome.test.succeed();
47                   };
48                   readReader.onerror = function(e) {
49                     chrome.test.fail("Failed to read file after write.");
50                   };
51                   readReader.readAsText(readFile);
52                 }));
53               }));
54             } else {
55               chrome.test.fail(
56                  "'Could write to file that should not be writable.");
57             }
58           }
59         });
60         var blob = new Blob(["HoHoHo!"], {type: "text/plain"});
61         fileWriter.write(blob);
62       });
63     });
64     reader.onerror = chrome.test.callback(function(e) {
65       chrome.test.fail("Error reading file contents.");
66     });
67     reader.readAsText(file);
68   }));