@SmallTest @Feature({"AndroidWebView"}) public void testCalledForImage() throws Throwable { final TestAwContentsClient contentsClient = new TestAwContentsClient(); final AwTestContainerView testContainerView = createAwTestContainerViewOnMainSync(contentsClient); final AwContents awContents = testContainerView.getAwContents(); final TestAwContentsClient.ShouldInterceptRequestHelper shouldInterceptRequestHelper = contentsClient.getShouldInterceptRequestHelper(); final String imagePath = "/" + CommonResources.FAVICON_FILENAME; mWebServer.setResponseBase64( imagePath, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); final String pageWithImage = addPageToTestServer( mWebServer, "/page_with_image.html", CommonResources.getOnImageLoadedHtml(CommonResources.FAVICON_FILENAME)); int callCount = shouldInterceptRequestHelper.getCallCount(); loadUrlSync(awContents, contentsClient.getOnPageFinishedHelper(), pageWithImage); shouldInterceptRequestHelper.waitForCallback(callCount, 2); assertEquals(2, shouldInterceptRequestHelper.getUrls().size()); assertTrue( shouldInterceptRequestHelper.getUrls().get(1).endsWith(CommonResources.FAVICON_FILENAME)); }
@SmallTest public void testReceiveBasicFavicon() throws Throwable { int callCount = mContentsClient.getFaviconHelper().getCallCount(); final String faviconUrl = mWebServer.setResponseBase64( FAVICON1_URL, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); final String pageUrl = mWebServer.setResponse( FAVICON1_PAGE_URL, FAVICON1_PAGE_HTML, CommonResources.getTextHtmlHeaders(true)); loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); mContentsClient.getFaviconHelper().waitForCallback(callCount); assertEquals(1, mWebServer.getRequestCount(FAVICON1_URL)); Object originalFaviconSource = (new URL(faviconUrl)).getContent(); Bitmap originalFavicon = BitmapFactory.decodeStream((InputStream) originalFaviconSource); assertNotNull(originalFavicon); assertNotNull(mContentsClient.getFaviconHelper().getIcon()); assertTrue(mContentsClient.getFaviconHelper().getIcon().sameAs(originalFavicon)); // Make sure the request counter for favicon is incremented when the page is loaded again // successfully. loadUrlAsync(mAwContents, pageUrl); mContentsClient.getFaviconHelper().waitForCallback(callCount); assertEquals(2, mWebServer.getRequestCount(FAVICON1_URL)); }
@Override public void setUp() throws Exception { super.setUp(); mContentsClient = new TestAwContentsClient(); mTestView = createAwTestContainerViewOnMainSync(mContentsClient); mAwContents = mTestView.getAwContents(); mWebServer = TestWebServer.start(); final String imagePath = "/" + CommonResources.TEST_IMAGE_FILENAME; mWebServer.setResponseBase64( imagePath, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); }
@SmallTest @Feature({"AndroidWebView"}) public void testGetFavicon() throws Throwable { final AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient); final AwContents awContents = testView.getAwContents(); TestWebServer webServer = null; try { webServer = new TestWebServer(false); final String faviconUrl = webServer.setResponseBase64( "/" + CommonResources.FAVICON_FILENAME, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(false)); final String pageUrl = webServer.setResponse("/favicon.html", CommonResources.FAVICON_STATIC_HTML, null); // The getFavicon will return the right icon a certain time after // the page load completes which makes it slightly hard to test. final Bitmap defaultFavicon = awContents.getFavicon(); getAwSettingsOnUiThread(awContents).setImagesEnabled(true); loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); pollOnUiThread( new Callable<Boolean>() { @Override public Boolean call() { return awContents.getFavicon() != null && !awContents.getFavicon().sameAs(defaultFavicon); } }); final Object originalFaviconSource = (new URL(faviconUrl)).getContent(); final Bitmap originalFavicon = BitmapFactory.decodeStream((InputStream) originalFaviconSource); assertNotNull(originalFavicon); assertTrue(awContents.getFavicon().sameAs(originalFavicon)); } finally { if (webServer != null) webServer.shutdown(); } }
@SmallTest @Feature({"ShouldInterceptLoadRequest"}) public void testNoOnReceivedErrorCallback() throws Throwable { final String imagePath = "/" + CommonResources.FAVICON_FILENAME; final String imageUrl = mWebServer.setResponseBase64( imagePath, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); final String pageWithImage = addPageToTestServer( mWebServer, "/page_with_image.html", CommonResources.getOnImageLoadedHtml(CommonResources.FAVICON_FILENAME)); final OnReceivedErrorHelper onReceivedErrorHelper = mTestHelperBridge.getOnReceivedErrorHelper(); mShouldInterceptLoadRequestHelper.setReturnValueForUrl( imageUrl, new WebResourceResponse(null, null, null)); int onReceivedErrorHelperCallCount = onReceivedErrorHelper.getCallCount(); loadUrlSync(pageWithImage); assertEquals(onReceivedErrorHelperCallCount, onReceivedErrorHelper.getCallCount()); }
@SmallTest @Feature({"ShouldInterceptLoadRequest"}) public void testCalledForImage() throws Throwable { final String imagePath = "/" + CommonResources.FAVICON_FILENAME; mWebServer.setResponseBase64( imagePath, CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); final String pageWithImage = addPageToTestServer( mWebServer, "/page_with_image.html", CommonResources.getOnImageLoadedHtml(CommonResources.FAVICON_FILENAME)); int callCount = mShouldInterceptLoadRequestHelper.getCallCount(); loadUrlSync(pageWithImage); mShouldInterceptLoadRequestHelper.waitForCallback(callCount, 2); assertEquals(2, mShouldInterceptLoadRequestHelper.getUrls().size()); assertTrue( mShouldInterceptLoadRequestHelper .getUrls() .get(1) .endsWith(CommonResources.FAVICON_FILENAME)); }