From 1bc49354637137bd35701b0086c48ecb5906ec4a Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 9 Apr 2015 01:23:30 +0200 Subject: [PATCH] Fixed bug in FolderObserver, improved tests (ref #337). --- .../test/util/FolderObserverTest.java | 42 ++++++++++++++----- .../syncthingandroid/util/FolderObserver.java | 1 + 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java index 4af77ca4..8c9de1c9 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/FolderObserverTest.java @@ -2,7 +2,6 @@ package com.nutomic.syncthingandroid.test.util; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.MediumTest; -import android.test.suitebuilder.annotation.SmallTest; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.test.MockContext; @@ -55,40 +54,63 @@ public class FolderObserverTest extends AndroidTestCase mCurrentTest = "testRecursion"; File subFolder = new File(mTestFolder, "subfolder"); subFolder.mkdir(); - FolderObserver ro = new FolderObserver(this, createFolder(mCurrentTest)); + FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest)); File testFile = new File(subFolder, "test"); + mLatch = new CountDownLatch(1); testFile.createNewFile(); mLatch.await(1, TimeUnit.SECONDS); - ro.stopWatching(); + + fo.stopWatching(); } @MediumTest - public void testRemoveDirectory() throws IOException { - mCurrentTest = "testRemoveDirectory"; + public void testRemoveFile() throws IOException, InterruptedException { + mCurrentTest = "testRemoveFile"; + File test = new File(mTestFolder, "test"); + test.createNewFile(); + FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest)); + + mLatch = new CountDownLatch(1); + test.delete(); + mLatch.await(1, TimeUnit.SECONDS); + assertEquals(0, mLatch.getCount()); + + fo.stopWatching(); + } + + @MediumTest + public void testMoveDirectoryOut() throws IOException, InterruptedException { + mCurrentTest = "testMoveDirectory"; File subFolder = new File(mTestFolder, "subfolder"); subFolder.mkdir(); - FolderObserver ro = new FolderObserver(this, createFolder(mCurrentTest)); + FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest)); + File movedSubFolder = new File(getContext().getFilesDir(), subFolder.getName()); subFolder.renameTo(movedSubFolder); File testFile = new File(movedSubFolder, "should-not-notifiy"); mLatch = new CountDownLatch(1); testFile.createNewFile(); - ro.stopWatching(); - Util.deleteRecursive(subFolder); + mLatch.await(1, TimeUnit.SECONDS); + assertEquals(1, mLatch.getCount()); + + fo.stopWatching(); } @MediumTest public void testAddDirectory() throws IOException, InterruptedException { mCurrentTest = "testAddDirectory"; File subFolder = new File(mTestFolder, "subfolder"); - FolderObserver ro = new FolderObserver(this, createFolder(mCurrentTest)); subFolder.mkdir(); File testFile = new File(subFolder, "test"); + FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest)); + mLatch = new CountDownLatch(1); testFile.createNewFile(); mLatch.await(1, TimeUnit.SECONDS); - ro.stopWatching(); + assertEquals(0, mLatch.getCount()); + + fo.stopWatching(); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java b/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java index 56701f20..6e9b625b 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/FolderObserver.java @@ -107,6 +107,7 @@ public class FolderObserver extends FileObserver { break; } } + mListener.onFolderFileChange(mFolder.ID, fullPath.getPath()); break; case MOVED_TO: // fall through