11d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal/* 21d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * Copyright (C) 2017 The Android Open Source Project 31d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * 41d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License"); you may not 51d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * use this file except in compliance with the License. You may obtain a copy of 61d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * the License at 71d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * 81d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * http://www.apache.org/licenses/LICENSE-2.0 91d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * 101d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * Unless required by applicable law or agreed to in writing, software 111d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 121d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 131d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * License for the specific language governing permissions and limitations under 141d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * the License. 151d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal */ 161d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalpackage com.android.launcher3.util.rule; 171d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 181d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport android.content.ComponentName; 191d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport android.content.pm.ActivityInfo; 201d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport android.os.ParcelFileDescriptor; 211d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport android.support.test.InstrumentationRegistry; 221d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 231d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport org.junit.rules.TestRule; 241d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport org.junit.runner.Description; 251d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport org.junit.runners.model.Statement; 261d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 271d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport java.io.FileInputStream; 281d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport java.io.IOException; 291d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 301d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal/** 311d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * Test rule which executes a shell command at the start of the test. 321d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal */ 331d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalpublic class ShellCommandRule implements TestRule { 341d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 351d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal private final String mCmd; 361d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 371d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal public ShellCommandRule(String cmd) { 381d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal mCmd = cmd; 391d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 401d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 411d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal @Override 421d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal public Statement apply(Statement base, Description description) { 431d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal return new MyStatement(base, mCmd); 441d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 451d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 461d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal public static void runShellCommand(String command) throws IOException { 471d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal ParcelFileDescriptor pfd = InstrumentationRegistry.getInstrumentation().getUiAutomation() 481d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal .executeShellCommand(command); 491d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 501d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal // Read the input stream fully. 511d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd); 521d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal while (fis.read() != -1); 531d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal fis.close(); 541d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 551d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 561d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal private static class MyStatement extends Statement { 571d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal private final Statement mBase; 581d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal private final String mCmd; 591d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 601d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal public MyStatement(Statement base, String cmd) { 611d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal mBase = base; 621d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal mCmd = cmd; 631d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 641d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 651d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal @Override 661d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal public void evaluate() throws Throwable { 671d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal runShellCommand(mCmd); 681d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal mBase.evaluate(); 691d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 701d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 711d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 721d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal /** 731d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * Grants the launcher permission to bind widgets. 741d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal */ 751d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal public static ShellCommandRule grandWidgetBind() { 761d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal return new ShellCommandRule("appwidget grantbind --package " 771d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal + InstrumentationRegistry.getTargetContext().getPackageName()); 781d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 791d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal 801d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal /** 811d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal * Sets the target launcher as default launcher. 821d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal */ 831d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal public static ShellCommandRule setDefaultLauncher() { 841d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal ActivityInfo launcher = InstrumentationRegistry.getTargetContext().getPackageManager() 851d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal .queryIntentActivities(LauncherActivityRule.getHomeIntent(), 0).get(0) 861d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal .activityInfo; 871d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal return new ShellCommandRule("cmd package set-home-activity " + 881d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal new ComponentName(launcher.packageName, launcher.name).flattenToString()); 891d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal } 901d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal} 91