OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @type {DOMFileSystem} | 8 * @type {DOMFileSystem} |
9 */ | 9 */ |
10 var fileSystem = null; | 10 var fileSystem = null; |
11 | 11 |
12 /** | 12 /** |
13 * @type {string} | 13 * @type {string} |
14 * @const | 14 * @const |
15 */ | 15 */ |
16 var FILE_SYSTEM_ID = 'vanilla'; | |
17 | |
18 /** | |
19 * @type {string} | |
20 * @const | |
21 */ | |
22 var TESTING_MIME_TYPE = 'text/secret-testing-mime-type'; | 16 var TESTING_MIME_TYPE = 'text/secret-testing-mime-type'; |
23 | 17 |
24 /** | 18 /** |
25 * @type {Object} | 19 * @type {Object} |
26 * @const | 20 * @const |
27 */ | 21 */ |
28 var TESTING_ROOT = Object.freeze({ | 22 var TESTING_ROOT = Object.freeze({ |
29 isDirectory: true, | 23 isDirectory: true, |
30 name: '', | 24 name: '', |
31 size: 0, | 25 size: 0, |
(...skipping 17 matching lines...) Expand all Loading... |
49 * @const | 43 * @const |
50 */ | 44 */ |
51 var TESTING_WITHOUT_MIME_FILE = Object.freeze({ | 45 var TESTING_WITHOUT_MIME_FILE = Object.freeze({ |
52 isDirectory: false, | 46 isDirectory: false, |
53 name: 'second-file.abc', | 47 name: 'second-file.abc', |
54 size: 4096, | 48 size: 4096, |
55 modificationTime: new Date(2014, 4, 28, 10, 39, 15) | 49 modificationTime: new Date(2014, 4, 28, 10, 39, 15) |
56 }); | 50 }); |
57 | 51 |
58 /** | 52 /** |
59 * Gets volume information for the provided file system. | |
60 * | |
61 * @param {string} fileSystemId Id of the provided file system. | |
62 * @param {function(Object)} callback Callback to be called on result, with the | |
63 * volume information object in case of success, or null if not found. | |
64 */ | |
65 function getVolumeInfo(fileSystemId, callback) { | |
66 chrome.fileBrowserPrivate.getVolumeMetadataList(function(volumeList) { | |
67 for (var i = 0; i < volumeList.length; i++) { | |
68 if (volumeList[i].extensionId == chrome.runtime.id && | |
69 volumeList[i].fileSystemId == fileSystemId) { | |
70 callback(volumeList[i]); | |
71 return; | |
72 } | |
73 } | |
74 callback(null); | |
75 }); | |
76 } | |
77 | |
78 /** | |
79 * Returns metadata for a requested entry. | 53 * Returns metadata for a requested entry. |
80 * | 54 * |
81 * @param {GetMetadataRequestedOptions} options Options. | 55 * @param {GetMetadataRequestedOptions} options Options. |
82 * @param {function(Object)} onSuccess Success callback with metadata passed | 56 * @param {function(Object)} onSuccess Success callback with metadata passed |
83 * an argument. | 57 * an argument. |
84 * @param {function(string)} onError Error callback with an error code. | 58 * @param {function(string)} onError Error callback with an error code. |
85 */ | 59 */ |
86 function onGetMetadataRequested(options, onSuccess, onError) { | 60 function onGetMetadataRequested(options, onSuccess, onError) { |
87 if (options.fileSystemId != FILE_SYSTEM_ID) { | 61 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) { |
88 onError('SECURITY'); // enum ProviderError. | 62 onError('SECURITY'); // enum ProviderError. |
89 return; | 63 return; |
90 } | 64 } |
91 | 65 |
92 if (options.entryPath == '/') { | 66 if (options.entryPath == '/') { |
93 onSuccess(TESTING_ROOT); | 67 onSuccess(TESTING_ROOT); |
94 return; | 68 return; |
95 } | 69 } |
96 | 70 |
97 if (options.entryPath == '/' + TESTING_WITH_MIME_FILE.name) { | 71 if (options.entryPath == '/' + TESTING_WITH_MIME_FILE.name) { |
98 onSuccess(TESTING_WITH_MIME_FILE); | 72 onSuccess(TESTING_WITH_MIME_FILE); |
99 return; | 73 return; |
100 } | 74 } |
101 | 75 |
102 if (options.entryPath == '/' + TESTING_WITHOUT_MIME_FILE.name) { | 76 if (options.entryPath == '/' + TESTING_WITHOUT_MIME_FILE.name) { |
103 onSuccess(TESTING_WITHOUT_MIME_FILE); | 77 onSuccess(TESTING_WITHOUT_MIME_FILE); |
104 return; | 78 return; |
105 } | 79 } |
106 | 80 |
107 onError('NOT_FOUND'); // enum ProviderError. | 81 onError('NOT_FOUND'); // enum ProviderError. |
108 } | 82 } |
109 | 83 |
110 /** | 84 /** |
111 * Sets up the tests. Called once per all test cases. In case of a failure, | 85 * Sets up the tests. Called once per all test cases. In case of a failure, |
112 * the callback is not called. | 86 * the callback is not called. |
113 * | 87 * |
114 * @param {function()} callback Success callback. | 88 * @param {function()} callback Success callback. |
115 */ | 89 */ |
116 function setUp(callback) { | 90 function setUp(callback) { |
117 chrome.fileSystemProvider.mount( | 91 chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
118 {fileSystemId: FILE_SYSTEM_ID, displayName: 'chocolate.zip'}, | 92 onGetMetadataRequested); |
119 function() { | 93 test_util.mountFileSystem(callback); |
120 chrome.fileSystemProvider.onGetMetadataRequested.addListener( | |
121 onGetMetadataRequested); | |
122 | |
123 getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) { | |
124 chrome.test.assertTrue(!!volumeInfo); | |
125 chrome.fileBrowserPrivate.requestFileSystem( | |
126 volumeInfo.volumeId, | |
127 function(inFileSystem) { | |
128 chrome.test.assertTrue(!!inFileSystem); | |
129 | |
130 fileSystem = inFileSystem; | |
131 callback(); | |
132 }); | |
133 }); | |
134 }, | |
135 function() { | |
136 chrome.test.fail(); | |
137 }); | |
138 } | 94 } |
139 | 95 |
140 /** | 96 /** |
141 * Runs all of the test cases, one by one. | 97 * Runs all of the test cases, one by one. |
142 */ | 98 */ |
143 function runTests() { | 99 function runTests() { |
144 chrome.test.runTests([ | 100 chrome.test.runTests([ |
145 // Test if the file with a mime type handled by this testing extension | 101 // Test if the file with a mime type handled by this testing extension |
146 // appears on a task list. | 102 // appears on a task list. |
147 function withMimeIsTask() { | 103 function withMimeIsTask() { |
148 var onSuccess = chrome.test.callbackPass(); | 104 var onSuccess = chrome.test.callbackPass(); |
149 fileSystem.root.getFile(TESTING_WITH_MIME_FILE.name, {}, function(entry) { | 105 test_util.fileSystem.root.getFile( |
150 chrome.fileBrowserPrivate.getFileTasks( | 106 TESTING_WITH_MIME_FILE.name, |
151 [entry.toURL()], | 107 {}, |
152 function(tasks) { | 108 function(entry) { |
153 chrome.test.assertEq(1, tasks.length); | 109 chrome.fileBrowserPrivate.getFileTasks( |
154 chrome.test.assertEq( | 110 [entry.toURL()], |
155 "ddammdhioacbehjngdmkjcjbnfginlla|app|magic_handler", | 111 function(tasks) { |
156 tasks[0].taskId); | 112 chrome.test.assertEq(1, tasks.length); |
157 onSuccess(); | 113 chrome.test.assertEq( |
158 }); | 114 "ddammdhioacbehjngdmkjcjbnfginlla|app|magic_handler", |
159 }, function(error) { | 115 tasks[0].taskId); |
160 chrome.test.fail(error.name); | 116 onSuccess(); |
161 }); | 117 }); |
| 118 }, function(error) { |
| 119 chrome.test.fail(error.name); |
| 120 }); |
162 }, | 121 }, |
163 // Confirm, that executing that task, will actually run an OnLaunched event. | 122 // Confirm, that executing that task, will actually run an OnLaunched event. |
164 // This is another code path, than collecting tasks (tested above). | 123 // This is another code path, than collecting tasks (tested above). |
165 function withMimeExecute() { | 124 function withMimeExecute() { |
166 var onSuccess = chrome.test.callbackPass(); | 125 var onSuccess = chrome.test.callbackPass(); |
167 fileSystem.root.getFile(TESTING_WITH_MIME_FILE.name, {}, function(entry) { | 126 test_util.fileSystem.root.getFile( |
168 chrome.fileBrowserPrivate.getFileTasks( | 127 TESTING_WITH_MIME_FILE.name, {}, function(entry) { |
169 [entry.toURL()], | 128 chrome.fileBrowserPrivate.getFileTasks( |
170 function(tasks) { | 129 [entry.toURL()], |
171 chrome.test.assertEq(1, tasks.length); | 130 function(tasks) { |
172 chrome.test.assertEq( | 131 chrome.test.assertEq(1, tasks.length); |
173 "ddammdhioacbehjngdmkjcjbnfginlla|app|magic_handler", | |
174 tasks[0].taskId); | |
175 var onLaunched = function(event) { | |
176 chrome.test.assertTrue(!!event); | |
177 chrome.test.assertEq("magic_handler", event.id); | |
178 chrome.test.assertTrue(!!event.items); | |
179 chrome.test.assertEq(1, event.items.length); | |
180 chrome.test.assertEq( | 132 chrome.test.assertEq( |
181 TESTING_MIME_TYPE, event.items[0].type); | 133 "ddammdhioacbehjngdmkjcjbnfginlla|app|magic_handler", |
182 chrome.test.assertEq( | 134 tasks[0].taskId); |
183 TESTING_WITH_MIME_FILE.name, | 135 var onLaunched = function(event) { |
184 event.items[0].entry.name); | 136 chrome.test.assertTrue(!!event); |
185 chrome.app.runtime.onLaunched.removeListener(onLaunched); | 137 chrome.test.assertEq("magic_handler", event.id); |
186 onSuccess(); | 138 chrome.test.assertTrue(!!event.items); |
187 }; | 139 chrome.test.assertEq(1, event.items.length); |
188 chrome.app.runtime.onLaunched.addListener(onLaunched); | 140 chrome.test.assertEq( |
189 chrome.fileBrowserPrivate.executeTask( | 141 TESTING_MIME_TYPE, event.items[0].type); |
190 tasks[0].taskId, [entry.toURL()]); | 142 chrome.test.assertEq( |
191 }); | 143 TESTING_WITH_MIME_FILE.name, |
192 }, function(error) { | 144 event.items[0].entry.name); |
193 chrome.test.fail(error.name); | 145 chrome.app.runtime.onLaunched.removeListener(onLaunched); |
194 }); | 146 onSuccess(); |
| 147 }; |
| 148 chrome.app.runtime.onLaunched.addListener(onLaunched); |
| 149 chrome.fileBrowserPrivate.executeTask( |
| 150 tasks[0].taskId, [entry.toURL()]); |
| 151 }); |
| 152 }, function(error) { |
| 153 chrome.test.fail(error.name); |
| 154 }); |
195 }, | 155 }, |
196 // The file without a mime set must not appear on the task list for this | 156 // The file without a mime set must not appear on the task list for this |
197 // testing extension. | 157 // testing extension. |
198 function withoutMime() { | 158 function withoutMime() { |
199 var onSuccess = chrome.test.callbackPass(); | 159 var onSuccess = chrome.test.callbackPass(); |
200 fileSystem.root.getFile( | 160 test_util.fileSystem.root.getFile( |
201 TESTING_WITHOUT_MIME_FILE.name, | 161 TESTING_WITHOUT_MIME_FILE.name, |
202 {}, | 162 {}, |
203 function(entry) { | 163 function(entry) { |
204 chrome.fileBrowserPrivate.getFileTasks( | 164 chrome.fileBrowserPrivate.getFileTasks( |
205 [entry.toURL()], | 165 [entry.toURL()], |
206 function(tasks) { | 166 function(tasks) { |
207 chrome.test.assertEq(0, tasks.length); | 167 chrome.test.assertEq(0, tasks.length); |
208 onSuccess(); | 168 onSuccess(); |
209 }); | 169 }); |
210 }, function(error) { | 170 }, function(error) { |
211 chrome.test.fail(error.name); | 171 chrome.test.fail(error.name); |
212 }); | 172 }); |
213 } | 173 } |
214 ]); | 174 ]); |
215 } | 175 } |
216 | 176 |
217 // Setup and run all of the test cases. | 177 // Setup and run all of the test cases. |
218 setUp(runTests); | 178 setUp(runTests); |
OLD | NEW |