From 31c2a392f4d2cb161cb02131c635ea3e75e5737c Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Mon, 15 May 2023 17:10:22 +0200 Subject: [PATCH] Parameterize browser tests to execute them for Edge browser #671 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 https://github.com/eclipse-platform/eclipse.platform.swt/issues/671 --- .../Test_org_eclipse_swt_browser_Browser.java | 105 +++++++++++++----- 1 file changed, 79 insertions(+), 26 deletions(-) diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java index 6eb42e4b435..6589dffa218 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeFalse; @@ -40,6 +41,7 @@ import java.nio.file.Paths; import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -81,13 +83,17 @@ 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 { @@ -132,6 +138,23 @@ private void testLogAppend(String msg) { boolean ignoreNonDisposedShells; static List descriptors = new ArrayList<>(); + private final int swtBrowserSettings; + +@Parameters(name = "browser flags: {0}") +public static Collection browserFlagsToTest() { + List 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; +} + @Override @Before public void setUp() { @@ -147,7 +170,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"); @@ -206,6 +229,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) { @@ -249,8 +282,12 @@ private int reportOpenedDescriptors() { } private Browser createBrowser(Shell s, int flags) { + long maximumBrowserCreationMilliseconds = 10_000; + long createStartTime = System.currentTimeMillis(); Browser b = new Browser(s, flags); createdBroswers.add(b); + long createDuration = System.currentTimeMillis() - createStartTime; + assertTrue("creating browser took too long: " + createDuration + "ms", createDuration < maximumBrowserCreationMilliseconds); return b; } @@ -258,14 +295,13 @@ private Browser createBrowser(Shell s, int flags) { * Test that if Browser is constructed with the parent being "null", Browser throws an exception. */ @Override -@Test(expected = IllegalArgumentException.class) 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(); - browser = createBrowser(null, SWT.NONE); // Should throw. + assertThrows(IllegalArgumentException.class, () -> createBrowser(null, swtBrowserSettings)); } /** @@ -276,7 +312,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()); } @@ -374,7 +410,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(); } @@ -413,7 +449,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(); @@ -454,6 +490,8 @@ public void test_LocationListener_changing() { } @Test public void test_LocationListener_changed() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + AtomicBoolean changedFired = new AtomicBoolean(false); browser.addLocationListener(changedAdapter(e -> changedFired.set(true))); shell.open(); @@ -463,6 +501,8 @@ public void test_LocationListener_changed() { } @Test public void test_LocationListener_changingAndOnlyThenChanged() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + // Test proper order of events. // Check that 'changed' is only fired after 'changing' has fired at least once. AtomicBoolean changingFired = new AtomicBoolean(false); @@ -506,6 +546,8 @@ else if (!changingFired.get()) @Test public void test_LocationListener_then_ProgressListener() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + AtomicBoolean locationChanged = new AtomicBoolean(false); AtomicBoolean progressChanged = new AtomicBoolean(false); AtomicBoolean progressChangedAfterLocationChanged = new AtomicBoolean(false); @@ -599,6 +641,8 @@ public void changed(LocationEvent event) { @Test /** Ensue that only one changed and one completed event are fired for url changes */ public void test_LocationListener_ProgressListener_noExtraEvents() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + AtomicInteger changedCount = new AtomicInteger(0); AtomicInteger completedCount = new AtomicInteger(0); @@ -626,7 +670,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(); } @@ -651,7 +695,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); @@ -670,12 +714,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; @@ -705,6 +751,8 @@ public void test_OpenWindowListener_open_ChildPopup() { /** Validate event order : Child's visibility should come before progress completed event */ @Test public void test_OpenWindow_Progress_Listener_ValidateEventOrder() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + AtomicBoolean windowOpenFired = new AtomicBoolean(false); AtomicBoolean childCompleted = new AtomicBoolean(false); AtomicBoolean visibilityShowed = new AtomicBoolean(false); @@ -712,7 +760,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; @@ -762,7 +810,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(); } @@ -771,7 +819,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) { @@ -868,13 +916,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
"); @@ -914,7 +962,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(); @@ -1076,7 +1124,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(); } @@ -1085,7 +1133,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) { @@ -1131,7 +1179,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() { @@ -1182,6 +1230,8 @@ public void completed(ProgressEvent event) { */ @Test public void test_VisibilityWindowListener_eventSize() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + shell.setSize(200,300); AtomicBoolean childCompleted = new AtomicBoolean(false); AtomicReference result = new AtomicReference<>(new Point(0,0)); @@ -1190,7 +1240,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; @@ -1309,8 +1359,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) { @@ -1371,6 +1420,8 @@ public void completed(ProgressEvent event) { */ @Test public void test_LocationListener_evaluateInCallback() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + AtomicBoolean changingFinished = new AtomicBoolean(false); AtomicBoolean changedFinished = new AtomicBoolean(false); browser.addLocationListener(new LocationListener() { @@ -1419,6 +1470,8 @@ public void changed(LocationEvent event) { /** Verify that evaluation works inside an OpenWindowListener */ @Test public void test_OpenWindowListener_evaluateInCallback() { + assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); + AtomicBoolean eventFired = new AtomicBoolean(false); browser.addOpenWindowListener(event -> { browser.evaluate("SWTopenListener = true"); @@ -2366,7 +2419,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(); @@ -2378,8 +2431,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) {