- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_browser / filehandler_create / 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 EXTENSION_ID = 'kidcpjlbjdmcnmccjhjdckhbngnhnepk';
6 var FILE_CONTENTS = 'hello from test extension.';
7
8 function errorCallback(error) {
9   var msg = '';
10   if (!error.code) {
11     msg = error.message;
12   } else {
13     switch (error.code) {
14       case FileError.QUOTA_EXCEEDED_ERR:
15         msg = 'QUOTA_EXCEEDED_ERR';
16         break;
17       case FileError.NOT_FOUND_ERR:
18         msg = 'NOT_FOUND_ERR';
19         break;
20       case FileError.SECURITY_ERR:
21         msg = 'SECURITY_ERR';
22         break;
23       case FileError.INVALID_MODIFICATION_ERR:
24         msg = 'INVALID_MODIFICATION_ERR';
25         break;
26       case FileError.INVALID_STATE_ERR:
27         msg = 'INVALID_STATE_ERR';
28         break;
29       default:
30         msg = 'Unknown Error';
31         break;
32     };
33   }
34
35   chrome.test.fail(msg);
36 }
37
38 function ensureFileExists(entry, successCallback, errorCallback) {
39   entry.filesystem.root.getFile(entry.fullPath,
40                                 {create: true},
41                                 successCallback,
42                                 errorCallback);
43 }
44
45 function writeToFile(entry) {
46   entry.createWriter(function(writer) {
47     writer.onerror = function(e) {
48       errorCallback(writer.error);
49     };
50     writer.onwrite = chrome.test.succeed;
51
52     var blob = new Blob([FILE_CONTENTS], {type: 'text/plain'});
53     writer.write(blob);
54   }, errorCallback);
55 }
56
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'] },
65         function(result) {
66           chrome.test.assertTrue(!!result);
67           chrome.test.assertTrue(result.success);
68           chrome.test.assertTrue(!!result.entry);
69
70           ensureFileExists(result.entry, writeToFile, errorCallback);
71       });
72   },
73   function selectionFails() {
74     // The test expects that selectFile returns failure with an empty entry.
75     chrome.fileBrowserHandler.selectFile({ suggestedName: 'fail' },
76         function(result) {
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();
82         });
83   }]);