1 /*
   2  * Copyright (c) 2004, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug      4665566 4855876 7025314 8012375 8015997 8016328 8024756
  27  * @summary  Verify that the output has the right javascript.
  28  * @author   jamieh
  29  * @library  ../lib
  30  * @modules jdk.javadoc
  31  * @build    JavadocTester
  32  * @run main TestJavascript
  33  */
  34 
  35 public class TestJavascript extends JavadocTester {
  36 
  37     public static void main(String... args) throws Exception {
  38         TestJavascript tester = new TestJavascript();
  39         tester.runTests();
  40     }
  41 
  42     @Test
  43     void test() {
  44         javadoc("-d", "out",
  45                 "-sourcepath", testSrc,
  46                 "pkg", testSrc("TestJavascript.java"));
  47         checkExit(Exit.OK);
  48 
  49         checkOutput("pkg/C.html", true,
  50                 "<a href=\"../index.html?pkg/C.html\" target=\"_top\">Frames</a>");
  51 
  52         checkOutput("TestJavascript.html", true,
  53                 "<a href=\"index.html?TestJavascript.html\" target=\"_top\">Frames</a>");
  54 
  55         checkOutput("index.html", true,
  56                 "<script type=\"text/javascript\">\n"
  57                 + "    targetPage = \"\" + window.location.search;\n"
  58                 + "    if (targetPage != \"\" && targetPage != \"undefined\")\n"
  59                 + "        targetPage = targetPage.substring(1);\n"
  60                 + "    if (targetPage.indexOf(\":\") != -1 || (targetPage != \"\" && !validURL(targetPage)))\n"
  61                 + "        targetPage = \"undefined\";\n"
  62                 + "    function validURL(url) {\n"
  63                 + "        try {\n"
  64                 + "            url = decodeURIComponent(url);\n"
  65                 + "        }\n"
  66                 + "        catch (error) {\n"
  67                 + "            return false;\n"
  68                 + "        }\n"
  69                 + "        var pos = url.indexOf(\".html\");\n"
  70                 + "        if (pos == -1 || pos != url.length - 5)\n"
  71                 + "            return false;\n"
  72                 + "        var allowNumber = false;\n"
  73                 + "        var allowSep = false;\n"
  74                 + "        var seenDot = false;\n"
  75                 + "        for (var i = 0; i < url.length - 5; i++) {\n"
  76                 + "            var ch = url.charAt(i);\n"
  77                 + "            if ('a' <= ch && ch <= 'z' ||\n"
  78                 + "                    'A' <= ch && ch <= 'Z' ||\n"
  79                 + "                    ch == '$' ||\n"
  80                 + "                    ch == '_' ||\n"
  81                 + "                    ch.charCodeAt(0) > 127) {\n"
  82                 + "                allowNumber = true;\n"
  83                 + "                allowSep = true;\n"
  84                 + "            } else if ('0' <= ch && ch <= '9'\n"
  85                 + "                    || ch == '-') {\n"
  86                 + "                if (!allowNumber)\n"
  87                 + "                     return false;\n"
  88                 + "            } else if (ch == '/' || ch == '.') {\n"
  89                 + "                if (!allowSep)\n"
  90                 + "                    return false;\n"
  91                 + "                allowNumber = false;\n"
  92                 + "                allowSep = false;\n"
  93                 + "                if (ch == '.')\n"
  94                 + "                     seenDot = true;\n"
  95                 + "                if (ch == '/' && seenDot)\n"
  96                 + "                     return false;\n"
  97                 + "            } else {\n"
  98                 + "                return false;\n"
  99                 + "            }\n"
 100                 + "        }\n"
 101                 + "        return true;\n"
 102                 + "    }\n"
 103                 + "</script>");
 104 
 105         //Make sure title javascript only runs if is-external is not true
 106         checkOutput("pkg/C.html", true,
 107                 "    try {\n"
 108                 + "        if (location.href.indexOf('is-external=true') == -1) {\n"
 109                 + "            parent.document.title=\"C\";\n"
 110                 + "        }\n"
 111                 + "    }\n"
 112                 + "    catch(err) {\n"
 113                 + "    }");
 114     }
 115 }