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.server.adb; 18 19 import static com.android.server.adb.AdbDebuggingManagerTest.TestResult; 20 21 import android.app.Activity; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.os.Message; 25 import android.util.Log; 26 27 import java.util.concurrent.BlockingQueue; 28 29 /** 30 * Helper Activity used to test the AdbDebuggingManager's prompt to allow an adb key. 31 */ 32 public class AdbDebuggingManagerTestActivity extends Activity { 33 34 private static final String TAG = "AdbDebuggingManagerTestActivity"; 35 36 /* 37 * Static values that must be set before each test to modify the behavior of the Activity. 38 */ 39 private static AdbDebuggingManager.AdbDebuggingHandler sHandler; 40 private static boolean sAllowKey; 41 private static boolean sAlwaysAllow; 42 private static String sExpectedKey; 43 private static BlockingQueue sBlockingQueue; 44 45 /** 46 * Receives the Intent sent from the AdbDebuggingManager and sends the preconfigured response. 47 */ 48 @Override onCreate(Bundle bundle)49 public void onCreate(Bundle bundle) { 50 super.onCreate(bundle); 51 Intent intent = getIntent(); 52 String key = intent.getStringExtra("key"); 53 if (!key.equals(sExpectedKey)) { 54 TestResult result = new TestResult(TestResult.RESULT_UNEXPECTED_KEY, key); 55 postResult(result); 56 finish(); 57 return; 58 } 59 // Post the result that the activity was successfully launched as expected and a response 60 // is being sent to let the test method know that it should move on to waiting for the next 61 // expected response from the AdbDebuggingManager. 62 TestResult result = new TestResult( 63 AdbDebuggingManagerTest.TestResult.RESULT_ACTIVITY_LAUNCHED); 64 postResult(result); 65 66 // Initialize the message based on the preconfigured values. If the key is accepted the 67 // AdbDebuggingManager expects the key to be in the obj field of the message, and if the 68 // user selects the 'Always allow' option the manager expects the arg1 field to be set to 1. 69 int messageType; 70 if (sAllowKey) { 71 messageType = AdbDebuggingManager.AdbDebuggingHandler.MESSAGE_ADB_ALLOW; 72 } else { 73 messageType = AdbDebuggingManager.AdbDebuggingHandler.MESSAGE_ADB_DENY; 74 } 75 Message message = sHandler.obtainMessage(messageType); 76 message.obj = key; 77 if (sAlwaysAllow) { 78 message.arg1 = 1; 79 } 80 finish(); 81 sHandler.sendMessage(message); 82 } 83 84 /** 85 * Posts the result of the activity to the test method. 86 */ postResult(TestResult result)87 private void postResult(TestResult result) { 88 try { 89 sBlockingQueue.put(result); 90 } catch (InterruptedException e) { 91 Log.e(TAG, "Caught an InterruptedException posting the result " + result, e); 92 } 93 } 94 95 /** 96 * Allows test methods to specify the behavior of the Activity before it is invoked by the 97 * AdbDebuggingManager. 98 */ 99 public static class Configurator { 100 101 /** 102 * Sets the test handler to be used by this activity to send the configured response. 103 */ setHandler(AdbDebuggingManager.AdbDebuggingHandler handler)104 public Configurator setHandler(AdbDebuggingManager.AdbDebuggingHandler handler) { 105 sHandler = handler; 106 return this; 107 } 108 109 /** 110 * Sets whether the key should be allowed for this test. 111 */ setAllowKey(boolean allow)112 public Configurator setAllowKey(boolean allow) { 113 sAllowKey = allow; 114 return this; 115 } 116 117 /** 118 * Sets whether the 'Always allow' option should be selected for this test. 119 */ setAlwaysAllow(boolean alwaysAllow)120 public Configurator setAlwaysAllow(boolean alwaysAllow) { 121 sAlwaysAllow = alwaysAllow; 122 return this; 123 } 124 125 /** 126 * Sets the key that should be expected from the AdbDebuggingManager for this test. 127 */ setExpectedKey(String expectedKey)128 public Configurator setExpectedKey(String expectedKey) { 129 sExpectedKey = expectedKey; 130 return this; 131 } 132 133 /** 134 * Sets the BlockingQueue that should be used to post the result of the Activity back to the 135 * test method. 136 */ setBlockingQueue(BlockingQueue blockingQueue)137 public Configurator setBlockingQueue(BlockingQueue blockingQueue) { 138 sBlockingQueue = blockingQueue; 139 return this; 140 } 141 } 142 } 143