- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / file_access_saved_to_prefs_test / test.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 var textToWrite = 'def';
6
7 function truncateAndWriteToFile(writableEntry, callback) {
8   writableEntry.createWriter(function(fileWriter) {
9     fileWriter.onerror = function(e) {
10       console.error("Couldn't write file: " + e.toString());
11     };
12     fileWriter.onwriteend = function(e) {
13       fileWriter.onwriteend = function(e) {
14         callback();
15       };
16       var blob = new Blob([textToWrite], {type: 'text/plain'});
17       fileWriter.write(blob);
18     };
19     fileWriter.truncate(0);
20   });
21 }
22
23 chrome.app.runtime.onLaunched.addListener(function() {
24   chrome.app.window.create('index.html', {width: 100, height: 100},
25       function(win) {
26     var fs = win.contentWindow.chrome.fileSystem;
27     fs.chooseEntry({type: 'openFile'}, function(entry) {
28       chrome.fileSystem.retainEntry(entry);
29       fs.getWritableEntry(entry, function(writableEntry) {
30         chrome.fileSystem.retainEntry(writableEntry);
31         truncateAndWriteToFile(writableEntry, function() {
32           chrome.test.sendMessage('fileWritten');
33           win.close();
34         });
35       });
36     });
37   });
38 });