From: <rb...@us...> - 2017-07-25 16:23:21
|
Revision: 14705 http://sourceforge.net/p/htmlunit/code/14705 Author: rbri Date: 2017-07-25 16:23:19 +0000 (Tue, 25 Jul 2017) Log Message: ----------- next step on our way to an immutable BrowserVersion Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/HtmlUnitContextFactoryTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfigurationTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-07-25 15:48:34 UTC (rev 14704) +++ trunk/htmlunit/src/changes/changes.xml 2017-07-25 16:23:19 UTC (rev 14705) @@ -12,9 +12,12 @@ BrowserVersion.clone now copies the system-timezone attribute. </action> <action type="remove" dev="rbri" issue="1890"> - INCOMPATIBLE CHANGE: Support for BrowserVersionFeatures parameter of the BrowserVersion constructor - has been removed. + INCOMPATIBLE CHANGE: BrowserVersion is final now and all constructors are removed. + The only way to create new customizes Browser versions is to clone one of the predefined. </action> + <action type="change" dev="rbri" issue="1890"> + BrowserVersion setter methods are fluent now. + </action> <action type="fix" dev="asashour" issue="1905"> DomElement: Fix .getChildElements(). </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java 2017-07-25 15:48:34 UTC (rev 14704) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersion.java 2017-07-25 16:23:19 UTC (rev 14705) @@ -35,7 +35,7 @@ * Objects of this class represent one specific version of a given browser. Predefined * constants are provided for common browser versions. * - * <p>You can change the constants by something like: + * <p>You can create a different browser setup by simply clone from the predefined ones. * <pre id='htmlUnitCode'> * String applicationName = "APPNAME"; * String applicationVersion = "APPVERSION"; @@ -42,14 +42,14 @@ * String userAgent = "USERAGENT"; * int browserVersionNumeric = NUMERIC; * - * BrowserVersion browser = new BrowserVersion(applicationName, applicationVersion, userAgent, browserVersionNumeric) { - * public boolean hasFeature(BrowserVersionFeatures property) { - * - * // change features here - * return BrowserVersion.BROWSER.hasFeature(property); - * } - * }; + * BrowserVersion browser = BrowserVersion.FF52.clone() + * .setApplicationName(applicationName) + * .setApplicationVersion(applicationVersion) + * .setUserAgent(userAgent); * </pre> + * <p>But keep in mind that this still behaves like a FF52, only the stuff reported to the + * outside is changed. This is more or less the same you can do with real browsers installing + * plugins like UserAgentSwitcher. * <script> var pre = document.getElementById('htmlUnitCode'); pre.innerHTML = pre.innerHTML.replace('APPNAME', navigator.appName); @@ -92,7 +92,7 @@ * @author Frank Danek * @author Ronald Brill */ -public class BrowserVersion implements Serializable, Cloneable { +public final class BrowserVersion implements Serializable, Cloneable { /** * Application name the Netscape navigator series of browsers. @@ -340,7 +340,7 @@ EDGE.getPlugins().add(flash); } - private int browserVersionNumeric_; + private final int browserVersionNumeric_; private final String nickname_; private String applicationCodeName_ = "Mozilla"; @@ -368,24 +368,6 @@ private Map<String, String> uploadMimeTypes_ = new HashMap<>(); /** - * Instantiates one. - * - * @param applicationName the name of the application - * @param applicationVersion the version string of the application - * @param userAgent the user agent string that will be sent to the server - * @param browserVersionNumeric the number version of the browser - */ - public BrowserVersion(final String applicationName, final String applicationVersion, - final String userAgent, final int browserVersionNumeric) { - - this(browserVersionNumeric, applicationName + browserVersionNumeric); - - setApplicationName(applicationName) - .setApplicationVersion(applicationVersion) - .setUserAgent(userAgent); - } - - /** * Creates a new browser version instance. * * @param browserVersionNumeric the floating number version of the browser @@ -465,7 +447,7 @@ * version of Internet Explorer. * @return whether or not this version is a version of IE */ - public final boolean isIE() { + public boolean isIE() { return getNickname().startsWith("IE"); } @@ -475,7 +457,7 @@ * in the application name, we have to look in the nickname. * @return whether or not this version is a version of a Chrome browser */ - public final boolean isChrome() { + public boolean isChrome() { return getNickname().startsWith("Chrome"); } @@ -484,7 +466,7 @@ * version of Microsoft Edge. * @return whether or not this version is a version of an Edge browser */ - public final boolean isEdge() { + public boolean isEdge() { return getNickname().startsWith("Edge"); } @@ -493,7 +475,7 @@ * version of Firefox. * @return whether or not this version is a version of a Firefox browser */ - public final boolean isFirefox() { + public boolean isFirefox() { return getNickname().startsWith("FF"); } @@ -517,7 +499,7 @@ * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * @return whether or not this version version 52 of a Firefox browser */ - public final boolean isFirefox52() { + public boolean isFirefox52() { return isFirefox() && getBrowserVersionNumeric() == 52; } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/HtmlUnitContextFactoryTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/HtmlUnitContextFactoryTest.java 2017-07-25 15:48:34 UTC (rev 14704) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/HtmlUnitContextFactoryTest.java 2017-07-25 16:23:19 UTC (rev 14705) @@ -35,10 +35,13 @@ @Test public void customBrowserVersion() throws Exception { final String html = "<html></html>"; - final String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0"; - final BrowserVersion browserVersion = new BrowserVersion("Firefox", - "5.0 (Windows NT 10.0; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0", userAgent, 39); + final BrowserVersion browserVersion + = BrowserVersion.FIREFOX_52.clone() + .setApplicationName("Firefox") + .setApplicationVersion("5.0 (Windows NT 10.0; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0") + .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0"); + loadPage(browserVersion, html, null, URL_FIRST); } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java 2017-07-25 15:48:34 UTC (rev 14704) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngineTest.java 2017-07-25 16:23:19 UTC (rev 14705) @@ -34,7 +34,6 @@ import com.gargoylesoftware.htmlunit.BrowserRunner; import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; import com.gargoylesoftware.htmlunit.BrowserVersion; -import com.gargoylesoftware.htmlunit.BrowserVersionFeatures; import com.gargoylesoftware.htmlunit.CollectingAlertHandler; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.HttpMethod; @@ -1295,12 +1294,9 @@ */ @Test public void nonStandardBrowserVersion() throws Exception { - final BrowserVersion browser = new BrowserVersion("Mozilla", "5.0", "Mozilla/5.0", 11) { - @Override - public boolean hasFeature(final BrowserVersionFeatures feature) { - return BrowserVersion.INTERNET_EXPLORER.hasFeature(feature); - } - }; + final BrowserVersion browser = BrowserVersion.INTERNET_EXPLORER.clone() + .setApplicationName("Mozilla") + .setApplicationVersion("5.0"); try (WebClient client = new WebClient(browser)) { client.openWindow(WebClient.URL_ABOUT_BLANK, "TestWindow"); Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfigurationTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfigurationTest.java 2017-07-25 15:48:34 UTC (rev 14704) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfigurationTest.java 2017-07-25 16:23:19 UTC (rev 14705) @@ -74,12 +74,21 @@ field.setAccessible(true); final Map<?, ?> leakyMap = (Map<?, ?>) field.get(null); - // maybe some BrowserVersions are already known + leakyMap.clear(); final int knownBrowsers = leakyMap.size(); - for (int i = 0; i < 3; i++) { - final BrowserVersion browserVersion = new BrowserVersion("App", "Version", "User agent", 1); - JavaScriptConfiguration.getInstance(browserVersion); - } + + BrowserVersion browserVersion = BrowserVersion.FIREFOX_52.clone() + .setApplicationVersion("App") + .setApplicationVersion("Version") + .setUserAgent("User agent"); + JavaScriptConfiguration.getInstance(browserVersion); + + browserVersion = BrowserVersion.FIREFOX_52.clone() + .setApplicationVersion("App2") + .setApplicationVersion("Version2") + .setUserAgent("User agent2"); + JavaScriptConfiguration.getInstance(browserVersion); + assertEquals(knownBrowsers + 1, leakyMap.size()); } @@ -94,11 +103,10 @@ long count = 0; while (count++ < 3000) { - final BrowserVersion browserVersion = new BrowserVersion( - "App" + generator.generate(20), - "Version" + generator.generate(20), - "User Agent" + generator.generate(20), - 1); + final BrowserVersion browserVersion = BrowserVersion.FIREFOX_52.clone() + .setApplicationVersion("App" + generator.generate(20)) + .setApplicationVersion("Version" + generator.generate(20)) + .setUserAgent("User Agent" + generator.generate(20)); JavaScriptConfiguration.getInstance(browserVersion); if (LOG.isInfoEnabled()) { LOG.info("count: " + count + "; memory stats: " + getMemoryStats()); |