Search in sources :

Example 1 with StatusTextListener

use of org.eclipse.swt.browser.StatusTextListener 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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StatusTextListener(org.eclipse.swt.browser.StatusTextListener) URL(java.net.URL) Assume.assumeFalse(org.junit.Assume.assumeFalse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationAdapter(org.eclipse.swt.browser.LocationAdapter) CloseWindowListener(org.eclipse.swt.browser.CloseWindowListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) TitleListener(org.eclipse.swt.browser.TitleListener) VisibilityWindowAdapter(org.eclipse.swt.browser.VisibilityWindowAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Point(org.eclipse.swt.graphics.Point) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) TestName(org.junit.rules.TestName) LocationListener.changedAdapter(org.eclipse.swt.browser.LocationListener.changedAdapter) VisibilityWindowListener.showAdapter(org.eclipse.swt.browser.VisibilityWindowListener.showAdapter) WindowEvent(org.eclipse.swt.browser.WindowEvent) LocationListener(org.eclipse.swt.browser.LocationListener) ProgressListener.completedAdapter(org.eclipse.swt.browser.ProgressListener.completedAdapter) Assert.fail(org.junit.Assert.fail) SWTException(org.eclipse.swt.SWTException) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) FillLayout(org.eclipse.swt.layout.FillLayout) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Before(org.junit.Before) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Browser(org.eclipse.swt.browser.Browser) Shell(org.eclipse.swt.widgets.Shell) MalformedURLException(java.net.MalformedURLException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) LocationListener.changingAdapter(org.eclipse.swt.browser.LocationListener.changingAdapter) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) LocationEvent(org.eclipse.swt.browser.LocationEvent) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Browser(org.eclipse.swt.browser.Browser) Display(org.eclipse.swt.widgets.Display) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Instant (java.time.Instant)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)1 Supplier (java.util.function.Supplier)1 SWT (org.eclipse.swt.SWT)1 SWTException (org.eclipse.swt.SWTException)1 Browser (org.eclipse.swt.browser.Browser)1 BrowserFunction (org.eclipse.swt.browser.BrowserFunction)1 CloseWindowListener (org.eclipse.swt.browser.CloseWindowListener)1 LocationAdapter (org.eclipse.swt.browser.LocationAdapter)1 LocationEvent (org.eclipse.swt.browser.LocationEvent)1 LocationListener (org.eclipse.swt.browser.LocationListener)1 LocationListener.changedAdapter (org.eclipse.swt.browser.LocationListener.changedAdapter)1