1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package android.slice.cts; 16 17 import static org.mockito.Mockito.mock; 18 import static org.mockito.Mockito.withSettings; 19 20 import android.app.PendingIntent; 21 import android.app.slice.Slice; 22 import android.app.slice.SliceManager; 23 import android.app.slice.SliceSpec; 24 import android.content.Context; 25 import android.content.ContextWrapper; 26 import android.content.Intent; 27 import android.content.pm.ProviderInfo; 28 import android.net.Uri; 29 30 import org.mockito.Answers; 31 import org.mockito.stubbing.Answer; 32 33 import java.util.Collection; 34 import java.util.Set; 35 36 public class LocalSliceProvider extends SliceProvider { 37 public static SliceProvider sProxy; 38 public static Answer sAnswer; 39 40 private SliceManager mSliceService; 41 42 @Override 43 public boolean onCreate() { 44 return sProxy == null || sProxy.onCreate(); 45 } 46 47 @Override 48 public void attachInfo(Context context, ProviderInfo info) { 49 mSliceService = mock(SliceManager.class, withSettings() 50 .spiedInstance(context.getSystemService(SliceManager.class)) 51 .defaultAnswer(invocation -> { 52 Answer s = sAnswer != null ? sAnswer : Answers.CALLS_REAL_METHODS; 53 return s.answer(invocation); 54 })); 55 Context wrapped = new ContextWrapper(context) { 56 @Override 57 public Object getSystemService(String name) { 58 if (getSystemServiceName(SliceManager.class).equals(name)) { 59 return mSliceService; 60 } 61 return super.getSystemService(name); 62 } 63 }; 64 super.attachInfo(wrapped, info); 65 } 66 67 @Override 68 public Slice onBindSlice(Uri sliceUri, Set<SliceSpec> specs) { 69 if (sProxy != null) return sProxy.onBindSlice(sliceUri, specs); 70 return super.onBindSlice(sliceUri, specs); 71 } 72 73 @Override 74 public Uri onMapIntentToUri(Intent intent) { 75 if (sProxy != null) return sProxy.onMapIntentToUri(intent); 76 return super.onMapIntentToUri(intent); 77 } 78 79 @Override 80 public Collection<Uri> onGetSliceDescendants(Uri uri) { 81 if (sProxy != null) return sProxy.onGetSliceDescendants(uri); 82 return super.onGetSliceDescendants(uri); 83 } 84 85 @Override 86 public void onSlicePinned(Uri sliceUri) { 87 if (sProxy != null) sProxy.onSlicePinned(sliceUri); 88 super.onSlicePinned(sliceUri); 89 } 90 91 @Override 92 public void onSliceUnpinned(Uri sliceUri) { 93 if (sProxy != null) sProxy.onSliceUnpinned(sliceUri); 94 super.onSliceUnpinned(sliceUri); 95 } 96 97 @Override 98 public PendingIntent onCreatePermissionRequest(Uri sliceUri) { 99 if (sProxy != null) return sProxy.onCreatePermissionRequest(sliceUri); 100 return super.onCreatePermissionRequest(sliceUri); 101 } 102 } 103