Browse Source

Test: Ensure property threading across test classes.

pull/245/head
weisj 3 years ago
parent
commit
8ec49ddb47
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 15
      core/src/test/java/test/CustomTitleBarTest.java
  2. 15
      core/src/test/java/test/MemoryTest.java
  3. 28
      core/src/test/java/test/NonThreadSafeTest.java
  4. 15
      core/src/test/java/test/TestUtils.java
  5. 2
      core/src/test/java/test/TooltipTest.java

15
core/src/test/java/test/CustomTitleBarTest.java

@ -33,8 +33,6 @@ import javax.swing.*;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import com.github.weisj.darklaf.LafManager; import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.color.ColorUtil; import com.github.weisj.darklaf.color.ColorUtil;
@ -43,14 +41,12 @@ import com.github.weisj.darklaf.theme.IntelliJTheme;
import com.github.weisj.darklaf.ui.rootpane.DarkRootPaneUI; import com.github.weisj.darklaf.ui.rootpane.DarkRootPaneUI;
import com.github.weisj.darklaf.util.SystemInfo; import com.github.weisj.darklaf.util.SystemInfo;
@Execution(ExecutionMode.SAME_THREAD) class CustomTitleBarTest extends AbstractImageTest implements NonThreadSafeTest {
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CustomTitleBarTest extends AbstractImageTest {
private static final Color TITLE_BAR_COLOR = Color.RED; private static final Color TITLE_BAR_COLOR = Color.RED;
private static final Color CONTENT_COLOR = Color.BLUE; private static final Color CONTENT_COLOR = Color.BLUE;
private static final int TITLE_BAR_Y = 10; private static final int TITLE_BAR_Y = 10;
private static final int TOLERANCE = SystemInfo.isMac ? 55 : 0; private static final int TOLERANCE = SystemInfo.isMac ? 60 : 0;
public CustomTitleBarTest() { public CustomTitleBarTest() {
super("titlebar"); super("titlebar");
@ -77,9 +73,11 @@ class CustomTitleBarTest extends AbstractImageTest {
TestUtils.runOnSwingThreadNotThrowing(() -> { TestUtils.runOnSwingThreadNotThrowing(() -> {
JFrame f = new JFrame(""); JFrame f = new JFrame("");
frame.set(f); frame.set(f);
JPanel content = new JPanel(); JPanel content = new JPanel(new GridBagLayout());
content.setBackground(CONTENT_COLOR); content.setBackground(CONTENT_COLOR);
content.setPreferredSize(new Dimension(200, 200)); content.setPreferredSize(new Dimension(200, 200));
JLabel contentLabel = new JLabel(LafManager.getInstalledTheme().getDisplayName());
content.add(contentLabel, null);
f.setContentPane(content); f.setContentPane(content);
f.pack(); f.pack();
f.setLocationRelativeTo(null); f.setLocationRelativeTo(null);
@ -153,6 +151,7 @@ class CustomTitleBarTest extends AbstractImageTest {
rect.setLocation(0, 0); rect.setLocation(0, 0);
check.accept(saveWindowScreenShot(getPath(fileName), frame.get())); check.accept(saveWindowScreenShot(getPath(fileName), frame.get()));
}); });
TestUtils.runOnSwingThreadNotThrowing(() -> TestUtils.closeWindow(frame.get()));
} }
@Test @Test
@ -223,7 +222,7 @@ class CustomTitleBarTest extends AbstractImageTest {
} }
@Test @Test
@EnabledOnOs(OS.WINDOWS) @EnabledOnOs({OS.MAC, OS.WINDOWS})
void checkDisableCustomDecoration() { void checkDisableCustomDecoration() {
TestUtils.ensureLafInstalled(); TestUtils.ensureLafInstalled();
checkImage("native_title_bar_window", checkImage("native_title_bar_window",

15
core/src/test/java/test/MemoryTest.java

@ -23,8 +23,6 @@ package test;
import java.awt.FlowLayout; import java.awt.FlowLayout;
import java.awt.KeyboardFocusManager; import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -126,7 +124,7 @@ class MemoryTest {
frame.set(f); frame.set(f);
}); });
WeakReference<JFrame> ref = new WeakReference<>(frame.get()); WeakReference<JFrame> ref = new WeakReference<>(frame.get());
TestUtils.runOnSwingThreadNotThrowing(() -> closeWindow(frame.get())); TestUtils.runOnSwingThreadNotThrowing(() -> TestUtils.closeWindow(frame.get()));
Assertions.assertFalse(frame.get().isVisible()); Assertions.assertFalse(frame.get().isVisible());
resetFocusWindow(ref.get()); resetFocusWindow(ref.get());
frame.set(null); frame.set(null);
@ -143,18 +141,13 @@ class MemoryTest {
}); });
TestUtils.runOnSwingThreadNotThrowing(() -> { TestUtils.runOnSwingThreadNotThrowing(() -> {
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Assertions.assertEquals(f.get(), manager.getFocusedWindow()); Assertions.assertTrue(f.get() == manager.getFocusedWindow() || manager.getFocusedWindow() == null);
Assertions.assertEquals(f.get(), manager.getActiveWindow()); Assertions.assertTrue(f.get() == manager.getActiveWindow() || manager.getActiveWindow() == null);
}); });
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Assertions.assertNotEquals(frame, manager.getFocusedWindow()); Assertions.assertNotEquals(frame, manager.getFocusedWindow());
Assertions.assertNotEquals(frame, manager.getActiveWindow()); Assertions.assertNotEquals(frame, manager.getActiveWindow());
TestUtils.runOnSwingThreadNotThrowing(() -> closeWindow(f.get())); TestUtils.runOnSwingThreadNotThrowing(() -> TestUtils.closeWindow(f.get()));
}
private void closeWindow(final Window window) {
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
window.dispose();
} }
private void waitForGarbageCollection(WeakReference<?> ref) { private void waitForGarbageCollection(WeakReference<?> ref) {

28
core/src/test/java/test/NonThreadSafeTest.java

@ -0,0 +1,28 @@
/*
* MIT License
*
* Copyright (c) 2021 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package test;
import org.junit.jupiter.api.parallel.ResourceLock;
@ResourceLock(value = "LafManager")
public interface NonThreadSafeTest {
}

15
core/src/test/java/test/TestUtils.java

@ -21,6 +21,8 @@
*/ */
package test; package test;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -36,13 +38,17 @@ final class TestUtils {
private TestUtils() {} private TestUtils() {}
private static final Object lock = new Object();
static void ensureLafInstalled() { static void ensureLafInstalled() {
ensureLafInstalled(new IntelliJTheme()); ensureLafInstalled(new IntelliJTheme());
} }
static void ensureLafInstalled(final Theme theme) { static void ensureLafInstalled(final Theme theme) {
if (!LafManager.isInstalled()) { synchronized (lock) {
runOnSwingThreadNotThrowing(() -> LafManager.install(theme)); if (!LafManager.isInstalled() || !LafManager.getInstalledTheme().equals(theme)) {
runOnSwingThreadNotThrowing(() -> LafManager.install(theme));
}
} }
} }
@ -87,4 +93,9 @@ final class TestUtils {
Assertions.fail(exceptionRef.get().getMessage(), exceptionRef.get()); Assertions.fail(exceptionRef.get().getMessage(), exceptionRef.get());
} }
} }
static void closeWindow(final Window window) {
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
window.dispose();
}
} }

2
core/src/test/java/test/TooltipTest.java

@ -39,7 +39,7 @@ import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.LogUtil; import com.github.weisj.darklaf.util.LogUtil;
import com.github.weisj.darklaf.util.SystemInfo; import com.github.weisj.darklaf.util.SystemInfo;
class TooltipTest extends AbstractImageTest { class TooltipTest extends AbstractImageTest implements NonThreadSafeTest {
private static final Logger LOGGER = LogUtil.getDetachedLogger(TooltipTest.class); private static final Logger LOGGER = LogUtil.getDetachedLogger(TooltipTest.class);

Loading…
Cancel
Save