Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / truncate / test.js
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.
4
5 'use strict';
6
7 /**
8  * @type {string}
9  * @const
10  */
11 var TESTING_TIRAMISU_FILE_NAME = 'tiramisu.txt';
12
13 /**
14  * Requests truncating a file to the specified length.
15  *
16  * @param {TruncateRequestedOptions} options Options.
17  * @param {function()} onSuccess Success callback.
18  * @param {function(string)} onError Error callback.
19  */
20 function onTruncateRequested(options, onSuccess, onError) {
21   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
22     onError('SECURITY');  // enum ProviderError.
23     return;
24   }
25
26   if (!(options.filePath in test_util.defaultMetadata)) {
27     onError('INVALID_OPERATION');  // enum ProviderError.
28     return;
29   }
30
31   var metadata = test_util.defaultMetadata[options.filePath];
32
33   // Truncating beyond the end of the file.
34   if (options.length > metadata.size) {
35     onError('INVALID_OPERATION');
36     return;
37   }
38
39   metadata.size = options.length;
40   onSuccess();
41 }
42
43 /**
44  * Sets up the tests. Called once per all test cases. In case of a failure,
45  * the callback is not called.
46  *
47  * @param {function()} callback Success callback.
48  */
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);
58
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)
64   };
65
66   chrome.fileSystemProvider.onTruncateRequested.addListener(
67       onTruncateRequested);
68
69   test_util.mountFileSystem(callback);
70 }
71
72 /**
73  * Runs all of the test cases, one by one.
74  */
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);
97                   };
98                   fileWriter.truncate(64);
99                 }),
100                 function(error) {
101                   chrome.test.fail(error.name);
102                 });
103           }),
104           function(error) {
105             chrome.test.fail(error.name);
106           });
107     },
108
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.');
123                   });
124                   fileWriter.onerror = chrome.test.callbackPass(function(e) {
125                     chrome.test.assertEq(
126                         'InvalidModificationError', fileWriter.error.name);
127                   });
128                   fileWriter.truncate(test_util.defaultMetadata[
129                       '/' + TESTING_TIRAMISU_FILE_NAME].size * 2);
130                 }),
131                 function(error) {
132                   chrome.test.fail();
133                 });
134           }),
135           function(error) {
136             chrome.test.fail(error.name);
137           });
138     }
139   ]);
140 }
141
142 // Setup and run all of the test cases.
143 setUp(runTests);