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
3 // found in the LICENSE file.
11 var TESTING_TIRAMISU_FILE_NAME = 'tiramisu.txt';
14 * Requests truncating a file to the specified length.
16 * @param {TruncateRequestedOptions} options Options.
17 * @param {function()} onSuccess Success callback.
18 * @param {function(string)} onError Error callback.
20 function onTruncateRequested(options, onSuccess, onError) {
21 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
22 onError('SECURITY'); // enum ProviderError.
26 if (!(options.filePath in test_util.defaultMetadata)) {
27 onError('INVALID_OPERATION'); // enum ProviderError.
31 var metadata = test_util.defaultMetadata[options.filePath];
33 // Truncating beyond the end of the file.
34 if (options.length > metadata.size) {
35 onError('INVALID_OPERATION');
39 metadata.size = options.length;
44 * Sets up the tests. Called once per all test cases. In case of a failure,
45 * the callback is not called.
47 * @param {function()} callback Success callback.
49 function setUp(callback) {
50 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
51 test_util.onGetMetadataRequestedDefault);
52 chrome.fileSystemProvider.onOpenFileRequested.addListener(
53 test_util.onOpenFileRequested);
54 chrome.fileSystemProvider.onCloseFileRequested.addListener(
55 test_util.onCloseFileRequested);
56 chrome.fileSystemProvider.onCreateFileRequested.addListener(
57 test_util.onCreateFileRequested);
59 test_util.defaultMetadata['/' + TESTING_TIRAMISU_FILE_NAME] = {
61 name: TESTING_TIRAMISU_FILE_NAME,
63 modificationTime: new Date(2014, 1, 24, 6, 35, 11)
66 chrome.fileSystemProvider.onTruncateRequested.addListener(
69 test_util.mountFileSystem(callback);
73 * Runs all of the test cases, one by one.
76 chrome.test.runTests([
77 // Truncate a file. It should succeed.
78 function truncateFileSuccess() {
79 var onTestSuccess = chrome.test.callbackPass();
80 test_util.fileSystem.root.getFile(
81 TESTING_TIRAMISU_FILE_NAME,
82 {create: false, exclusive: true},
84 fileEntry.createWriter(function(fileWriter) {
85 fileWriter.onwriteend = function(e) {
86 // Note that onwriteend() is called even if an error happened.
91 test_util.defaultMetadata[
92 '/' + TESTING_TIRAMISU_FILE_NAME].size);
95 fileWriter.onerror = function(e) {
96 chrome.test.fail(fileWriter.error.name);
98 fileWriter.truncate(64);
101 chrome.test.fail(error.name);
105 chrome.test.fail(error.name);
109 // Truncate a file to a length larger than size. This should result in an
111 function truncateBeyondFileError() {
112 var onTestSuccess = chrome.test.callbackPass();
113 test_util.fileSystem.root.getFile(
114 TESTING_TIRAMISU_FILE_NAME,
115 {create: false, exclusive: false},
116 function(fileEntry) {
117 fileEntry.createWriter(function(fileWriter) {
118 fileWriter.onwriteend = function(e) {
119 if (fileWriter.error)
122 'Unexpectedly succeeded to truncate beyond a fiile.');
124 fileWriter.onerror = function(e) {
125 chrome.test.assertEq(
126 'InvalidModificationError', fileWriter.error.name);
129 fileWriter.truncate(test_util.defaultMetadata[
130 '/' + TESTING_TIRAMISU_FILE_NAME].size * 2);
137 chrome.test.fail(error.name);
143 // Setup and run all of the test cases.