Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2019 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.transition.cts;
     18 
     19 import android.app.SharedElementCallback;
     20 import android.content.Context;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Color;
     23 import android.graphics.ColorSpace;
     24 import android.graphics.Matrix;
     25 import android.graphics.RectF;
     26 import android.graphics.drawable.BitmapDrawable;
     27 import android.graphics.drawable.Drawable;
     28 import android.os.Parcelable;
     29 import android.view.View;
     30 import android.widget.ImageView;
     31 
     32 import androidx.test.InstrumentationRegistry;
     33 import androidx.test.runner.AndroidJUnit4;
     34 
     35 import static org.junit.Assert.assertNotNull;
     36 import static org.junit.Assert.assertSame;
     37 import static org.junit.Assert.assertTrue;
     38 
     39 import org.junit.Test;
     40 import org.junit.runner.RunWith;
     41 
     42 @RunWith(AndroidJUnit4.class)
     43 public class SharedElementCallbackTest {
     44     private static class Callback extends SharedElementCallback {}
     45 
     46     @Test
     47     public void testSnapshot() {
     48         final int SIZE = 10;
     49         ColorSpace cs = ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
     50         Bitmap bitmap = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888, true, cs);
     51         bitmap.eraseColor(Color.BLUE);
     52         bitmap = bitmap.copy(Bitmap.Config.HARDWARE, false);
     53         Context context = InstrumentationRegistry.getContext();
     54         ImageView originalView = new ImageView(context);
     55         originalView.setImageBitmap(bitmap);
     56 
     57         Callback cb = new Callback();
     58         Matrix matrix = new Matrix();
     59         RectF screenBounds = new RectF(0, 0, SIZE, SIZE);
     60         Parcelable snapshot = cb.onCaptureSharedElementSnapshot(originalView, matrix, screenBounds);
     61         assertNotNull(snapshot);
     62 
     63         View view = cb.onCreateSnapshotView(context, snapshot);
     64         assertNotNull(view);
     65         assertTrue(view instanceof ImageView);
     66 
     67         ImageView finalView = (ImageView) view;
     68         Drawable drawable = finalView.getDrawable();
     69         assertTrue(drawable instanceof BitmapDrawable);
     70         BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
     71         Bitmap finalBitmap = bitmapDrawable.getBitmap();
     72         assertNotNull(finalView);
     73 
     74         assertSame(Bitmap.Config.HARDWARE, finalBitmap.getConfig());
     75         assertSame(cs, finalBitmap.getColorSpace());
     76     }
     77 }
     78