• 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.app.Activity;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Rect;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.android.wallpaper.asset.Asset;
26 import com.android.wallpaper.asset.Asset.BitmapReceiver;
27 import com.android.wallpaper.model.WallpaperInfo;
28 import com.android.wallpaper.module.InjectorProvider;
29 import com.android.wallpaper.module.WallpaperChangedNotifier;
30 import com.android.wallpaper.module.WallpaperPersister;
31 import com.android.wallpaper.module.WallpaperPreferences;
32 
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     @WallpaperPosition
56     private int mWallpaperPosition;
57     private WallpaperInfo mWallpaperInfo;
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
setIndividualWallpaperWithPosition(Activity activity, WallpaperInfo wallpaper, @WallpaperPosition int wallpaperPosition, SetWallpaperCallback callback)99     public void setIndividualWallpaperWithPosition(Activity activity, WallpaperInfo wallpaper,
100             @WallpaperPosition int wallpaperPosition, SetWallpaperCallback callback) {
101         wallpaper.getAsset(activity).decodeBitmap(50, 50, new BitmapReceiver() {
102             @Override
103             public void onBitmapDecoded(@Nullable Bitmap bitmap) {
104                 mPendingHomeWallpaper = bitmap;
105                 mPrefs.setHomeWallpaperAttributions(wallpaper.getAttributions(mAppContext));
106                 mPrefs.setHomeWallpaperBaseImageUrl(wallpaper.getBaseImageUrl());
107                 mPrefs.setHomeWallpaperActionUrl(wallpaper.getActionUrl(mAppContext));
108                 mPrefs.setHomeWallpaperCollectionId(wallpaper.getCollectionId(mAppContext));
109                 mPrefs.setHomeWallpaperRemoteId(wallpaper.getWallpaperId());
110                 mPrefs.setWallpaperPresentationMode(WallpaperPreferences.PRESENTATION_MODE_STATIC);
111                 mPendingLockWallpaper = bitmap;
112                 mPrefs.setLockWallpaperAttributions(wallpaper.getAttributions(mAppContext));
113                 mPrefs.setLockWallpaperRemoteId(wallpaper.getWallpaperId());
114 
115                 mDestination = WallpaperPersister.DEST_BOTH;
116                 mCallback = callback;
117                 mWallpaperPosition = wallpaperPosition;
118                 mWallpaperInfo = wallpaper;
119             }
120         });
121     }
122 
123     @Override
setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions, int actionLabelRes, int actionIconRes, String actionUrl, String collectionId)124     public boolean setWallpaperInRotation(Bitmap wallpaperBitmap, List<String> attributions,
125             int actionLabelRes, int actionIconRes, String actionUrl, String collectionId) {
126         if (mFailNextCall) {
127             return false;
128         }
129 
130         mCurrentHomeWallpaper = wallpaperBitmap;
131         mCurrentLockWallpaper = wallpaperBitmap;
132         mHomeAttributions = attributions;
133         mHomeActionUrl = actionUrl;
134         return true;
135     }
136 
137     @Override
setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap, List<String> attributions, String actionUrl, String collectionId)138     public int setWallpaperBitmapInNextRotation(Bitmap wallpaperBitmap, List<String> attributions,
139             String actionUrl, String collectionId) {
140         mCurrentHomeWallpaper = wallpaperBitmap;
141         mCurrentLockWallpaper = wallpaperBitmap;
142         mHomeAttributions = attributions;
143         mHomeActionUrl = actionUrl;
144         return 1;
145     }
146 
147     @Override
finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl, int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId)148     public boolean finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl,
149             int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId) {
150         mHomeAttributions = attributions;
151         mHomeActionUrl = actionUrl;
152         return true;
153     }
154 
155     /** Returns mock system wallpaper bitmap. */
getCurrentHomeWallpaper()156     public Bitmap getCurrentHomeWallpaper() {
157         return mCurrentHomeWallpaper;
158     }
159 
160     /** Returns mock lock screen wallpaper bitmap. */
getCurrentLockWallpaper()161     public Bitmap getCurrentLockWallpaper() {
162         return mCurrentLockWallpaper;
163     }
164 
165     /** Returns mock home attributions. */
getHomeAttributions()166     public List<String> getHomeAttributions() {
167         return mHomeAttributions;
168     }
169 
170     /** Returns the home wallpaper action URL. */
getHomeActionUrl()171     public String getHomeActionUrl() {
172         return mHomeActionUrl;
173     }
174 
175     /** Returns the Destination a wallpaper was most recently set on. */
176     @Destination
getLastDestination()177     public int getLastDestination() {
178         return mDestination;
179     }
180 
181     /**
182      * Sets whether the next "set wallpaper" operation should fail or succeed.
183      */
setFailNextCall(Boolean failNextCall)184     public void setFailNextCall(Boolean failNextCall) {
185         mFailNextCall = failNextCall;
186     }
187 
188     /**
189      * Implemented so synchronous test methods can control the completion of what would otherwise be
190      * an asynchronous operation.
191      */
finishSettingWallpaper()192     public void finishSettingWallpaper() {
193         if (mFailNextCall) {
194             mCallback.onError(null /* throwable */);
195         } else {
196             if (mDestination == DEST_HOME_SCREEN || mDestination == DEST_BOTH) {
197                 mCurrentHomeWallpaper = mPendingHomeWallpaper;
198                 mPendingHomeWallpaper = null;
199             }
200             if (mDestination == DEST_LOCK_SCREEN || mDestination == DEST_BOTH) {
201                 mCurrentLockWallpaper = mPendingLockWallpaper;
202                 mPendingLockWallpaper = null;
203             }
204             mCallback.onSuccess(mWallpaperInfo, mDestination);
205             mWallpaperChangedNotifier.notifyWallpaperChanged();
206         }
207     }
208 
209     @Override
setWallpaperInfoInPreview(WallpaperInfo wallpaperInfo)210     public void setWallpaperInfoInPreview(WallpaperInfo wallpaperInfo) {
211     }
212 
213     @Override
onLiveWallpaperSet(@estination int destination)214     public void onLiveWallpaperSet(@Destination int destination) {
215     }
216 
217     @Override
setLiveWallpaperMetadata(WallpaperInfo wallpaperInfo, String effects, @Destination int destination)218     public void setLiveWallpaperMetadata(WallpaperInfo wallpaperInfo, String effects,
219             @Destination int destination) {
220     }
221 
222     /** Returns the last requested wallpaper bitmap scale. */
getScale()223     public float getScale() {
224         return mScale;
225     }
226 
227     /** Returns the last requested wallpaper crop. */
getCropRect()228     public Rect getCropRect() {
229         return mCropRect;
230     }
231 
232     /** Returns the last selected wallpaper position option. */
233     @WallpaperPosition
getWallpaperPosition()234     public int getWallpaperPosition() {
235         return mWallpaperPosition;
236     }
237 
238     @Override
saveStaticWallpaperMetadata(List<String> attributions, String actionUrl, int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId)239     public boolean saveStaticWallpaperMetadata(List<String> attributions, String actionUrl,
240             int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId) {
241         return false;
242     }
243 
244     @Override
getDefaultWhichWallpaper()245     public int getDefaultWhichWallpaper() {
246         return 0;
247     }
248 
249     @Override
setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup, int whichWallpaper)250     public int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
251             int whichWallpaper) {
252         return 0;
253     }
254 }
255