1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.javafx.scene.web;
  27 
  28 import java.io.File;
  29 import static java.lang.String.format;
  30 import java.net.HttpURLConnection;
  31 import java.util.ArrayList;
  32 import java.util.Random;
  33 import java.util.concurrent.Callable;
  34 import javafx.application.Platform;
  35 import javafx.beans.value.ChangeListener;
  36 import javafx.beans.value.ObservableValue;
  37 import javafx.concurrent.Worker.State;
  38 import javafx.scene.canvas.Canvas;
  39 import javafx.scene.canvas.GraphicsContext;
  40 import javafx.scene.paint.LinearGradient;
  41 import javafx.scene.web.WebEngine;
  42 import javafx.scene.web.WebView;
  43 import static org.junit.Assert.assertEquals;
  44 import static org.junit.Assert.assertNotNull;
  45 import static org.junit.Assert.fail;
  46 import org.junit.Test;
  47 import org.w3c.dom.Document;
  48 
  49 public class MiscellaneousTest extends TestBase {
  50 
  51     @Test public void testNoEffectOnFollowRedirects() {
  52         assertEquals("Unexpected HttpURLConnection.getFollowRedirects() result",
  53                 true, HttpURLConnection.getFollowRedirects());
  54         load("test/html/ipsum.html");
  55         assertEquals("Unexpected HttpURLConnection.getFollowRedirects() result",
  56                 true, HttpURLConnection.getFollowRedirects());
  57     }
  58 
  59     @Test public void testRT22458() throws Exception {
  60         final WebEngine webEngine = createWebEngine();
  61         Platform.runLater(() -> {
  62             webEngine.load(format("file://%d.ajax.googleapis.com/ajax",
  63                                   new Random().nextInt()));
  64         });
  65         Thread.sleep(200);
  66         long startTime = System.currentTimeMillis();
  67         DummyClass.dummyField++;
  68         long time = System.currentTimeMillis() - startTime;
  69         if (time > 2000) {
  70             fail(format("DummyClass took %f seconds to load", time / 1000.));
  71         }
  72     }
  73 
  74     private static final class DummyClass {
  75         private static int dummyField;
  76     }
  77 
  78     @org.junit.Ignore
  79     @Test public void testRT30835() throws Exception {
  80         class Record {
  81             private final Document document;
  82             private final String location;
  83             public Record(Document document, String location) {
  84                 this.document = document;
  85                 this.location = location;
  86             }
  87         }
  88         final ArrayList<Record> records = new ArrayList<Record>();
  89         ChangeListener<State> listener = (ov, oldValue, newValue) -> {
  90             if (newValue == State.SUCCEEDED) {
  91                 records.add(new Record(
  92                         getEngine().getDocument(),
  93                         getEngine().getLocation()));
  94             }
  95         };
  96         submit(() -> {
  97             getEngine().getLoadWorker().stateProperty().addListener(listener);
  98         });
  99         String location = new File("src/test/resources/test/html/RT30835.html")
 100                 .toURI().toASCIIString().replaceAll("^file:/", "file:///");
 101         load(location);
 102         assertEquals(1, records.size());
 103         assertNotNull(records.get(0).document);
 104         assertEquals(location, records.get(0).location);
 105     }
 106 
 107     @Test public void testRT26306() {
 108         loadContent(
 109                 "<script language='javascript'>\n" +
 110                 "var s = '0123456789abcdef';\n" +
 111                 "while (true) {\n" +
 112                 "    alert(s.length);\n" +
 113                 "    s = s + s;\n" +
 114                 "}\n" +
 115                 "</script>");
 116     }
 117 
 118     @Test public void testWebViewWithoutSceneGraph() {
 119         submit(() -> {
 120              WebEngine engine = new WebView().getEngine();
 121              engine.getLoadWorker().stateProperty().addListener(
 122                     (observable, oldValue, newValue) -> {
 123                         if (State.SUCCEEDED == newValue) {
 124                             engine.executeScript(
 125                                 "window.scrollTo" +
 126                                 "(0, document.documentElement.scrollHeight)");
 127                         }
 128                     });
 129              engine.loadContent("<body> <a href=#>hello</a></body>");
 130         });
 131     }
 132 
 133     // JDK-8133775
 134     @Test(expected = IllegalStateException.class) public void testDOMObjectThreadOwnership() {
 135           class IllegalStateExceptionChecker {
 136               public Object resultObject;
 137               public void start() {
 138                  WebEngine engine = new WebEngine();
 139                  // Get DOM object from JavaFX Application Thread.
 140                  resultObject = engine.executeScript("document.createElement('span')");
 141               }
 142            }
 143            IllegalStateExceptionChecker obj = new IllegalStateExceptionChecker();
 144            submit(obj::start);
 145            // Try accessing the resultObject created in JavaFX Application Thread
 146            // from someother thread. It should throw an exception.
 147            obj.resultObject.toString();
 148      }
 149 
 150     // JDK-8155903
 151     @Test public void testGCfillWithLinearGradient() {
 152         submit(()->{
 153             Canvas canvas = new Canvas(100, 100);
 154             GraphicsContext gc = canvas.getGraphicsContext2D();
 155             LinearGradient lg = LinearGradient.valueOf(
 156                   "linear-gradient(white 0%, black 1%, red 1%, green 2%,"
 157                 + "yellow 3%, blue 4%, red 5%, red 6%, red 7%, red 8%, red 9%,"
 158                 + "red 10%, green 70%)");
 159             gc.setFill(lg);
 160             gc.fill();
 161             // Force paint
 162             assertNotNull(canvas.snapshot(null, null));
 163         });
 164     }
 165 
 166     private WebEngine createWebEngine() {
 167         return submit(() -> new WebEngine());
 168     }
 169 }