ListDirectoryOperation { public void compute(File rootDirectory) { if (rootDirectory.canRead()) { cancelPreviousOperation(); updateDataSet(rootDirectory); Observable<File> observable = new ListDirectoryObservable().create(rootDirectory); DisposableObserver<File> observer = new ListDirectoryObserver(dataSet, bus); disposables.add(observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(observer)); } else { bus.post(new OperationFailedEvent()); } } ListDirectoryOperation(DirectoryTree dataSet, EventBus bus); void compute(File rootDirectory); void cancelPreviousOperation();  }
@Test public void shouldCreateNewDisposableWhenRootDirectoryCouldBeRead() throws Exception { File stubRootFile = mock(File.class); when(stubRootFile.canRead()).thenReturn(true); sut.compute(stubRootFile); Assert.assertEquals(1, sut.disposables.size()); }
@Test public void shouldNotCreateAddNewDisposableWhenRootDirectoryCouldNotBeRead() throws Exception { File stubRootFile = mock(File.class); when(stubRootFile.canRead()).thenReturn(false); sut.compute(stubRootFile); Assert.assertEquals(0, sut.disposables.size()); }
@Test public void shouldUpdateDirectoryTreeRootAndParentIfExists() throws Exception { File stubCurrentDirectory = mock(File.class); File parentDirectory = mock(File.class); when(parentDirectory.getPath()).thenReturn("/some/parent/path"); CustomFile expectedParentDirectory = new CustomFile(parentDirectory.getPath()); when(stubCurrentDirectory.canRead()).thenReturn(true); when(stubCurrentDirectory.getParentFile()).thenReturn(parentDirectory); sut.compute(stubCurrentDirectory); assertThat(directoryTree.getRoot(), is(stubCurrentDirectory)); assertThat(directoryTree.getParentDirectory(), Is.<File>is(expectedParentDirectory)); }
@Test public void shouldOnlyUpdateDirectoryTreeRootAndNotParentIfDoesNotExist() throws Exception { File stubCurrentDirectory = mock(File.class); when(stubCurrentDirectory.canRead()).thenReturn(true); when(stubCurrentDirectory.getParentFile()).thenReturn(null); sut.compute(stubCurrentDirectory); assertThat(directoryTree.getRoot(), is(stubCurrentDirectory)); assertNull(directoryTree.getParentDirectory()); }