Skip to content

Commit

Permalink
Added API for checking if BASE_URI
Browse files Browse the repository at this point in the history
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
  • Loading branch information
amartya4256 committed Aug 30, 2024
1 parent 85c26ca commit 5671ddd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*******************************************************************************/
package org.eclipse.swt.browser;

import java.net.*;
import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.swt.browser;

import java.net.*;
import java.util.*;
import java.util.List;

Expand All @@ -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<String[]> NativePendingCookies = new ArrayList<> ();
static String CookieName, CookieValue, CookieUrl;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5671ddd

Please sign in to comment.