From: <rb...@us...> - 2017-11-29 18:54:56
|
Revision: 14981 http://sourceforge.net/p/htmlunit/code/14981 Author: rbri Date: 2017-11-29 18:54:53 +0000 (Wed, 29 Nov 2017) Log Message: ----------- JavaScript: performance.navigation.toJSON() added Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigation.java Added Paths: ----------- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigationTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-11-27 20:16:26 UTC (rev 14980) +++ trunk/htmlunit/src/changes/changes.xml 2017-11-29 18:54:53 UTC (rev 14981) @@ -9,6 +9,9 @@ <body> <release version="2.29" date="xx, 2017" description=""> <action type="add" dev="rbri"> + JavaScript: performance.navigation.toJSON() added. + </action> + <action type="add" dev="rbri"> JavaScript: URL.searchParams added. </action> <action type="add" dev="rbri"> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigation.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigation.java 2017-11-27 20:16:26 UTC (rev 14980) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigation.java 2017-11-29 18:54:53 UTC (rev 14981) @@ -17,8 +17,10 @@ import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME; import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE; import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF; -import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstant; @@ -26,14 +28,21 @@ import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; +import net.sourceforge.htmlunit.corejs.javascript.Context; +import net.sourceforge.htmlunit.corejs.javascript.json.JsonParser; +import net.sourceforge.htmlunit.corejs.javascript.json.JsonParser.ParseException; + /** * A JavaScript object for {@code PerformanceNavigation}. * * @author Ahmed Ashour + * @author Ronald Brill */ @JsxClass public class PerformanceNavigation extends SimpleScriptable { + private static final Log LOG = LogFactory.getLog(PerformanceNavigation.class); + /** Navigate. */ @JsxConstant public static final int TYPE_NAVIGATE = 0; @@ -79,9 +88,21 @@ * The {@code toJSON} function. * @return the {@code toJSON} object */ - @JsxFunction({FF, IE}) + @JsxFunction public Object toJSON() { - throw new UnsupportedOperationException(); + final String jsonString = new StringBuilder() + .append("{\"type\":") + .append(Integer.toString(getType())) + .append(", \"redirectCount\":") + .append(Integer.toString(getRedirectCount())) + .append("}").toString(); + try { + return new JsonParser(Context.getCurrentContext(), getParentScope()).parseValue(jsonString); + } + catch (final ParseException e) { + LOG.warn("Failed parsingJSON '" + jsonString + "' reason: " + e.getMessage()); + } + return null; } } Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigationTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigationTest.java (rev 0) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigationTest.java 2017-11-29 18:54:53 UTC (rev 14981) @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2002-2017 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host.performance; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openqa.selenium.WebDriver; + +import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.WebDriverTestCase; +import com.gargoylesoftware.htmlunit.html.HtmlPageTest; + +/** + * Tests for {@link Performance}. + * + * @author Ronald Brill + */ +@RunWith(BrowserRunner.class) +public class PerformanceNavigationTest extends WebDriverTestCase { + + /** + * @throws Exception if the test fails + */ + @Test + public void toJSON() throws Exception { + final String html = + HtmlPageTest.STANDARDS_MODE_PREFIX_ + + "<html>\n" + + "<head>\n" + + "<script>\n" + + " function test() {\n" + + " alert(JSON.stringify(performance.navigation.toJSON()));\n" + + " }\n" + + " test();\n" + + "</script>\n" + + "</head>\n" + + "<body></body>\n" + + "</html>"; + + final WebDriver driver = loadPage2(html); + final String json = getCollectedAlerts(driver, 1).get(0); + assertTrue(json, json.contains("\"type\":0")); + assertTrue(json, json.contains("\"redirectCount\":0")); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("0") + public void redirectCount() throws Exception { + final String html = + HtmlPageTest.STANDARDS_MODE_PREFIX_ + + "<html>\n" + + "<head>\n" + + "<script>\n" + + " function test() {\n" + + " alert(performance.navigation.redirectCount);\n" + + " }\n" + + " test();\n" + + "</script>\n" + + "</head>\n" + + "<body></body>\n" + + "</html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("0") + public void type() throws Exception { + final String html = + HtmlPageTest.STANDARDS_MODE_PREFIX_ + + "<html>\n" + + "<head>\n" + + "<script>\n" + + " function test() {\n" + + " alert(performance.navigation.type);\n" + + " }\n" + + " test();\n" + + "</script>\n" + + "</head>\n" + + "<body></body>\n" + + "</html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts({"number", "function"}) + public void methods() throws Exception { + final String html + = "<html>\n" + + "<body>\n" + + "<script>\n" + + " alert(typeof performance.navigation.redirectCount);\n" + + " alert(typeof performance.navigation.toJSON);\n" + + "</script>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } +} Property changes on: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/performance/PerformanceNavigationTest.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property |