/* * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package test.javafx.scene.web; import java.io.File; import static java.lang.String.format; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.Random; import java.util.concurrent.Callable; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.concurrent.Worker.State; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.LinearGradient; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import org.junit.Test; import org.w3c.dom.Document; public class MiscellaneousTest extends TestBase { @Test public void testNoEffectOnFollowRedirects() { assertEquals("Unexpected HttpURLConnection.getFollowRedirects() result", true, HttpURLConnection.getFollowRedirects()); load("test/html/ipsum.html"); assertEquals("Unexpected HttpURLConnection.getFollowRedirects() result", true, HttpURLConnection.getFollowRedirects()); } @Test public void testRT22458() throws Exception { final WebEngine webEngine = createWebEngine(); Platform.runLater(() -> { webEngine.load(format("file://%d.ajax.googleapis.com/ajax", new Random().nextInt())); }); Thread.sleep(200); long startTime = System.currentTimeMillis(); DummyClass.dummyField++; long time = System.currentTimeMillis() - startTime; if (time > 2000) { fail(format("DummyClass took %f seconds to load", time / 1000.)); } } private static final class DummyClass { private static int dummyField; } @org.junit.Ignore @Test public void testRT30835() throws Exception { class Record { private final Document document; private final String location; public Record(Document document, String location) { this.document = document; this.location = location; } } final ArrayList records = new ArrayList(); ChangeListener listener = (ov, oldValue, newValue) -> { if (newValue == State.SUCCEEDED) { records.add(new Record( getEngine().getDocument(), getEngine().getLocation())); } }; submit(() -> { getEngine().getLoadWorker().stateProperty().addListener(listener); }); String location = new File("src/test/resources/test/html/RT30835.html") .toURI().toASCIIString().replaceAll("^file:/", "file:///"); load(location); assertEquals(1, records.size()); assertNotNull(records.get(0).document); assertEquals(location, records.get(0).location); } @Test public void testRT26306() { loadContent( ""); } @Test public void testWebViewWithoutSceneGraph() { submit(() -> { WebEngine engine = new WebView().getEngine(); engine.getLoadWorker().stateProperty().addListener( (observable, oldValue, newValue) -> { if (State.SUCCEEDED == newValue) { engine.executeScript( "window.scrollTo" + "(0, document.documentElement.scrollHeight)"); } }); engine.loadContent(" hello"); }); } // JDK-8133775 @Test(expected = IllegalStateException.class) public void testDOMObjectThreadOwnership() { class IllegalStateExceptionChecker { public Object resultObject; public void start() { WebEngine engine = new WebEngine(); // Get DOM object from JavaFX Application Thread. resultObject = engine.executeScript("document.createElement('span')"); } } IllegalStateExceptionChecker obj = new IllegalStateExceptionChecker(); submit(obj::start); // Try accessing the resultObject created in JavaFX Application Thread // from someother thread. It should throw an exception. obj.resultObject.toString(); } // JDK-8155903 @Test public void testGCfillWithLinearGradient() { submit(()->{ Canvas canvas = new Canvas(100, 100); GraphicsContext gc = canvas.getGraphicsContext2D(); LinearGradient lg = LinearGradient.valueOf( "linear-gradient(white 0%, black 1%, red 1%, green 2%," + "yellow 3%, blue 4%, red 5%, red 6%, red 7%, red 8%, red 9%," + "red 10%, green 70%)"); gc.setFill(lg); gc.fill(); // Force paint assertNotNull(canvas.snapshot(null, null)); }); } private WebEngine createWebEngine() { return submit(() -> new WebEngine()); } }