From 5671ddd7ce1df205ccc61696b817ea56a8a0873c Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Fri, 16 Aug 2024 16:16:15 +0200 Subject: [PATCH] Added API for checking if BASE_URI The contribution provides an API to check if the location is the location where custom texts are loaded in a browser. By construct, "about:blank" is used for that purpose. This API can be adapted by the clients instead of explicitly defining the string and checking. Contributes to #213 --- .../org/eclipse/swt/browser/Browser.java | 17 +++++ .../org/eclipse/swt/browser/WebBrowser.java | 6 ++ .../examples/controlexample/BrowserTab.java | 2 +- .../Test_org_eclipse_swt_browser_Browser.java | 65 +++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java index 0bcc46d6cc6..5d856260eee 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java @@ -13,6 +13,9 @@ *******************************************************************************/ package org.eclipse.swt.browser; +import java.net.*; +import java.util.*; + import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; @@ -1187,6 +1190,20 @@ public boolean setUrl (String url, String postData, String[] headers) { return webBrowser.setUrl (url, postData, headers); } +/** + * Checks if the location supplied is the location for setting custom text in the browser. + * This means, when passing the browser URL to this method after a custom text has been set + * via {@link #setText(String)} will yield true as long as no further navigation + * to some other location is performed. + * + * @param location the URL to be checked. Null values are not allowed and would lead to a NullPointerException. + * + * @since 3.127 + */ +public boolean isLocationForCustomText(String location) { + return URI.create(Objects.requireNonNull(location)).equals(webBrowser.getBaseUri()); +} + /** * Stop any loading and rendering activity. * diff --git a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java index 6d1eef30a98..176c0d48778 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.swt.browser; +import java.net.*; import java.util.*; import java.util.List; @@ -36,6 +37,7 @@ abstract class WebBrowser { static final String ERROR_ID = "org.eclipse.swt.browser.error"; // $NON-NLS-1$ static final String EXECUTE_ID = "SWTExecuteTemporaryFunction"; // $NON-NLS-1$ + private static final URI URI_FOR_CUSTOM_TEXT_PAGE = URI.create("about:blank"); static List NativePendingCookies = new ArrayList<> (); static String CookieName, CookieValue, CookieUrl; @@ -724,6 +726,10 @@ public void setBrowser (Browser browser) { this.browser = browser; } +URI getBaseUri() { + return URI_FOR_CUSTOM_TEXT_PAGE; +} + public abstract boolean setText (String html, boolean trusted); public abstract boolean setUrl (String url, String postData, String[] headers); diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/BrowserTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/BrowserTab.java index a1e3c477c63..e48f01f7963 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/BrowserTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/BrowserTab.java @@ -209,7 +209,7 @@ void disposeExampleWidgets () { /* store the state of the Browser if applicable */ if (browser != null) { String url = browser.getUrl(); - if (url.length() > 0 && !url.equals("about:blank")) { //$NON-NLS-1$ + if (url.length() > 0 && !browser.isLocationForCustomText(url)) { //$NON-NLS-1$ lastUrl = url; } else { String text = browser.getText(); 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 422f55c93e1..56a7822fc05 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 @@ -34,6 +34,7 @@ import java.lang.management.ThreadMXBean; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.nio.file.DirectoryStream; import java.nio.file.Files; @@ -518,6 +519,70 @@ public void changing(LocationEvent event) { for (int i = 0; i < 100; i++) browser.removeLocationListener(listener); } +@Test +public void test_setUrlAfterDisposed() { + Browser testBrowser = createBrowser(shell, SWT.NONE); + testBrowser.dispose(); + assertThrows(SWTException.class, () -> testBrowser.setUrl("about:blank")); +} + +@Test +public void test_isSetUrlNotCustomTextUrlAfterSetText() { + String pluginPath = System.getProperty("PLUGIN_PATH"); + testLogAppend("PLUGIN_PATH: " + pluginPath); + URI url; + // Depending on how the jUnit test is ran, (gui/maven/ant), url for local file needs to be acquired differently. + if (pluginPath != null) { + url = URI.create(pluginPath + "/data/testWebsiteWithTitle.html"); + } else { + // used when ran from Eclipse gui. + url = URI.create(Test_org_eclipse_swt_browser_Browser.class.getClassLoader().getResource("testWebsiteWithTitle.html").toString()); + } + AtomicBoolean changed = new AtomicBoolean(false); + browser.addLocationListener(changedAdapter(event -> { + if (URI.create(event.location).equals(url)) { + changed.set(true); + } + })); + + browser.setText("Custom text"); + browser.setUrl(url.toASCIIString()); + shell.open(); + assertTrue("Time Out: The Browser didn't navigate to the URL", waitForPassCondition(changed::get)); + assertTrue("Url is wrongly considered Custom Text Url", URI.create(browser.getUrl()).equals(url) + && !browser.isLocationForCustomText(browser.getUrl())); +} + +@Test +public void test_isSetUrlNotCustomTextUrl() { + AtomicBoolean changed = new AtomicBoolean(false); + browser.addLocationListener(changedAdapter(event -> changed.set(true))); + String pluginPath = System.getProperty("PLUGIN_PATH"); + testLogAppend("PLUGIN_PATH: " + pluginPath); + String url; + // Depending on how the jUnit test is ran, (gui/maven/ant), url for local file needs to be acquired differently. + if (pluginPath != null) { + url = pluginPath + "/data/testWebsiteWithTitle.html"; + } else { + // used when ran from Eclipse gui. + url = Test_org_eclipse_swt_browser_Browser.class.getClassLoader().getResource("testWebsiteWithTitle.html").toString(); + } + browser.setUrl(url); + shell.open(); + waitForPassCondition(changed::get); + assertFalse("Url is wrongly considered Custom Text Url", browser.isLocationForCustomText(browser.getUrl())); +} + +@Test +public void test_isLocationForCustomText() { + AtomicBoolean changingFired = new AtomicBoolean(false); + browser.addLocationListener(changingAdapter(e -> changingFired.set(true))); + shell.open(); + browser.setText("Hello world"); + assertTrue("Timeout: LocationListener.changing() event was never fired", waitForPassCondition(changingFired::get)); + assertTrue("Custom Text URI was not loaded on setText", browser.isLocationForCustomText(browser.getUrl())); +} + @Test public void test_LocationListener_changing() { AtomicBoolean changingFired = new AtomicBoolean(false);