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.compat.BuildCompat;
21 import com.android.wallpaper.model.WallpaperInfo;
22 import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
23 import com.android.wallpaper.module.InjectorProvider;
24 import com.android.wallpaper.module.WallpaperRefresher;
25 
26 import java.util.List;
27 
28 /**
29  * Test double of {@link CurrentWallpaperInfoFactory}.
30  */
31 public class TestCurrentWallpaperInfoFactory implements CurrentWallpaperInfoFactory {
32 
33     private WallpaperRefresher mRefresher;
34 
TestCurrentWallpaperInfoFactory(Context context)35     public TestCurrentWallpaperInfoFactory(Context context) {
36         mRefresher = InjectorProvider.getInjector().getWallpaperRefresher(
37                 context.getApplicationContext());
38     }
39 
40     @Override
createCurrentWallpaperInfos(final WallpaperInfoCallback callback, boolean forceRefresh)41     public void createCurrentWallpaperInfos(final WallpaperInfoCallback callback,
42             boolean forceRefresh) {
43         mRefresher.refresh((homeWallpaperMetadata, lockWallpaperMetadata, presentationMode) -> {
44 
45             WallpaperInfo homeWallpaper = createTestWallpaperInfo(
46                     homeWallpaperMetadata.getAttributions(),
47                     homeWallpaperMetadata.getActionUrl(),
48                     homeWallpaperMetadata.getCollectionId());
49 
50             WallpaperInfo lockWallpaper = null;
51             if (lockWallpaperMetadata != null && BuildCompat.isAtLeastN()) {
52                 lockWallpaper = createTestWallpaperInfo(
53                         lockWallpaperMetadata.getAttributions(),
54                         lockWallpaperMetadata.getActionUrl(),
55                         lockWallpaperMetadata.getCollectionId());
56             }
57 
58             callback.onWallpaperInfoCreated(homeWallpaper, lockWallpaper, presentationMode);
59         });
60     }
61 
createTestWallpaperInfo(List<String> attributions, String actionUrl, String collectionId)62     private static WallpaperInfo createTestWallpaperInfo(List<String> attributions,
63             String actionUrl, String collectionId) {
64         TestWallpaperInfo wallpaper = new TestWallpaperInfo(TestWallpaperInfo.COLOR_BLACK);
65         wallpaper.setAttributions(attributions);
66         wallpaper.setActionUrl(actionUrl);
67         wallpaper.setCollectionId(collectionId);
68         return wallpaper;
69     }
70 }
71