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 package com.android.launcher3.util; 17 18 import static androidx.test.InstrumentationRegistry.getContext; 19 import static androidx.test.InstrumentationRegistry.getInstrumentation; 20 21 import android.content.res.Resources; 22 23 import androidx.test.uiautomator.UiDevice; 24 25 import java.io.FileOutputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 29 public class TestUtil { 30 public static final String DUMMY_PACKAGE = "com.example.android.aardwolf"; 31 installDummyApp()32 public static void installDummyApp() throws IOException { 33 // Copy apk from resources to a local file and install from there. 34 final Resources resources = getContext().getResources(); 35 final InputStream in = resources.openRawResource( 36 resources.getIdentifier("aardwolf_dummy_app", 37 "raw", getContext().getPackageName())); 38 final String apkFilename = getInstrumentation().getTargetContext(). 39 getFilesDir().getPath() + "/dummy_app.apk"; 40 41 final FileOutputStream out = new FileOutputStream(apkFilename); 42 byte[] buff = new byte[1024]; 43 int read; 44 45 while ((read = in.read(buff)) > 0) { 46 out.write(buff, 0, read); 47 } 48 in.close(); 49 out.close(); 50 51 UiDevice.getInstance(getInstrumentation()).executeShellCommand("pm install " + apkFilename); 52 } 53 uninstallDummyApp()54 public static void uninstallDummyApp() throws IOException { 55 UiDevice.getInstance(getInstrumentation()).executeShellCommand( 56 "pm uninstall " + DUMMY_PACKAGE); 57 } 58 } 59