Przeglądaj źródła

Fix test cases for AutoGrow panels

Sam Jaffe 5 lat temu
rodzic
commit
0c9bf2af28

+ 26 - 0
src/test/java/org/leumasjaffe/mock/ListenerInjector.java

@@ -0,0 +1,26 @@
+package org.leumasjaffe.mock;
+
+import java.awt.Component;
+import java.awt.event.FocusEvent;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+
+public abstract class ListenerInjector {
+	private ListenerInjector() {}
+	
+	private static final Supplier<IllegalStateException> noMatch(final String expected) {
+		return () -> new IllegalStateException("No listener whose classname is: " + expected);
+	}
+	
+	private static final <C> Predicate<C> classNameIs(final String name) {
+		return obj -> obj.getClass().getSimpleName().equals(name);
+	}
+
+	public static final <C extends Component> void injectFocusLost(
+			final C context, final String toListenerClass) {
+		Stream.of(context.getFocusListeners()).filter(classNameIs(toListenerClass))
+			.findFirst().orElseThrow(noMatch(toListenerClass))
+			.focusLost(new FocusEvent(context, FocusEvent.FOCUS_LOST));
+	}
+}

+ 9 - 5
src/test/java/org/leumasjaffe/recipe/view/AutoGrowPanelTest.java

@@ -3,6 +3,7 @@ package org.leumasjaffe.recipe.view;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
 import static org.mockito.Mockito.*;
+import static org.leumasjaffe.mock.ListenerInjector.*;
 
 import java.awt.event.FocusListener;
 import java.util.ArrayList;
@@ -151,27 +152,29 @@ class AutoGrowPanelTest extends SwingTestCase {
 	}
 
 	@Test
-	void testEmptyingContentClearsPanel() {
+	void testLosingFocusWithoutContentClearsPanel() {
 		final MockComponent mock = spy(new MockComponent("A"));
 		
 		AutoGrowPanel<MockComponent, MockComponent> panel = create(mock);
 		getTestFrame().add(panel);
 		mock.setText("");
-		
+		injectFocusLost(mock, "ShrinkOnEmpty");
+
 		assertThat(panel.getComponents(), arrayWithSize(1));
 		verify(shared, times(1)).remove(0);
 	}
 
 
 	@Test
-	void testEmptyingContentAdjustsAllPositions() {
+	void testLosingFocusWithoutContentAdjustsAllPositions() {
 		final MockComponent mock = spy(new MockComponent("A"));
 		
 		@SuppressWarnings("unused")
 		AutoGrowPanel<MockComponent, MockComponent> panel = create(mock);
 		
 		mock.setText("");
-		
+		injectFocusLost(mock, "ShrinkOnEmpty");
+	
 		verify(internal.get(0), times(2)).setListPosition(0);
 	}
 
@@ -193,7 +196,7 @@ class AutoGrowPanelTest extends SwingTestCase {
 		
 		AutoGrowPanel<MockComponent, MockComponent> panel = create(mock);
 		getTestFrame().add(panel);
-		mock.getDocument().insertString(0, "B", null);
+		mock.setText("B");
 		
 		assertThat(panel.getComponents(), arrayWithSize(2));
 		verify(shared, never()).remove(anyInt());
@@ -210,6 +213,7 @@ class AutoGrowPanelTest extends SwingTestCase {
 		verify(callback).accept(true);
 
 		internal.get(0).setText("");
+		injectFocusLost(internal.get(0), "ShrinkOnEmpty");
 		verify(callback).accept(false);
 	}
 	

+ 3 - 2
src/test/java/org/leumasjaffe/recipe/view/PhasePanelIT.java

@@ -1,5 +1,6 @@
 package org.leumasjaffe.recipe.view;
 
+import static org.leumasjaffe.mock.ListenerInjector.injectFocusLost;
 import static org.mockito.Mockito.*;
 
 import java.util.ArrayList;
@@ -64,7 +65,7 @@ class PhasePanelIT extends SwingTestCase {
 		final StepPanel oldIngredient =
 				(StepPanel) panel.getPanelSteps().getComponent(0);
 		oldIngredient.getTxtpnInstructions().setText("");
-		waitForSwing();
+		injectFocusLost(oldIngredient.getTxtpnInstructions(), "ShrinkOnEmpty");
 
 		verify(listener, times(1)).updateWasSignalled();
 	}
@@ -75,8 +76,8 @@ class PhasePanelIT extends SwingTestCase {
 		final StepPanel oldIngredient =
 				(StepPanel) panel.getPanelSteps().getComponent(0);
 		oldIngredient.getTxtpnInstructions().setText("");
+		injectFocusLost(oldIngredient.getTxtpnInstructions(), "ShrinkOnEmpty");
 
-		waitForSwing();
 		clearInvocations(listener);
 
 		ObserverDispatch.notifySubscribers(st);

+ 3 - 2
src/test/java/org/leumasjaffe/recipe/view/RecipeCardPanelIT.java

@@ -1,5 +1,6 @@
 package org.leumasjaffe.recipe.view;
 
+import static org.leumasjaffe.mock.ListenerInjector.injectFocusLost;
 import static org.mockito.Mockito.*;
 
 import java.util.ArrayList;
@@ -72,7 +73,7 @@ class RecipeCardPanelIT extends SwingTestCase {
 		final ElementPanel oldElement =
 				(ElementPanel) getPanelIngredients().getComponent(0);
 		oldElement.getTxtName().setText("");
-		waitForSwing();
+		injectFocusLost(oldElement.getTxtName(), "ShrinkOnEmpty");
 
 		verify(listener, times(1)).updateWasSignalled();
 	}
@@ -83,8 +84,8 @@ class RecipeCardPanelIT extends SwingTestCase {
 		final ElementPanel oldElement =
 				(ElementPanel) getPanelIngredients().getComponent(0);
 		oldElement.getTxtName().setText("");
+		injectFocusLost(oldElement.getTxtName(), "ShrinkOnEmpty");
 
-		waitForSwing();
 		clearInvocations(listener);
 
 		ObserverDispatch.notifySubscribers(elem);

+ 3 - 2
src/test/java/org/leumasjaffe/recipe/view/StepPanelIT.java

@@ -1,5 +1,6 @@
 package org.leumasjaffe.recipe.view;
 
+import static org.leumasjaffe.mock.ListenerInjector.injectFocusLost;
 import static org.mockito.Mockito.*;
 
 import java.util.ArrayList;
@@ -72,7 +73,7 @@ class StepPanelIT extends SwingTestCase {
 		final IngredientPanel oldIngredient =
 				(IngredientPanel) getPanelIngredients().getComponent(0);
 		oldIngredient.getTxtName().setText("");
-		waitForSwing();
+		injectFocusLost(oldIngredient.getTxtName(), "ShrinkOnEmpty");
 
 		verify(listener, times(1)).updateWasSignalled();
 	}
@@ -83,8 +84,8 @@ class StepPanelIT extends SwingTestCase {
 		final IngredientPanel oldIngredient =
 				(IngredientPanel) getPanelIngredients().getComponent(0);
 		oldIngredient.getTxtName().setText("");
+		injectFocusLost(oldIngredient.getTxtName(), "ShrinkOnEmpty");
 
-		waitForSwing();
 		clearInvocations(listener);
 
 		ObserverDispatch.notifySubscribers(ing);