1/* 2 * Copyright (C) 2016 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 17package com.android.settings.testutils.shadow; 18 19import static org.robolectric.util.ReflectionHelpers.ClassParameter.from; 20 21import android.content.Context; 22import android.os.Looper; 23import android.os.UserHandle; 24 25import com.android.internal.content.PackageMonitor; 26import com.android.internal.os.BackgroundThread; 27 28import org.robolectric.annotation.Implementation; 29import org.robolectric.annotation.Implements; 30import org.robolectric.annotation.RealObject; 31import org.robolectric.shadow.api.Shadow; 32import org.robolectric.shadows.ShadowApplication; 33import org.robolectric.shadows.ShadowMessageQueue; 34 35/* 36 * Shadow for hidden {@link PackageMonitor}. 37 */ 38@Implements(value = PackageMonitor.class, isInAndroidSdk = false) 39public class ShadowPackageMonitor { 40 41 @RealObject 42 private PackageMonitor mPackageMonitor; 43 44 @Implementation 45 public void register(Context context, Looper thread, UserHandle user, boolean externalStorage) { 46 // Call through to @RealObject's method. 47 Shadow.directlyOn(mPackageMonitor, PackageMonitor.class, "register", 48 from(Context.class, context), from(Looper.class, thread), 49 from(UserHandle.class, user), from(Boolean.TYPE, externalStorage)); 50 // When <code>thread</code> is null, the {@link BackgroundThread} is used. Here we have to 51 // setup background Robolectric scheduler for it. 52 if (thread == null) { 53 setupBackgroundThreadScheduler(); 54 } 55 } 56 57 private static void setupBackgroundThreadScheduler() { 58 ShadowMessageQueue shadowMessageQueue = Shadow.extract( 59 BackgroundThread.getHandler().getLooper().getQueue()); 60 shadowMessageQueue.setScheduler( 61 ShadowApplication.getInstance().getBackgroundThreadScheduler()); 62 } 63} 64