Fixed bug in FolderObserver, improved tests (ref #337).

This commit is contained in:
Felix Ableitner 2015-04-09 01:23:30 +02:00
parent 8c75a8913d
commit 1bc4935463
2 changed files with 33 additions and 10 deletions

View File

@ -2,7 +2,6 @@ package com.nutomic.syncthingandroid.test.util;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.test.MockContext; import com.nutomic.syncthingandroid.test.MockContext;
@ -55,40 +54,63 @@ public class FolderObserverTest extends AndroidTestCase
mCurrentTest = "testRecursion"; mCurrentTest = "testRecursion";
File subFolder = new File(mTestFolder, "subfolder"); File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir(); subFolder.mkdir();
FolderObserver ro = new FolderObserver(this, createFolder(mCurrentTest)); FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest));
File testFile = new File(subFolder, "test"); File testFile = new File(subFolder, "test");
mLatch = new CountDownLatch(1); mLatch = new CountDownLatch(1);
testFile.createNewFile(); testFile.createNewFile();
mLatch.await(1, TimeUnit.SECONDS); mLatch.await(1, TimeUnit.SECONDS);
ro.stopWatching();
fo.stopWatching();
} }
@MediumTest @MediumTest
public void testRemoveDirectory() throws IOException { public void testRemoveFile() throws IOException, InterruptedException {
mCurrentTest = "testRemoveDirectory"; 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"); File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir(); subFolder.mkdir();
FolderObserver ro = new FolderObserver(this, createFolder(mCurrentTest)); FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest));
File movedSubFolder = new File(getContext().getFilesDir(), subFolder.getName()); File movedSubFolder = new File(getContext().getFilesDir(), subFolder.getName());
subFolder.renameTo(movedSubFolder); subFolder.renameTo(movedSubFolder);
File testFile = new File(movedSubFolder, "should-not-notifiy"); File testFile = new File(movedSubFolder, "should-not-notifiy");
mLatch = new CountDownLatch(1); mLatch = new CountDownLatch(1);
testFile.createNewFile(); testFile.createNewFile();
ro.stopWatching(); mLatch.await(1, TimeUnit.SECONDS);
Util.deleteRecursive(subFolder); assertEquals(1, mLatch.getCount());
fo.stopWatching();
} }
@MediumTest @MediumTest
public void testAddDirectory() throws IOException, InterruptedException { public void testAddDirectory() throws IOException, InterruptedException {
mCurrentTest = "testAddDirectory"; mCurrentTest = "testAddDirectory";
File subFolder = new File(mTestFolder, "subfolder"); File subFolder = new File(mTestFolder, "subfolder");
FolderObserver ro = new FolderObserver(this, createFolder(mCurrentTest));
subFolder.mkdir(); subFolder.mkdir();
File testFile = new File(subFolder, "test"); File testFile = new File(subFolder, "test");
FolderObserver fo = new FolderObserver(this, createFolder(mCurrentTest));
mLatch = new CountDownLatch(1); mLatch = new CountDownLatch(1);
testFile.createNewFile(); testFile.createNewFile();
mLatch.await(1, TimeUnit.SECONDS); mLatch.await(1, TimeUnit.SECONDS);
ro.stopWatching(); assertEquals(0, mLatch.getCount());
fo.stopWatching();
} }
} }

View File

@ -107,6 +107,7 @@ public class FolderObserver extends FileObserver {
break; break;
} }
} }
mListener.onFolderFileChange(mFolder.ID, fullPath.getPath());
break; break;
case MOVED_TO: case MOVED_TO:
// fall through // fall through