diff options
| author | Leo Ufimtsev | 2017-01-31 21:09:53 +0000 |
|---|---|---|
| committer | Alexander Kurtakov | 2017-02-06 15:06:31 +0000 |
| commit | 8002c6f096b6fd4250b49a56c189b223ab656184 (patch) | |
| tree | b7ad99112188fb44395c4fb01273fa7dd43f0cb6 | |
| parent | 722e7419d87fb21ca7b0c953a3423e1c57e6e394 (diff) | |
| download | eclipse.platform.swt-8002c6f096b6fd4250b49a56c189b223ab656184.tar.gz eclipse.platform.swt-8002c6f096b6fd4250b49a56c189b223ab656184.tar.xz eclipse.platform.swt-8002c6f096b6fd4250b49a56c189b223ab656184.zip | |
Bug 509615 Replace outdated Browser9 test
BrowserTest9 uses some deprecated javascript in it's browser9.html:
window.status=text
This function sets the text at the bottom of a browser window bar (e.g
this also happens when you hover over a link, it show's it's location).
This api has been disabled in most modern browsers because *1 it's a
security vulnerability. (It can produce a fake status bar that
miss-leads what a link links to).
However, statusTextListener can still be triggered if you hover your
mouse over a link.
I deleted the old test case and wrote two new test cases to replace the
deprecated test case. The two new test cases test the equivalent of the
old one.
- Tests status listener by hovering mouse over a link
- Tests execute by closing a window with javascript and observing if it
was actually closed.
Works on Webkit1 and Webkit2.
Patch set 3:
- removed redundant sysout.
[1] http://stackoverflow.com/questions/3273320/javascript-window-status
Change-Id: I601566d9c984e7a5c26312b443b90153896e9073
Signed-off-by: Leo Ufimtsev <lufimtse@redhat.com>
6 files changed, 105 insertions, 152 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java index 048c002179..79d740fbf3 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java +++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java @@ -76,7 +76,7 @@ class WebKit extends WebBrowser { static final int NAVIGATION_POLICY_DECISION_REQUESTED = 3; static final int NOTIFY_TITLE = 4; static final int POPULATE_POPUP = 5; - static final int STATUS_BAR_TEXT_CHANGED = 6; + static final int STATUS_BAR_TEXT_CHANGED = 6; // webkit1 only. static final int CREATE_WEB_VIEW = 7; static final int WEB_VIEW_READY = 8; static final int NOTIFY_LOAD_STATUS = 9; @@ -516,7 +516,7 @@ long /*int*/ webViewProc (long /*int*/ handle, long /*int*/ arg0, long /*int*/ u case NOTIFY_PROGRESS: return webkit_notify_progress (handle, arg0); case NOTIFY_TITLE: return webkit_notify_title (handle, arg0); case POPULATE_POPUP: return webkit_populate_popup (handle, arg0); - case STATUS_BAR_TEXT_CHANGED: return webkit_status_bar_text_changed (handle, arg0); + case STATUS_BAR_TEXT_CHANGED: return webkit_status_bar_text_changed (handle, arg0); // Webkit1 only. case AUTHENTICATE: return webkit_authenticate (handle, arg0); default: return 0; } @@ -2253,6 +2253,14 @@ long /*int*/ webkit_resource_request_starting (long /*int*/ web_view, long /*int return 0; } +/** + * Webkit1 only. + * Normally triggered by javascript that runs "window.status=txt". + * + * On webkit2 this signal doesn't exist anymore. + * In general, window.status=text is not supported on most newer browsers anymore. + * status bar now only changes when you hover you mouse over it. + */ long /*int*/ webkit_status_bar_text_changed (long /*int*/ web_view, long /*int*/ text) { int length = OS.strlen (text); byte[] bytes = new byte[length]; diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java index 325302bffb..6a429efdb1 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java +++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java @@ -69,6 +69,8 @@ public class WebKitGTK extends C { public static final byte[] populate_popup = ascii ("populate-popup"); // $NON-NLS-1$ public static final byte[] resource_request_starting = ascii ("resource-request-starting"); // $NON-NLS-1$ public static final byte[] resource_load_started = ascii ("resource-load-started"); // $NON-NLS-1$ + /** Webkit1 only. On webkit2 & newer browsers 'window.status=txt' has no effect anymore. + * Status bar only updated when you hover mouse over hyperlink.*/ public static final byte[] status_bar_text_changed = ascii ("status-bar-text-changed"); // $NON-NLS-1$ public static final byte[] web_view_ready = ascii ("web-view-ready"); // $NON-NLS-1$ public static final byte[] ready_to_show = ascii ("ready-to-show"); // $NON-NLS-1$ diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java index 08df9ba711..87eaea190a 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java @@ -39,6 +39,7 @@ import org.eclipse.swt.browser.TitleListener; import org.eclipse.swt.browser.VisibilityWindowAdapter; import org.eclipse.swt.browser.VisibilityWindowListener; import org.eclipse.swt.browser.WindowEvent; +import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; @@ -494,14 +495,75 @@ public void test_listener_changedLorg_eclipse_swt_browser_ProgressEvent() { shell.close(); } +/** + * 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 "browser_debug" 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_listener_changedLorg_eclipse_swt_browser_StatusTextEvent() { - Display display = Display.getCurrent(); - Shell shell = new Shell(display); + 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(new ProgressListener() { + @Override + public void completed(ProgressEvent 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++) { + display.setCursorLocation(i, i); + runLoopTimer(browser_debug ? 3 : 0); + } + display.setCursorLocation(cachedLocation); // for convenience of developer. Not needed for test. + + } + @Override + public void changed(ProgressEvent event) {} + }); + browser.addStatusTextListener(event -> { + statusChanged.set(true); }); - shell.close(); + + shell.open(); + for (int i = 0; i < (loopMultipier * secondsToWaitTillFail); i++) { // Wait up to seconds before declaring test as failed. + runLoopTimer(waitMS); + if (statusChanged.get()) { + return; //passed. + } + } + //5) Else timeout & fail. + fail(); } @Test @@ -690,6 +752,34 @@ public void test_stop() { } /** + * Test execute and windowCloseListener. + * Close listener used to tell if execute actually worked in some meaningful way. + */ +@Test +public void test_execute_and_closeListener () { + AtomicBoolean hasClosed = new AtomicBoolean(false); + + browser.setText("You should not see this page, it should have been closed by javascript"); + browser.addCloseWindowListener(e -> { + hasClosed.set(true); + }); + + browser.execute("window.close()"); + + shell.open(); + for (int i = 0; i < (loopMultipier * secondsToWaitTillFail); i++) { // Wait up to seconds before declaring test as failed. + runLoopTimer(waitMS); + if (hasClosed.get()) { + disposedIntentionally = true; + return; // passed. + } + } + fail("Either browser.execute() did not work (if you still see the html page) or closeListener Was not triggered if " + + "browser looks disposed, but test still fails."); +} + + +/** * Test the evaluate() api that returns a String type. Functionality based on Snippet308. * Only wait till success. Otherwise timeout after 3 seconds. */ diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/browser/Browser9_execute_fromFile.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/browser/Browser9_execute_fromFile.java deleted file mode 100644 index c94f7b21cd..0000000000 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/browser/Browser9_execute_fromFile.java +++ /dev/null @@ -1,125 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.swt.tests.junit.browser; - -import java.util.concurrent.atomic.AtomicBoolean; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.browser.Browser; -import org.eclipse.swt.browser.ProgressEvent; -import org.eclipse.swt.browser.ProgressListener; -import org.eclipse.swt.layout.FillLayout; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; - -/** - * Test if execute works on html that is loaded from file. - * - */ -public class Browser9_execute_fromFile { - public static boolean verbose = false; - public static boolean passed = false; - - static String html[] = {"browser9.html"}; - static String script[] = { - "changeStatus('userAgent: ' + navigator.userAgent);"}; - static String status[] = {"userAgent: "}; - - public static boolean test(String url, final String script, final String status) { - if (verbose) System.out.println("Javascript - verify execute("+script+") works on a static HTML file "+url); - passed = false; - final Display display = new Display(); - final Shell shell = new Shell(display); - shell.setLayout(new FillLayout()); - final Browser browser = new Browser(shell, SWT.NONE); - browser.addStatusTextListener(event -> browser.setData("query", event.text)); - final AtomicBoolean finished = new AtomicBoolean(); // initially false. - browser.addProgressListener(new ProgressListener() { - @Override - public void changed(ProgressEvent event) { - } - @Override - public void completed(ProgressEvent event) { - boolean result = browser.execute(script); - if (!result) { - if (verbose) System.out.println("execute failed for "+script); - passed = false; - finished.set(true); - return; - } - /* Script may additionally set the Status value */ - String value = (String)browser.getData("query"); - System.out.println("Browser9: window.status after script: "+value); - if (value != null) { - passed = value.startsWith(status); - } else { - if (verbose) System.out.println("Failure - expected "+script+", not "+value); - } - finished.set(true); - } - }); - shell.open(); - browser.setUrl(url); - - for (int i = 0; i < 5 && !passed && !finished.get(); i++) { - runLoopTimer(display, shell, 2); - if (!display.isDisposed()) - display.readAndDispatch (); - } - display.dispose(); - return passed; - } - - static boolean runLoopTimer(final Display display, final Shell shell, final int seconds) { - final boolean[] timeout = {false}; - new Thread() { - @Override - public void run() { - try { - for (int i = 0; i < seconds; i++) { - Thread.sleep(1000); - if (display.isDisposed() || shell.isDisposed()) return; - } - } - catch (Exception e) {} - timeout[0] = true; - /* wake up the event loop */ - if (!display.isDisposed()) { - display.asyncExec(() -> { - if (!shell.isDisposed()) shell.redraw(); - }); - } - } - }.start(); - while (!timeout[0] && !shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); - return timeout[0]; - } - - public static boolean test() { - int fail = 0; - - String pluginPath = System.getProperty("PLUGIN_PATH"); - if (verbose) System.out.println("PLUGIN_PATH <"+pluginPath+">"); - String url; - for (int i = 0; i < html.length; i++) { - if (pluginPath == null) url = Browser9_execute_fromFile.class.getClassLoader().getResource(html[i]).toString(); - else url = pluginPath + "/data/"+html[i]; - boolean result = test(url, script[i], status[i]); - if (verbose) System.out.print(result ? "." : "E"); - if (!result) fail++; - } - return fail == 0; - } - - public static void main(String[] argv) { - System.out.println("\r\nTests Finished. SUCCESS: "+test()); - } -} diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/browser/Test_BrowserSuite.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/browser/Test_BrowserSuite.java index 5bbd5dcf58..165e05c361 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/browser/Test_BrowserSuite.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/browser/Test_BrowserSuite.java @@ -93,13 +93,6 @@ public void testBrowser8() { manualTearDown(); } -@Test -public void testBrowser9() { - manualSetUp(); - assertTrue(Browser9_execute_fromFile.test()); - manualTearDown(); -} - //Bug 509658. Display.dispose can cause webkit1 to crash in some cases. // Thus we have a situation where the setup causes a crash even in test cases // that are marked not to be run on hudson. This is very confusing. diff --git a/tests/org.eclipse.swt.tests/data/browser9.html b/tests/org.eclipse.swt.tests/data/browser9.html deleted file mode 100644 index bfa06ee906..0000000000 --- a/tests/org.eclipse.swt.tests/data/browser9.html +++ /dev/null @@ -1,15 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> -<html> -<head> -<title>Hello Title</title> -<script type="text/javascript"> -function changeStatus(txt) { - document.bgColor = "yellow"; - window.status = txt; -} -</script> -</head> -<body> -Hello body -</body> -</html> |
