use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback_with_integer.
/**
* Test that javascript can call java and pass an integer to java.
* loosely based on Snippet307.
*/
@Test
public void test_BrowserFunction_callback_with_integer() {
// On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
// culprit seems to be the main_context_iteration() call in shell.setVisible().
// See Bug 509587. Solution: Webkit2.
// It's useful to run at least one function test on webkit1 locally.
// run locally. Skip on hudson that runs webkit1.
assumeFalse(webkit1SkipMsg(), (SwtTestUtil.isRunningOnEclipseOrgHudsonGTK && isWebkit1));
AtomicInteger returnInt = new AtomicInteger(0);
class JavascriptCallback extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
Double returnedDouble = (Double) arguments[0];
// 5.0 -> 5
returnInt.set(returnedDouble.intValue());
return null;
}
}
String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + // Define a javascript function.
"function callCustomFunction() {\n" + " document.body.style.backgroundColor = 'red'\n" + // This calls the javafunction that we registered ** with value of 5.
" jsCallbackToJava(5)\n" + "}" + "</script>\n" + "</head>\n" + "<body> I'm going to make a callback to java </body>\n" + "</html>\n";
browser.setText(htmlWithScript);
new JavascriptCallback(browser, "jsCallbackToJava");
browser.addProgressListener(callCustomFunctionUponLoad);
shell.open();
boolean passed = waitForPassCondition(() -> returnInt.get() == 5);
String message = "Javascript should have passed an integer to java. But this did not happen";
assertTrue(message, passed);
}
use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback_with_javaReturningInt.
/**
* Test that javascript can call java, java returns an Integer back to javascript.
*
* It's a bit tricky to tell if javascript actually received the correct value from java.
* Solution: make a second function/callback that is called with the value that javascript received from java.
*
* Logic:
* 1) Java registers function callCustomFunction() by setting html body.
* 2) which in turn calls JavascriptCallback, which returns value 42 back to javascript.
* 3) javascript then calls JavascriptCallback_javascriptReceivedJavaInt() and passes it value received from java.
* 4) Java validates that the correct value (42) was passed to javascript and was passed back to java.
*
* loosely based on Snippet307.
*/
@Test
public void test_BrowserFunction_callback_with_javaReturningInt() {
// On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
// culprit seems to be the main_context_iteration() call in shell.setVisible().
// See Bug 509587. Solution: Webkit2.
assumeFalse(webkit1SkipMsg(), isWebkit1);
AtomicInteger returnInt = new AtomicInteger(0);
class JavascriptCallback extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
return 42;
}
}
class JavascriptCallback_javascriptReceivedJavaInt extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback_javascriptReceivedJavaInt(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
Double returnVal = (Double) arguments[0];
// 4)
returnInt.set(returnVal.intValue());
return null;
}
}
String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + // Define a javascript function.
"function callCustomFunction() {\n" + " document.body.style.backgroundColor = 'red'\n" + // 2)
" var retVal = jsCallbackToJava()\n" + // This calls the javafunction that we registered. Set HTML body to return value.
" document.write(retVal)\n" + // 3)
" jsSuccess(retVal)\n" + "}" + "</script>\n" + "</head>\n" + "<body> If you see this, javascript did not receive anything from Java. This page should just be '42' </body>\n" + "</html>\n";
// 1)
browser.setText(htmlWithScript);
new JavascriptCallback(browser, "jsCallbackToJava");
new JavascriptCallback_javascriptReceivedJavaInt(browser, "jsSuccess");
browser.addProgressListener(callCustomFunctionUponLoad);
shell.open();
boolean passed = waitForPassCondition(() -> returnInt.get() == 42);
String message = "Java should have returned something back to javascript. But something went wrong";
assertTrue(message, passed);
}
use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method test_VisibilityWindowListener_newListener_closeShell.
@Test
public void test_VisibilityWindowListener_newListener_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.addVisibilityWindowListener(new VisibilityWindowListener() {
@Override
public void hide(WindowEvent event) {
}
@Override
public void show(WindowEvent event) {
}
});
shell.close();
}
use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method setUp.
@Override
@Before
public void setUp() {
super.setUp();
secondsToWaitTillFail = Math.max(15, debug_show_browser_timeout_seconds);
// Print test name if running on hudson. This makes it easier to tell in the logs which test case caused crash.
if (SwtTestUtil.isRunningOnEclipseOrgHudsonGTK || debug_print_test_names) {
System.out.println("Running Test_org_eclipse_swt_browser_Browser#" + name.getMethodName());
}
shell.setLayout(new FillLayout());
browser = new Browser(shell, SWT.NONE);
String shellTitle = name.getMethodName();
if (SwtTestUtil.isGTK && browser.getBrowserType().equals("webkit")) {
// Note, webkitGtk version is only available once Browser is instantiated.
// $NON-NLS-1$
String webkitGtkVersionStr = System.getProperty("org.eclipse.swt.internal.webkitgtk.version");
shellTitle = shellTitle + " Webkit version: " + webkitGtkVersionStr;
String[] webkitGtkVersionStrParts = webkitGtkVersionStr.split("\\.");
for (int i = 0; i < 3; i++) {
webkitGtkVersionInts[i] = Integer.parseInt(webkitGtkVersionStrParts[i]);
}
// webkitgtk 2.5 and onwards uses webkit2.
if (webkitGtkVersionInts[0] == 1 || (webkitGtkVersionInts[0] == 2 && webkitGtkVersionInts[1] <= 4)) {
isWebkit1 = true;
} else if (webkitGtkVersionInts[0] == 2 && webkitGtkVersionInts[1] > 4) {
isWebkit2 = true;
}
}
shell.setText(shellTitle);
// For browser to occupy the whole shell, not just half of it.
setWidget(browser);
testLog = new StringBuilder("\nTest log:\n");
}
use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method test_ProgressListener_newListener_closeShell.
@Test
public void test_ProgressListener_newListener_closeShell() {
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.addProgressListener(new ProgressListener() {
@Override
public void changed(ProgressEvent event) {
}
@Override
public void completed(ProgressEvent event) {
}
});
shell.close();
}
Aggregations