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.content.Context;
     27 import android.cts.util.WidgetTestUtils;
     28 import android.graphics.Canvas;
     29 import android.graphics.Movie;
     30 import android.graphics.Paint;
     31 import android.test.ActivityInstrumentationTestCase2;
     32 
     33 public class MovieTest extends ActivityInstrumentationTestCase2<MockActivity> {
     34     private Movie mMovie;
     35     private final int MOVIE = com.android.cts.graphics.R.drawable.animated;
     36 
     37     public MovieTest() {
     38         super("com.android.cts.graphics", MockActivity.class);
     39     }
     40 
     41     @Override
     42     protected void setUp() throws Exception {
     43         super.setUp();
     44         mMovie = getActivity().getResources().getMovie(MOVIE);
     45     }
     46 
     47     public void testDraw1() {
     48         Canvas c = new Canvas();
     49         Paint p = new Paint();
     50         mMovie.draw(c, 100, 200, p);
     51     }
     52 
     53     public void testDraw2() {
     54         Canvas c = new Canvas();
     55         mMovie.draw(c, 100, 200);
     56     }
     57 
     58     public void testDecodeFile() throws Exception {
     59         mMovie = null;
     60         File dbDir = getInstrumentation().getTargetContext().getDir("tests",
     61                 Context.MODE_PRIVATE);
     62         File imagefile = new File(dbDir, "animated.gif");
     63         if (imagefile.exists()) {
     64             imagefile.delete();
     65         }
     66         writeSampleImage(imagefile);
     67         mMovie = Movie.decodeFile(imagefile.getPath());
     68         assertNotNull(mMovie);
     69 
     70         mMovie = null;
     71         mMovie = Movie.decodeFile("/no file path");
     72         assertNull(mMovie);
     73     }
     74 
     75     private void writeSampleImage(File imagefile) throws Exception {
     76         InputStream source = null;
     77         OutputStream target = null;
     78 
     79         try {
     80             source = getActivity().getResources().openRawResource(MOVIE);
     81             target = new FileOutputStream(imagefile);
     82 
     83             byte[] buffer = new byte[1024];
     84             for (int len = source.read(buffer); len > 0; len = source
     85                     .read(buffer)) {
     86                 target.write(buffer, 0, len);
     87             }
     88         } finally {
     89             if (source != null) {
     90                 source.close();
     91             }
     92             if (target != null) {
     93                 target.close();
     94             }
     95         }
     96     }
     97 
     98     private byte[] inputStreamToBytes(InputStream in) throws IOException {
     99         ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    100         byte[] buffer = new byte[1024];
    101         int len;
    102         while ((len = in.read(buffer)) >= 0) {
    103             out.write(buffer, 0, len);
    104         }
    105         in.close();
    106         out.close();
    107         return out.toByteArray();
    108 
    109     }
    110 
    111     public void testDecodeByteArray() throws Exception {
    112         mMovie = null;
    113         InputStream is = getActivity().getResources().openRawResource(MOVIE);
    114         byte[] bytes = inputStreamToBytes(is);
    115         mMovie = Movie.decodeByteArray(bytes, 0, bytes.length);
    116         is.close();
    117         assertNotNull(mMovie);
    118     }
    119 
    120     public void testDecodeStream() {
    121         assertFalse(mMovie.isOpaque());
    122         mMovie = null;
    123         try {
    124             InputStream is = getActivity().getResources()
    125                     .openRawResource(MOVIE);
    126             mMovie = Movie.decodeStream(is);
    127             is.close();
    128         } catch (Exception e) {
    129             fail("shouldn't throw exception");
    130         }
    131         assertNotNull(mMovie);
    132     }
    133 
    134     public void testSetTime() {
    135         assertTrue(mMovie.setTime(1000));
    136         assertFalse(mMovie.setTime(Integer.MAX_VALUE));
    137         assertFalse(mMovie.setTime(Integer.MIN_VALUE));
    138         assertFalse(mMovie.setTime(-1));
    139     }
    140 
    141     public void testGetMovieProperties() {
    142         assertEquals(1000, mMovie.duration());
    143         assertFalse(mMovie.isOpaque());
    144 
    145         int expectedHeight = getActivity().getResources().getDrawable(MOVIE).getIntrinsicHeight();
    146         int scaledHeight = WidgetTestUtils.convertDipToPixels(getActivity(), mMovie.height());
    147         assertEquals(expectedHeight, scaledHeight);
    148 
    149         int expectedWidth = getActivity().getResources().getDrawable(MOVIE).getIntrinsicWidth();
    150         int scaledWidth = WidgetTestUtils.convertDipToPixels(getActivity(), mMovie.width());
    151         assertEquals(expectedWidth, scaledWidth);
    152 
    153     }
    154 }