1 /*
2  * Copyright (C) 2023 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 
17 package com.android.providers.media.photopicker.viewmodel;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import androidx.annotation.NonNull;
23 
24 import com.android.providers.media.ConfigStore;
25 import com.android.providers.media.photopicker.data.UserIdManager;
26 import com.android.providers.media.photopicker.data.UserManagerState;
27 
28 class BannerTestUtils {
getTestBannerController(@onNull Context context, @NonNull UserHandle userHandle, @NonNull ConfigStore configStore)29     static BannerController getTestBannerController(@NonNull Context context,
30             @NonNull UserHandle userHandle, @NonNull ConfigStore configStore) {
31         return new BannerController(context, userHandle, configStore) {
32             @Override
33             void updateCloudProviderDataFile() {
34                 // No-op
35             }
36         };
37     }
38 
getTestCloudBannerManager(@onNull Context context, @NonNull UserIdManager userIdManager, @NonNull ConfigStore configStore)39     static BannerManager getTestCloudBannerManager(@NonNull Context context,
40             @NonNull UserIdManager userIdManager, @NonNull ConfigStore configStore) {
41         return new BannerManager.CloudBannerManager(context, userIdManager, configStore) {
42             @Override
43             void maybeInitialiseAndSetBannersForCurrentUser() {
44                 // Get (iff exists) or create the banner controller for the current user
45                 final BannerController bannerController =
46                         getBannerControllersPerUser().forUser(getCurrentUserProfileId());
47                 // Post the banner related live data values from this current user banner controller
48                 getCloudMediaProviderAuthorityLiveData()
49                         .postValue(bannerController.getCloudMediaProviderAuthority());
50                 getCloudMediaProviderAppTitleLiveData()
51                         .postValue(bannerController.getCloudMediaProviderLabel());
52                 getCloudMediaAccountNameLiveData()
53                         .postValue(bannerController.getCloudMediaProviderAccountName());
54                 setChooseCloudMediaAccountActivityIntent(
55                         bannerController.getChooseCloudMediaAccountActivityIntent());
56                 shouldShowChooseAppBannerLiveData()
57                         .postValue(bannerController.shouldShowChooseAppBanner());
58                 shouldShowCloudMediaAvailableBannerLiveData()
59                         .postValue(bannerController.shouldShowCloudMediaAvailableBanner());
60                 shouldShowAccountUpdatedBannerLiveData()
61                         .postValue(bannerController.shouldShowAccountUpdatedBanner());
62                 shouldShowChooseAccountBannerLiveData()
63                         .postValue(bannerController.shouldShowChooseAccountBanner());
64             }
65 
66             @NonNull
67             @Override
68             BannerController createBannerController(@NonNull Context context,
69                     @NonNull UserHandle userHandle, @NonNull ConfigStore configStore) {
70                 return getTestBannerController(context, userHandle, configStore);
71             }
72         };
73     }
74 
75     static BannerManager getTestCloudBannerManager(@NonNull Context context,
76             @NonNull UserManagerState userManagerState, @NonNull ConfigStore configStore) {
77         return new BannerManager.CloudBannerManager(context, userManagerState, configStore) {
78             @Override
79             void maybeInitialiseAndSetBannersForCurrentUser() {
80                 // Get (iff exists) or create the banner controller for the current user
81                 final BannerController bannerController =
82                         getBannerControllersPerUser().forUser(getCurrentUserProfileId());
83                 // Post the banner related live data values from this current user banner controller
84                 getCloudMediaProviderAuthorityLiveData()
85                         .postValue(bannerController.getCloudMediaProviderAuthority());
86                 getCloudMediaProviderAppTitleLiveData()
87                         .postValue(bannerController.getCloudMediaProviderLabel());
88                 getCloudMediaAccountNameLiveData()
89                         .postValue(bannerController.getCloudMediaProviderAccountName());
90                 setChooseCloudMediaAccountActivityIntent(
91                         bannerController.getChooseCloudMediaAccountActivityIntent());
92                 shouldShowChooseAppBannerLiveData()
93                         .postValue(bannerController.shouldShowChooseAppBanner());
94                 shouldShowCloudMediaAvailableBannerLiveData()
95                         .postValue(bannerController.shouldShowCloudMediaAvailableBanner());
96                 shouldShowAccountUpdatedBannerLiveData()
97                         .postValue(bannerController.shouldShowAccountUpdatedBanner());
98                 shouldShowChooseAccountBannerLiveData()
99                         .postValue(bannerController.shouldShowChooseAccountBanner());
100             }
101 
102             @NonNull
103             @Override
104             BannerController createBannerController(@NonNull Context context,
105                     @NonNull UserHandle userHandle, @NonNull ConfigStore configStore) {
106                 return getTestBannerController(context, userHandle, configStore);
107             }
108         };
109     }
110 }
111