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 19, 2024
1 parent 85c26ca commit 42c40bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.swt.browser;

import java.net.*;

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

Expand Down Expand Up @@ -1201,4 +1203,15 @@ public void stop () {
checkWidget();
webBrowser.stop ();
}

/**
* Checks if the location supplied is the location for setting custom text in the browser
*
* @param location the URL to be checked
*
* @since 3.127
*/
public boolean isLocationForCustomText(String location) {
return URI.create(location).equals(webBrowser.getBaseUri());
}
}
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 BASE_URI = 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;
}

public URI getBaseUri() {
return BASE_URI;
}

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



@Test
public void test_isLocationForCustomText() {
AtomicBoolean changingFired = new AtomicBoolean(false);
browser.addLocationListener(changingAdapter(e -> changingFired.set(true)));
shell.open();
browser.setText("Hello world");
waitForPassCondition(changingFired::get);
assertTrue("LocationListener.changing() event was never fired", browser.isLocationForCustomText(browser.getUrl()));
}

@Test
public void test_LocationListener_changing() {
AtomicBoolean changingFired = new AtomicBoolean(false);
Expand Down Expand Up @@ -1141,7 +1153,7 @@ private void validateTitleChanged(String expectedTitle, Runnable browserSetFunc)
shell.open();

boolean hasFinished = waitForPassCondition(() -> actualTitle.get().length() != 0
&& !actualTitle.get().contains("about:blank")); // Windows sometimes does 2 loads, one "about:blank", and one actual load.
&& !browser.isLocationForCustomText(actualTitle.get())); // Windows sometimes does 2 loads, one BASE_URI, and one actual load.
boolean passed = hasFinished && actualTitle.get().equals(expectedTitle);
String errMsg = "";
if (!hasFinished)
Expand Down

0 comments on commit 42c40bc

Please sign in to comment.