- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_browser / file_browser_handler_lazy / background.js
1 // Copyright (c) 2013 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 /**
6  * This extension provides two file browser handlers: 'ReadOnly' and
7  * 'ReadWrite'. 'ReadOnly' handler handles .xul files and has read-only file
8  * access. 'ReadWrite' handler handles .tiff files and has read-write file
9  * access. The extension does not have persistent background page.
10  *
11  * The extension waits until 'ReadWrite' handler is executed and then runs tests
12  * for it. The tests verify that the extension is able to read and write the
13  * handled file. If there is an error before the tests are run,
14  * chrome.test.notifyFail will be called, and further onExecute events will be
15  * ignored.
16  *
17  * 'ReadOnly' handler is ignored by this test extension, but it must be defined
18  * to conform to the expectations of file_browser/handler_test_runner
19  *
20  * The handlers are executed by 'file_browser/handler_test_runner' extension.
21  * 'ReadWrite' handler are executed once, and should carry exactly one  handled
22  * file.
23  */
24
25 // Initial content of handled files. The content is set in
26 // external_filesystem_apitest.cc.
27 var kInitialTestFileContent = 'This is some test content.';
28 // Content written by write test.
29 var kTextToWrite = ' Yay!';
30
31 /**
32  * Asserts that |value| equals |expectedValue|. If the assert fails, current
33  * test fails with |errorMessage|. Otherwise, |callback()| is called.
34  */
35 function assertEqAndRunCallback(expectedValue, value, errorMessage, callback) {
36   chrome.test.assertEq(expectedValue, value, errorMessage);
37   callback();
38 }
39
40 /**
41  * Attempts to read file and asserts that the read success and file content are
42  * as expected (|expectSuccess|, |expectedContent|). On success |callback| is
43  * called, otherwise current test is failed.
44  *
45  * @param {FileEntry} entry Entry to be read.
46  * @param {boolean} expectSuccess Whether the read should succeed.
47  * @param {string} expectedContent If the read succeeds, the expected content to
48  *     be read from file. If the read fails, it is ignored.
49  * @param {function()} callback Function called if the read ends as defined by
50  *     |expectSuccess| and |expectedContent|.
51  */
52 function readAndExpectContent(entry, expectSuccess, expectedContent, callback) {
53   var error = 'Reading file \'' + entry.fullPath + '\'.';
54   var reader = new FileReader();
55
56   reader.onload = function() {
57     chrome.test.assertTrue(expectSuccess, error);
58     assertEqAndRunCallback(expectedContent, reader.result, error, callback);
59   };
60
61   entry.file(reader.readAsText.bind(reader),
62              assertEqAndRunCallback.bind(null,
63                  false, expectSuccess, error, callback));
64 };
65
66 /**
67  * Attempts to write |content| to the end of the |entry| and verifies that the
68  * operation success is as expected. On success |callback| is called, else the
69  * current test is failed.
70  *
71  * @param {FileEntry} entry File entry to be read.
72  * @param {string} content String content to be appended to the file.
73  * @param {boolean} expectSuccess Whether the write should succeed.
74  * @param {function()} callback Function called if write ends as defined by
75  *     |expectSuccess|.
76  */
77 function write(entry, content, expectSuccess, callback) {
78   var error = 'Writing to: \'' + entry.fullPath + '\'.';
79
80   entry.createWriter(function(writer) {
81     writer.onerror = assertEqAndRunCallback.bind(null, expectSuccess, false,
82                                                  error, callback);
83     writer.onwrite = assertEqAndRunCallback.bind(null, expectSuccess, true,
84                                                  error, callback);
85
86     writer.seek(kInitialTestFileContent.length);
87     var blob = new Blob([kTextToWrite], {type: 'text/plain'});
88     writer.write(blob);
89   },
90   assertEqAndRunCallback.bind(null, expectSuccess, false,
91       'Getting writer for: \'' + entry.fullPath + '\'.', callback));
92 };
93
94 /**
95  * Runs read test.
96  *
97  * @params {FileEntry} entry File entry for which the test should be run.
98  * @params {boolean} expectSuccess Whether the read should succeed.
99  */
100 function readTest(entry, expectSuccess) {
101   readAndExpectContent(entry, expectSuccess, kInitialTestFileContent,
102                        chrome.test.succeed)
103 }
104
105 /**
106  * Runs test for a file that is not executed for any handler. Test tries to get
107  * an existing file entry from the |entry|'s filesystem. The file path is
108  * constructed by appending '.foo' to the |entry|'s path (the Chrome part of the
109  * test should ensure that the file exists). The get operation is expected to
110  * fail.
111  */
112 function getSiblingTest(entry) {
113   var error = 'Got file (\'' + entry.fullPath.concat('.foo') + '\') for which' +
114               'file access was not granted.';
115   entry.filesystem.root.getFile(
116       entry.fullPath.concat('.foo'), {},
117       chrome.test.fail.bind(null, error),
118       chrome.test.succeed);
119 }
120
121 /**
122  * Runs write test.
123  * Attempts to write to the entry. If the write operation ends as expected, the
124  * test verifies new content of the file.
125  *
126  * @param {FileEntry} entry Entry to be written.
127  * @param {boolean} expectSuccess Whether the test should succeed.
128  */
129 function writeTest(entry, expectSuccess) {
130   var verifyFileContent = function() {
131     var expectedContent = kInitialTestFileContent;
132     if (expectSuccess)
133       expectedContent = expectedContent.concat(kTextToWrite);
134
135     readAndExpectContent(entry, true, expectedContent, chrome.test.succeed);
136   };
137
138   write(entry, kTextToWrite, expectSuccess, verifyFileContent);
139 }
140
141 /**
142  * Listens for onExecute events, and runs tests once the event for 'ReadWrite'
143  * handler is received.
144  */
145 function executeListener(id, details) {
146   if (id == 'ReadWrite') {
147     var fileEntries = details.entries;
148     if (!fileEntries || fileEntries.length != 1) {
149       chrome.test.notifyFail('Unexpected file entries size.');
150       return;
151     }
152
153     var entry = fileEntries[0];
154
155     // Run tests for read-write handler.
156     chrome.test.runTests([
157         function readReadWrite() {
158           readTest(entry, true);
159         },
160         function getSilblingReadWrite() {
161           getSiblingTest(entry);
162         },
163         function writeReadWrite() {
164           writeTest(entry, true);
165         },
166     ]);
167   } else if (id != 'ReadOnly') {
168     chrome.test.notifyFail('Unexpected action id: ' + id);
169     return;
170   }
171 }
172
173 chrome.fileBrowserHandler.onExecute.addListener(executeListener);