From 8ec49ddb47bfdc272205dad2dd5a77503adfecaf Mon Sep 17 00:00:00 2001 From: weisj <31143295+weisJ@users.noreply.github.com> Date: Wed, 23 Jun 2021 13:13:24 +0200 Subject: [PATCH] Test: Ensure property threading across test classes. --- .../test/java/test/CustomTitleBarTest.java | 15 +++++----- core/src/test/java/test/MemoryTest.java | 15 +++------- .../src/test/java/test/NonThreadSafeTest.java | 28 +++++++++++++++++++ core/src/test/java/test/TestUtils.java | 15 ++++++++-- core/src/test/java/test/TooltipTest.java | 2 +- 5 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 core/src/test/java/test/NonThreadSafeTest.java diff --git a/core/src/test/java/test/CustomTitleBarTest.java b/core/src/test/java/test/CustomTitleBarTest.java index 41795601..fa256ddb 100644 --- a/core/src/test/java/test/CustomTitleBarTest.java +++ b/core/src/test/java/test/CustomTitleBarTest.java @@ -33,8 +33,6 @@ import javax.swing.*; import org.junit.jupiter.api.*; import org.junit.jupiter.api.condition.EnabledOnOs; 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.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.util.SystemInfo; -@Execution(ExecutionMode.SAME_THREAD) -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -class CustomTitleBarTest extends AbstractImageTest { +class CustomTitleBarTest extends AbstractImageTest implements NonThreadSafeTest { private static final Color TITLE_BAR_COLOR = Color.RED; private static final Color CONTENT_COLOR = Color.BLUE; 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() { super("titlebar"); @@ -77,9 +73,11 @@ class CustomTitleBarTest extends AbstractImageTest { TestUtils.runOnSwingThreadNotThrowing(() -> { JFrame f = new JFrame(""); frame.set(f); - JPanel content = new JPanel(); + JPanel content = new JPanel(new GridBagLayout()); content.setBackground(CONTENT_COLOR); content.setPreferredSize(new Dimension(200, 200)); + JLabel contentLabel = new JLabel(LafManager.getInstalledTheme().getDisplayName()); + content.add(contentLabel, null); f.setContentPane(content); f.pack(); f.setLocationRelativeTo(null); @@ -153,6 +151,7 @@ class CustomTitleBarTest extends AbstractImageTest { rect.setLocation(0, 0); check.accept(saveWindowScreenShot(getPath(fileName), frame.get())); }); + TestUtils.runOnSwingThreadNotThrowing(() -> TestUtils.closeWindow(frame.get())); } @Test @@ -223,7 +222,7 @@ class CustomTitleBarTest extends AbstractImageTest { } @Test - @EnabledOnOs(OS.WINDOWS) + @EnabledOnOs({OS.MAC, OS.WINDOWS}) void checkDisableCustomDecoration() { TestUtils.ensureLafInstalled(); checkImage("native_title_bar_window", diff --git a/core/src/test/java/test/MemoryTest.java b/core/src/test/java/test/MemoryTest.java index 897855d3..f6311a20 100644 --- a/core/src/test/java/test/MemoryTest.java +++ b/core/src/test/java/test/MemoryTest.java @@ -23,8 +23,6 @@ package test; import java.awt.FlowLayout; import java.awt.KeyboardFocusManager; -import java.awt.Window; -import java.awt.event.WindowEvent; import java.lang.ref.WeakReference; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -126,7 +124,7 @@ class MemoryTest { frame.set(f); }); WeakReference ref = new WeakReference<>(frame.get()); - TestUtils.runOnSwingThreadNotThrowing(() -> closeWindow(frame.get())); + TestUtils.runOnSwingThreadNotThrowing(() -> TestUtils.closeWindow(frame.get())); Assertions.assertFalse(frame.get().isVisible()); resetFocusWindow(ref.get()); frame.set(null); @@ -143,18 +141,13 @@ class MemoryTest { }); TestUtils.runOnSwingThreadNotThrowing(() -> { KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); - Assertions.assertEquals(f.get(), manager.getFocusedWindow()); - Assertions.assertEquals(f.get(), manager.getActiveWindow()); + Assertions.assertTrue(f.get() == manager.getFocusedWindow() || manager.getFocusedWindow() == null); + Assertions.assertTrue(f.get() == manager.getActiveWindow() || manager.getActiveWindow() == null); }); KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); Assertions.assertNotEquals(frame, manager.getFocusedWindow()); Assertions.assertNotEquals(frame, manager.getActiveWindow()); - TestUtils.runOnSwingThreadNotThrowing(() -> closeWindow(f.get())); - } - - private void closeWindow(final Window window) { - window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); - window.dispose(); + TestUtils.runOnSwingThreadNotThrowing(() -> TestUtils.closeWindow(f.get())); } private void waitForGarbageCollection(WeakReference ref) { diff --git a/core/src/test/java/test/NonThreadSafeTest.java b/core/src/test/java/test/NonThreadSafeTest.java new file mode 100644 index 00000000..32c45a93 --- /dev/null +++ b/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 { +} diff --git a/core/src/test/java/test/TestUtils.java b/core/src/test/java/test/TestUtils.java index fc24cf7e..60826ca3 100644 --- a/core/src/test/java/test/TestUtils.java +++ b/core/src/test/java/test/TestUtils.java @@ -21,6 +21,8 @@ */ package test; +import java.awt.Window; +import java.awt.event.WindowEvent; import java.lang.reflect.InvocationTargetException; import java.util.concurrent.atomic.AtomicReference; @@ -36,13 +38,17 @@ final class TestUtils { private TestUtils() {} + private static final Object lock = new Object(); + static void ensureLafInstalled() { ensureLafInstalled(new IntelliJTheme()); } static void ensureLafInstalled(final Theme theme) { - if (!LafManager.isInstalled()) { - runOnSwingThreadNotThrowing(() -> LafManager.install(theme)); + synchronized (lock) { + 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()); } } + + static void closeWindow(final Window window) { + window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); + window.dispose(); + } } diff --git a/core/src/test/java/test/TooltipTest.java b/core/src/test/java/test/TooltipTest.java index 3a3d3fae..88f5abf9 100644 --- a/core/src/test/java/test/TooltipTest.java +++ b/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.SystemInfo; -class TooltipTest extends AbstractImageTest { +class TooltipTest extends AbstractImageTest implements NonThreadSafeTest { private static final Logger LOGGER = LogUtil.getDetachedLogger(TooltipTest.class);