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 var EXTENSION_ID = 'kidcpjlbjdmcnmccjhjdckhbngnhnepk';
6 var FILE_CONTENTS = 'hello from test extension.';
8 function errorCallback(error) {
14 case FileError.QUOTA_EXCEEDED_ERR:
15 msg = 'QUOTA_EXCEEDED_ERR';
17 case FileError.NOT_FOUND_ERR:
18 msg = 'NOT_FOUND_ERR';
20 case FileError.SECURITY_ERR:
23 case FileError.INVALID_MODIFICATION_ERR:
24 msg = 'INVALID_MODIFICATION_ERR';
26 case FileError.INVALID_STATE_ERR:
27 msg = 'INVALID_STATE_ERR';
30 msg = 'Unknown Error';
35 chrome.test.fail(msg);
38 function ensureFileExists(entry, successCallback, errorCallback) {
39 entry.filesystem.root.getFile(entry.fullPath,
45 function writeToFile(entry) {
46 entry.createWriter(function(writer) {
47 writer.onerror = function(e) {
48 errorCallback(writer.error);
50 writer.onwrite = chrome.test.succeed;
52 var blob = new Blob([FILE_CONTENTS], {type: 'text/plain'});
57 chrome.test.runTests([
58 function selectionSuccessful() {
59 // The test will call selectFile function and expect it to succeed.
60 // When it gets the file entry, it verifies that the permissions given in
61 // the method allow the extension to read/write to selected file.
62 chrome.fileBrowserHandler.selectFile(
63 { suggestedName: 'some_file_name.txt',
64 allowedFileExtensions: ['txt', 'html'] },
66 chrome.test.assertTrue(!!result);
67 chrome.test.assertTrue(result.success);
68 chrome.test.assertTrue(!!result.entry);
70 ensureFileExists(result.entry, writeToFile, errorCallback);
73 function selectionFails() {
74 // The test expects that selectFile returns failure with an empty entry.
75 chrome.fileBrowserHandler.selectFile({ suggestedName: 'fail' },
77 chrome.test.assertTrue(!!result);
78 // Entry should be set iff operation succeeded.
79 chrome.test.assertEq(false, result.success);
80 chrome.test.assertTrue(result.entry == null);
81 chrome.test.succeed();