Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.graphics.cts;
     18 
     19 import java.io.ByteArrayOutputStream;
     20 import java.io.File;
     21 import java.io.FileOutputStream;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 import java.io.OutputStream;
     25 
     26 import android.app.cts.MockActivity;
     27 import android.content.Context;
     28 import android.graphics.Canvas;
     29 import android.graphics.Movie;
     30 import android.graphics.Paint;
     31 import android.test.ActivityInstrumentationTestCase2;
     32 import android.widget.cts.WidgetTestUtils;
     33 
     34 
     35 public class MovieTest extends ActivityInstrumentationTestCase2<MockActivity> {
     36     private Movie mMovie;
     37     private final int MOVIE = com.android.cts.stub.R.drawable.animated;
     38 
     39     public MovieTest() {
     40         super("com.android.cts.stub", MockActivity.class);
     41     }
     42 
     43     @Override
     44     protected void setUp() throws Exception {
     45         super.setUp();
     46         mMovie = getActivity().getResources().getMovie(MOVIE);
     47     }
     48 
     49     public void testDraw1() {
     50         Canvas c = new Canvas();
     51         Paint p = new Paint();
     52         mMovie.draw(c, 100, 200, p);
     53     }
     54 
     55     public void testDraw2() {
     56         Canvas c = new Canvas();
     57         mMovie.draw(c, 100, 200);
     58     }
     59 
     60     public void testDecodeFile() throws Exception {
     61         mMovie = null;
     62         File dbDir = getInstrumentation().getTargetContext().getDir("tests",
     63                 Context.MODE_PRIVATE);
     64         File imagefile = new File(dbDir, "animated.gif");
     65         if (imagefile.exists()) {
     66             imagefile.delete();
     67         }
     68         writeSampleImage(imagefile);
     69         mMovie = Movie.decodeFile(imagefile.getPath());
     70         assertNotNull(mMovie);
     71 
     72         mMovie = null;
     73         mMovie = Movie.decodeFile("/no file path");
     74         assertNull(mMovie);
     75     }
     76 
     77     private void writeSampleImage(File imagefile) throws Exception {
     78         InputStream source = null;
     79         OutputStream target = null;
     80 
     81         try {
     82             source = getActivity().getResources().openRawResource(MOVIE);
     83             target = new FileOutputStream(imagefile);
     84 
     85             byte[] buffer = new byte[1024];
     86             for (int len = source.read(buffer); len > 0; len = source
     87                     .read(buffer)) {
     88                 target.write(buffer, 0, len);
     89             }
     90         } finally {
     91             if (source != null) {
     92                 source.close();
     93             }
     94             if (target != null) {
     95                 target.close();
     96             }
     97         }
     98     }
     99 
    100     private byte[] inputStreamToBytes(InputStream in) throws IOException {
    101         ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    102         byte[] buffer = new byte[1024];
    103         int len;
    104         while ((len = in.read(buffer)) >= 0) {
    105             out.write(buffer, 0, len);
    106         }
    107         in.close();
    108         out.close();
    109         return out.toByteArray();
    110 
    111     }
    112 
    113     public void testDecodeByteArray() throws Exception {
    114         mMovie = null;
    115         InputStream is = getActivity().getResources().openRawResource(MOVIE);
    116         byte[] bytes = inputStreamToBytes(is);
    117         mMovie = Movie.decodeByteArray(bytes, 0, bytes.length);
    118         is.close();
    119         assertNotNull(mMovie);
    120     }
    121 
    122     public void testDecodeStream() {
    123         assertFalse(mMovie.isOpaque());
    124         mMovie = null;
    125         try {
    126             InputStream is = getActivity().getResources()
    127                     .openRawResource(MOVIE);
    128             mMovie = Movie.decodeStream(is);
    129             is.close();
    130         } catch (Exception e) {
    131             fail("shouldn't throw exception");
    132         }
    133         assertNotNull(mMovie);
    134     }
    135 
    136     public void testSetTime() {
    137         assertTrue(mMovie.setTime(1000));
    138         assertFalse(mMovie.setTime(Integer.MAX_VALUE));
    139         assertFalse(mMovie.setTime(Integer.MIN_VALUE));
    140         assertFalse(mMovie.setTime(-1));
    141     }
    142 
    143     public void testGetMovieProperties() {
    144         assertEquals(1000, mMovie.duration());
    145         assertFalse(mMovie.isOpaque());
    146 
    147         int expectedHeight = getActivity().getResources().getDrawable(MOVIE).getIntrinsicHeight();
    148         int scaledHeight = WidgetTestUtils.convertDipToPixels(getActivity(), mMovie.height());
    149         assertEquals(expectedHeight, scaledHeight);
    150 
    151         int expectedWidth = getActivity().getResources().getDrawable(MOVIE).getIntrinsicWidth();
    152         int scaledWidth = WidgetTestUtils.convertDipToPixels(getActivity(), mMovie.width());
    153         assertEquals(expectedWidth, scaledWidth);
    154 
    155     }
    156 }