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.
11 var TESTING_DIRECTORY = Object.freeze({
15 modificationTime: new Date(2014, 4, 28, 10, 39, 15)
22 var TESTING_TAG = "hello-puppy";
28 var TESTING_ANOTHER_TAG = "hello-giraffe";
31 * List of directory changed events received from the chrome.fileManagerPrivate
33 * @type {Array.<Object>}
35 var directoryChangedEvents = [];
38 * Callback to be called when a directory changed event arrives.
39 * @type {function()|null}
41 var directoryChangedCallback = null;
44 * Handles an event dispatched from the chrome.fileManagerPrivate API.
45 * @param {Object} event Event with the directory change details.
47 function onDirectoryChanged(event) {
48 directoryChangedEvents.push(event);
49 chrome.test.assertTrue(!!directoryChangedCallback);
50 directoryChangedCallback();
54 * Sets up the tests. Called once per all test cases. In case of a failure,
55 * the callback is not called.
57 * @param {function()} callback Success callback.
59 function setUp(callback) {
60 chrome.fileManagerPrivate.onDirectoryChanged.addListener(onDirectoryChanged);
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);
69 test_util.defaultMetadata['/' + TESTING_DIRECTORY.name] = TESTING_DIRECTORY;
71 test_util.mountFileSystem(callback, {supportsNotifyTag: true});
75 * Runs all of the test cases, one by one.
78 chrome.test.runTests([
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,
85 chrome.test.callbackPass(function(fileEntry) {
86 chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
87 chrome.fileManagerPrivate.addFileWatch(
89 chrome.test.callbackPass(function(result) {
90 chrome.test.assertTrue(result);
91 // Verify closure called when an even arrives.
92 directoryChangedCallback = chrome.test.callbackPass(
94 chrome.test.assertEq(1, directoryChangedEvents.length);
96 'changed', directoryChangedEvents[0].eventType);
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);
111 // TODO(mtomasz): Add more advanced tests, eg. for the details
113 chrome.fileSystemProvider.notify({
114 fileSystemId: test_util.FILE_SYSTEM_ID,
115 observedPath: fileEntry.fullPath,
117 changeType: 'CHANGED',
119 }, chrome.test.callbackPass());
121 }), function(error) {
122 chrome.test.fail(error.name);
126 // Passing an empty tag (or no tag) is invalid when the file system supports
128 function notifyEmptyTag() {
129 test_util.fileSystem.root.getDirectory(
130 TESTING_DIRECTORY.name,
132 chrome.test.callbackPass(function(fileEntry) {
133 chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
134 directoryChangedCallback = function() {
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,
142 changeType: 'CHANGED',
143 }, chrome.test.callbackFail('SECURITY'));
146 chrome.test.fail(error.name);
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,
156 chrome.test.callbackPass(function(fileEntry) {
157 chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
158 directoryChangedCallback = function() {
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,
166 changeType: 'CHANGED',
167 tag: TESTING_ANOTHER_TAG,
168 }, chrome.test.callbackFail('SECURITY'));
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,
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(
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);
196 // TODO(mtomasz): Add more advanced tests, eg. for the details
198 chrome.fileSystemProvider.notify({
199 fileSystemId: test_util.FILE_SYSTEM_ID,
200 observedPath: fileEntry.fullPath,
202 changeType: 'DELETED',
203 tag: TESTING_ANOTHER_TAG
204 }, chrome.test.callbackPass());
208 // Notify about an entry which is not watched. That should result in an
210 function notifyNotWatched() {
211 test_util.fileSystem.root.getDirectory(
212 TESTING_DIRECTORY.name,
214 chrome.test.callbackPass(function(fileEntry) {
215 chrome.test.assertEq(TESTING_DIRECTORY.name, fileEntry.name);
216 directoryChangedCallback = function() {
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,
224 changeType: 'CHANGED',
225 tag: TESTING_ANOTHER_TAG
226 }, chrome.test.callbackFail('SECURITY'));
232 // Setup and run all of the test cases.