Search in sources :

Example 26 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_OpenWindowListener_openHasValidEventDetails.

@Test
public void test_OpenWindowListener_openHasValidEventDetails() {
    AtomicBoolean openFiredCorrectly = new AtomicBoolean(false);
    final Browser browserChild = new Browser(shell, SWT.None);
    browser.addOpenWindowListener(event -> {
        assertTrue("Expected Browser1 instance, but have another instance", (Browser) event.widget == browser);
        assertTrue("Expected event.browser to be null", event.browser == null);
        openFiredCorrectly.set(true);
        event.browser = browserChild;
    });
    shell.open();
    browser.setText("<html><script type='text/javascript'>window.open()</script>\n" + "<body>This test uses javascript to open a new window.</body></html>");
    boolean passed = waitForPassCondition(() -> openFiredCorrectly.get());
    assertTrue("Test timed out. OpenWindow event not fired.", passed);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 27 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_TitleListener_addListener_closeShell.

@Test
public void test_TitleListener_addListener_closeShell() {
    Display display = Display.getCurrent();
    Shell shell = new Shell(display);
    Browser browser = new Browser(shell, SWT.NONE);
    browser.addTitleListener(event -> {
    });
    shell.close();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 28 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_ProgressListener_newProgressAdapter_closeShell.

@Test
public void test_ProgressListener_newProgressAdapter_closeShell() {
    Display display = Display.getCurrent();
    Shell shell = new Shell(display);
    Browser browser = new Browser(shell, SWT.NONE);
    browser.addProgressListener(new ProgressAdapter() {
    });
    shell.close();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Display(org.eclipse.swt.widgets.Display) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 29 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_setJavascriptEnabled_multipleInstances.

/**
 * Check that if there are two browser instances, turning off JS in one instance doesn't turn off JS in the other instance.
 */
@Test
public void test_setJavascriptEnabled_multipleInstances() {
    AtomicInteger pageLoadCount = new AtomicInteger(1);
    AtomicInteger pageLoadCountSecondInstance = new AtomicInteger(1);
    AtomicBoolean instanceOneFinishedCorrectly = new AtomicBoolean(false);
    AtomicBoolean instanceTwoFinishedCorrectly = new AtomicBoolean(false);
    Browser browserSecondInsance = new Browser(shell, SWT.None);
    browser.addProgressListener(completedAdapter(event -> {
        if (pageLoadCount.get() == 1) {
            browser.setJavascriptEnabled(false);
            pageLoadCount.set(2);
            browser.setText("First instance, second page (with javascript turned off)");
            pageLoadCountSecondInstance.set(2);
            browserSecondInsance.setText("Second instance, second page (javascript execution not changed)");
        } else if (pageLoadCount.get() == 2) {
            pageLoadCount.set(3);
            Boolean shouldBeNull = (Boolean) browser.evaluate("return true");
            assertNull("1) Evaluate execution should be null, but 'true was returned'", shouldBeNull);
            instanceOneFinishedCorrectly.set(true);
        }
    }));
    browserSecondInsance.addProgressListener(new ProgressAdapter() {

        @Override
        public void completed(ProgressEvent event) {
            if (pageLoadCountSecondInstance.get() == 2) {
                pageLoadCountSecondInstance.set(3);
                Boolean shouldBeTrue = (Boolean) browserSecondInsance.evaluate("return true");
                assertTrue("2) Javascript should be executable in second instance (as javascript was not turned off), but it was not. " + "Expected:'someStr', Actual:" + shouldBeTrue, shouldBeTrue);
                instanceTwoFinishedCorrectly.set(true);
            }
        }
    });
    browser.setText("First Instance, first page");
    browserSecondInsance.setText("Second instance, first page");
    shell.open();
    boolean passed = waitForPassCondition(() -> {
        return instanceOneFinishedCorrectly.get() && instanceTwoFinishedCorrectly.get();
    });
    String message = "3) Test timed out. Debug Info:\n" + "InstanceOneFinishedCorrectly: " + instanceOneFinishedCorrectly.get() + "\n" + "InstanceTwoFinishedCorrectly: " + instanceTwoFinishedCorrectly.get() + "\n" + "Instance 1 & 2 page counts: " + pageLoadCount.get() + " & " + pageLoadCountSecondInstance.get();
    assertTrue(message, 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) Browser(org.eclipse.swt.browser.Browser) Test(org.junit.Test)

Example 30 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_OpenWindowListener_open_ChildPopup.

/**
 * Test that a script 'window.open()' opens a child popup shell.
 */
@Test
public void test_OpenWindowListener_open_ChildPopup() {
    AtomicBoolean childCompleted = 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;
    });
    browserChild.addVisibilityWindowListener(showAdapter(event -> {
        childShell.open();
        browserChild.setText("Child Browser");
    }));
    // Triggers test to finish.
    browserChild.addProgressListener(completedAdapter(event -> childCompleted.set(true)));
    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(() -> childCompleted.get());
    String errMsg = "\nTest timed out.";
    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