Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / read_directory / 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 {Object}
9  * @const
10  */
11 var TESTING_HELLO_DIR = Object.freeze({
12   isDirectory: true,
13   name: 'hello',
14   size: 0,
15   modificationTime: new Date(2014, 3, 27, 9, 38, 14)
16 });
17
18 /**
19  * @type {Object}
20  * @const
21  */
22 var TESTING_CANDIES_DIR = Object.freeze({
23   isDirectory: true,
24   name: 'candies',
25   size: 0,
26   modificationTime: new Date(2014, 2, 26, 8, 37, 13)
27 });
28
29 /**
30  * @type {Object}
31  * @const
32  */
33 var TESTING_TIRAMISU_FILE = Object.freeze({
34   isDirectory: false,
35   name: 'tiramisu.txt',
36   size: 1986,
37   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
38 });
39
40 /**
41  * Returns entries in the requested directory.
42  *
43  * @param {ReadDirectoryRequestedOptions} options Options.
44  * @param {function(Array.<Object>, boolean)} onSuccess Success callback with
45  *     a list of entries. May be called multiple times.
46  * @param {function(string)} onError Error callback with an error code.
47  */
48 function onReadDirectoryRequested(options, onSuccess, onError) {
49   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
50     onError('SECURITY');  // enum ProviderError.
51     return;
52   }
53
54   if (options.directoryPath != '/' + TESTING_HELLO_DIR.name) {
55     onError('NOT_FOUND');  // enum ProviderError.
56     return;
57   }
58
59   onSuccess([TESTING_TIRAMISU_FILE], true /* hasMore */);
60   onSuccess([TESTING_CANDIES_DIR], false /* hasMore */);
61 }
62
63 /**
64  * Sets up the tests. Called once per all test cases. In case of a failure,
65  * the callback is not called.
66  *
67  * @param {function()} callback Success callback.
68  */
69 function setUp(callback) {
70   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
71       test_util.onGetMetadataRequestedDefault);
72
73   test_util.defaultMetadata['/' + TESTING_HELLO_DIR.name] =
74       TESTING_HELLO_DIR;
75   test_util.defaultMetadata['/' + TESTING_HELLO_DIR.name + '/' +
76         TESTING_TIRAMISU_FILE.name] = TESTING_TIRAMISU_FILE;
77   test_util.defaultMetadata['/' + TESTING_HELLO_DIR.name + '/' +
78       TESTING_CANDIES_DIR.name] = TESTING_CANDIES_DIR;
79
80   chrome.fileSystemProvider.onReadDirectoryRequested.addListener(
81       onReadDirectoryRequested);
82
83   test_util.mountFileSystem(callback);
84 }
85
86 /**
87  * Runs all of the test cases, one by one.
88  */
89 function runTests() {
90   chrome.test.runTests([
91     // Read contents of the /hello directory. This directory exists, so it
92     // should succeed.
93     function readEntriesSuccess() {
94       var onTestSuccess = chrome.test.callbackPass();
95       test_util.fileSystem.root.getDirectory(
96           'hello',
97           {create: false},
98           function(dirEntry) {
99             var dirReader = dirEntry.createReader();
100             var entries = [];
101             var readEntriesNext = function() {
102               dirReader.readEntries(function(inEntries) {
103                 Array.prototype.push.apply(entries, inEntries);
104                 if (!inEntries.length) {
105                   // No more entries, so verify.
106                   chrome.test.assertEq(2, entries.length);
107                   chrome.test.assertTrue(entries[0].isFile);
108                   chrome.test.assertEq('tiramisu.txt', entries[0].name);
109                   chrome.test.assertEq(
110                       '/hello/tiramisu.txt', entries[0].fullPath);
111                   chrome.test.assertTrue(entries[1].isDirectory);
112                   chrome.test.assertEq('candies', entries[1].name);
113                   chrome.test.assertEq('/hello/candies', entries[1].fullPath);
114                   onTestSuccess();
115                 } else {
116                   readEntriesNext();
117                 }
118               }, function(error) {
119                 chrome.test.fail();
120               });
121             };
122             readEntriesNext();
123           },
124           function(error) {
125             chrome.test.fail();
126           });
127     },
128     // Read contents of a directory which does not exist, what should return an
129     // error.
130     function readEntriesError() {
131       var onTestSuccess = chrome.test.callbackPass();
132       test_util.fileSystem.root.getDirectory(
133           'cranberries',
134           {create: false},
135           function(dirEntry) {
136             chrome.test.fail();
137           },
138           function(error) {
139             chrome.test.assertEq('NotFoundError', error.name);
140             onTestSuccess();
141           });
142     }
143   ]);
144 }
145
146 // Setup and run all of the test cases.
147 setUp(runTests);