Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#287: the view model's publish() method no longer needs an UI thread,… #288

Merged
merged 3 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions mvvmfx/src/main/java/de/saxsys/mvvmfx/ViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
public interface ViewModel {

/**
* Publishes a notification to the subscribers of the messageName. This notification will be send to the
* UI-Thread (if the UI-toolkit was bootstrapped). If no UI-Toolkit is available the notification will be directly
* published. This is typically the case in unit tests.
* Publishes a notification to the subscribers of the messageName.
* <p>
*
* This notification mechanism uses the {@link NotificationCenter} internally with the difference that messages send
Expand All @@ -64,22 +62,7 @@ public interface ViewModel {
* to be send
*/
default void publish(String messageName, Object... payload) {
if (Platform.isFxApplicationThread()) {
MvvmFX.getNotificationCenter().publish(this, messageName, payload);
} else {
try {
Platform.runLater(() -> MvvmFX.getNotificationCenter().publish(this, messageName, payload));
} catch(IllegalStateException e) {

// If the toolkit isn't initialized yet we will publish the notification directly.
// In most cases this means that we are in a unit test and not JavaFX application is running.
if(e.getMessage().equals("Toolkit not initialized")) {
MvvmFX.getNotificationCenter().publish(this, messageName, payload);
} else {
throw e;
}
}
}
MvvmFX.getNotificationCenter().publish(this, messageName, payload);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
******************************************************************************/
package de.saxsys.mvvmfx.utils.notifications;

import de.saxsys.mvvmfx.ViewModel;
import javafx.application.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import de.saxsys.mvvmfx.ViewModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Default implementation of {@link NotificationCenter}.
*
Expand Down Expand Up @@ -61,12 +61,35 @@ public void unsubscribe(NotificationObserver observer) {
public void publish(String messageName, Object... payload) {
publish(messageName, payload, globalObservers);
}


/**
* This notification will be send to the UI-Thread (if the UI-toolkit was bootstrapped).
* If no UI-Toolkit is available the notification will be directly published. This is typically the case in unit tests.
*
* @param viewModel the ViewModel
* @param messageName the message to sent
* @param payload additional arguments to the message
*/
@Override
public void publish(ViewModel viewModel, String messageName, Object[] payload) {
ObserverMap observerMap = viewModelObservers.get(viewModel);
if (observerMap != null) {
publish(messageName, payload, observerMap);
if (Platform.isFxApplicationThread()) {
publish(messageName, payload, observerMap);
} else {
try {
Platform.runLater(() -> publish(messageName, payload, observerMap));
} catch(IllegalStateException e) {

// If the toolkit isn't initialized yet we will publish the notification directly.
// In most cases this means that we are in a unit test and not JavaFX application is running.
if(e.getMessage().equals("Toolkit not initialized")) {
publish(messageName, payload, observerMap);
} else {
throw e;
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
* This class is used to get instances of the {@link NotificationCenter} interface.
*/
public class NotificationCenterFactory {

private static NotificationCenter singleton = new DefaultNotificationCenter();


private static final NotificationCenter defaultNotificationCenter = new DefaultNotificationCenter();
private static NotificationCenter currentNotificationCenter = defaultNotificationCenter;

public static NotificationCenter getNotificationCenter() {
return singleton;
return currentNotificationCenter;
}


public static void setNotificationCenter(final NotificationCenter notificationCenter) {
currentNotificationCenter = notificationCenter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@

package de.saxsys.mvvmfx.utils.notifications;

import de.saxsys.mvvmfx.ViewModel;
import de.saxsys.mvvmfx.testingutils.jfxrunner.JfxRunner;
import javafx.application.Platform;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(JfxRunner.class)
public class DefaultNotificationCenterTest {

private static final String TEST_NOTIFICATION = "test_notification";
Expand All @@ -38,7 +49,7 @@ public void init() {
observer1 = Mockito.mock(DummyNotificationObserver.class);
observer2 = Mockito.mock(DummyNotificationObserver.class);
observer3 = Mockito.mock(DummyNotificationObserver.class);
defaultCenter = Mockito.spy(new DefaultNotificationCenter());
defaultCenter = new DefaultNotificationCenter();
}

@Test
Expand Down Expand Up @@ -108,6 +119,34 @@ public void unsubscribeObserverThatWasSubscribedMultipleTimes() {
defaultCenter.publish(TEST_NOTIFICATION);
Mockito.verify(observer1, Mockito.never()).receivedNotification(TEST_NOTIFICATION);
}

@Test
public void observerForViewModelIsCalledFromUiThread() throws InterruptedException, ExecutionException, TimeoutException {
// Check that there is a UI-Thread available. This JUnit-Test isn't running on the UI-Thread but there needs to
// be a UI-Thread available in the background.
CompletableFuture<Void> uiThreadIsAvailable = new CompletableFuture<>();
Platform.runLater(() -> uiThreadIsAvailable.complete(null)); // This would throw an IllegalStateException if no
// UI-Thread is available.
uiThreadIsAvailable.get(1l, TimeUnit.SECONDS);

CompletableFuture<Boolean> future = new CompletableFuture<>();

// The test doesn't run on the FX thread.
assertThat(Platform.isFxApplicationThread()).isFalse();

final ViewModel viewModel = Mockito.mock(ViewModel.class);
defaultCenter.subscribe(viewModel, TEST_NOTIFICATION, (key, payload) -> {
// the notification is executed on the FX thread.
future.complete(Platform.isFxApplicationThread());
});

// view model publish() should be executed in the UI-thread
defaultCenter.publish(viewModel, TEST_NOTIFICATION, new Object[]{});

final Boolean wasCalledOnUiThread = future.get(1l, TimeUnit.SECONDS);

assertThat(wasCalledOnUiThread).isTrue();
}

private class DummyNotificationObserver implements NotificationObserver {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import de.saxsys.mvvmfx.testingutils.jfxrunner.JfxRunner;
import javafx.application.Platform;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
Expand All @@ -15,8 +14,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(JfxRunner.class)
public class ViewModelTest {

Expand All @@ -36,37 +33,7 @@ public void init() {
viewModel = new ViewModel() {
};
}

@Test
public void observerIsCalledFromUiThread() throws InterruptedException, ExecutionException, TimeoutException {
// Check that there is a UI-Thread available. This JUnit-Test isn't running on the UI-Thread but there needs to
// be a UI-Thread available in the background.
CompletableFuture<Void> uiThreadIsAvailable = new CompletableFuture<>();
Platform.runLater(() -> uiThreadIsAvailable.complete(null)); // This would throw an IllegalStateException if no
// UI-Thread is available.
uiThreadIsAvailable.get(1l, TimeUnit.SECONDS);



CompletableFuture<Boolean> future = new CompletableFuture<>();

// The test doesn't run on the FX thread.
assertThat(Platform.isFxApplicationThread()).isFalse();

viewModel.subscribe(TEST_NOTIFICATION, (key, payload) -> {
// the notification is executed on the FX thread.
future.complete(Platform.isFxApplicationThread());
});

viewModel.publish(TEST_NOTIFICATION);


final Boolean wasCalledOnUiThread = future.get(1l, TimeUnit.SECONDS);

assertThat(wasCalledOnUiThread).isTrue();
}



@Test
public void observerFromOutsideDoesNotReceiveNotifications() {
MvvmFX.getNotificationCenter().subscribe(TEST_NOTIFICATION, observer1);
Expand Down