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 org.junit.Assert; 26 27 import java.io.FileOutputStream; 28 import java.io.IOException; 29 import java.io.InputStream; 30 31 public class TestUtil { 32 public static final String DUMMY_PACKAGE = "com.example.android.aardwolf"; 33 installDummyApp()34 public static void installDummyApp() throws IOException { 35 // Copy apk from resources to a local file and install from there. 36 final Resources resources = getContext().getResources(); 37 final InputStream in = resources.openRawResource( 38 resources.getIdentifier("aardwolf_dummy_app", 39 "raw", getContext().getPackageName())); 40 final String apkFilename = getInstrumentation().getTargetContext(). 41 getFilesDir().getPath() + "/dummy_app.apk"; 42 43 final FileOutputStream out = new FileOutputStream(apkFilename); 44 byte[] buff = new byte[1024]; 45 int read; 46 47 while ((read = in.read(buff)) > 0) { 48 out.write(buff, 0, read); 49 } 50 in.close(); 51 out.close(); 52 53 final String result = UiDevice.getInstance(getInstrumentation()) 54 .executeShellCommand("pm install " + apkFilename); 55 Assert.assertTrue("Failed to install wellbeing test apk; make sure the device is rooted", 56 "Success".equals(result.replaceAll("\\s+", ""))); 57 } 58 uninstallDummyApp()59 public static void uninstallDummyApp() throws IOException { 60 UiDevice.getInstance(getInstrumentation()).executeShellCommand( 61 "pm uninstall " + DUMMY_PACKAGE); 62 } 63 } 64