Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / notify / 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_DIRECTORY = Object.freeze({
12   isDirectory: true,
13   name: 'nakameguro',
14   size: 0,
15   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
16 });
17
18 /**
19  * @type {string}
20  * @const
21  */
22 var TESTING_TAG = "hello-puppy";
23
24 /**
25  * @type {string}
26  * @const
27  */
28 var TESTING_ANOTHER_TAG = "hello-giraffe";
29
30 /**
31  * List of directory changed events received from the chrome.fileManagerPrivate
32  * API.
33  * @type {Array.<Object>}
34  */
35 var directoryChangedEvents = [];
36
37 /**
38  * Callback to be called when a directory changed event arrives.
39  * @type {function()|null}
40  */
41 var directoryChangedCallback = null;
42
43 /**
44  * Handles an event dispatched from the chrome.fileManagerPrivate API.
45  * @param {Object} event Event with the directory change details.
46  */
47 function onDirectoryChanged(event) {
48   directoryChangedEvents.push(event);
49   chrome.test.assertTrue(!!directoryChangedCallback);
50   directoryChangedCallback();
51 }
52
53 /**
54  * Sets up the tests. Called once per all test cases. In case of a failure,
55  * the callback is not called.
56  *
57  * @param {function()} callback Success callback.
58  */
59 function setUp(callback) {
60   chrome.fileManagerPrivate.onDirectoryChanged.addListener(onDirectoryChanged);
61
62   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
63       test_util.onGetMetadataRequestedDefault);
64   chrome.fileSystemProvider.onAddWatcherRequested.addListener(
65       test_util.onAddWatcherRequested);
66   chrome.fileSystemProvider.onRemoveWatcherRequested.addListener(
67       test_util.onRemoveWatcherRequested);
68
69   test_util.defaultMetadata['/' + TESTING_DIRECTORY.name] = TESTING_DIRECTORY;
70
71   test_util.mountFileSystem(callback, {supportsNotifyTag: true});
72 }
73
74 /**
75  * Runs all of the test cases, one by one.
76  */
77 function runTests() {
78   chrome.test.runTests([
79
80     // Add a watcher, and then notifies that the entry has changed.
81     function notifyChanged() {
82       test_util.fileSystem.root.getDirectory(
83           TESTING_DIRECTORY.name,
84           {create: false},
85           chrome.test.callbackPass(function(fileEntry) {
86             chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
87             chrome.fileManagerPrivate.addFileWatch(
88                 fileEntry.toURL(),
89                 chrome.test.callbackPass(function(result) {
90                   chrome.test.assertTrue(result);
91                   // Verify closure called when an even arrives.
92                   directoryChangedCallback = chrome.test.callbackPass(
93                       function() {
94                         chrome.test.assertEq(1, directoryChangedEvents.length);
95                         chrome.test.assertEq(
96                             'changed', directoryChangedEvents[0].eventType);
97                         chrome.test.assertEq(
98                             fileEntry.toURL(),
99                             directoryChangedEvents[0].entry.toURL());
100                         // Confirm that the tag is updated.
101                         chrome.fileSystemProvider.getAll(
102                             chrome.test.callbackPass(function(fileSystems) {
103                               chrome.test.assertEq(1, fileSystems.length);
104                               chrome.test.assertEq(
105                                   1, fileSystems[0].watchers.length);
106                               var watcher = fileSystems[0].watchers[0];
107                               chrome.test.assertEq(
108                                   TESTING_TAG, watcher.lastTag);
109                             }));
110                       });
111                   // TODO(mtomasz): Add more advanced tests, eg. for the details
112                   // of changes.
113                   chrome.fileSystemProvider.notify({
114                     fileSystemId: test_util.FILE_SYSTEM_ID,
115                     observedPath: fileEntry.fullPath,
116                     recursive: false,
117                     changeType: 'CHANGED',
118                     tag: TESTING_TAG
119                   }, chrome.test.callbackPass());
120                 }));
121           }), function(error) {
122             chrome.test.fail(error.name);
123           });
124     },
125
126     // Passing an empty tag (or no tag) is invalid when the file system supports
127     // the tag.
128     function notifyEmptyTag() {
129       test_util.fileSystem.root.getDirectory(
130           TESTING_DIRECTORY.name,
131           {create: false},
132           chrome.test.callbackPass(function(fileEntry) {
133             chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
134             directoryChangedCallback = function() {
135               chrome.test.fail();
136             };
137             // TODO(mtomasz): NOT_FOUND error should be returned instead.
138             chrome.fileSystemProvider.notify({
139               fileSystemId: test_util.FILE_SYSTEM_ID,
140               observedPath: fileEntry.fullPath,
141               recursive: false,
142               changeType: 'CHANGED',
143             }, chrome.test.callbackFail('SECURITY'));
144           }),
145           function(error) {
146             chrome.test.fail(error.name);
147           });
148     },
149
150     // Notifying for the watched entry but in a wrong mode (recursive, while the
151     // watcher is not recursive) should fail.
152     function notifyWatchedPathButDifferentModeTag() {
153       test_util.fileSystem.root.getDirectory(
154           TESTING_DIRECTORY.name,
155           {create: false},
156           chrome.test.callbackPass(function(fileEntry) {
157             chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
158             directoryChangedCallback = function() {
159               chrome.test.fail();
160             };
161             // TODO(mtomasz): NOT_FOUND error should be returned instead.
162             chrome.fileSystemProvider.notify({
163               fileSystemId: test_util.FILE_SYSTEM_ID,
164               observedPath: fileEntry.fullPath,
165               recursive: true,
166               changeType: 'CHANGED',
167               tag: TESTING_ANOTHER_TAG,
168             }, chrome.test.callbackFail('SECURITY'));
169           }));
170     },
171
172     // Notify about the watched entry being removed. That should result in the
173     // watcher being removed.
174     function notifyDeleted() {
175       test_util.fileSystem.root.getDirectory(
176           TESTING_DIRECTORY.name,
177           {create: false},
178           chrome.test.callbackPass(function(fileEntry) {
179             chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
180             // Verify closure called when an even arrives.
181             directoryChangedCallback = chrome.test.callbackPass(
182                 function() {
183                   chrome.test.assertEq(2, directoryChangedEvents.length);
184                   chrome.test.assertEq(
185                       'changed', directoryChangedEvents[1].eventType);
186                   chrome.test.assertEq(fileEntry.toURL(),
187                                        directoryChangedEvents[1].entry.toURL());
188                   // Confirm that the watcher is removed.
189                   chrome.fileSystemProvider.getAll(
190                       chrome.test.callbackPass(function(fileSystems) {
191                         chrome.test.assertEq(1, fileSystems.length);
192                         chrome.test.assertEq(
193                             0, fileSystems[0].watchers.length);
194                       }));
195                 });
196             // TODO(mtomasz): Add more advanced tests, eg. for the details
197             // of changes.
198             chrome.fileSystemProvider.notify({
199               fileSystemId: test_util.FILE_SYSTEM_ID,
200               observedPath: fileEntry.fullPath,
201               recursive: false,
202               changeType: 'DELETED',
203               tag: TESTING_ANOTHER_TAG
204             }, chrome.test.callbackPass());
205           }));
206     },
207
208     // Notify about an entry which is not watched. That should result in an
209     // error.
210     function notifyNotWatched() {
211       test_util.fileSystem.root.getDirectory(
212           TESTING_DIRECTORY.name,
213           {create: false},
214           chrome.test.callbackPass(function(fileEntry) {
215             chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
216             directoryChangedCallback = function() {
217               chrome.test.fail();
218             };
219             // TODO(mtomasz): NOT_FOUND error should be returned instead.
220             chrome.fileSystemProvider.notify({
221               fileSystemId: test_util.FILE_SYSTEM_ID,
222               observedPath: fileEntry.fullPath,
223               recursive: false,
224               changeType: 'CHANGED',
225               tag: TESTING_ANOTHER_TAG
226             }, chrome.test.callbackFail('SECURITY'));
227           }));
228     }
229   ]);
230 }
231
232 // Setup and run all of the test cases.
233 setUp(runTests);