Browse Source

Tests: Wait for demo window reference to be set

This should resolve flaky tests due to race conditions
pull/295/head
Jannis Weis 3 years ago
parent
commit
7d8122fb73
  1. 3
      core/src/test/java/com/github/weisj/darklaf/core/test/DemoTest.java
  2. 10
      core/src/test/java/com/github/weisj/darklaf/ui/DemoLauncher.java
  3. 20
      core/src/test/java/com/github/weisj/darklaf/ui/demo/DemoExecutor.java

3
core/src/test/java/com/github/weisj/darklaf/core/test/DemoTest.java

@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019-2021 Jannis Weis
* Copyright (c) 2019-2022 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,
@ -53,7 +53,6 @@ class DemoTest implements NonThreadSafeTest {
@Test
void runningDemosDoesNotThrow() {
@SuppressWarnings("Convert2MethodRef")
Robot robot = Lambdas.orDefault(() -> new Robot(), null).get();
LafManager.setLogLevel(Level.WARNING);
for (Theme theme : LafManager.getRegisteredThemes()) {

10
core/src/test/java/com/github/weisj/darklaf/ui/DemoLauncher.java

@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020-2021 Jannis Weis
* Copyright (c) 2020-2022 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,
@ -28,6 +28,7 @@ import java.util.stream.Collectors;
import javax.swing.*;
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.core.test.DarklafOnly;
import com.github.weisj.darklaf.core.test.DelicateDemo;
import com.github.weisj.darklaf.core.test.util.ClassFinder;
@ -90,14 +91,15 @@ public class DemoLauncher extends BaseComponentDemo {
this.demo = demo;
}
public AtomicReference<Window> start() {
return start(null).getFirst();
public void start() {
start(null);
}
public Pair<AtomicReference<Window>, ComponentDemo> start(final Level logLevel) {
ComponentDemo componentDemo = demo.instantiate();
LafManager.setLogLevel(logLevel != null ? logLevel : Level.FINE);
return new Pair<>(
DemoExecutor.showDemo(componentDemo, true, logLevel),
DemoExecutor.showDemoWithoutSetup(componentDemo, true),
componentDemo);
}

20
core/src/test/java/com/github/weisj/darklaf/ui/demo/DemoExecutor.java

@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2021 Jannis Weis
* Copyright (c) 2021-2022 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,
@ -25,7 +25,6 @@ import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import javax.swing.Icon;
import javax.swing.JDialog;
@ -55,16 +54,14 @@ public final class DemoExecutor {
showDemo(demo, false);
}
public static AtomicReference<Window> showDemo(final ComponentDemo demo, final boolean asDialog,
final Level logLevel) {
LafManager.setLogLevel(logLevel != null ? logLevel : Level.FINE);
return showDemo(demo, asDialog);
}
public static AtomicReference<Window> showDemo(final ComponentDemo demo, final boolean asDialog) {
InspectorKt.installInspector();
LafManager.enabledPreferenceChangeReporting(false);
LafManager.addThemePreferenceChangeListener(LafManager::installTheme);
return showDemoWithoutSetup(demo, asDialog);
}
public static AtomicReference<Window> showDemoWithoutSetup(final ComponentDemo demo, final boolean asDialog) {
AtomicReference<Window> windowRef = new AtomicReference<>();
DemoExecutionSpec executionSpec = demo.getExecutionSpec();
@ -88,6 +85,13 @@ public final class DemoExecutor {
windowRef.notifyAll();
}
});
synchronized (windowRef) {
try {
windowRef.wait();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
return windowRef;
}

Loading…
Cancel
Save