1 /* 2 * Copyright (C) 2021 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.intentresolver; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 24 import android.content.res.Resources; 25 import android.database.Cursor; 26 import android.os.UserHandle; 27 28 import com.android.intentresolver.chooser.TargetInfo; 29 import com.android.intentresolver.contentpreview.ImageLoader; 30 import com.android.intentresolver.emptystate.CrossProfileIntentsChecker; 31 import com.android.intentresolver.shortcuts.ShortcutLoader; 32 33 import kotlin.jvm.functions.Function2; 34 35 import java.util.function.Consumer; 36 import java.util.function.Function; 37 38 /** 39 * Singleton providing overrides to be applied by any {@code IChooserWrapper} used in testing. 40 * We cannot directly mock the activity created since instrumentation creates it, so instead we use 41 * this singleton to modify behavior. 42 */ 43 public class ChooserActivityOverrideData { 44 private static ChooserActivityOverrideData sInstance = null; 45 getInstance()46 public static ChooserActivityOverrideData getInstance() { 47 if (sInstance == null) { 48 sInstance = new ChooserActivityOverrideData(); 49 } 50 return sInstance; 51 } 52 public Function<TargetInfo, Boolean> onSafelyStartInternalCallback; 53 public Function<TargetInfo, Boolean> onSafelyStartCallback; 54 public Function2<UserHandle, Consumer<ShortcutLoader.Result>, ShortcutLoader> 55 shortcutLoaderFactory = (userHandle, callback) -> null; 56 public ChooserListController resolverListController; 57 public ChooserListController workResolverListController; 58 public Boolean isVoiceInteraction; 59 public Cursor resolverCursor; 60 public boolean resolverForceException; 61 public ImageLoader imageLoader; 62 public Resources resources; 63 public boolean hasCrossProfileIntents; 64 public boolean isQuietModeEnabled; 65 public Integer myUserId; 66 public CrossProfileIntentsChecker mCrossProfileIntentsChecker; 67 reset()68 public void reset() { 69 onSafelyStartInternalCallback = null; 70 isVoiceInteraction = null; 71 imageLoader = null; 72 resolverCursor = null; 73 resolverForceException = false; 74 resolverListController = mock(ChooserListController.class); 75 workResolverListController = mock(ChooserListController.class); 76 resources = null; 77 hasCrossProfileIntents = true; 78 isQuietModeEnabled = false; 79 myUserId = null; 80 shortcutLoaderFactory = ((userHandle, resultConsumer) -> null); 81 mCrossProfileIntentsChecker = mock(CrossProfileIntentsChecker.class); 82 when(mCrossProfileIntentsChecker.hasCrossProfileIntents(any(), anyInt(), anyInt())) 83 .thenAnswer(invocation -> hasCrossProfileIntents); 84 } 85 ChooserActivityOverrideData()86 private ChooserActivityOverrideData() {} 87 } 88 89