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 test_util
.fileSystem
.root
.getFile(
80 TESTING_TIRAMISU_FILE_NAME
,
81 {create
: false, exclusive
: true},
82 chrome
.test
.callbackPass(function(fileEntry
) {
83 fileEntry
.createWriter(
84 chrome
.test
.callbackPass(function(fileWriter
) {
85 fileWriter
.onwriteend
= chrome
.test
.callbackPass(function(e
) {
86 // Note that onwriteend() is called even if an error
92 test_util
.defaultMetadata
[
93 '/' + 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 test_util
.fileSystem
.root
.getFile(
113 TESTING_TIRAMISU_FILE_NAME
,
114 {create
: false, exclusive
: false},
115 chrome
.test
.callbackPass(function(fileEntry
) {
116 fileEntry
.createWriter(
117 chrome
.test
.callbackPass(function(fileWriter
) {
118 fileWriter
.onwriteend
= chrome
.test
.callbackPass(function(e
) {
119 if (fileWriter
.error
)
122 'Unexpectedly succeeded to truncate beyond a fiile.');
124 fileWriter
.onerror
= chrome
.test
.callbackPass(function(e
) {
125 chrome
.test
.assertEq(
126 'InvalidModificationError', fileWriter
.error
.name
);
128 fileWriter
.truncate(test_util
.defaultMetadata
[
129 '/' + TESTING_TIRAMISU_FILE_NAME
].size
* 2);
136 chrome
.test
.fail(error
.name
);
142 // Setup and run all of the test cases.