Upstream version 9.38.198.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       var onTestSuccess = chrome.test.callbackPass();
80       test_util.fileSystem.root.getFile(
81           TESTING_TIRAMISU_FILE_NAME,
82           {create: false, exclusive: true},
83           function(fileEntry) {
84             fileEntry.createWriter(function(fileWriter) {
85               fileWriter.onwriteend = function(e) {
86                 // Note that onwriteend() is called even if an error happened.
87                 if (fileWriter.error)
88                   return;
89                 chrome.test.assertEq(
90                     64,
91                     test_util.defaultMetadata[
92                         '/' + TESTING_TIRAMISU_FILE_NAME].size);
93                 onTestSuccess();
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       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)
120                   return;
121                 chrome.test.fail(
122                     'Unexpectedly succeeded to truncate beyond a fiile.');
123               };
124               fileWriter.onerror = function(e) {
125                 chrome.test.assertEq(
126                     'InvalidModificationError', fileWriter.error.name);
127                 onTestSuccess();
128               };
129               fileWriter.truncate(test_util.defaultMetadata[
130                   '/' + TESTING_TIRAMISU_FILE_NAME].size * 2);
131             },
132             function(error) {
133               chrome.test.fail();
134             });
135           },
136           function(error) {
137             chrome.test.fail(error.name);
138           });
139     }
140   ]);
141 }
142
143 // Setup and run all of the test cases.
144 setUp(runTests);