Search in sources :

Example 41 with Browser

use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.

the class BrowserTab method createExampleWidgets.

/**
 * Creates the "Example" widgets.
 */
@Override
void createExampleWidgets() {
    /* Compute the widget style */
    int style = getDefaultStyle();
    if (borderButton.getSelection())
        style |= SWT.BORDER;
    if (webKitButton.getSelection())
        style |= SWT.WEBKIT;
    /* Create the example widgets */
    try {
        browser = new Browser(browserGroup, style);
    } catch (SWTError e) {
        // Probably missing browser
        try {
            browser = new Browser(browserGroup, style & ~(SWT.WEBKIT));
        } catch (SWTError e2) {
            // Unsupported platform
            errorMessage = e.getMessage();
            return;
        }
        MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
        String resourceString = "WebKitNotFound";
        dialog.setMessage(ControlExample.getResourceString(resourceString, e.getMessage()));
        dialog.open();
    }
    if (lastUrl != null) {
        browser.setUrl(lastUrl);
    } else if (lastText != null) {
        browser.setText(lastText);
    } else {
        StringBuilder sb = new StringBuilder(300);
        try (InputStream htmlStream = ControlExample.class.getResourceAsStream("browser-content.html");
            BufferedReader br = new BufferedReader(new InputStreamReader(htmlStream))) {
            int read = 0;
            while ((read = br.read()) != -1) sb.append((char) read);
        } catch (IOException e) {
            log(e.getMessage());
        }
        String text = sb.toString();
        browser.setText(text);
    }
    lastText = lastUrl = null;
}
Also used : SWTError(org.eclipse.swt.SWTError) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Browser(org.eclipse.swt.browser.Browser) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 42 with Browser

use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.

the class Bug497705_BrowserDragDetect method main.

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Browser b = new Browser(shell, SWT.BORDER);
    b.setText("TEXT");
    final Label label2 = new Label(shell, SWT.BORDER);
    // doesn't seem to have an effect.
    setDrag(b);
    setDrop(label2);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Label(org.eclipse.swt.widgets.Label) FillLayout(org.eclipse.swt.layout.FillLayout) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser)

Example 43 with Browser

use of org.eclipse.swt.browser.Browser in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_browser_Browser method test_LocationListener_adapter_closeShell.

@Test
public void test_LocationListener_adapter_closeShell() {
    Display display = Display.getCurrent();
    Shell shell = new Shell(display);
    Browser browser = new Browser(shell, SWT.NONE);
    LocationAdapter adapter = new LocationAdapter() {
    };
    // shouldn't throw
    browser.addLocationListener(adapter);
    shell.close();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) LocationAdapter(org.eclipse.swt.browser.LocationAdapter) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 44 with Browser

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);
}
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) Shell(org.eclipse.swt.widgets.Shell) AtomicReference(java.util.concurrent.atomic.AtomicReference) Point(org.eclipse.swt.graphics.Point) FillLayout(org.eclipse.swt.layout.FillLayout) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 45 with Browser

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);
}
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) Shell(org.eclipse.swt.widgets.Shell) FillLayout(org.eclipse.swt.layout.FillLayout) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Aggregations

Browser (org.eclipse.swt.browser.Browser)62 Shell (org.eclipse.swt.widgets.Shell)26 Display (org.eclipse.swt.widgets.Display)25 Test (org.junit.Test)22 FillLayout (org.eclipse.swt.layout.FillLayout)19 GridData (org.eclipse.swt.layout.GridData)16 IOException (java.io.IOException)13 ProgressEvent (org.eclipse.swt.browser.ProgressEvent)13 Composite (org.eclipse.swt.widgets.Composite)13 BrowserFunction (org.eclipse.swt.browser.BrowserFunction)12 LocationEvent (org.eclipse.swt.browser.LocationEvent)12 URL (java.net.URL)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 ProgressListener (org.eclipse.swt.browser.ProgressListener)11 WindowEvent (org.eclipse.swt.browser.WindowEvent)11 GridLayout (org.eclipse.swt.layout.GridLayout)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 LocationAdapter (org.eclipse.swt.browser.LocationAdapter)10 LocationListener (org.eclipse.swt.browser.LocationListener)10 ProgressAdapter (org.eclipse.swt.browser.ProgressAdapter)10