Home | History | Annotate | Download | only in testutils
      1 /*
      2  * Copyright (C) 2018 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.tv.settings.testutils;
     18 
     19 import static org.robolectric.Shadows.shadowOf;
     20 
     21 import android.Manifest;
     22 import android.content.Intent;
     23 import android.content.pm.ApplicationInfo;
     24 import android.content.pm.PackageInfo;
     25 import android.content.pm.ResolveInfo;
     26 import android.content.pm.ServiceInfo;
     27 
     28 import org.robolectric.RuntimeEnvironment;
     29 import org.robolectric.shadows.ShadowPackageManager;
     30 
     31 public class Utils {
     32 
     33     public static void addAutofill(String packageName, String className) {
     34         final ShadowPackageManager shadowPackageManager = shadowOf(
     35                 RuntimeEnvironment.application.getPackageManager());
     36         final Intent intent = new Intent("android.service.autofill.AutofillService");
     37         final ResolveInfo resolveInfo = new ResolveInfo();
     38         resolveInfo.resolvePackageName = packageName;
     39         final ServiceInfo serviceInfo = new ServiceInfo();
     40         serviceInfo.permission = Manifest.permission.BIND_AUTOFILL_SERVICE;
     41         serviceInfo.packageName = packageName;
     42         serviceInfo.name = className;
     43         resolveInfo.serviceInfo = serviceInfo;
     44         final ApplicationInfo applicationInfo = new ApplicationInfo();
     45         applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
     46         serviceInfo.applicationInfo = applicationInfo;
     47         final PackageInfo autofillPackageInfo = new PackageInfo();
     48         autofillPackageInfo.packageName = packageName;
     49         autofillPackageInfo.applicationInfo = applicationInfo;
     50         shadowPackageManager.addPackage(autofillPackageInfo);
     51         shadowPackageManager.addResolveInfoForIntent(intent, resolveInfo);
     52     }
     53 
     54 }
     55