forked from CinemaMod/java-cef
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTestSetupExtension.java
More file actions
90 lines (75 loc) · 2.79 KB
/
TestSetupExtension.java
File metadata and controls
90 lines (75 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2019 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
package tests.junittests;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
import org.cef.CefApp;
import org.cef.CefApp.CefAppState;
import org.cef.CefSettings;
import org.cef.handler.CefAppHandlerAdapter;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.util.concurrent.CountDownLatch;
// All test cases must install this extension for CEF to be properly initialized
// and shut down.
//
// For example:
//
// @ExtendWith(TestSetupExtension.class)
// class FooTest {
// @Test
// void testCaseThatRequiresCEF() {}
// }
//
// This code is based on https://stackoverflow.com/a/51556718.
public class TestSetupExtension
implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean initialized_ = false;
private static CountDownLatch countdown_ = new CountDownLatch(1);
@Override
public void beforeAll(ExtensionContext context) {
if (!initialized_) {
initialized_ = true;
initialize(context);
}
}
// Executed before any tests are run.
private void initialize(ExtensionContext context) {
TestSetupContext.initialize(context);
if (TestSetupContext.debugPrint()) {
System.out.println("TestSetupExtension.initialize");
}
// Register a callback hook for when the root test context is shut down.
context.getRoot().getStore(GLOBAL).put("jcef_test_setup", this);
// Perform startup initialization on platforms that require it.
if (!CefApp.startup(null)) {
System.out.println("Startup initialization failed!");
return;
}
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
@Override
public void stateHasChanged(org.cef.CefApp.CefAppState state) {
if (state == CefAppState.TERMINATED) {
// Signal completion of CEF shutdown.
countdown_.countDown();
}
}
});
// Initialize the singleton CefApp instance.
CefSettings settings = new CefSettings();
CefApp.getInstance(settings);
}
// Executed after all tests have completed.
@Override
public void close() {
if (TestSetupContext.debugPrint()) {
System.out.println("TestSetupExtension.close");
}
CefApp.getInstance().dispose();
// Wait for CEF shutdown to complete.
try {
countdown_.await();
} catch (InterruptedException e) {
}
}
}