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