Skip to content

Commit

Permalink
Parameterize browser tests to execute them for Edge browser #671
Browse files Browse the repository at this point in the history
Browser tests were only executed for the default configuration of a
system's browser using the SWT.NONE flag. Other configurations, such as
using the Edge browser in Windows, were not tested.

This change parameterizes the browser tests to also execute them for the
Edge browser on Windows. It also deactivates those tests for the Edge
browser for which the implementation does (currently) not work. This
allows to detect regressions when performing future changes to the Edge
browser.

Fixes
#671
  • Loading branch information
HeikoKlare committed Sep 25, 2024
1 parent 1d12c5f commit 2e422ed
Showing 1 changed file with 74 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -78,17 +80,22 @@
import org.eclipse.swt.widgets.Shell;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
* Automated Test Suite for class org.eclipse.swt.browser.Browser
*
* @see org.eclipse.swt.browser.Browser
*/
@RunWith(Parameterized.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Test_org_eclipse_swt_browser_Browser extends Test_org_eclipse_swt_widgets_Composite {

Expand Down Expand Up @@ -133,6 +140,33 @@ private void testLogAppend(String msg) {
boolean ignoreNonDisposedShells;
static List<String> descriptors = new ArrayList<>();

private final int swtBrowserSettings;

@Parameters(name = "browser flags: {0}")
public static Collection<Object[]> browserFlagsToTest() {
List<Object[]> browserFlags = new ArrayList<>();
browserFlags.add(new Object[] {SWT.NONE});
if (SwtTestUtil.isWindows) {
// Execute Edge tests first, because IE starts some OS timer that conflicts with Edge event handling
browserFlags.add(0, new Object[] {SWT.EDGE});
}
return browserFlags;
}

public Test_org_eclipse_swt_browser_Browser(int swtBrowserSettings) {
this.swtBrowserSettings = swtBrowserSettings;
}

@BeforeClass
public static void setupEdgeEnvironment() {
// initialize Edge environment before any test runs to isolate environment setup
if (SwtTestUtil.isWindows) {
Shell shell = new Shell();
new Browser(shell, SWT.EDGE);
shell.dispose();
}
}

@Override
@Before
public void setUp() {
Expand All @@ -148,7 +182,7 @@ public void setUp() {
System.out.println("Running Test_org_eclipse_swt_browser_Browser#" + name.getMethodName());

shell.setLayout(new FillLayout());
browser = createBrowser(shell, SWT.NONE);
browser = createBrowser(shell, swtBrowserSettings);

isEdge = browser.getBrowserType().equals("edge");

Expand Down Expand Up @@ -207,6 +241,16 @@ public void tearDown() {
printThreadsInfo();
}
}
// if (isEdge) {
// // wait for and process pending events to properly cleanup Edge browser resources
// do {
// processUiEvents();
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// }
// } while (Display.getCurrent().readAndDispatch());
// }
if (SwtTestUtil.isGTK) {
int descriptorDiff = reportOpenedDescriptors();
if(descriptorDiff > 0) {
Expand Down Expand Up @@ -250,8 +294,12 @@ private int reportOpenedDescriptors() {
}

private Browser createBrowser(Shell s, int flags) {
Duration maximumBrowserCreationDuration = Duration.ofSeconds(10);
Duration createStartTime = Duration.ofMillis(System.currentTimeMillis());
Browser b = new Browser(s, flags);
createdBroswers.add(b);
Duration createDuration = Duration.ofMillis(System.currentTimeMillis()).minus(createStartTime);
assertTrue("creating browser took too long: " + createDuration.toMillis() + " ms", !maximumBrowserCreationDuration.minus(createDuration).isNegative());
return b;
}

Expand All @@ -260,12 +308,12 @@ private Browser createBrowser(Shell s, int flags) {
*/
@Override
public void test_ConstructorLorg_eclipse_swt_widgets_CompositeI() {
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.dispose();
browser = createBrowser(shell, SWT.BORDER);
browser = createBrowser(shell, SWT.BORDER | swtBrowserSettings);
// System.out.println("Test_org_eclipse_swt_browser_Browser#test_Constructor*#getBrowserType(): " + browser.getBrowserType());
browser.dispose();
assertThrows(IllegalArgumentException.class, () -> createBrowser(null, SWT.NONE));
assertThrows(IllegalArgumentException.class, () -> createBrowser(null, swtBrowserSettings));
}

/**
Expand All @@ -276,7 +324,7 @@ public void test_Constructor_asyncParentDisposal() {
Display.getCurrent().asyncExec(() -> {
shell.dispose();
});
Browser browser = createBrowser(shell, SWT.EDGE);
Browser browser = createBrowser(shell, swtBrowserSettings);
assertFalse(browser.isDisposed());
}

Expand Down Expand Up @@ -449,7 +497,7 @@ public void test_getChildren() {
public void test_CloseWindowListener_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.addCloseWindowListener(event -> {}); // shouldn't throw
shell.close();
}
Expand Down Expand Up @@ -488,7 +536,7 @@ public void test_CloseWindowListener_close () {
public void test_LocationListener_adapter_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
LocationAdapter adapter = new LocationAdapter() {};
browser.addLocationListener(adapter); // shouldn't throw
shell.close();
Expand Down Expand Up @@ -796,7 +844,7 @@ public void test_LocationListener_ProgressListener_noExtraEvents() {
public void test_OpenWindowListener_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.addOpenWindowListener(event -> {});
shell.close();
}
Expand All @@ -821,7 +869,7 @@ public void test_OpenWindowListener_addAndRemove() {
@Test
public void test_OpenWindowListener_openHasValidEventDetails() {
AtomicBoolean openFiredCorrectly = new AtomicBoolean(false);
final Browser browserChild = createBrowser(shell, SWT.None);
final Browser browserChild = createBrowser(shell, swtBrowserSettings);
browser.addOpenWindowListener(event -> {
assertSame("Expected Browser1 instance, but have another instance", browser, event.widget);
assertNull("Expected event.browser to be null", event.browser);
Expand All @@ -840,12 +888,14 @@ public void test_OpenWindowListener_openHasValidEventDetails() {
/** Test that a script 'window.open()' opens a child popup shell. */
@Test
public void test_OpenWindowListener_open_ChildPopup() {
assumeFalse("behavior is not (yet) supported by Edge browser", isEdge);

AtomicBoolean childCompleted = new AtomicBoolean(false);

Shell childShell = new Shell(shell, SWT.None);
childShell.setText("Child shell");
childShell.setLayout(new FillLayout());
final Browser browserChild = createBrowser(childShell, SWT.NONE);
final Browser browserChild = createBrowser(childShell, swtBrowserSettings);

browser.addOpenWindowListener(event -> {
event.browser = browserChild;
Expand Down Expand Up @@ -883,7 +933,7 @@ public void test_OpenWindow_Progress_Listener_ValidateEventOrder() {
Shell childShell = new Shell(shell, SWT.None);
childShell.setText("Child shell");
childShell.setLayout(new FillLayout());
final Browser browserChild = createBrowser(childShell, SWT.NONE);
final Browser browserChild = createBrowser(childShell, swtBrowserSettings);

browser.addOpenWindowListener(event -> {
event.browser = browserChild;
Expand Down Expand Up @@ -933,7 +983,7 @@ public void test_ProgressListener_newProgressAdapter() {
public void test_ProgressListener_newProgressAdapter_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.addProgressListener(new ProgressAdapter() {});
shell.close();
}
Expand All @@ -942,7 +992,7 @@ public void test_ProgressListener_newProgressAdapter_closeShell() {
public void test_ProgressListener_newListener_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.addProgressListener(new ProgressListener() {
@Override
public void changed(ProgressEvent event) {
Expand Down Expand Up @@ -1039,13 +1089,13 @@ public void test_StatusTextListener_addAndRemove() {
*/
@Test
public void test_StatusTextListener_hoverMouseOverLink() {
assumeFalse(isEdge); // no API in Edge for this
assumeFalse("no API in Edge for this", isEdge);

AtomicBoolean statusChanged = new AtomicBoolean(false);
int size = 500;

// 1) Create a page that has a hyper link (covering the whole page)
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
StringBuilder longhtml = new StringBuilder();
for (int i = 0; i < 200; i++) {
longhtml.append("text text text text text text text text text text text text text text text text text text text text text text text text<br>");
Expand Down Expand Up @@ -1085,7 +1135,7 @@ public void test_StatusTextListener_hoverMouseOverLink() {
public void test_TitleListener_addListener_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.addTitleListener(event -> {
});
shell.close();
Expand Down Expand Up @@ -1264,7 +1314,7 @@ public void test_VisibilityWindowListener_newAdapter() {
public void test_VisibilityWindowListener_newAdapter_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.addVisibilityWindowListener(new VisibilityWindowAdapter(){});
shell.close();
}
Expand All @@ -1273,7 +1323,7 @@ public void test_VisibilityWindowListener_newAdapter_closeShell() {
public void test_VisibilityWindowListener_newListener_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = createBrowser(shell, SWT.NONE);
Browser browser = createBrowser(shell, swtBrowserSettings);
browser.addVisibilityWindowListener(new VisibilityWindowListener() {
@Override
public void hide(WindowEvent event) {
Expand Down Expand Up @@ -1319,7 +1369,7 @@ public void test_VisibilityWindowListener_multiple_shells() {
Shell childShell = new Shell(shell);
childShell.setText("Child shell " + childCount.get());
childShell.setLayout(new FillLayout());
Browser browserChild = createBrowser(childShell, SWT.NONE);
Browser browserChild = createBrowser(childShell, swtBrowserSettings);
event.browser = browserChild;
browserChild.setText("Child window");
browserChild.addVisibilityWindowListener(new VisibilityWindowAdapter() {
Expand Down Expand Up @@ -1378,7 +1428,7 @@ public void test_VisibilityWindowListener_eventSize() {
childShell.setSize(250, 350);
childShell.setText("Child shell");
childShell.setLayout(new FillLayout());
final Browser browserChild = createBrowser(childShell, SWT.NONE);
final Browser browserChild = createBrowser(childShell, swtBrowserSettings);

browser.addOpenWindowListener(event -> {
event.browser = browserChild;
Expand Down Expand Up @@ -1497,8 +1547,7 @@ public void test_setJavascriptEnabled_multipleInstances() {
AtomicBoolean instanceOneFinishedCorrectly = new AtomicBoolean(false);
AtomicBoolean instanceTwoFinishedCorrectly = new AtomicBoolean(false);


Browser browserSecondInsance = createBrowser(shell, SWT.None);
Browser browserSecondInsance = createBrowser(shell, swtBrowserSettings);

browser.addProgressListener(completedAdapter(event -> {
if (pageLoadCount.get() == 1) {
Expand Down Expand Up @@ -2558,7 +2607,7 @@ public Object function(Object[] arguments) {
browser.setText("1st (initial) page load");
new JavascriptCallback(browser, "jsCallbackToJava");
browser.execute("jsCallbackToJava()");
// see if function still works after a page change:
// see if function still works after a page change:
browser.addProgressListener(completedAdapter(e -> browser.execute("jsCallbackToJava()")));

shell.open();
Expand All @@ -2570,8 +2619,8 @@ public Object function(Object[] arguments) {
@Test
public void test_BrowserFunction_multiprocess() {
// Test that BrowserFunctions work in multiple Browser instances simultaneously.
Browser browser1 = createBrowser(shell, SWT.NONE);
Browser browser2 = createBrowser(shell, SWT.NONE);
Browser browser1 = createBrowser(shell, swtBrowserSettings);
Browser browser2 = createBrowser(shell, swtBrowserSettings);

class JavaFunc extends BrowserFunction {
JavaFunc(Browser browser) {
Expand Down

0 comments on commit 2e422ed

Please sign in to comment.