Skip to content

Commit

Permalink
Extend Browser API to check for custom text location
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 Sep 4, 2024
1 parent c095b37 commit 50c18c5
Show file tree
Hide file tree
Showing 4 changed files with 100 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,21 @@ 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, must not be null
*
* @since 3.128
*/
public boolean isLocationForCustomText(String location) {
checkWidget();
return URI.create(Objects.requireNonNull(location)).equals(webBrowser.getUriForCustomText());
}

/**
* 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 getUriForCustomText() {
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,80 @@ public void changing(LocationEvent event) {
for (int i = 0; i < 100; i++) browser.removeLocationListener(listener);
}

@Test
public void test_isLocationForCustomText_setUrlAfterDisposedThrowsSwtException() {
Browser testBrowser = createBrowser(shell, SWT.NONE);
testBrowser.dispose();
assertThrows(SWTException.class, () -> testBrowser.isLocationForCustomText("about:blank"));
}

@Test
public void test_isLocationForCustomText_isSetUrlNotCustomTextUrlAfterSetText() {
assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); // Edge doesn't fire a changed event if the URL is the same as the previous location.
URI url = getValidUrl();
AtomicBoolean locationChanged = new AtomicBoolean(false);
browser.addLocationListener(changedAdapter(event -> {
locationChanged.set(true);
}));

browser.setText("Custom text");
assertTrue("Time Out: The Browser didn't navigate to the URL", waitForPassCondition(locationChanged::get));
locationChanged.set(false);
browser.setUrl(url.toASCIIString());
assertTrue("Time Out: The Browser didn't navigate to the URL", waitForPassCondition(locationChanged::get));
assertEquals("Url is wrongly considered Custom Text Url", URI.create(browser.getUrl()), url);
assertFalse("The navigated URL is falsly indicated to be the custom text URL", browser.isLocationForCustomText(browser.getUrl()));
}

@Test
public void test_isLocationForCustomText_isFirstSetTextURLStillCustomTextUrlAfterSetUrl() {
assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); // Edge doesn't fire a changed event if the URL is the same as the previous location.
AtomicBoolean locationChanged = new AtomicBoolean(false);
browser.addLocationListener(changedAdapter(event -> locationChanged.set(true)));
URI url = getValidUrl();
browser.setText("Custom text");
assertTrue(waitForPassCondition(locationChanged::get));
String firstUrl = browser.getUrl();
locationChanged.set(false);
browser.setUrl(url.toASCIIString());
assertTrue("Time Out: The Browser didn't navigate to the URL", waitForPassCondition(locationChanged::get));
assertTrue(browser.isLocationForCustomText(firstUrl));
assertFalse(browser.isLocationForCustomText(browser.getUrl()));
}

private URI getValidUrl() {
String pluginPath = System.getProperty("PLUGIN_PATH");
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());
}
return url;
}

@Test
public void test_isLocationForCustomText_isSetUrlNotCustomTextUrl() {
AtomicBoolean locationChanged = new AtomicBoolean(false);
browser.addLocationListener(changedAdapter(event -> locationChanged.set(true)));
URI url = getValidUrl();
browser.setUrl(url.toASCIIString());
waitForPassCondition(locationChanged::get);
assertFalse("Url is wrongly considered Custom Text Url", browser.isLocationForCustomText(browser.getUrl()));
}

@Test
public void test_isLocationForCustomText() {
assumeFalse("behavior is not (yet) supported by Edge browser", isEdge); // Edge doesn't fire a changed event if the URL is the same as the previous location.
AtomicBoolean locationChanged = new AtomicBoolean(false);
browser.addLocationListener(changedAdapter(e -> locationChanged.set(true)));
browser.setText("Hello world");
assertTrue("Timeout: LocationListener.changing() event was never fired", waitForPassCondition(locationChanged::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 50c18c5

Please sign in to comment.