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_eventSize.
/**
* Validate that when javascript opens a new window and specifies size,
* it's size is passed to the visibility event correctly.
*/
@Test
public void test_VisibilityWindowListener_eventSize() {
shell.setSize(200, 300);
AtomicBoolean childCompleted = new AtomicBoolean(false);
AtomicReference<Point> result = new AtomicReference<>(new Point(0, 0));
Shell childShell = new Shell(shell);
childShell.setSize(250, 350);
childShell.setText("Child shell");
childShell.setLayout(new FillLayout());
final Browser browserChild = new Browser(childShell, SWT.NONE);
browser.addOpenWindowListener(event -> {
event.browser = browserChild;
testLog.append("openWindowListener fired");
});
browserChild.addVisibilityWindowListener(showAdapter(event -> {
testLog.append("Visibilty show eventfired.\nEvent size: " + event.size);
result.set(event.size);
childShell.open();
childCompleted.set(true);
}));
shell.open();
browser.setText("<html>" + "<script type='text/javascript'>" + "window.open('javascript:\"Child Window\"','', \"height=200,width=300\")\n" + "</script>\n" + "<body>This test uses javascript to open a new window.</body></html>");
boolean finishedWithoutTimeout = waitForPassCondition(() -> childCompleted.get());
browserChild.dispose();
boolean passed = false;
if (isWebkit1 || SwtTestUtil.isCocoa) {
// On webkit1, event.size doesn't work properly. Fields are differently. Solution: Webkit2.
// On Cocoa, event height/width aren't respected if declared by javascript.
passed = finishedWithoutTimeout && result.get().x != 0 && result.get().y != 0;
} else
passed = finishedWithoutTimeout && result.get().x == 300 && result.get().y == 200;
String errMsg = finishedWithoutTimeout ? "Incorrect size received:" + "\nexpected width=300, actual:" + result.get().x + "\nexpected height=100, actual:" + result.get().y : "test timed out. Child's visibility Window listener didn't trigger";
assertTrue(errMsg + testLog.toString(), 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_OpenWindow_Progress_Listener_ValidateEventOrder.
/**
* Validate event order : Child's visibility should come before progress completed event
*/
@Test
public void test_OpenWindow_Progress_Listener_ValidateEventOrder() {
AtomicBoolean windowOpenFired = new AtomicBoolean(false);
AtomicBoolean childCompleted = new AtomicBoolean(false);
AtomicBoolean visibilityShowed = new AtomicBoolean(false);
Shell childShell = new Shell(shell, SWT.None);
childShell.setText("Child shell");
childShell.setLayout(new FillLayout());
final Browser browserChild = new Browser(childShell, SWT.NONE);
browser.addOpenWindowListener(event -> {
event.browser = browserChild;
// Validate event order.
assertFalse("OpenWindowListenr should have been fired first", visibilityShowed.get() || childCompleted.get());
windowOpenFired.set(true);
});
browserChild.addVisibilityWindowListener(showAdapter(event -> {
childShell.open();
assertTrue("Child Visibility.show should have fired before progress completed", // Validate event order.
windowOpenFired.get() && !childCompleted.get());
visibilityShowed.set(true);
}));
browserChild.addProgressListener(completedAdapter(event -> {
assertTrue("Child's Progress Completed before parent's expected events", // Validate event order.
windowOpenFired.get() && visibilityShowed.get());
// Triggers test to finish.
childCompleted.set(true);
browserChild.setText("Child Browser!");
}));
shell.open();
browser.setText("<html>" + "<script type='text/javascript'>" + // opens child window.
"var newWin = window.open();" + "</script>\n" + "<body>This test uses javascript to open a new window.</body></html>");
boolean passed = waitForPassCondition(() -> windowOpenFired.get() && visibilityShowed.get() && childCompleted.get());
String errMsg = "\nTest timed out." + "\nExpected true for the below, but have:" + "\nWindoOpenFired:" + windowOpenFired.get() + "\nVisibilityShowed:" + visibilityShowed.get() + "\nChildCompleted:" + childCompleted.get();
assertTrue(errMsg, 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_StatusTextListener_hoverMouseOverLink.
/**
* Test if hovering over a hyperlink triggers status Text change listener.
* Logic:
* 1) Create a page that has a hyper link (covering the whole page)
* 2) Move shell to top left corner
* 3) Upon compleation of page load, move cursor across whole shell.
* (Note, in current jUnit, browser sometimes only takes up half the shell).
* 4) StatusTextListener should get triggered. Test passes.
* 5) Else timeout & fail.
*
* Set variable "debug_show_browser" to true to see this being performed at human-observable speed.
*
* Note: Historically one could execute some javascript to change status bar (window.status=txt).
* But most browsers don't support this anymore. Only hovering over a hyperlink changes status.
*
* StatusTextListener may be triggerd upon page load also. So this test can pass if
* a page load sets the status text (on older browsers) or passes when the mouse hovers
* over the hyperlink (newer Webkit2+) browser.
*/
@Test
public void test_StatusTextListener_hoverMouseOverLink() {
AtomicBoolean statusChanged = new AtomicBoolean(false);
int size = 500;
// 1) Create a page that has a hyper link (covering the whole page)
Browser browser = new Browser(shell, SWT.NONE);
StringBuilder longhtml = new StringBuilder();
for (int i = 0; i < 200; i++) {
longhtml.append("text text text text text text text text text text text text text text text text text text text text text text text text<br>");
}
browser.setText("<a href='http://localhost'>" + longhtml + "</a>");
// 2) Move shell to top left corner
shell.setLocation(0, 0);
shell.setSize(size, size);
browser.addProgressListener(completedAdapter(event -> {
// * 3) Upon compleation of page load, move cursor across whole shell.
// * (Note, in current jUnit, browser sometimes only takes up half the shell).
Display display = event.display;
Point cachedLocation = display.getCursorLocation();
display.setCursorLocation(20, 10);
browser.getBounds();
for (int i = 0; i < size; i = i + 5) {
display.setCursorLocation(i, i);
// Move mouse slower during debug.
waitForMilliseconds(debug_show_browser ? 3 : 1);
}
// for convenience of developer. Not needed for test.
display.setCursorLocation(cachedLocation);
}));
browser.addStatusTextListener(event -> {
statusChanged.set(true);
});
shell.open();
boolean passed = waitForPassCondition(() -> statusChanged.get());
String msg = "Mouse movent over text was suppose to trigger StatusTextListener. But it didn't";
assertTrue(msg, 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_afterPageReload.
/**
* Test that a callback works even after a new page is loaded.
* I.e, BrowserFunctions should have to be re-initialized after a page load.
*
* Logic:
* - load a page.
* - Register java callback.
* - call java callback from javascript. (exec)
*
* - java callback instantiates new page load.
* - new page load triggers 'completed' listener
* - completed listener calls the registered function again.
*
* - once regiseterd function is called a 2nd time, it sets the test to pass.
*/
@Test
public void test_BrowserFunction_callback_afterPageReload() {
// 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);
AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
AtomicInteger callCount = 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) {
if (callCount.get() == 0) {
callCount.set(1);
browser.setText("2nd page load");
} else {
javaCallbackExecuted.set(true);
}
return null;
}
}
browser.setText("1st (initial) page load");
new JavascriptCallback(browser, "jsCallbackToJava");
browser.execute("jsCallbackToJava()");
// see if function still works after a page change:
browser.addProgressListener(completedAdapter(e -> browser.execute("jsCallbackToJava()")));
shell.open();
boolean passed = waitForPassCondition(() -> javaCallbackExecuted.get());
String message = "A javascript callback should work after a page has been reloaded. 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_BrowserFunction_callback.
/**
* Test that javascript can call java.
* loosely based on Snippet307.
*/
@Test
public void test_BrowserFunction_callback() {
// 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);
AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
class JavascriptCallback extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
javaCallbackExecuted.set(true);
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.
" jsCallbackToJava()\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(() -> javaCallbackExecuted.get());
String message = "Java failed to get a callback from javascript. Test timed out";
assertTrue(message, passed);
}
Aggregations