Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / file_system_provider / truncate / test.js
blob7f7316d6eb8a27ef63bbc9416473988fdd58a339
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.
5 'use strict';
7 /**
8 * @type {string}
9 * @const
11 var TESTING_TIRAMISU_FILE_NAME = 'tiramisu.txt';
13 /**
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.
23 return;
26 if (!(options.filePath in test_util.defaultMetadata)) {
27 onError('INVALID_OPERATION'); // enum ProviderError.
28 return;
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');
36 return;
39 metadata.size = options.length;
40 onSuccess();
43 /**
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] = {
60 isDirectory: false,
61 name: TESTING_TIRAMISU_FILE_NAME,
62 size: 128,
63 modificationTime: new Date(2014, 1, 24, 6, 35, 11)
66 chrome.fileSystemProvider.onTruncateRequested.addListener(
67 onTruncateRequested);
69 test_util.mountFileSystem(callback);
72 /**
73 * Runs all of the test cases, one by one.
75 function runTests() {
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
87 // happened.
88 if (fileWriter.error)
89 return;
90 chrome.test.assertEq(
91 64,
92 test_util.defaultMetadata[
93 '/' + TESTING_TIRAMISU_FILE_NAME].size);
94 });
95 fileWriter.onerror = function(e) {
96 chrome.test.fail(fileWriter.error.name);
98 fileWriter.truncate(64);
99 }),
100 function(error) {
101 chrome.test.fail(error.name);
104 function(error) {
105 chrome.test.fail(error.name);
109 // Truncate a file to a length larger than size. This should result in an
110 // error.
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)
120 return;
121 chrome.test.fail(
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);
131 function(error) {
132 chrome.test.fail();
135 function(error) {
136 chrome.test.fail(error.name);
142 // Setup and run all of the test cases.
143 setUp(runTests);