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.widget.cts;
     18 
     19 import static org.junit.Assert.assertNotNull;
     20 import static org.junit.Assert.assertNull;
     21 import static org.junit.Assert.assertSame;
     22 import static org.junit.Assert.fail;
     23 
     24 import android.app.Activity;
     25 import android.content.Context;
     26 import android.content.res.Resources;
     27 import android.graphics.Bitmap;
     28 import android.graphics.drawable.BitmapDrawable;
     29 import android.graphics.drawable.Drawable;
     30 import android.net.Uri;
     31 import android.support.test.annotation.UiThreadTest;
     32 import android.support.test.filters.MediumTest;
     33 import android.support.test.rule.ActivityTestRule;
     34 import android.support.test.runner.AndroidJUnit4;
     35 import android.util.AttributeSet;
     36 import android.util.Xml;
     37 import android.widget.ImageSwitcher;
     38 import android.widget.ImageView;
     39 
     40 import com.android.compatibility.common.util.WidgetTestUtils;
     41 
     42 import org.junit.Before;
     43 import org.junit.Rule;
     44 import org.junit.Test;
     45 import org.junit.runner.RunWith;
     46 import org.xmlpull.v1.XmlPullParser;
     47 
     48 import java.io.File;
     49 import java.io.FileOutputStream;
     50 import java.io.IOException;
     51 import java.io.InputStream;
     52 import java.io.OutputStream;
     53 
     54 /**
     55  * Test {@link ImageSwitcher}.
     56  */
     57 @MediumTest
     58 @RunWith(AndroidJUnit4.class)
     59 public class ImageSwitcherTest {
     60     private Activity mActivity;
     61     private ImageSwitcher mImageSwitcher;
     62 
     63     @Rule
     64     public ActivityTestRule<ImageSwitcherCtsActivity> mActivityRule =
     65             new ActivityTestRule<>(ImageSwitcherCtsActivity.class);
     66 
     67     @Before
     68     public void setup() {
     69         mActivity = mActivityRule.getActivity();
     70         mImageSwitcher = (ImageSwitcher) mActivity.findViewById(R.id.switcher);
     71     }
     72 
     73     @Test
     74     public void testConstructor() {
     75         new ImageSwitcher(mActivity);
     76 
     77         new ImageSwitcher(mActivity, null);
     78 
     79         XmlPullParser parser = mActivity.getResources().getXml(R.layout.imageswitcher_layout);
     80         AttributeSet attrs = Xml.asAttributeSet(parser);
     81         assertNotNull(attrs);
     82         new ImageSwitcher(mActivity, attrs);
     83     }
     84 
     85     @UiThreadTest
     86     @Test
     87     public void testSetImageResource() {
     88         ImageView iv = new ImageView(mActivity);
     89         mImageSwitcher.addView(iv);
     90         ImageView iv1 = new ImageView(mActivity);
     91         mImageSwitcher.addView(iv1);
     92 
     93         assertSame(iv, mImageSwitcher.getCurrentView());
     94         mImageSwitcher.setImageResource(R.drawable.scenery);
     95         assertSame(iv1, mImageSwitcher.getCurrentView());
     96         Resources resources = mActivity.getResources();
     97         Drawable drawable = resources.getDrawable(R.drawable.scenery);
     98         BitmapDrawable sceneryBitmap = (BitmapDrawable) drawable;
     99         BitmapDrawable currViewBitmap =
    100             (BitmapDrawable) ((ImageView) mImageSwitcher.getCurrentView()).getDrawable();
    101         WidgetTestUtils.assertEquals(sceneryBitmap.getBitmap(), currViewBitmap.getBitmap());
    102 
    103         mImageSwitcher.setImageResource(R.drawable.testimage);
    104         assertSame(iv, mImageSwitcher.getCurrentView());
    105         drawable = resources.getDrawable(R.drawable.testimage);
    106         BitmapDrawable testimageBitmap = (BitmapDrawable) drawable;
    107         currViewBitmap =
    108             (BitmapDrawable) ((ImageView) mImageSwitcher.getCurrentView()).getDrawable();
    109         WidgetTestUtils.assertEquals(testimageBitmap.getBitmap(), currViewBitmap.getBitmap());
    110 
    111         mImageSwitcher.setImageResource(-1);
    112         assertNull(((ImageView) mImageSwitcher.getCurrentView()).getDrawable());
    113     }
    114 
    115     @UiThreadTest
    116     @Test
    117     public void testSetImageURI() {
    118         ImageView iv = new ImageView(mActivity);
    119         mImageSwitcher.addView(iv);
    120         ImageView iv1 = new ImageView(mActivity);
    121         mImageSwitcher.addView(iv1);
    122 
    123         File dbDir = mActivity.getDir("tests", Context.MODE_PRIVATE);
    124         File imagefile = new File(dbDir, "tempimage.jpg");
    125         if (imagefile.exists()) {
    126             imagefile.delete();
    127         }
    128         createSampleImage(imagefile, R.raw.testimage);
    129 
    130         assertSame(iv, mImageSwitcher.getCurrentView());
    131         Uri uri = Uri.parse(imagefile.getPath());
    132         mImageSwitcher.setImageURI(uri);
    133         assertSame(iv1, mImageSwitcher.getCurrentView());
    134 
    135         BitmapDrawable currViewBitmap =
    136             (BitmapDrawable) ((ImageView) mImageSwitcher.getCurrentView()).getDrawable();
    137         Bitmap testImageBitmap = WidgetTestUtils.getUnscaledAndDitheredBitmap(
    138                 mActivity.getResources(), R.raw.testimage,
    139                 currViewBitmap.getBitmap().getConfig());
    140         WidgetTestUtils.assertEquals(testImageBitmap, currViewBitmap.getBitmap());
    141 
    142         createSampleImage(imagefile, R.raw.scenery);
    143         uri = Uri.parse(imagefile.getPath());
    144         mImageSwitcher.setImageURI(uri);
    145         assertSame(iv, mImageSwitcher.getCurrentView());
    146         Bitmap sceneryImageBitmap = WidgetTestUtils.getUnscaledAndDitheredBitmap(
    147                 mActivity.getResources(), R.raw.scenery,
    148                 currViewBitmap.getBitmap().getConfig());
    149         currViewBitmap =
    150             (BitmapDrawable) ((ImageView) mImageSwitcher.getCurrentView()).getDrawable();
    151         WidgetTestUtils.assertEquals(sceneryImageBitmap, currViewBitmap.getBitmap());
    152 
    153         imagefile.delete();
    154 
    155         mImageSwitcher.setImageURI(null);
    156     }
    157 
    158     @UiThreadTest
    159     @Test
    160     public void testSetImageDrawable() {
    161         ImageView iv = new ImageView(mActivity);
    162         mImageSwitcher.addView(iv);
    163         ImageView iv1 = new ImageView(mActivity);
    164         mImageSwitcher.addView(iv1);
    165 
    166         Resources resources = mActivity.getResources();
    167         assertSame(iv, mImageSwitcher.getCurrentView());
    168         Drawable drawable = resources.getDrawable(R.drawable.scenery);
    169         mImageSwitcher.setImageDrawable(drawable);
    170         assertSame(iv1, mImageSwitcher.getCurrentView());
    171         assertSame(drawable, ((ImageView) mImageSwitcher.getCurrentView()).getDrawable());
    172 
    173         drawable = resources.getDrawable(R.drawable.testimage);
    174         mImageSwitcher.setImageDrawable(drawable);
    175         assertSame(iv, mImageSwitcher.getCurrentView());
    176         assertSame(drawable, ((ImageView) mImageSwitcher.getCurrentView()).getDrawable());
    177 
    178         mImageSwitcher.setImageDrawable(null);
    179     }
    180 
    181     private void createSampleImage(File imagefile, int resid) {
    182         try (InputStream source = mActivity.getResources().openRawResource(resid);
    183              OutputStream target = new FileOutputStream(imagefile)) {
    184             byte[] buffer = new byte[1024];
    185             for (int len = source.read(buffer); len > 0; len = source.read(buffer)) {
    186                 target.write(buffer, 0, len);
    187             }
    188         } catch (IOException e) {
    189             fail(e.getMessage());
    190         }
    191     }
    192 }
    193