Index: chrome/test/data/extensions/api_test/file_system_provider/truncate/test.js |
diff --git a/chrome/test/data/extensions/api_test/file_system_provider/truncate/test.js b/chrome/test/data/extensions/api_test/file_system_provider/truncate/test.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1f0ffd0bd87e514e0dce51e0cb7be1f04f8d0389 |
--- /dev/null |
+++ b/chrome/test/data/extensions/api_test/file_system_provider/truncate/test.js |
@@ -0,0 +1,144 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+'use strict'; |
+ |
+/** |
+ * @type {string} |
+ * @const |
+ */ |
+var TESTING_TIRAMISU_FILE_NAME = 'tiramisu.txt'; |
+ |
+/** |
+ * Requests truncating a file to the specified length. |
+ * |
+ * @param {TruncateRequestedOptions} options Options. |
+ * @param {function()} onSuccess Success callback. |
+ * @param {function(string)} onError Error callback. |
+ */ |
+function onTruncateRequested(options, onSuccess, onError) { |
+ if (options.fileSystemId != test_util.FILE_SYSTEM_ID) { |
+ onError('SECURITY'); // enum ProviderError. |
+ return; |
+ } |
+ |
+ if (!(options.filePath in test_util.defaultMetadata)) { |
+ onError('INVALID_OPERATION'); // enum ProviderError. |
+ return; |
+ } |
+ |
+ var metadata = test_util.defaultMetadata[options.filePath]; |
+ |
+ // Truncating beyond the end of the file. |
+ if (options.length > metadata.size) { |
+ onError('INVALID_OPERATION'); |
+ return; |
+ } |
+ |
+ metadata.size = options.length; |
+ onSuccess(); |
+} |
+ |
+/** |
+ * Sets up the tests. Called once per all test cases. In case of a failure, |
+ * the callback is not called. |
+ * |
+ * @param {function()} callback Success callback. |
+ */ |
+function setUp(callback) { |
+ chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
+ test_util.onGetMetadataRequestedDefault); |
+ chrome.fileSystemProvider.onOpenFileRequested.addListener( |
+ test_util.onOpenFileRequested); |
+ chrome.fileSystemProvider.onCloseFileRequested.addListener( |
+ test_util.onCloseFileRequested); |
+ chrome.fileSystemProvider.onCreateFileRequested.addListener( |
+ test_util.onCreateFileRequested); |
+ |
+ test_util.defaultMetadata['/' + TESTING_TIRAMISU_FILE_NAME] = { |
+ isDirectory: false, |
+ name: TESTING_TIRAMISU_FILE_NAME, |
+ size: 128, |
+ modificationTime: new Date(2014, 1, 24, 6, 35, 11) |
+ }; |
+ |
+ chrome.fileSystemProvider.onTruncateRequested.addListener( |
+ onTruncateRequested); |
+ |
+ test_util.mountFileSystem(callback); |
+} |
+ |
+/** |
+ * Runs all of the test cases, one by one. |
+ */ |
+function runTests() { |
+ chrome.test.runTests([ |
+ // Truncate a file. It should succeed. |
+ function truncateFileSuccess() { |
+ var onTestSuccess = chrome.test.callbackPass(); |
+ test_util.fileSystem.root.getFile( |
+ TESTING_TIRAMISU_FILE_NAME, |
+ {create: false, exclusive: true}, |
+ function(fileEntry) { |
+ fileEntry.createWriter(function(fileWriter) { |
+ fileWriter.onwriteend = function(e) { |
+ // Note that onwriteend() is called even if an error happened. |
+ if (fileWriter.error) |
+ return; |
+ chrome.test.assertEq( |
+ 64, |
+ test_util.defaultMetadata[ |
+ '/' + TESTING_TIRAMISU_FILE_NAME].size); |
+ onTestSuccess(); |
+ }; |
+ fileWriter.onerror = function(e) { |
+ chrome.test.fail(fileWriter.error.name); |
+ }; |
+ fileWriter.truncate(64); |
+ }, |
+ function(error) { |
+ chrome.test.fail(error.name); |
+ }); |
+ }, |
+ function(error) { |
+ chrome.test.fail(error.name); |
+ }); |
+ }, |
+ |
+ // Truncate a file to a length larger than size. This should result in an |
+ // error. |
+ function truncateBeyondFileError() { |
+ var onTestSuccess = chrome.test.callbackPass(); |
+ test_util.fileSystem.root.getFile( |
+ TESTING_TIRAMISU_FILE_NAME, |
+ {create: false, exclusive: false}, |
+ function(fileEntry) { |
+ fileEntry.createWriter(function(fileWriter) { |
+ fileWriter.onwriteend = function(e) { |
+ if (fileWriter.error) |
+ return; |
+ chrome.test.fail( |
+ 'Unexpectedly succeeded to truncate beyond a fiile.'); |
+ }; |
+ fileWriter.onerror = function(e) { |
+ chrome.test.assertEq( |
+ 'InvalidModificationError', fileWriter.error.name); |
+ onTestSuccess(); |
+ }; |
+ fileWriter.truncate(test_util.defaultMetadata[ |
+ '/' + TESTING_TIRAMISU_FILE_NAME].size * 2); |
+ }, |
+ function(error) { |
+ chrome.test.fail(); |
+ }); |
+ }, |
+ function(error) { |
+ chrome.test.fail(error.name); |
+ }); |
+ } |
+ ]); |
+} |
+ |
+// Setup and run all of the test cases. |
+setUp(runTests); |