diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientAutoLoginTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientAutoLoginTest.java index 96cf269873076..2fac9192b5ad3 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientAutoLoginTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientAutoLoginTest.java @@ -34,9 +34,8 @@ private void autoLoginTestHelper(final String testName, final String xAutoLoginH List> headers = new ArrayList>(); headers.add(Pair.create("x-auto-login", xAutoLoginHeader)); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String pageUrl = webServer.setResponse(path, html, headers); final int callCount = loginRequestHelper.getCallCount(); loadUrlAsync(awContents, pageUrl); @@ -46,7 +45,7 @@ private void autoLoginTestHelper(final String testName, final String xAutoLoginH assertEquals(expectedAccount, loginRequestHelper.getAccount()); assertEquals(expectedArgs, loginRequestHelper.getArgs()); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFaviconTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFaviconTest.java index 973155eb53c10..f70e7a92785a0 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFaviconTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFaviconTest.java @@ -89,7 +89,7 @@ public void onReceivedTouchIconUrl(String url, boolean precomposed) { protected void setUp() throws Exception { super.setUp(); AwContents.setShouldDownloadFavicons(); - mWebServer = new TestWebServer(false); + mWebServer = TestWebServer.start(); } private void init(TestAwContentsClientBase contentsClient) throws Exception { diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientOnFormResubmissionTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientOnFormResubmissionTest.java index b6535b37e7694..bd7a282d67886 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientOnFormResubmissionTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientOnFormResubmissionTest.java @@ -67,7 +67,7 @@ public void onFormResubmission(Message dontResend, Message resend) { @Override public void setUp() throws Exception { super.setUp(); - mServer = new TestWebServer(false); + mServer = TestWebServer.start(); mContentsClient = new TestAwContentsClient(); final AwTestContainerView testContainerView = createAwTestContainerViewOnMainSync(mContentsClient); diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java index 8cc47d532d923..47cb465e49c8c 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldInterceptRequestTest.java @@ -159,7 +159,7 @@ protected void setUp() throws Exception { mAwContents = mTestContainerView.getAwContents(); mShouldInterceptRequestHelper = mContentsClient.getShouldInterceptRequestHelper(); - mWebServer = new TestWebServer(false); + mWebServer = TestWebServer.start(); } @Override diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldOverrideUrlLoadingTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldOverrideUrlLoadingTest.java index 25f8eac8d4048..97331c176c08f 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldOverrideUrlLoadingTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientShouldOverrideUrlLoadingTest.java @@ -36,7 +36,7 @@ public class AwContentsClientShouldOverrideUrlLoadingTest extends AwTestBase { @Override protected void setUp() throws Exception { super.setUp(); - mWebServer = new TestWebServer(false); + mWebServer = TestWebServer.start(); } @Override diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientVisitedHistoryTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientVisitedHistoryTest.java index 42a3b708e3332..5d5c9fc01af7d 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientVisitedHistoryTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientVisitedHistoryTest.java @@ -69,9 +69,8 @@ public void testUpdateVisitedHistoryCallback() throws Throwable { final String path = "/testUpdateVisitedHistoryCallback.html"; final String html = "testUpdateVisitedHistoryCallback"; - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String pageUrl = webServer.setResponse(path, html, null); final DoUpdateVisitedHistoryHelper doUpdateVisitedHistoryHelper = mContentsClient.getDoUpdateVisitedHistoryHelper(); @@ -88,7 +87,7 @@ public void testUpdateVisitedHistoryCallback() throws Throwable { assertEquals(pageUrl, doUpdateVisitedHistoryHelper.getUrl()); assertEquals(true, doUpdateVisitedHistoryHelper.getIsReload()); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -110,9 +109,8 @@ public void testGetVisitedHistoryExerciseCodePath() throws Throwable { final String visitedLinks[] = {"http://foo.com", "http://bar.com", null}; final String html = "foobar"; - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String pageUrl = webServer.setResponse(path, html, null); loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); visitedHistoryHelper.waitForCallback(callCount); @@ -123,7 +121,7 @@ public void testGetVisitedHistoryExerciseCodePath() throws Throwable { loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java index d2db7ddad7ed3..e81a5563cd765 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java @@ -254,9 +254,8 @@ public void testClearCacheMemoryAndDisk() throws Throwable { createAwTestContainerViewOnMainSync(mContentsClient); final AwContents awContents = testContainer.getAwContents(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String pagePath = "/clear_cache_test.html"; List> headers = new ArrayList>(); // Set Cache-Control headers to cache this request. One century should be long enough. @@ -296,7 +295,7 @@ public void testClearCacheMemoryAndDisk() throws Throwable { pageUrl); assertEquals(2, webServer.getRequestCount(pagePath)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -324,10 +323,8 @@ public void testGetFavicon() throws Throwable { final AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient); final AwContents awContents = testView.getAwContents(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - final String faviconUrl = webServer.setResponseBase64( "/" + CommonResources.FAVICON_FILENAME, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(false)); @@ -357,7 +354,7 @@ public Boolean call() { assertTrue(awContents.getFavicon().sameAs(originalFavicon)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -376,9 +373,8 @@ public void testDownload() throws Throwable { downloadHeaders.add(Pair.create("Content-Type", mimeType)); downloadHeaders.add(Pair.create("Content-Length", Integer.toString(data.length()))); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String pageUrl = webServer.setResponse( "/download.txt", data, downloadHeaders); final OnDownloadStartHelper downloadStartHelper = @@ -392,7 +388,7 @@ public void testDownload() throws Throwable { assertEquals(mimeType, downloadStartHelper.getMimeType()); assertEquals(data.length(), downloadStartHelper.getContentLength()); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -479,9 +475,8 @@ public void testCanInjectHeaders() throws Throwable { createAwTestContainerViewOnMainSync(mContentsClient); final AwContents awContents = testContainer.getAwContents(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String pagePath = "/test_can_inject_headers.html"; final String pageUrl = webServer.setResponse( pagePath, "foo", null); @@ -504,7 +499,7 @@ public void testCanInjectHeaders() throws Throwable { assertEquals(value.getValue(), matchingHeaders[0].getValue()); } } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwQuotaManagerBridgeTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwQuotaManagerBridgeTest.java index f1e846a565e2b..df1c0d6a91e31 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwQuotaManagerBridgeTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwQuotaManagerBridgeTest.java @@ -36,7 +36,7 @@ public void setUp() throws Exception { mContentsClient = new TestAwContentsClient(); mTestView = createAwTestContainerViewOnMainSync(mContentsClient); mAwContents = mTestView.getAwContents(); - mWebServer = new TestWebServer(false); + mWebServer = TestWebServer.start(); mOrigin = mWebServer.getBaseUrl(); AwSettings settings = getAwSettingsOnUiThread(mAwContents); diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java index 9ecf9717e0223..016f9ced8e359 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java @@ -1728,10 +1728,9 @@ public void testUserAgentWithTestServer() throws Throwable { final String customUserAgentString = "testUserAgentWithTestServerUserAgent"; - TestWebServer webServer = null; String fileName = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String httpPath = "/testUserAgentWithTestServer.html"; final String url = webServer.setResponse(httpPath, "foo", null); @@ -1748,7 +1747,7 @@ public void testUserAgentWithTestServer() throws Throwable { Header header = matchingHeaders[0]; assertEquals(customUserAgentString, header.getValue()); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -1935,9 +1934,8 @@ public void testBlockNetworkImagesBlocksNetworkImageAndReloadInPlace() throws Th settings.setJavaScriptEnabled(true); ImagePageGenerator generator = new ImagePageGenerator(0, false); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String httpImageUrl = generator.getPageUrl(webServer); settings.setImagesEnabled(false); @@ -1954,7 +1952,7 @@ public Boolean call() throws Exception { } }); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -1962,9 +1960,8 @@ public Boolean call() throws Exception { @Feature({"AndroidWebView", "Preferences"}) public void testBlockNetworkImagesWithTwoViews() throws Throwable { ViewPair views = createViews(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); runPerViewSettingsTest( new AwSettingsImagesEnabledHelper( views.getContainer0(), @@ -1977,7 +1974,7 @@ public void testBlockNetworkImagesWithTwoViews() throws Throwable { webServer, new ImagePageGenerator(1, true))); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -1992,11 +1989,10 @@ public void testBlockNetworkLoadsWithHttpResources() throws Throwable { awSettings.setJavaScriptEnabled(true); ImagePageGenerator generator = new ImagePageGenerator(0, false); - TestWebServer webServer = null; String fileName = null; + TestWebServer webServer = TestWebServer.start(); try { // Set up http image. - webServer = new TestWebServer(false); final String httpPath = "/image.png"; final String imageUrl = webServer.setResponseBase64( httpPath, generator.getImageSourceNoAdvance(), @@ -2029,8 +2025,8 @@ public void testBlockNetworkLoadsWithHttpResources() throws Throwable { assertEquals(1, webServer.getRequestCount(httpPath)); assertEquals("img_onload_fired", getTitleOnUiThread(awContents)); } finally { + webServer.shutdown(); if (fileName != null) TestFileUtil.deleteFile(fileName); - if (webServer != null) webServer.shutdown(); } } @@ -2062,9 +2058,8 @@ public void testBlockNetworkLoadsWithAudio() throws Throwable { final CallbackHelper callback = new CallbackHelper(); awSettings.setJavaScriptEnabled(true); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String httpPath = "/audio.mp3"; // Don't care about the response is correct or not, just want // to know whether Url is accessed. @@ -2100,7 +2095,7 @@ public void run() { assertTrue(0 != webServer.getRequestCount(httpPath)); */ } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -2223,9 +2218,8 @@ public void testCacheMode() throws Throwable { clearCacheOnUiThread(awContents, true); assertEquals(WebSettings.LOAD_DEFAULT, awSettings.getCacheMode()); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String htmlPath = "/testCacheMode.html"; final String url = webServer.setResponse(htmlPath, "response", null); awSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); @@ -2254,7 +2248,7 @@ public void testCacheMode() throws Throwable { urlNotInCache); assertEquals(0, webServer.getRequestCount(htmlNotInCachePath)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -2273,9 +2267,8 @@ public void testCacheModeWithBlockedNetworkLoads() throws Throwable { assertEquals(WebSettings.LOAD_DEFAULT, awSettings.getCacheMode()); awSettings.setBlockNetworkLoads(true); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String htmlPath = "/testCacheModeWithBlockedNetworkLoads.html"; final String url = webServer.setResponse(htmlPath, "response", null); loadUrlSyncAndExpectError(awContents, @@ -2305,7 +2298,7 @@ public void testCacheModeWithBlockedNetworkLoads() throws Throwable { url); assertEquals(0, webServer.getRequestCount(htmlPath)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -2313,16 +2306,15 @@ public void testCacheModeWithBlockedNetworkLoads() throws Throwable { @Feature({"AndroidWebView", "Preferences"}) public void testCacheModeWithTwoViews() throws Throwable { ViewPair views = createViews(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); runPerViewSettingsTest( new AwSettingsCacheModeTestHelper( views.getContainer0(), views.getClient0(), 0, webServer), new AwSettingsCacheModeTestHelper( views.getContainer1(), views.getClient1(), 1, webServer)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -2389,9 +2381,8 @@ public void testAppCache() throws Throwable { // Note that the cache isn't actually enabled until the call to setAppCachePath. settings.setAppCacheEnabled(true); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); ManifestTestHelper helper = new ManifestTestHelper( webServer, "testAppCache.html", "appcache.manifest"); loadUrlSync( @@ -2410,7 +2401,7 @@ public void testAppCache() throws Throwable { helper.getHtmlUrl()); helper.waitUntilManifestIsRequested(0); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -2431,9 +2422,8 @@ public void testAppCacheWithTwoViews() throws Throwable { // AppCachePath setting is global, no need to set it for the second view. settings1.setAppCacheEnabled(true); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); ManifestTestHelper helper0 = new ManifestTestHelper( webServer, "testAppCache_0.html", "appcache.manifest_0"); loadUrlSync( @@ -2468,7 +2458,7 @@ public void testAppCacheWithTwoViews() throws Throwable { assertEquals( prevManifestRequestCount, webServer.getRequestCount(helper1.getManifestPath())); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -2777,8 +2767,8 @@ public void onReceivedSslError(ValueCallback callback, SslError error) TestWebServer httpsServer = null; TestWebServer httpServer = null; try { - httpsServer = new TestWebServer(true); - httpServer = new TestWebServer(false); + httpsServer = TestWebServer.startSsl(); + httpServer = TestWebServer.start(); final String jsUrl = "/insecure.js"; final String imageUrl = "/insecure.png"; diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/ClientOnPageFinishedTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/ClientOnPageFinishedTest.java index 7c565e231ecd0..6c321412a4acb 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/ClientOnPageFinishedTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/ClientOnPageFinishedTest.java @@ -118,9 +118,8 @@ public void testOnPageFinishedCalledAfterRedirectedUrlIsOverridden() throws Thro * 4. url2 onPageFinishedCalled */ - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String redirectTargetPath = "/redirect_target.html"; final String redirectTargetUrl = webServer.setResponse(redirectTargetPath, "hello world", null); @@ -141,7 +140,7 @@ public void testOnPageFinishedCalledAfterRedirectedUrlIsOverridden() throws Thro // onPageFinished needs to be called for redirectTargetUrl, but not for redirectUrl assertEquals(redirectTargetUrl, onPageFinishedHelper.getUrl()); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -151,10 +150,8 @@ public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable { TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = mContentsClient.getOnPageFinishedHelper(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - final String testHtml = "HeaderBody"; final String testPath = "/test.html"; final String syncPath = "/sync.html"; @@ -181,9 +178,8 @@ public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable { onPageFinishedHelper.waitForCallback(synchronizationPageCallCount); assertEquals(syncUrl, onPageFinishedHelper.getUrl()); assertEquals(2, onPageFinishedHelper.getCallCount()); - } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -194,10 +190,8 @@ public void testOnPageFinishedNotCalledForHistoryApi() throws Throwable { mContentsClient.getOnPageFinishedHelper(); enableJavaScriptOnUiThread(mAwContents); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - final String testHtml = "HeaderBody"; final String testPath = "/test.html"; final String historyPath = "/history.html"; @@ -223,9 +217,8 @@ public void testOnPageFinishedNotCalledForHistoryApi() throws Throwable { onPageFinishedHelper.waitForCallback(synchronizationPageCallCount); assertEquals(syncUrl, onPageFinishedHelper.getUrl()); assertEquals(2, onPageFinishedHelper.getCallCount()); - } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -248,10 +241,8 @@ private void doTestOnPageFinishedCalledForHrefNavigations(boolean useBaseUrl) th mContentsClient.getOnPageStartedHelper(); enableJavaScriptOnUiThread(mAwContents); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - final String testHtml = CommonResources.makeHtmlPageFrom("", "anchor"); final String testPath = "/test.html"; @@ -282,7 +273,7 @@ private void doTestOnPageFinishedCalledForHrefNavigations(boolean useBaseUrl) th onPageFinishedHelper.waitForCallback(onPageFinishedCallCount); assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCount()); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerStartupTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerStartupTest.java index 9f597e6b7dc4a..f22bb1cef7a52 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerStartupTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerStartupTest.java @@ -61,9 +61,8 @@ public void run() { @MediumTest @Feature({"AndroidWebView"}) public void testStartup() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); String path = "/cookie_test.html"; String url = webServer.setResponse(path, CommonResources.ABOUT_HTML, null); @@ -87,7 +86,7 @@ public void testStartup() throws Throwable { assertEquals("count=42", cookieManager.getCookie(url)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java index eea00276b597a..e58d3100d8158 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java @@ -65,9 +65,8 @@ protected void tearDown() throws Exception { @MediumTest @Feature({"AndroidWebView", "Privacy"}) public void testAcceptCookie() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); String path = "/cookie_test.html"; String responseStr = "TEST!HELLO!"; @@ -108,7 +107,7 @@ public void testAcceptCookie() throws Throwable { assertNotNull(cookie); validateCookies(cookie, "test2", "header-test2"); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -356,20 +355,17 @@ public void testCookieExpiration() throws Exception { @MediumTest @Feature({"AndroidWebView", "Privacy"}) public void testThirdPartyCookie() throws Throwable { - TestWebServer webServer = null; + // In theory we need two servers to test this, one server ('the first party') which returns + // a response with a link to a second server ('the third party') at different origin. This + // second server attempts to set a cookie which should fail if AcceptThirdPartyCookie() is + // false. + // Strictly according to the letter of RFC6454 it should be possible to set this situation + // up with two TestServers on different ports (these count as having different origins) but + // Chrome is not strict about this and does not check the port. Instead we cheat making some + // of the urls come from localhost and some from 127.0.0.1 which count (both in theory and + // pratice) as having different origins. + TestWebServer webServer = TestWebServer.start(); try { - // In theory we need two servers to test this, one server ('the first party') - // which returns a response with a link to a second server ('the third party') - // at different origin. This second server attempts to set a cookie which should - // fail if AcceptThirdPartyCookie() is false. - // Strictly according to the letter of RFC6454 it should be possible to set this - // situation up with two TestServers on different ports (these count as having - // different origins) but Chrome is not strict about this and does not check the - // port. Instead we cheat making some of the urls come from localhost and some - // from 127.0.0.1 which count (both in theory and pratice) as having different - // origins. - webServer = new TestWebServer(false); - // Turn global allow on. mCookieManager.setAcceptCookie(true); assertTrue(mCookieManager.acceptCookie()); @@ -401,7 +397,7 @@ public void testThirdPartyCookie() throws Throwable { assertNotNull(cookie); validateCookies(cookie, "test2"); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -437,10 +433,9 @@ private String makeScriptLinkUrl(TestWebServer webServer, String path, String ur @MediumTest @Feature({"AndroidWebView", "Privacy"}) public void testThirdPartyJavascriptCookie() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { // This test again uses 127.0.0.1/localhost trick to simulate a third party. - webServer = new TestWebServer(false); ThirdPartyCookiesTestHelper thirdParty = new ThirdPartyCookiesTestHelper(webServer); @@ -461,16 +456,15 @@ public void testThirdPartyJavascriptCookie() throws Throwable { // ...we can set third party cookies. thirdParty.assertThirdPartyIFrameCookieResult("2", true); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @MediumTest @Feature({"AndroidWebView", "Privacy"}) public void testThirdPartyCookiesArePerWebview() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); mCookieManager.setAcceptCookie(true); mCookieManager.removeAllCookie(); assertTrue(mCookieManager.acceptCookie()); @@ -504,7 +498,7 @@ public void testThirdPartyCookiesArePerWebview() throws Throwable { helperOne.assertThirdPartyIFrameCookieResult("7", true); helperTwo.assertThirdPartyIFrameCookieResult("8", false); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/GetTitleTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/GetTitleTest.java index e933e06e34895..aca004471c293 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/GetTitleTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/GetTitleTest.java @@ -60,17 +60,14 @@ private String loadFromDataAndGetTitle(String html) throws Throwable { } private PageInfo loadFromUrlAndGetTitle(String html, String filename) throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - final String url = webServer.setResponse(filename, html, null); loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); return new PageInfo(getTitleOnUiThread(mAwContents), url.replaceAll("http:\\/\\/", "")); - } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/LoadDataWithBaseUrlTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/LoadDataWithBaseUrlTest.java index c9168c3191419..a018e67be4345 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/LoadDataWithBaseUrlTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/LoadDataWithBaseUrlTest.java @@ -88,9 +88,8 @@ private String getCrossOriginAccessTestPageHtml(final String iframeUrl) { @SmallTest @Feature({"AndroidWebView"}) public void testImageLoad() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); webServer.setResponseBase64("/" + CommonResources.FAVICON_FILENAME, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); @@ -104,17 +103,15 @@ public void testImageLoad() throws Throwable { assertEquals("5", getTitleOnUiThread(mAwContents)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @SmallTest @Feature({"AndroidWebView"}) public void testScriptLoad() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - final String scriptUrl = webServer.setResponse(SCRIPT_FILE, SCRIPT_JS, CommonResources.getTextJavascriptHeaders(true)); final String pageHtml = getScriptFileTestPageHtml(scriptUrl); @@ -122,18 +119,16 @@ public void testScriptLoad() throws Throwable { getAwSettingsOnUiThread(mAwContents).setJavaScriptEnabled(true); loadDataWithBaseUrlSync(pageHtml, "text/html", false, webServer.getBaseUrl(), null); assertEquals(SCRIPT_LOADED, getTitleOnUiThread(mAwContents)); - } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @SmallTest @Feature({"AndroidWebView"}) public void testSameOrigin() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String frameUrl = webServer.setResponse("/" + CommonResources.ABOUT_FILENAME, CommonResources.ABOUT_HTML, CommonResources.getTextHtmlHeaders(true)); final String html = getCrossOriginAccessTestPageHtml(frameUrl); @@ -141,18 +136,16 @@ public void testSameOrigin() throws Throwable { getAwSettingsOnUiThread(mAwContents).setJavaScriptEnabled(true); loadDataWithBaseUrlSync(html, "text/html", false, webServer.getBaseUrl(), null); assertEquals(frameUrl, getTitleOnUiThread(mAwContents)); - } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @SmallTest @Feature({"AndroidWebView"}) public void testCrossOrigin() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String frameUrl = webServer.setResponse("/" + CommonResources.ABOUT_FILENAME, CommonResources.ABOUT_HTML, CommonResources.getTextHtmlHeaders(true)); final String html = getCrossOriginAccessTestPageHtml(frameUrl); @@ -162,9 +155,8 @@ public void testCrossOrigin() throws Throwable { loadDataWithBaseUrlSync(html, "text/html", false, baseUrl, null); assertEquals("Exception", getTitleOnUiThread(mAwContents)); - } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -238,9 +230,8 @@ public void testHistoryUrlIgnoredWithDataSchemeBaseUrl() throws Throwable { */ @DisabledTest public void testHistoryUrlNavigation() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String historyUrl = webServer.setResponse("/" + CommonResources.ABOUT_FILENAME, CommonResources.ABOUT_HTML, CommonResources.getTextHtmlHeaders(true)); @@ -263,9 +254,8 @@ public void testHistoryUrlNavigation() throws Throwable { HistoryUtils.goBackSync(getInstrumentation(), mWebContents, onPageFinishedHelper); // The title of the 'about.html' specified via historyUrl. assertEquals(CommonResources.ABOUT_TITLE, getTitleOnUiThread(mAwContents)); - } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/LoadUrlTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/LoadUrlTest.java index 410b9d3bdf4b9..519f63d360b35 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/LoadUrlTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/LoadUrlTest.java @@ -142,9 +142,8 @@ public void testLoadUrlWithExtraHeaders() throws Throwable { createAwTestContainerViewOnMainSync(contentsClient); final AwContents awContents = testContainerView.getAwContents(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String imagePath = "/" + CommonResources.FAVICON_FILENAME; webServer.setResponseBase64(imagePath, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); @@ -166,7 +165,7 @@ public void testLoadUrlWithExtraHeaders() throws Throwable { // Verify that extra headers are only passed for the main resource. validateNoRequestHeaders(extraHeaders, webServer.getLastRequest(imagePath)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -178,9 +177,8 @@ public void testNoOverridingOfExistingHeaders() throws Throwable { createAwTestContainerViewOnMainSync(contentsClient); final AwContents awContents = testContainerView.getAwContents(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String path = "/no_overriding_of_existing_headers_test.html"; final String url = webServer.setResponse( path, @@ -203,7 +201,7 @@ public void testNoOverridingOfExistingHeaders() throws Throwable { assertTrue(header.getValue().length() > 0); assertFalse(extraHeaders[1].equals(header.getValue())); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -215,9 +213,8 @@ public void testReloadWithExtraHeaders() throws Throwable { createAwTestContainerViewOnMainSync(contentsClient); final AwContents awContents = testContainerView.getAwContents(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String path = "/reload_with_extra_headers_test.html"; final String url = webServer.setResponse(path, "foo", @@ -237,7 +234,7 @@ public void testReloadWithExtraHeaders() throws Throwable { assertEquals(2, webServer.getRequestCount(path)); validateRequestHeaders(extraHeaders, webServer.getLastRequest(path)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -249,9 +246,8 @@ public void testRedirectAndReloadWithExtraHeaders() throws Throwable { createAwTestContainerViewOnMainSync(contentsClient); final AwContents awContents = testContainerView.getAwContents(); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String path = "/redirect_and_reload_with_extra_headers_test.html"; final String url = webServer.setResponse(path, "foo", @@ -276,7 +272,7 @@ public void testRedirectAndReloadWithExtraHeaders() throws Throwable { // No extra headers. This is consistent with legacy behavior. validateNoRequestHeaders(extraHeaders, webServer.getLastRequest(path)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } @@ -290,9 +286,8 @@ public void testRendererNavigationAndGoBackWithExtraHeaders() throws Throwable { final AwSettings settings = getAwSettingsOnUiThread(awContents); settings.setJavaScriptEnabled(true); - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); final String nextPath = "/next.html"; final String nextUrl = webServer.setResponse(nextPath, "Next!", @@ -328,7 +323,7 @@ public void testRendererNavigationAndGoBackWithExtraHeaders() throws Throwable { assertEquals(2, webServer.getRequestCount(path)); validateRequestHeaders(extraHeaders, webServer.getLastRequest(path)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/MediaAccessPermissionRequestTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/MediaAccessPermissionRequestTest.java index 05c528d599b6f..ba01f4a487a84 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/MediaAccessPermissionRequestTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/MediaAccessPermissionRequestTest.java @@ -58,7 +58,7 @@ public boolean canceled() { @Override protected void setUp() throws Exception { super.setUp(); - mTestWebServer = new TestWebServer(false); + mTestWebServer = TestWebServer.start(); mWebRTCPage = mTestWebServer.setResponse("/WebRTC", DATA, CommonResources.getTextHtmlHeaders(true)); } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/NavigationHistoryTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/NavigationHistoryTest.java index 2f81905b7764e..ebf929838414f 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/NavigationHistoryTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/NavigationHistoryTest.java @@ -46,7 +46,7 @@ public void setUp() throws Exception { final AwTestContainerView testContainerView = createAwTestContainerViewOnMainSync(mContentsClient); mAwContents = testContainerView.getAwContents(); - mWebServer = new TestWebServer(false); + mWebServer = TestWebServer.start(); } @Override diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java index 94339c2f45a96..8b3f74a84dc3a 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/SaveRestoreStateTest.java @@ -64,7 +64,7 @@ public void setUp() throws Exception { super.setUp(); mVars = createNewView(); mUrls = new String[NUM_NAVIGATIONS]; - mWebServer = new TestWebServer(false); + mWebServer = TestWebServer.start(); } @Override diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/WebKitHitTestTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/WebKitHitTestTest.java index 32ce543a8263e..eb0f179120e31 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/WebKitHitTestTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/WebKitHitTestTest.java @@ -38,7 +38,7 @@ public void setUp() throws Exception { mContentsClient = new TestAwContentsClient(); mTestView = createAwTestContainerViewOnMainSync(mContentsClient); mAwContents = mTestView.getAwContents(); - mWebServer = new TestWebServer(false); + mWebServer = TestWebServer.start(); } @Override diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/util/VideoTestWebServer.java b/android_webview/javatests/src/org/chromium/android_webview/test/util/VideoTestWebServer.java index 13f8f807b534a..5f244f402b43f 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/util/VideoTestWebServer.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/util/VideoTestWebServer.java @@ -34,7 +34,7 @@ public class VideoTestWebServer { private TestWebServer mTestWebServer; public VideoTestWebServer(Context context) throws Exception { - mTestWebServer = new TestWebServer(false); + mTestWebServer = TestWebServer.start(); List> headers = getWebmHeaders(true); mOnePixelOneFrameWebmURL = mTestWebServer.setResponseBase64("/" + ONE_PIXEL_ONE_FRAME_WEBM_FILENAME, diff --git a/content/public/android/javatests/src/org/chromium/content/browser/TransitionTest.java b/content/public/android/javatests/src/org/chromium/content/browser/TransitionTest.java index 086efd10eb19d..f304b05c951f0 100644 --- a/content/public/android/javatests/src/org/chromium/content/browser/TransitionTest.java +++ b/content/public/android/javatests/src/org/chromium/content/browser/TransitionTest.java @@ -174,48 +174,45 @@ public void testWillHandleDeferAfterResponseStartedNotCalled() throws Throwable */ @SmallTest public void testAddStylesheetToTransitionCalled() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - - final String url2 = webServer.setResponse(URL_2, URL_2_DATA, null); - ContentShellActivity activity = launchContentShellWithUrl(url2); - waitForActiveShellToBeDoneLoading(); - ContentViewCore contentViewCore = activity.getActiveContentViewCore(); - TestCallbackHelperContainer testCallbackHelperContainer = - new TestCallbackHelperContainer(contentViewCore); - contentViewCore.getWebContents().setHasPendingNavigationTransitionForTesting(); - TestNavigationTransitionDelegate delegate = - new TestNavigationTransitionDelegate(contentViewCore, true); - contentViewCore.getWebContents().setNavigationTransitionDelegate(delegate); - - int currentCallCount = testCallbackHelperContainer - .getOnPageFinishedHelper().getCallCount(); - String[] headers = { - "link", - ";rel=transition-entering-stylesheet;scope=*", - "link", - ";rel=transition-entering-stylesheet;scope=*", - "link", - ";rel=transition-entering-stylesheet;scope=*" - }; - final String url3 = webServer.setResponse(URL_3, - URL_3_DATA, - createHeadersList(headers)); - LoadUrlParams url3_params = new LoadUrlParams(url3); - loadUrl(contentViewCore, testCallbackHelperContainer, url3_params); - testCallbackHelperContainer.getOnPageFinishedHelper().waitForCallback( - currentCallCount, - 1, - 10000, - TimeUnit.MILLISECONDS); - - assertTrue("addStylesheetToTransition called.", - delegate.getDidCallAddStylesheet()); - assertTrue("Three stylesheets are added", - delegate.getTransitionStylesheets().size() == 3); + final String url2 = webServer.setResponse(URL_2, URL_2_DATA, null); + ContentShellActivity activity = launchContentShellWithUrl(url2); + waitForActiveShellToBeDoneLoading(); + ContentViewCore contentViewCore = activity.getActiveContentViewCore(); + TestCallbackHelperContainer testCallbackHelperContainer = + new TestCallbackHelperContainer(contentViewCore); + contentViewCore.getWebContents().setHasPendingNavigationTransitionForTesting(); + TestNavigationTransitionDelegate delegate = + new TestNavigationTransitionDelegate(contentViewCore, true); + contentViewCore.getWebContents().setNavigationTransitionDelegate(delegate); + + int currentCallCount = testCallbackHelperContainer + .getOnPageFinishedHelper().getCallCount(); + String[] headers = { + "link", + ";rel=transition-entering-stylesheet;scope=*", + "link", + ";rel=transition-entering-stylesheet;scope=*", + "link", + ";rel=transition-entering-stylesheet;scope=*" + }; + final String url3 = webServer.setResponse(URL_3, + URL_3_DATA, + createHeadersList(headers)); + LoadUrlParams url3_params = new LoadUrlParams(url3); + loadUrl(contentViewCore, testCallbackHelperContainer, url3_params); + testCallbackHelperContainer.getOnPageFinishedHelper().waitForCallback( + currentCallCount, + 1, + 10000, + TimeUnit.MILLISECONDS); + + assertTrue("addStylesheetToTransition called.", + delegate.getDidCallAddStylesheet()); + assertTrue("Three stylesheets are added", + delegate.getTransitionStylesheets().size() == 3); } finally { - if (webServer != null) webServer.shutdown(); } } @@ -226,38 +223,35 @@ public void testAddStylesheetToTransitionCalled() throws Throwable { */ @SmallTest public void testAddStylesheetToTransitionNotCalled() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - - final String url2 = webServer.setResponse(URL_2, URL_2_DATA, null); - ContentShellActivity activity = launchContentShellWithUrl(url2); - waitForActiveShellToBeDoneLoading(); - ContentViewCore contentViewCore = activity.getActiveContentViewCore(); - TestCallbackHelperContainer testCallbackHelperContainer = - new TestCallbackHelperContainer(contentViewCore); - contentViewCore.getWebContents().setHasPendingNavigationTransitionForTesting(); - TestNavigationTransitionDelegate delegate = - new TestNavigationTransitionDelegate(contentViewCore, true); - contentViewCore.getWebContents().setNavigationTransitionDelegate(delegate); - - int currentCallCount = testCallbackHelperContainer - .getOnPageFinishedHelper().getCallCount(); - final String url3 = webServer.setResponse(URL_3, URL_3_DATA, null); - LoadUrlParams url3_params = new LoadUrlParams(url3); - loadUrl(contentViewCore, testCallbackHelperContainer, url3_params); - testCallbackHelperContainer.getOnPageFinishedHelper().waitForCallback( - currentCallCount, - 1, - 10000, - TimeUnit.MILLISECONDS); - - assertFalse("addStylesheetToTransition is not called.", - delegate.getDidCallAddStylesheet()); - assertTrue("No stylesheets are added", - delegate.getTransitionStylesheets().size() == 0); + final String url2 = webServer.setResponse(URL_2, URL_2_DATA, null); + ContentShellActivity activity = launchContentShellWithUrl(url2); + waitForActiveShellToBeDoneLoading(); + ContentViewCore contentViewCore = activity.getActiveContentViewCore(); + TestCallbackHelperContainer testCallbackHelperContainer = + new TestCallbackHelperContainer(contentViewCore); + contentViewCore.getWebContents().setHasPendingNavigationTransitionForTesting(); + TestNavigationTransitionDelegate delegate = + new TestNavigationTransitionDelegate(contentViewCore, true); + contentViewCore.getWebContents().setNavigationTransitionDelegate(delegate); + + int currentCallCount = testCallbackHelperContainer + .getOnPageFinishedHelper().getCallCount(); + final String url3 = webServer.setResponse(URL_3, URL_3_DATA, null); + LoadUrlParams url3_params = new LoadUrlParams(url3); + loadUrl(contentViewCore, testCallbackHelperContainer, url3_params); + testCallbackHelperContainer.getOnPageFinishedHelper().waitForCallback( + currentCallCount, + 1, + 10000, + TimeUnit.MILLISECONDS); + + assertFalse("addStylesheetToTransition is not called.", + delegate.getDidCallAddStylesheet()); + assertTrue("No stylesheets are added", + delegate.getTransitionStylesheets().size() == 0); } finally { - if (webServer != null) webServer.shutdown(); } } @@ -268,10 +262,8 @@ public void testAddStylesheetToTransitionNotCalled() throws Throwable { */ @SmallTest public void testParseTransitionEnteringColor() throws Throwable { - TestWebServer webServer = null; + TestWebServer webServer = TestWebServer.start(); try { - webServer = new TestWebServer(false); - final String url2 = webServer.setResponse(URL_2, URL_2_DATA, null); ContentShellActivity activity = launchContentShellWithUrl(url2); waitForActiveShellToBeDoneLoading(); @@ -307,7 +299,7 @@ public void testParseTransitionEnteringColor() throws Throwable { delegate.getTransitionEnteringColor(), transitionEnteringColor)); } finally { - if (webServer != null) webServer.shutdown(); + webServer.shutdown(); } } } diff --git a/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java b/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java index 36972e6ba9bd5..ede16a16efe5e 100644 --- a/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java +++ b/net/test/android/javatests/src/org/chromium/net/test/util/TestWebServer.java @@ -60,7 +60,7 @@ * * Based heavily on the CTSWebServer in Android. */ -public final class TestWebServer { +public class TestWebServer { private static final String TAG = "TestWebServer"; public static final String SHUTDOWN_PREFIX = "/shutdown"; @@ -72,6 +72,7 @@ public final class TestWebServer { private final ServerThread mServerThread; private String mServerUri; private final boolean mSsl; + private final int mPort; private static class Response { final byte[] mResponseData; @@ -100,10 +101,13 @@ private static class Response { /** * Create and start a local HTTP server instance. + * @param port Port number the server must use, or 0 to automatically choose a free port. * @param ssl True if the server should be using secure sockets. * @throws Exception */ - public TestWebServer(boolean ssl) throws Exception { + private TestWebServer(int port, boolean ssl) throws Exception { + mPort = port; + mSsl = ssl; if (mSsl) { mServerUri = "https:"; @@ -117,16 +121,50 @@ public TestWebServer(boolean ssl) throws Exception { } } - setInstance(this, mSsl); - mServerThread = new ServerThread(this, mSsl); - mServerThread.start(); + mServerThread = new ServerThread(this, mPort, mSsl); mServerUri += "//localhost:" + mServerThread.mSocket.getLocalPort(); } + public static TestWebServer start(int port) throws Exception { + if (sInstance != null) { + throw new IllegalStateException("Tried to start multiple TestWebServers"); + } + + TestWebServer server = new TestWebServer(port, false); + server.mServerThread.start(); + setInstance(server); + return server; + } + + public static TestWebServer start() throws Exception { + return start(0); + } + + public static TestWebServer startSsl(int port) throws Exception { + if (sSecureInstance != null) { + throw new IllegalStateException("Tried to start multiple SSL TestWebServers"); + } + + TestWebServer server = new TestWebServer(port, true); + server.mServerThread.start(); + setSecureInstance(server); + return server; + } + + public static TestWebServer startSsl() throws Exception { + return startSsl(0); + } + /** * Terminate the http server. */ public void shutdown() { + if (mSsl) { + setSecureInstance(null); + } else { + setInstance(null); + } + try { // Avoid a deadlock between two threads where one is trying to call // close() and the other one is calling accept() by sending a GET @@ -154,16 +192,17 @@ public void shutdown() { } catch (KeyManagementException e) { throw new IllegalStateException(e); } + } - setInstance(null, mSsl); + // Setting static variables from instance methods causes findbugs warnings. Calling static + // methods which set static variables from instance methods isn't any better, but it silences + // the warnings. + private static void setInstance(TestWebServer instance) { + sInstance = instance; } - private static void setInstance(TestWebServer instance, boolean isSsl) { - if (isSsl) { - sSecureInstance = instance; - } else { - sInstance = instance; - } + private static void setSecureInstance(TestWebServer instance) { + sSecureInstance = instance; } private static final int RESPONSE_STATUS_NORMAL = 0; @@ -542,7 +581,7 @@ private KeyManager[] getKeyManagers() throws Exception { } - public ServerThread(TestWebServer server, boolean ssl) throws Exception { + public ServerThread(TestWebServer server, int port, boolean ssl) throws Exception { super("ServerThread"); mServer = server; mIsSsl = ssl; @@ -552,9 +591,9 @@ public ServerThread(TestWebServer server, boolean ssl) throws Exception { if (mIsSsl) { mSslContext = SSLContext.getInstance("TLS"); mSslContext.init(getKeyManagers(), null, null); - mSocket = mSslContext.getServerSocketFactory().createServerSocket(0); + mSocket = mSslContext.getServerSocketFactory().createServerSocket(port); } else { - mSocket = new ServerSocket(0); + mSocket = new ServerSocket(port); } return; } catch (IOException e) {