Home | History | Annotate | Download | only in test
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 package org.chromium.android_webview.test;
      6 
      7 import android.content.Context;
      8 import android.graphics.Bitmap;
      9 import android.graphics.BitmapFactory;
     10 import android.test.suitebuilder.annotation.SmallTest;
     11 import android.util.Log;
     12 
     13 import org.chromium.android_webview.AwWebResourceResponse;
     14 import org.chromium.android_webview.DefaultVideoPosterRequestHandler;
     15 import org.chromium.base.test.util.Feature;
     16 import org.chromium.content.browser.test.util.CallbackHelper;
     17 
     18 import java.io.IOException;
     19 import java.io.InputStream;
     20 import java.util.concurrent.TimeoutException;
     21 
     22 /**
     23  * Tests for AwContentClient.GetDefaultVideoPoster.
     24  */
     25 public class AwContentsClientGetDefaultVideoPosterTest extends AwTestBase {
     26     private static final String TAG = "AwContentsClientGetDefaultVideoPosterTest";
     27 
     28     private static class DefaultVideoPosterClient extends TestAwContentsClient {
     29         private CallbackHelper mVideoPosterCallbackHelper = new CallbackHelper();
     30         private Bitmap mPoster;
     31         private Context mContext;
     32 
     33         public DefaultVideoPosterClient(Context context) {
     34             mContext = context;
     35         }
     36 
     37         @Override
     38         public Bitmap getDefaultVideoPoster() {
     39             mVideoPosterCallbackHelper.notifyCalled();
     40             return getPoster();
     41         }
     42 
     43         public void waitForGetDefaultVideoPosterCalled() throws InterruptedException,
     44                 TimeoutException {
     45             mVideoPosterCallbackHelper.waitForCallback(0);
     46         }
     47 
     48         public Bitmap getPoster() {
     49             if (mPoster == null) {
     50                 try {
     51                     mPoster = BitmapFactory.decodeStream(
     52                             mContext.getAssets().open("asset_icon.png"));
     53                 } catch (IOException e) {
     54                     Log.e(TAG, null, e);
     55                 }
     56             }
     57             return mPoster;
     58         }
     59     }
     60 
     61     @Feature({"AndroidWebView"})
     62     @SmallTest
     63     public void testGetDefaultVideoPoster() throws Throwable {
     64         DefaultVideoPosterClient contentsClient =
     65                 new DefaultVideoPosterClient(getInstrumentation().getContext());
     66         AwTestContainerView testContainerView =
     67                 createAwTestContainerViewOnMainSync(contentsClient);
     68         String data = "<html><head><body><video id='video' control src='' /> </body></html>";
     69         loadDataAsync(testContainerView.getAwContents(), data, "text/html", false);
     70         contentsClient.waitForGetDefaultVideoPosterCalled();
     71     }
     72 
     73     @Feature({"AndroidWebView"})
     74     @SmallTest
     75     public void testInterceptDefaultVidoePosterURL() throws Throwable {
     76         DefaultVideoPosterClient contentsClient =
     77                 new DefaultVideoPosterClient(getInstrumentation().getTargetContext());
     78         DefaultVideoPosterRequestHandler handler =
     79                 new DefaultVideoPosterRequestHandler(contentsClient);
     80         AwWebResourceResponse requestData =
     81                 handler.shouldInterceptRequest(handler.getDefaultVideoPosterURL());
     82         assertTrue(requestData.getMimeType().equals("image/png"));
     83         Bitmap bitmap = BitmapFactory.decodeStream(requestData.getData());
     84         Bitmap poster = contentsClient.getPoster();
     85         assertEquals("poster.getHeight() not equal to bitmap.getHeight()",
     86                 poster.getHeight(), bitmap.getHeight());
     87         assertEquals("poster.getWidth() not equal to bitmap.getWidth()",
     88                 poster.getWidth(), bitmap.getWidth());
     89     }
     90 
     91     @Feature({"AndroidWebView"})
     92     @SmallTest
     93     public void testNoDefaultVideoPoster() throws Throwable {
     94         NullContentsClient contentsClient = new NullContentsClient();
     95         DefaultVideoPosterRequestHandler handler =
     96                 new DefaultVideoPosterRequestHandler(contentsClient);
     97         AwWebResourceResponse requestData =
     98                 handler.shouldInterceptRequest(handler.getDefaultVideoPosterURL());
     99         assertTrue(requestData.getMimeType().equals("image/png"));
    100         InputStream in = requestData.getData();
    101         assertEquals("Should get -1", in.read(), -1);
    102     }
    103 }
    104