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.content.Context;
19 import android.graphics.Bitmap;
20 import android.graphics.Point;
21 import android.graphics.Rect;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 
26 import com.android.wallpaper.asset.Asset;
27 import com.android.wallpaper.model.StaticWallpaperPrefMetadata;
28 import com.android.wallpaper.model.WallpaperInfo;
29 import com.android.wallpaper.module.InjectorProvider;
30 import com.android.wallpaper.module.WallpaperChangedNotifier;
31 import com.android.wallpaper.module.WallpaperPersister;
32 import com.android.wallpaper.module.WallpaperPreferences;
33 
34 import java.io.InputStream;
35 import java.util.List;
36 import java.util.Map;
37 
38 /**
39  * Test double for {@link WallpaperPersister}.
40  */
41 public class TestWallpaperPersister implements WallpaperPersister {
42 
43     private Context mAppContext;
44     private WallpaperPreferences mPrefs;
45     private WallpaperChangedNotifier mWallpaperChangedNotifier;
46     private Bitmap mCurrentHomeWallpaper;
47     private Bitmap mCurrentLockWallpaper;
48     private Bitmap mPendingHomeWallpaper;
49 
50     private Bitmap mPendingLockWallpaper;
51     private List<String> mHomeAttributions;
52     private String mHomeActionUrl;
53     @Destination
54     private int mDestination;
55     private WallpaperPersister.SetWallpaperCallback mCallback;
56     private boolean mFailNextCall;
57     private Rect mCropRect;
58     private float mScale;
59     private WallpaperInfo mWallpaperInfo;
60     private StaticWallpaperPrefMetadata mHomeStaticWallpaperPrefMetadata;
61     private StaticWallpaperPrefMetadata mLockStaticWallpaperPrefMetadata;
62 
TestWallpaperPersister(Context appContext)63     public TestWallpaperPersister(Context appContext) {
64         mAppContext = appContext;
65         mPrefs = InjectorProvider.getInjector().getPreferences(appContext);
66         mWallpaperChangedNotifier = WallpaperChangedNotifier.getInstance();
67 
68         mCurrentHomeWallpaper = null;
69         mCurrentLockWallpaper = null;
70         mPendingHomeWallpaper = null;
71         mPendingLockWallpaper = null;
72         mWallpaperInfo = null;
73         mFailNextCall = false;
74         mScale = -1.0f;
75     }
76 
77     @Override
setIndividualWallpaper(final WallpaperInfo wallpaperInfo, Asset asset, @Nullable final Rect cropRect, final float scale, final @Destination int destination, final WallpaperPersister.SetWallpaperCallback callback)78     public void setIndividualWallpaper(final WallpaperInfo wallpaperInfo, Asset asset,
79             @Nullable final Rect cropRect, final float scale, final @Destination int destination,
80             final WallpaperPersister.SetWallpaperCallback callback) {
81         asset.decodeBitmap(50, 50, bitmap -> {
82             if (destination == DEST_HOME_SCREEN || destination == DEST_BOTH) {
83                 mPendingHomeWallpaper = bitmap;
84                 mPrefs.setHomeWallpaperAttributions(wallpaperInfo.getAttributions(mAppContext));
85                 mPrefs.setWallpaperPresentationMode(
86                         WallpaperPreferences.PRESENTATION_MODE_STATIC);
87                 mPrefs.setHomeWallpaperRemoteId(wallpaperInfo.getWallpaperId());
88             }
89             if (destination == DEST_LOCK_SCREEN || destination == DEST_BOTH) {
90                 mPendingLockWallpaper = bitmap;
91                 mPrefs.setLockWallpaperAttributions(wallpaperInfo.getAttributions(mAppContext));
92                 mPrefs.setLockWallpaperRemoteId(wallpaperInfo.getWallpaperId());
93             }
94             mDestination = destination;
95             mCallback = callback;
96             mCropRect = cropRect;
97             mScale = scale;
98             mWallpaperInfo = wallpaperInfo;
99         });
100     }
101 
102     @Override
setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions, String actionUrl, String collectionId, String remoteId)103     public boolean setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions,
104             String actionUrl, String collectionId, String remoteId) {
105         if (mFailNextCall) {
106             return false;
107         }
108 
109         mCurrentHomeWallpaper = wallpaperBitmap;
110         mCurrentLockWallpaper = wallpaperBitmap;
111         mHomeAttributions = attributions;
112         mHomeActionUrl = actionUrl;
113         return true;
114     }
115 
116     @Override
setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap, List<String> attributions, String actionUrl, String collectionId)117     public int setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap, List<String> attributions,
118             String actionUrl, String collectionId) {
119         mCurrentHomeWallpaper = wallpaperBitmap;
120         mCurrentLockWallpaper = wallpaperBitmap;
121         mHomeAttributions = attributions;
122         mHomeActionUrl = actionUrl;
123         return 1;
124     }
125 
126     @Override
finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl, String collectionId, int wallpaperId, String remoteId)127     public boolean finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl,
128             String collectionId, int wallpaperId, String remoteId) {
129         mHomeAttributions = attributions;
130         mHomeActionUrl = actionUrl;
131         return true;
132     }
133 
134     /** Returns mock system wallpaper bitmap. */
getCurrentHomeWallpaper()135     public Bitmap getCurrentHomeWallpaper() {
136         return mCurrentHomeWallpaper;
137     }
138 
139     /** Returns mock lock screen wallpaper bitmap. */
getCurrentLockWallpaper()140     public Bitmap getCurrentLockWallpaper() {
141         return mCurrentLockWallpaper;
142     }
143 
144     /** Returns mock home attributions. */
getHomeAttributions()145     public List<String> getHomeAttributions() {
146         return mHomeAttributions;
147     }
148 
149     /** Returns the home wallpaper action URL. */
getHomeActionUrl()150     public String getHomeActionUrl() {
151         return mHomeActionUrl;
152     }
153 
154     /** Returns the Destination a wallpaper was most recently set on. */
155     @Destination
getLastDestination()156     public int getLastDestination() {
157         return mDestination;
158     }
159 
160     /**
161      * Sets whether the next "set wallpaper" operation should fail or succeed.
162      */
setFailNextCall(Boolean failNextCall)163     public void setFailNextCall(Boolean failNextCall) {
164         mFailNextCall = failNextCall;
165     }
166 
167     /**
168      * Implemented so synchronous test methods can control the completion of what would otherwise be
169      * an asynchronous operation.
170      */
finishSettingWallpaper()171     public void finishSettingWallpaper() {
172         if (mFailNextCall) {
173             mCallback.onError(null /* throwable */);
174         } else {
175             if (mDestination == DEST_HOME_SCREEN || mDestination == DEST_BOTH) {
176                 mCurrentHomeWallpaper = mPendingHomeWallpaper;
177                 mPendingHomeWallpaper = null;
178             }
179             if (mDestination == DEST_LOCK_SCREEN || mDestination == DEST_BOTH) {
180                 mCurrentLockWallpaper = mPendingLockWallpaper;
181                 mPendingLockWallpaper = null;
182             }
183             mCallback.onSuccess(mWallpaperInfo, mDestination);
184             mWallpaperChangedNotifier.notifyWallpaperChanged();
185         }
186     }
187 
188     @Override
setWallpaperInfoInPreview(WallpaperInfo wallpaperInfo)189     public void setWallpaperInfoInPreview(WallpaperInfo wallpaperInfo) {
190     }
191 
192     @Override
onLiveWallpaperSet(@estination int destination)193     public void onLiveWallpaperSet(@Destination int destination) {
194     }
195 
196     @Override
setLiveWallpaperMetadata(WallpaperInfo wallpaperInfo, String effects, @Destination int destination)197     public void setLiveWallpaperMetadata(WallpaperInfo wallpaperInfo, String effects,
198             @Destination int destination) {
199     }
200 
201     /** Returns the last requested wallpaper bitmap scale. */
getScale()202     public float getScale() {
203         return mScale;
204     }
205 
206     /** Returns the last requested wallpaper crop. */
getCropRect()207     public Rect getCropRect() {
208         return mCropRect;
209     }
210 
211     @Override
saveStaticWallpaperMetadata(List<String> attributions, String actionUrl, String collectionId, int wallpaperId, String remoteId, @Destination int destination)212     public boolean saveStaticWallpaperMetadata(List<String> attributions, String actionUrl,
213             String collectionId, int wallpaperId, String remoteId, @Destination int destination) {
214         return false;
215     }
216 
217     @Override
saveStaticWallpaperToPreferences(int destination, StaticWallpaperPrefMetadata metadata)218     public boolean saveStaticWallpaperToPreferences(int destination,
219             StaticWallpaperPrefMetadata metadata) {
220         if (destination == DEST_HOME_SCREEN || destination == DEST_BOTH) {
221             mHomeStaticWallpaperPrefMetadata = metadata;
222         }
223 
224         if (destination == DEST_LOCK_SCREEN || destination == DEST_BOTH) {
225             mLockStaticWallpaperPrefMetadata = metadata;
226         }
227         return true;
228     }
229 
230     @Override
getDefaultWhichWallpaper()231     public int getDefaultWhichWallpaper() {
232         return 0;
233     }
234 
235     @Override
setBitmapToWallpaperManager(Bitmap wallpaperBitmap, Rect cropHint, boolean allowBackup, int whichWallpaper)236     public int setBitmapToWallpaperManager(Bitmap wallpaperBitmap, Rect cropHint,
237             boolean allowBackup, int whichWallpaper) {
238         return 1;
239     }
240 
241     @Override
setStreamToWallpaperManager(InputStream inputStream, Rect cropHint, boolean allowBackup, int whichWallpaper)242     public int setStreamToWallpaperManager(InputStream inputStream, Rect cropHint,
243             boolean allowBackup, int whichWallpaper) {
244         return 1;
245     }
246 
247     @Override
setStreamWithCropsToWallpaperManager(InputStream inputStream, @NonNull Map<Point, Rect> cropHints, boolean allowBackup, int whichWallpaper)248     public int setStreamWithCropsToWallpaperManager(InputStream inputStream,
249             @NonNull Map<Point, Rect> cropHints, boolean allowBackup, int whichWallpaper) {
250         return 1;
251     }
252 }
253