summaryrefslogtreecommitdiffstats
path: root/toolkit/components/filewatcher/tests/xpcshell/test_watch_multi_paths.js
blob: c55b262f197a9e376f96843ccac4cc0092c23182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

function run_test() {
  // Set up profile. We will use profile path create some test files
  do_get_profile();

  // Start executing the tests
  run_next_test();
}

/**
 * Tests the watcher by watching several resources.
 * This test creates the specified number of directory inside the profile
 * directory, adds each one of them to the watched list the creates
 * a file in them in order to trigger the notification.
 * The test keeps track of the number of times the changes callback is
 * called in order to verify the success of the test.
 */
add_task(function* test_watch_multi_paths() {

  // The number of resources to watch. We expect changes for
  // creating a file within each directory.
  let resourcesToWatch = 5;
  let watchedDir = OS.Constants.Path.profileDir;

  // The directories to be watched will be created with.
  let tempDirNameBase = "FileWatcher_Test_";
  let tempFileName = "test.tmp";

  // Instantiate the native watcher.
  let watcher = makeWatcher();

  // This will be used to keep track of the number of changes within the watched
  // resources.
  let detectedChanges = 0;
  let watchedResources = 0;
  let unwatchedResources = 0;

  let deferredChanges = Promise.defer();
  let deferredSuccesses = Promise.defer();
  let deferredShutdown = Promise.defer();

  // Define the change callback function.
  let changeCallback = function(changed) {
      do_print(changed + " has changed.");

      detectedChanges += 1;

      // Resolve the promise if we get all the expected changes.
      if (detectedChanges === resourcesToWatch) {
        deferredChanges.resolve();
      }
    };

  // Define the watch success callback function.
  let watchSuccessCallback = function(resourcePath) {
      do_print(resourcePath + " is being watched.");

      watchedResources += 1;

      // Resolve the promise when all the resources are being
      // watched.
      if (watchedResources === resourcesToWatch) {
        deferredSuccesses.resolve();
      }
    };

  // Define the watch success callback function.
  let unwatchSuccessCallback = function(resourcePath) {
      do_print(resourcePath + " is being un-watched.");

      unwatchedResources += 1;

      // Resolve the promise when all the resources are being
      // watched.
      if (unwatchedResources === resourcesToWatch) {
        deferredShutdown.resolve();
      }
    };

  // Create the directories and add them to the watched resources list.
  for (let i = 0; i < resourcesToWatch; i++) {
    let tmpSubDirPath = OS.Path.join(watchedDir, tempDirNameBase + i);
    do_print("Creating the " + tmpSubDirPath + " directory.");
    yield OS.File.makeDir(tmpSubDirPath);
    watcher.addPath(tmpSubDirPath, changeCallback, deferredChanges.reject, watchSuccessCallback);
  }

  // Wait until the watcher informs us that all the desired resources
  // are being watched.
  yield deferredSuccesses.promise;

  // Create a file within each watched directory.
  for (let i = 0; i < resourcesToWatch; i++) {
    let tmpFilePath = OS.Path.join(watchedDir, tempDirNameBase + i, tempFileName);
    yield OS.File.writeAtomic(tmpFilePath, "test content");
  }

  // Wait until the watcher informs us that all the files were created.
  yield deferredChanges.promise;

  // Remove the directories we have created.
  for (let i = 0; i < resourcesToWatch; i++) {
    let tmpSubDirPath = OS.Path.join(watchedDir, tempDirNameBase + i);
    watcher.removePath(tmpSubDirPath, changeCallback, deferredChanges.reject, unwatchSuccessCallback);
  }

  // Wait until the watcher un-watches the resources.
  yield deferredShutdown.promise;
});