public void scan() {
    List<VirtualFile> workQueue = myWorkQueue;
    myWorkQueue = new ArrayList<VirtualFile>();
    boolean hasEventsToFire = myFinishRunnable != null || !myEvents.isEmpty();

    if (!workQueue.isEmpty()) {
      LocalFileSystemImpl fs = (LocalFileSystemImpl) LocalFileSystem.getInstance();
      fs.markSuspiciousFilesDirty(workQueue);
      FileWatcher watcher = fs.getFileWatcher();

      for (VirtualFile file : workQueue) {
        NewVirtualFile nvf = (NewVirtualFile) file;
        if (!myIsRecursive
            && (!myIsAsync
                || !watcher.isWatched(
                    nvf))) { // We're unable to definitely refresh synchronously by means of file
                             // watcher.
          nvf.markDirty();
        }

        RefreshWorker worker = new RefreshWorker(file, myIsRecursive);
        long t = LOG.isDebugEnabled() ? System.currentTimeMillis() : 0;
        worker.scan();
        List<VFileEvent> events = worker.getEvents();
        if (t != 0) {
          t = System.currentTimeMillis() - t;
          LOG.debug(file + " scanned in " + t + " ms, events: " + events);
        }
        myEvents.addAll(events);
        if (!events.isEmpty()) hasEventsToFire = true;
      }
    }
    iHaveEventsToFire = hasEventsToFire;
  }
  private void checkProjectRoots() {
    LocalFileSystem fs = LocalFileSystem.getInstance();
    if (!(fs instanceof LocalFileSystemImpl)) return;
    FileWatcher watcher = ((LocalFileSystemImpl) fs).getFileWatcher();
    if (!watcher.isOperational()) return;
    Collection<String> manualWatchRoots = watcher.getManualWatchRoots();
    if (manualWatchRoots.isEmpty()) return;
    VirtualFile[] roots = ProjectRootManager.getInstance(myProject).getContentRoots();
    if (roots.length == 0) return;

    List<String> nonWatched = new SmartList<String>();
    for (VirtualFile root : roots) {
      if (!(root.getFileSystem() instanceof LocalFileSystem)) continue;
      String rootPath = root.getPath();
      for (String manualWatchRoot : manualWatchRoots) {
        if (FileUtil.isAncestor(manualWatchRoot, rootPath, false)) {
          nonWatched.add(rootPath);
        }
      }
    }

    if (!nonWatched.isEmpty()) {
      String message = ApplicationBundle.message("watcher.non.watchable.project");
      watcher.notifyOnFailure(message, null);
      LOG.info("unwatched roots: " + nonWatched);
      LOG.info("manual watches: " + manualWatchRoots);
    }
  }
  @Override
  protected void setUp() throws Exception {
    LOG.debug("================== setting up " + getName() + " ==================");

    super.setUp();

    myFileSystem = LocalFileSystem.getInstance();
    assertNotNull(myFileSystem);

    myWatcher = ((LocalFileSystemImpl) myFileSystem).getFileWatcher();
    assertNotNull(myWatcher);
    assertFalse(myWatcher.isOperational());
    myWatcher.startup(myNotifier);
    assertTrue(myWatcher.isOperational());

    myAlarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, getProject());
    myTimeout = NATIVE_PROCESS_DELAY;

    myConnection = ApplicationManager.getApplication().getMessageBus().connect();
    myConnection.subscribe(
        VirtualFileManager.VFS_CHANGES,
        new BulkFileListener.Adapter() {
          @Override
          public void after(@NotNull List<? extends VFileEvent> events) {
            synchronized (myEvents) {
              myEvents.addAll(events);
            }
          }
        });

    ((LocalFileSystemImpl) myFileSystem).cleanupForNextTest();

    LOG = FileWatcher.getLog();
    LOG.debug("================== setting up " + getName() + " ==================");
  }
  private void waitForFileWatcher(@NotNull Project project) {
    LocalFileSystem fs = LocalFileSystem.getInstance();
    if (!(fs instanceof LocalFileSystemImpl)) return;

    final FileWatcher watcher = ((LocalFileSystemImpl) fs).getFileWatcher();
    if (!watcher.isOperational() || !watcher.isSettingRoots()) return;

    LOG.info("FW/roots waiting started");
    Task.Modal task =
        new Task.Modal(project, ProjectBundle.message("project.load.progress"), true) {
          @Override
          public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            indicator.setText(ProjectBundle.message("project.load.waiting.watcher"));
            if (indicator instanceof ProgressWindow) {
              ((ProgressWindow) indicator).setCancelButtonText(CommonBundle.message("button.skip"));
            }
            while (watcher.isSettingRoots() && !indicator.isCanceled()) {
              TimeoutUtil.sleep(10);
            }
            LOG.info("FW/roots waiting finished");
          }
        };
    myProgressManager.run(task);
  }
  @Override
  protected void tearDown() throws Exception {
    LOG.debug("================== tearing down " + getName() + " ==================");

    try {
      myConnection.disconnect();
      myWatcher.shutdown();
      assertFalse(myWatcher.isOperational());
    } finally {
      myFileSystem = null;
      myWatcher = null;
      super.tearDown();
    }

    LOG.debug("================== tearing down " + getName() + " ==================");
  }
 @NotNull
 private LocalFileSystem.WatchRequest watch(final File watchFile, final boolean recursive) {
   final Ref<LocalFileSystem.WatchRequest> request = Ref.create();
   getEvents(
       "events to add watch " + watchFile,
       new Runnable() {
         @Override
         public void run() {
           request.set(myFileSystem.addRootToWatch(watchFile.getAbsolutePath(), recursive));
         }
       });
   assertFalse(request.isNull());
   assertFalse(myWatcher.isSettingRoots());
   return request.get();
 }