From: <asa...@us...> - 2012-12-09 08:07:14
|
Revision: 7844 http://sourceforge.net/p/htmlunit/code/7844 Author: asashour Date: 2012-12-09 08:07:09 +0000 (Sun, 09 Dec 2012) Log Message: ----------- - JavaScript: fix Date.toLocaleTimeString Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/DateCustom.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/BrowserRunner.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeDateTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2012-12-09 07:25:36 UTC (rev 7843) +++ trunk/htmlunit/src/changes/changes.xml 2012-12-09 08:07:09 UTC (rev 7844) @@ -14,7 +14,7 @@ FIREFOX_17 and CHROME. </action> <action type="fix" dev="asashour" issue="1467"> - JavaScript: fix Date.toLocaleDateString(). + JavaScript: fix Date.toLocaleDateString() and .toLocaleTimeString(). </action> <action type="add" dev="mguillem"> Add BrowserVersion.clone(). Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2012-12-09 07:25:36 UTC (rev 7843) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2012-12-09 08:07:09 UTC (rev 7844) @@ -760,6 +760,10 @@ @BrowserFeature(@WebBrowser(FF)) JS_CONSTRUCTOR, + /** Is Date.toLocaleTimeString() in 24-hour format. */ + @BrowserFeature(@WebBrowser(CHROME)) + JS_DATE_LOCATE_TIME_24, + /** */ @BrowserFeature(@WebBrowser(IE)) JS_DEFERRED, Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java 2012-12-09 07:25:36 UTC (rev 7843) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java 2012-12-09 08:07:09 UTC (rev 7844) @@ -322,7 +322,7 @@ } final ScriptableObject datePrototype = (ScriptableObject) ScriptableObject.getClassPrototype(window, "Date"); - datePrototype.defineFunctionProperties(new String[] {"toLocaleDateString"}, + datePrototype.defineFunctionProperties(new String[] {"toLocaleDateString", "toLocaleTimeString"}, DateCustom.class, ScriptableObject.DONTENUM); window.setPrototypes(prototypes); window.initialize(webWindow); Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/DateCustom.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/DateCustom.java 2012-12-09 07:25:36 UTC (rev 7843) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/DateCustom.java 2012-12-09 08:07:09 UTC (rev 7844) @@ -14,6 +14,8 @@ */ package com.gargoylesoftware.htmlunit.javascript.host; +import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DATE_LOCATE_TIME_24; + import java.lang.reflect.Field; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -50,20 +52,38 @@ if (LOCAL_DATE_FORMAT_ == null) { LOCAL_DATE_FORMAT_ = new SimpleDateFormat("EEEE, MMMM dd, yyyy", Locale.US); } - initDateField(thisObj); - try { - final double value = (Double) DATE_FIELD_.get(thisObj); - return LOCAL_DATE_FORMAT_.format(new Date((long) value)); + return LOCAL_DATE_FORMAT_.format(new Date(getDateValue(thisObj))); + } + + /** + * Converts a date to a string, returning the "time" portion using the current locale's conventions. + * @param context the JavaScript context + * @param thisObj the scriptable + * @param args the arguments passed into the method + * @param function the function + * @return converted string + */ + public static String toLocaleTimeString( + final Context context, final Scriptable thisObj, final Object[] args, final Function function) { + final String formatString; + if (((Window) thisObj.getParentScope()).getWebWindow().getWebClient().getBrowserVersion() + .hasFeature(JS_DATE_LOCATE_TIME_24)) { + formatString = "HH:mm:ss"; } - catch (final Exception e) { - throw Context.throwAsScriptRuntimeEx(e); + else { + formatString = "hh:mm:ss a"; } + final DateFormat format = new SimpleDateFormat(formatString, Locale.US); + return format.format(new Date(getDateValue(thisObj))); } - private static void initDateField(final Scriptable thisObj) { + private static long getDateValue(final Scriptable thisObj) { try { - DATE_FIELD_ = thisObj.getClass().getDeclaredField("date"); - DATE_FIELD_.setAccessible(true); + if (DATE_FIELD_ == null) { + DATE_FIELD_ = thisObj.getClass().getDeclaredField("date"); + DATE_FIELD_.setAccessible(true); + } + return ((Double) DATE_FIELD_.get(thisObj)).longValue(); } catch (final Exception e) { throw Context.throwAsScriptRuntimeEx(e); Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/BrowserRunner.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/BrowserRunner.java 2012-12-09 07:25:36 UTC (rev 7843) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/BrowserRunner.java 2012-12-09 08:07:09 UTC (rev 7844) @@ -88,7 +88,6 @@ if (/*browsers.contains("hu") ||*/ browsers.contains("hu-ie9")) { runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.INTERNET_EXPLORER_9, false)); } - // in a first time, chrome can be specified but is not integrated in the default run if (/*browsers.contains("hu") || */browsers.contains("hu-chrome")) { runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.CHROME, false)); } @@ -100,6 +99,9 @@ if (browsers.contains("ff10")) { runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.FIREFOX_10, true)); } + if (browsers.contains("ff17")) { + runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.FIREFOX_17, true)); + } if (browsers.contains("ie6")) { runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.INTERNET_EXPLORER_6, true)); } @@ -109,9 +111,12 @@ if (browsers.contains("ie8")) { runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.INTERNET_EXPLORER_8, true)); } - if (browsers.contains("chrome16")) { - runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.CHROME_16, true)); + if (browsers.contains("ie9")) { + runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.INTERNET_EXPLORER_9, true)); } + if (browsers.contains("chrome")) { + runners_.add(new BrowserVersionClassRunner(klass, BrowserVersion.CHROME, true)); + } } } if (BrowserNoneClassRunner.containsTestMethods(klass)) { Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeDateTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeDateTest.java 2012-12-09 07:25:36 UTC (rev 7843) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/NativeDateTest.java 2012-12-09 08:07:09 UTC (rev 7844) @@ -151,7 +151,7 @@ * @throws Exception if the test fails */ @Test - public void toLocaleDateString_enumerable() throws Exception { + public void enumerable() throws Exception { final String html = "<html><head><title>foo</title><script>\n" + "function test() {\n" @@ -165,4 +165,22 @@ loadPageWithAlerts2(html); } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "12:00:00 AM", CHROME = "00:00:00") + public void toLocaleTimeString() throws Exception { + final String html + = "<html><head><title>foo</title><script>\n" + + "function test() {\n" + + " alert(new Date(2000, 0, 1).toLocaleTimeString());\n" + + "}\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + } |