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 
20 import com.android.wallpaper.model.WallpaperRotationInitializer;
21 import com.android.wallpaper.module.InjectorProvider;
22 import com.android.wallpaper.module.WallpaperPreferences;
23 
24 /**
25  * Test implementation of {@link WallpaperRotationInitializer}.
26  */
27 public class TestWallpaperRotationInitializer implements WallpaperRotationInitializer {
28 
29     private boolean mIsRotationInitialized;
30     @NetworkPreference
31     private int mNetworkPreference;
32     private Listener mListener;
33     @RotationInitializationState
34     private int mRotationInitializationState;
35 
TestWallpaperRotationInitializer()36     TestWallpaperRotationInitializer() {
37         mIsRotationInitialized = false;
38         mNetworkPreference = NETWORK_PREFERENCE_WIFI_ONLY;
39         mRotationInitializationState = WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED;
40     }
41 
TestWallpaperRotationInitializer(@otationInitializationState int rotationState)42     TestWallpaperRotationInitializer(@RotationInitializationState int rotationState) {
43         mIsRotationInitialized = false;
44         mNetworkPreference = NETWORK_PREFERENCE_WIFI_ONLY;
45         mRotationInitializationState = rotationState;
46     }
47 
48     @Override
setFirstWallpaperInRotation(Context context, @NetworkPreference int networkPreference, Listener listener)49     public void setFirstWallpaperInRotation(Context context,
50             @NetworkPreference int networkPreference,
51             Listener listener) {
52         mListener = listener;
53         mNetworkPreference = networkPreference;
54     }
55 
56     @Override
startRotation(Context appContext)57     public boolean startRotation(Context appContext) {
58         WallpaperPreferences wallpaperPreferences =
59                 InjectorProvider.getInjector().getPreferences(appContext);
60         wallpaperPreferences.setWallpaperPresentationMode(
61                 WallpaperPreferences.PRESENTATION_MODE_ROTATING);
62         return true;
63     }
64 
65     @Override
fetchRotationInitializationState(Context context, RotationStateListener listener)66     public void fetchRotationInitializationState(Context context, RotationStateListener listener) {
67         listener.onRotationStateReceived(mRotationInitializationState);
68     }
69 
70     @Override
getRotationInitializationStateDirty(Context context)71     public int getRotationInitializationStateDirty(Context context) {
72         return mRotationInitializationState;
73     }
74 
75     /** Sets the mocked rotation initialization state. */
setRotationInitializationState(@otationInitializationState int rotationState)76     public void setRotationInitializationState(@RotationInitializationState int rotationState) {
77         mRotationInitializationState = rotationState;
78     }
79 
80     /**
81      * Simulates completion of the asynchronous task to download the first wallpaper in a rotation..
82      *
83      * @param isSuccessful Whether the first wallpaper downloaded successfully.
84      */
finishDownloadingFirstWallpaper(boolean isSuccessful)85     public void finishDownloadingFirstWallpaper(boolean isSuccessful) {
86         if (isSuccessful) {
87             mIsRotationInitialized = true;
88             mListener.onFirstWallpaperInRotationSet();
89         } else {
90             mIsRotationInitialized = false;
91             mListener.onError();
92         }
93     }
94 
95     /** Returns whether a wallpaper rotation is initialized. */
isRotationInitialized()96     public boolean isRotationInitialized() {
97         return mIsRotationInitialized;
98     }
99 
100     /** Returns whether a wallpaper rotation is enabled for WiFi-only. */
isWifiOnly()101     public boolean isWifiOnly() {
102         return mNetworkPreference == NETWORK_PREFERENCE_WIFI_ONLY;
103     }
104 }
105