Author: amitman...@google.com Date: Thu Jul 16 16:40:42 2009 New Revision: 5750
Modified: branches/htmlunit/user/src/com/google/gwt/junit/HostedModePluginObject.java branches/htmlunit/user/src/com/google/gwt/junit/JUnitShell.java branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnitHosted.java Log: user agent fixes, so that the compilation can be limited to the relevant user agents Modified: branches/htmlunit/user/src/com/google/gwt/junit/HostedModePluginObject.java ============================================================================== --- branches/htmlunit/user/src/com/google/gwt/junit/HostedModePluginObject.java (original) +++ branches/htmlunit/user/src/com/google/gwt/junit/HostedModePluginObject.java Thu Jul 16 16:40:42 2009 @@ -149,4 +149,4 @@ public boolean init(String version) { return true; } -} \ No newline at end of file +} Modified: branches/htmlunit/user/src/com/google/gwt/junit/JUnitShell.java ============================================================================== --- branches/htmlunit/user/src/com/google/gwt/junit/JUnitShell.java (original) +++ branches/htmlunit/user/src/com/google/gwt/junit/JUnitShell.java Thu Jul 16 16:40:42 2009 @@ -66,10 +66,10 @@ * </p> * * <p> - * The client classes consist of the translatable version of {...@link - * com.google.gwt.junit.client.GWTTestCase}, translatable JUnit classes, and the - * user's own {...@link com.google.gwt.junit.client.GWTTestCase}-derived class. - * The client communicates to the server via RPC. + * The client classes consist of the translatable version of + * {...@link com.google.gwt.junit.client.GWTTestCase}, translatable JUnit classes, + * and the user's own {...@link com.google.gwt.junit.client.GWTTestCase}-derived + * class. The client communicates to the server via RPC. * </p> * * <p> @@ -228,8 +228,8 @@ @Override public boolean setString(String str) { String[] targets = str.split(","); - numClients = targets.length; - runStyle = RunStyleHtmlUnitHosted.create(JUnitShell.this, targets); + runStyle = new RunStyleHtmlUnitHosted(JUnitShell.this, targets); + numClients = ((RunStyleHtmlUnit) runStyle).numBrowsers(); return runStyle != null; } }); @@ -254,8 +254,8 @@ @Override public boolean setString(String str) { String[] targets = str.split(","); - numClients = targets.length; - runStyle = RunStyleHtmlUnit.create(JUnitShell.this, targets); + runStyle = new RunStyleHtmlUnit(JUnitShell.this, targets); + numClients = ((RunStyleHtmlUnit) runStyle).numBrowsers(); return runStyle != null; } }); @@ -389,8 +389,8 @@ /** * The amount of time to wait for all clients to complete a single test - * method, in milliseconds, measured from when the <i>last</i> client - * connects (and thus starts the test). 5 minutes. + * method, in milliseconds, measured from when the <i>last</i> client connects + * (and thus starts the test). 5 minutes. */ private static final long TEST_METHOD_TIMEOUT_MILLIS = 30000000; @@ -417,8 +417,8 @@ /** * Entry point for {...@link com.google.gwt.junit.client.GWTTestCase}. Gets or - * creates the singleton {...@link JUnitShell} and invokes its {...@link - * #runTestImpl(String, TestCase, TestResult, Strategy)}. + * creates the singleton {...@link JUnitShell} and invokes its + * {...@link #runTestImpl(String, TestCase, TestResult, Strategy)}. */ public static void runTest(String moduleName, TestCase testCase, TestResult testResult) throws UnableToCompleteException { @@ -675,16 +675,16 @@ } } - void compileForWebMode(String moduleName, String userAgentString) + void compileForWebMode(String moduleName, String... userAgents) throws UnableToCompleteException { // Never fresh during JUnit. ModuleDef module = ModuleDefLoader.loadFromClassPath(getTopLogger(), moduleName, false); - if (userAgentString != null) { + if (userAgents != null && userAgents.length > 0) { Properties props = module.getProperties(); Property userAgent = props.find("user.agent"); if (userAgent instanceof BindingProperty) { - ((BindingProperty) userAgent).setAllowedValues(userAgentString); + ((BindingProperty) userAgent).setAllowedValues(userAgents); } } super.compile(getTopLogger(), module); Modified: branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java ============================================================================== --- branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java (original) +++ branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java Thu Jul 16 16:40:42 2009 @@ -22,7 +22,6 @@ import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.IncorrectnessListener; -import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.OnbeforeunloadHandler; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.WebClient; @@ -31,7 +30,12 @@ import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; /** * Launches a web-mode test via HTMLUnit. @@ -51,7 +55,6 @@ public HtmlUnitThread(BrowserVersion browser, String url) { this.browser = browser; this.url = url; - start(); } public void handleAlert(Page page, String message) { @@ -81,103 +84,115 @@ webClient.setThrowExceptionOnFailingStatusCode(false); webClient.setThrowExceptionOnScriptError(true); webClient.setOnbeforeunloadHandler(this); - webClient.setAjaxController(new NicelyResynchronizingAjaxController()); + // TODO (amitmanjhi): synchronizing is unnecessary. + // webClient.setAjaxController(new NicelyResynchronizingAjaxController()); setupWebClient(webClient); try { Page page = webClient.getPage(url); // TODO(jat): is this necessary? webClient.waitForBackgroundJavaScriptStartingBefore(2000); page.getEnclosingWindow().getJobManager().waitForJobs(60000); - shell.getTopLogger().log(TreeLogger.INFO, "getPage returned " - + ((HtmlPage) page).asXml()); + shell.getTopLogger().log(TreeLogger.DEBUG, + "getPage returned " + ((HtmlPage) page).asXml()); } catch (FailingHttpStatusCodeException e) { - shell.getTopLogger().log(TreeLogger.ERROR, - "HTTP request failed", e); + shell.getTopLogger().log(TreeLogger.ERROR, "HTTP request failed", e); return; } catch (MalformedURLException e) { - shell.getTopLogger().log(TreeLogger.ERROR, - "Bad URL", e); + shell.getTopLogger().log(TreeLogger.ERROR, "Bad URL", e); return; } catch (IOException e) { - shell.getTopLogger().log(TreeLogger.ERROR, - "I/O error on HTTP request", e); + shell.getTopLogger().log(TreeLogger.ERROR, "I/O error on HTTP request", + e); return; } -// synchronized (waitForUnload) { -// try { -// waitForUnload.wait(); -// } catch (InterruptedException e) { -// shell.getTopLogger().log(TreeLogger.ERROR, "Interrupted wait", e); -// } -// } + // synchronized (waitForUnload) { + // try { + // waitForUnload.wait(); + // } catch (InterruptedException e) { + // shell.getTopLogger().log(TreeLogger.ERROR, "Interrupted wait", e); + // } + // } } /** * Additional setup of the WebClient before starting test. - * - * @param webClient */ protected void setupWebClient(WebClient webClient) { } } - /** - * Create a RunStyleHtmlUnit instance with a list of browsers - * - * @param shell - * @param targetsIn - * @return RunStyle instance - */ - public static RunStyle create(JUnitShell shell, String[] targetsIn) { - BrowserVersion[] browsers = new BrowserVersion[targetsIn.length]; - for (int i = 0; i < targetsIn.length; ++i) { - String browserName = targetsIn[i]; - BrowserVersion browser = BrowserVersion.FIREFOX_2; - // TODO(jat): better way to do this - if ("ff2".equalsIgnoreCase(browserName)) { - browser = BrowserVersion.FIREFOX_2; - } else if ("ff3".equalsIgnoreCase(browserName)) { - browser = BrowserVersion.FIREFOX_3; - } else if ("ie6".equalsIgnoreCase(browserName)) { - browser = BrowserVersion.INTERNET_EXPLORER_6; - } else if ("ie7".equalsIgnoreCase(browserName)) { - browser = BrowserVersion.INTERNET_EXPLORER_7; - } else { - shell.getTopLogger().log(TreeLogger.WARN, "Unrecognized browser " - + browserName + " -- using ff2"); - } - browsers[i] = browser; + private static final Map<String, BrowserVersion> BROWSER_MAP = createBrowserMap(); + + private static Map<String, BrowserVersion> createBrowserMap() { + Map<String, BrowserVersion> browserMap = new HashMap<String, BrowserVersion>(); + for (BrowserVersion browser : new BrowserVersion[] { + BrowserVersion.FIREFOX_2, BrowserVersion.FIREFOX_3, + BrowserVersion.INTERNET_EXPLORER_6, BrowserVersion.INTERNET_EXPLORER_7}) { + browserMap.put(browser.getNickname(), browser); } - RunStyleHtmlUnit runStyle = new RunStyleHtmlUnitHosted(shell, browsers); - return runStyle; + return Collections.unmodifiableMap(browserMap); } - private BrowserVersion[] browsers; - private List<Thread> threads = new ArrayList<Thread>(); + private final Set<BrowserVersion> browsers; + private final List<Thread> threads = new ArrayList<Thread>(); - protected RunStyleHtmlUnit(JUnitShell shell, BrowserVersion[] browsers) { + /** + * Create a RunStyle instance with the passed-in browser targets. + */ + public RunStyleHtmlUnit(JUnitShell shell, String[] targetsIn) { super(shell); - this.browsers = browsers; + this.browsers = getBrowserSet(targetsIn); } @Override - public void launchModule(String moduleName) throws UnableToCompleteException { + public void launchModule(String moduleName) { for (BrowserVersion browser : browsers) { String url = getMyUrl(moduleName); - shell.getTopLogger().log(TreeLogger.INFO, "Starting " + url - + " on browser " + browser); - threads.add(createHtmlUnitThread(browser, url)); + HtmlUnitThread hut = createHtmlUnitThread(browser, url); + shell.getTopLogger().log(TreeLogger.INFO, + "Starting " + url + " on browser " + browser.getNickname()); + hut.start(); + threads.add(hut); } } @Override public void maybeCompileModule(String moduleName) throws UnableToCompleteException { - // TODO(jat): substitute appropriate user agent, support multiple agents - shell.compileForWebMode(moduleName, "gecko1_8"); + shell.compileForWebMode(moduleName, getUserAgents()); + } + + public int numBrowsers() { + return browsers.size(); } - protected HtmlUnitThread createHtmlUnitThread(BrowserVersion browser, String url) { + protected HtmlUnitThread createHtmlUnitThread(BrowserVersion browser, + String url) { return new HtmlUnitThread(browser, url); + } + + private Set<BrowserVersion> getBrowserSet(String[] targetsIn) { + Set<BrowserVersion> browserSet = new HashSet<BrowserVersion>(); + for (String browserName : targetsIn) { + BrowserVersion browser = BROWSER_MAP.get(browserName); + assert browser != null; + browserSet.add(browser); + } + return Collections.unmodifiableSet(browserSet); + } + + private String[] getUserAgents() { + Map<BrowserVersion, String> userAgentMap = new HashMap<BrowserVersion, String>(); + userAgentMap.put(BrowserVersion.FIREFOX_2, "gecko1_8"); + userAgentMap.put(BrowserVersion.FIREFOX_3, "gecko"); + userAgentMap.put(BrowserVersion.INTERNET_EXPLORER_6, "ie6"); + userAgentMap.put(BrowserVersion.INTERNET_EXPLORER_7, "ie6"); + + String userAgents[] = new String[numBrowsers()]; + int index = 0; + for (BrowserVersion browser : browsers) { + userAgents[index++] = userAgentMap.get(browser); + } + return userAgents; } } Modified: branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnitHosted.java ============================================================================== --- branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnitHosted.java (original) +++ branches/htmlunit/user/src/com/google/gwt/junit/RunStyleHtmlUnitHosted.java Thu Jul 16 16:40:42 2009 @@ -15,8 +15,6 @@ */ package com.google.gwt.junit; -import com.google.gwt.core.ext.UnableToCompleteException; - import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebWindow; @@ -25,7 +23,6 @@ import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject; - /** * Runstyle for HTMLUnit in hosted mode. */ @@ -71,17 +68,15 @@ } } - protected RunStyleHtmlUnitHosted(JUnitShell shell, - BrowserVersion[] browsers) { - super(shell, browsers); + public RunStyleHtmlUnitHosted(JUnitShell unitShell, String[] targets) { + super(unitShell, targets); } - + @Override - public void maybeCompileModule(String moduleName) - throws UnableToCompleteException { + public void maybeCompileModule(String moduleName) { // No compilation needed for hosted mode } - + @Override protected HtmlUnitThread createHtmlUnitThread(BrowserVersion browser, String url) { --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---