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.WallpaperManager;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Rect;
22 import android.graphics.drawable.Drawable;
23 import android.os.ParcelFileDescriptor;
24 import android.util.Log;
25 
26 import com.android.wallpaper.compat.WallpaperManagerCompat;
27 
28 import java.io.IOException;
29 import java.io.InputStream;
30 
31 /** Test double for {@link WallpaperManagerCompat}. */
32 public class TestWallpaperManagerCompat extends WallpaperManagerCompat {
33     private static final String TAG = "TestWPManagerCompat";
34 
35     ParcelFileDescriptor mSystemParcelFd;
36     ParcelFileDescriptor mLockParcelFd;
37     int mHomeWallpaperId = 0;
38     int mLockWallpaperId = 0;
39 
40     private boolean mAllowBackup;
41     private Context mAppContext;
42     private Drawable mTestDrawable;
43 
TestWallpaperManagerCompat(Context appContext)44     public TestWallpaperManagerCompat(Context appContext) {
45         mAppContext = appContext;
46         mAllowBackup = true;
47     }
48 
49     @Override
setStream(InputStream stream, Rect visibleCropHint, boolean allowBackup, int whichWallpaper)50     public int setStream(InputStream stream, Rect visibleCropHint, boolean allowBackup,
51             int whichWallpaper) throws IOException {
52         mAllowBackup = allowBackup;
53         return ++mHomeWallpaperId;
54     }
55 
56     @Override
setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int whichWallpaper)57     public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup,
58             int whichWallpaper) throws IOException {
59         mAllowBackup = allowBackup;
60         return ++mLockWallpaperId;
61     }
62 
63     @Override
getWallpaperFile(int whichWallpaper)64     public ParcelFileDescriptor getWallpaperFile(int whichWallpaper) {
65         if (whichWallpaper == WallpaperManagerCompat.FLAG_SYSTEM) {
66             return mSystemParcelFd;
67         } else if (whichWallpaper == WallpaperManagerCompat.FLAG_LOCK) {
68             return mLockParcelFd;
69         } else {
70             // :(
71             return null;
72         }
73     }
74 
75     @Override
getDrawable()76     public Drawable getDrawable() {
77         if (mTestDrawable != null) {
78             return mTestDrawable;
79         }
80 
81         // Retrieve WallpaperManager using Context#getSystemService instead of
82         // WallpaperManager#getInstance so it can be mocked out in test.
83         WallpaperManager wallpaperManager =
84                 (WallpaperManager) mAppContext.getSystemService(Context.WALLPAPER_SERVICE);
85         return wallpaperManager.getDrawable();
86     }
87 
88     @Override
getWallpaperId(@allpaperLocation int whichWallpaper)89     public int getWallpaperId(@WallpaperLocation int whichWallpaper) {
90         switch (whichWallpaper) {
91             case WallpaperManagerCompat.FLAG_SYSTEM:
92                 return mHomeWallpaperId;
93             case WallpaperManagerCompat.FLAG_LOCK:
94                 return mLockWallpaperId;
95             default:
96                 throw new IllegalArgumentException(
97                         "Wallpaper location must be one of FLAG_SYSTEM or "
98                                 + "FLAG_LOCK but the value " + whichWallpaper + " was provided.");
99         }
100     }
101 
setWallpaperFile(int whichWallpaper, ParcelFileDescriptor file)102     public void setWallpaperFile(int whichWallpaper, ParcelFileDescriptor file) {
103         if (whichWallpaper == WallpaperManagerCompat.FLAG_SYSTEM) {
104             mSystemParcelFd = file;
105         } else if (whichWallpaper == WallpaperManagerCompat.FLAG_LOCK) {
106             mLockParcelFd = file;
107         } else {
108             Log.e(TAG, "Called setWallpaperFile without a valid distinct 'which' argument.");
109         }
110     }
111 
setWallpaperId(@allpaperLocation int whichWallpaper, int wallpaperId)112     public void setWallpaperId(@WallpaperLocation int whichWallpaper, int wallpaperId) {
113         switch (whichWallpaper) {
114             case WallpaperManagerCompat.FLAG_SYSTEM:
115                 mHomeWallpaperId = wallpaperId;
116                 break;
117             case WallpaperManagerCompat.FLAG_LOCK:
118                 mLockWallpaperId = wallpaperId;
119                 break;
120             default:
121                 throw new IllegalArgumentException(
122                         "Wallpaper location must be one of FLAG_SYSTEM or "
123                                 + "FLAG_LOCK but the value " + whichWallpaper + " was provided.");
124         }
125     }
126 
setDrawable(Drawable drawable)127     public void setDrawable(Drawable drawable) {
128         mTestDrawable = drawable;
129     }
130 
131     /** Returns whether backup is allowed for the last set wallpaper. */
isBackupAllowed()132     public boolean isBackupAllowed() {
133         return mAllowBackup;
134     }
135 }
136