1 /* 2 * Copyright (C) 2015 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.testinput.instrument; 18 19 import android.app.Activity; 20 import android.app.Instrumentation; 21 import android.os.Bundle; 22 import android.text.TextUtils; 23 import android.util.Log; 24 import com.android.tv.testing.constants.Constants; 25 import com.android.tv.testinput.TestTvInputService; 26 import com.android.tv.testinput.TestTvInputSetupActivity; 27 28 /** 29 * An instrumentation utility to set up the needed inputs, channels, programs and other settings for 30 * automated unit tests. 31 * 32 * <p> 33 * 34 * <pre>{@code 35 * adb shell am instrument \ 36 * -e testSetupMode {func,jank,unit} \ 37 * -w com.android.tv.testinput/.instrument.TestSetupInstrumentation 38 * }</pre> 39 * 40 * <p>Optional arguments are: 41 * 42 * <pre> 43 * -e channelCount number 44 * </pre> 45 */ 46 public class TestSetupInstrumentation extends Instrumentation { 47 private static final String TAG = "TestSetupInstrument"; 48 private static final String TEST_SETUP_MODE_ARG = "testSetupMode"; 49 private static final String CHANNEL_COUNT_ARG = "channelCount"; 50 private Bundle mArguments; 51 private String mInputId; 52 53 /** 54 * Fails an instrumentation request. 55 * 56 * @param errMsg an error message 57 */ fail(String errMsg)58 protected void fail(String errMsg) { 59 Log.e(TAG, errMsg); 60 Bundle result = new Bundle(); 61 result.putString("error", errMsg); 62 finish(Activity.RESULT_CANCELED, result); 63 } 64 65 @Override onCreate(Bundle arguments)66 public void onCreate(Bundle arguments) { 67 super.onCreate(arguments); 68 mArguments = arguments; 69 start(); 70 } 71 72 @Override onStart()73 public void onStart() { 74 super.onStart(); 75 try { 76 mInputId = TestTvInputService.buildInputId(getContext()); 77 setup(); 78 finish(Activity.RESULT_OK, new Bundle()); 79 } catch (TestSetupException e) { 80 fail(e.getMessage()); 81 } 82 } 83 setup()84 private void setup() throws TestSetupException { 85 final String testSetupMode = mArguments.getString(TEST_SETUP_MODE_ARG); 86 if (TextUtils.isEmpty(testSetupMode)) { 87 Log.i( 88 TAG, 89 "Performing no setup actions because " 90 + TEST_SETUP_MODE_ARG 91 + " was not passed as an argument"); 92 } else { 93 Log.i(TAG, "Running setup for " + testSetupMode + " tests."); 94 int channelCount; 95 switch (testSetupMode) { 96 case "func": 97 channelCount = 98 getArgumentAsInt(CHANNEL_COUNT_ARG, Constants.FUNC_TEST_CHANNEL_COUNT); 99 break; 100 case "jank": 101 channelCount = 102 getArgumentAsInt(CHANNEL_COUNT_ARG, Constants.JANK_TEST_CHANNEL_COUNT); 103 break; 104 case "unit": 105 channelCount = 106 getArgumentAsInt(CHANNEL_COUNT_ARG, Constants.UNIT_TEST_CHANNEL_COUNT); 107 break; 108 default: 109 throw new TestSetupException( 110 "Unknown " + TEST_SETUP_MODE_ARG + " of " + testSetupMode); 111 } 112 TestTvInputSetupActivity.registerChannels(getContext(), mInputId, channelCount); 113 } 114 } 115 getArgumentAsInt(String arg, int defaultValue)116 private int getArgumentAsInt(String arg, int defaultValue) { 117 String stringValue = mArguments.getString(arg); 118 if (stringValue != null) { 119 try { 120 return Integer.parseInt(stringValue); 121 } catch (NumberFormatException e) { 122 Log.w( 123 TAG, 124 "Unable to parse arg " 125 + arg 126 + " with value " 127 + stringValue 128 + " to a integer.", 129 e); 130 } 131 } 132 return defaultValue; 133 } 134 135 static class TestSetupException extends Exception { TestSetupException(String msg)136 public TestSetupException(String msg) { 137 super(msg); 138 } 139 fromMissingArg(String arg)140 public static TestSetupException fromMissingArg(String arg) { 141 return new TestSetupException( 142 String.format("Error: missing mandatory argument '%s'", arg)); 143 } 144 } 145 } 146