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 package com.android.wallpaper.testing;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Bitmap.Config;
22 import android.graphics.Point;
23 import android.graphics.Rect;
24 import android.widget.ImageView;
25 
26 import androidx.annotation.Nullable;
27 
28 import com.android.wallpaper.asset.Asset;
29 
30 
31 /**
32  * Test implementation of Asset which blocks on Bitmap decoding operations.
33  */
34 public final class TestAsset extends Asset {
35 
36     private Bitmap mBitmap;
37     private boolean mIsCorrupt;
38 
39     /**
40      * Constructs an asset underpinned by a 1x1 bitmap uniquely identifiable by the given pixel
41      * color.
42      *
43      * @param pixelColor Color of the asset's single pixel.
44      * @param isCorrupt  Whether or not the asset is corrupt and fails to validly decode bitmaps and
45      *                   dimensions.
46      */
TestAsset(int pixelColor, boolean isCorrupt)47     public TestAsset(int pixelColor, boolean isCorrupt) {
48         mIsCorrupt = isCorrupt;
49 
50         if (!mIsCorrupt) {
51             mBitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888);
52             mBitmap.setPixel(0, 0, pixelColor);
53         } else {
54             mBitmap = null;
55         }
56     }
57 
58     @Override
decodeBitmap(int targetWidth, int targetHeight, BitmapReceiver receiver)59     public void decodeBitmap(int targetWidth, int targetHeight, BitmapReceiver receiver) {
60         receiver.onBitmapDecoded(mBitmap);
61     }
62 
63     @Override
decodeBitmapRegion(Rect unused, int targetWidth, int targetHeight, boolean shouldAdjustForRtl, BitmapReceiver receiver)64     public void decodeBitmapRegion(Rect unused, int targetWidth, int targetHeight,
65             boolean shouldAdjustForRtl, BitmapReceiver receiver) {
66         receiver.onBitmapDecoded(mBitmap);
67     }
68 
69     @Override
decodeRawDimensions(Activity unused, DimensionsReceiver receiver)70     public void decodeRawDimensions(Activity unused, DimensionsReceiver receiver) {
71         receiver.onDimensionsDecoded(mIsCorrupt ? null : new Point(1, 1));
72     }
73 
74     @Override
supportsTiling()75     public boolean supportsTiling() {
76         return false;
77     }
78 
79     @Override
loadDrawableWithTransition( Context context, ImageView imageView, int transitionDurationMillis, @Nullable DrawableLoadedListener drawableLoadedListener, int placeholderColor)80     public void loadDrawableWithTransition(
81             Context context,
82             ImageView imageView,
83             int transitionDurationMillis,
84             @Nullable DrawableLoadedListener drawableLoadedListener,
85             int placeholderColor) {
86         if (drawableLoadedListener != null) {
87             drawableLoadedListener.onDrawableLoaded();
88         }
89     }
90 
91     /** Returns the bitmap synchronously. Convenience method for tests. */
getBitmap()92     public Bitmap getBitmap() {
93         return mBitmap;
94     }
95 
setBitmap(Bitmap bitmap)96     public void setBitmap(Bitmap bitmap) {
97         mBitmap = bitmap;
98     }
99 }
100