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 package com.android.cts.verifier.screenpinning; 17 18 import android.app.ActivityManager; 19 import android.os.Bundle; 20 import android.util.Log; 21 import android.widget.TextView; 22 23 import com.android.cts.verifier.OrderedTestActivity; 24 import com.android.cts.verifier.R; 25 26 public class ScreenPinningTestActivity extends OrderedTestActivity { 27 28 private static final String TAG = "ScreenPinningTestActivity"; 29 private static final long TASK_MODE_CHECK_DELAY = 200; 30 private static final int MAX_TASK_MODE_CHECK_COUNT = 5; 31 32 private ActivityManager mActivityManager; 33 onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 38 } 39 40 @Override getTests()41 protected Test[] getTests() { 42 return new Test[]{ 43 // Verify not already pinned. 44 mCheckStartedUnpinned, 45 // Enter pinning, verify pinned, try leaving and have the user exit. 46 mCheckStartPinning, 47 mCheckIsPinned, 48 mCheckTryLeave, 49 mCheckUnpin, 50 // Enter pinning, verify pinned, and use APIs to exit. 51 mCheckStartPinning, 52 mCheckIsPinned, 53 mCheckUnpinFromCode 54 }; 55 } 56 57 @Override onBackPressed()58 public void onBackPressed() { 59 // Block back button so we can test screen pinning exit functionality. 60 // Users can still leave by pressing fail (or when done the pass) button. 61 } 62 63 @Override error(int errorId)64 protected void error(int errorId) { 65 error(errorId, new Throwable()); 66 } 67 error(final int errorId, final Throwable cause)68 private void error(final int errorId, final Throwable cause) { 69 runOnUiThread(new Runnable() { 70 @Override 71 public void run() { 72 String error = getString(errorId); 73 Log.d(TAG, error, cause); 74 75 ((TextView) findViewById(R.id.txt_instruction)).setText(error); 76 } 77 }); 78 } 79 80 // Verify we don't start in screen pinning. 81 private final Test mCheckStartedUnpinned = new Test(0) { 82 public void run() { 83 if (mActivityManager.isInLockTaskMode()) { 84 error(R.string.error_screen_already_pinned); 85 } else { 86 succeed(); 87 } 88 } 89 }; 90 91 // Start screen pinning by having the user click next then confirm it for us. 92 private final Test mCheckStartPinning = new Test(R.string.screen_pin_instructions) { 93 protected void onNextClick() { 94 startLockTask(); 95 succeed(); 96 } 97 }; 98 99 // Click next and check that we got pinned. 100 // Wait for the user to click next to verify that they got back from the prompt 101 // successfully. 102 private final Test mCheckIsPinned = new Test(R.string.screen_pin_check_pinned) { 103 protected void onNextClick() { 104 if (mActivityManager.isInLockTaskMode()) { 105 succeed(); 106 } else { 107 error(R.string.error_screen_pinning_did_not_start); 108 } 109 } 110 }; 111 112 // Tell user to try to leave. 113 private final Test mCheckTryLeave = new Test(R.string.screen_pin_no_exit) { 114 protected void onNextClick() { 115 if (mActivityManager.isInLockTaskMode()) { 116 succeed(); 117 } else { 118 error(R.string.error_screen_no_longer_pinned); 119 } 120 } 121 }; 122 123 // Verify that the user unpinned and it worked. 124 private final Test mCheckUnpin = new Test(R.string.screen_pin_exit) { 125 protected void onNextClick() { 126 if (!mActivityManager.isInLockTaskMode()) { 127 succeed(); 128 } else { 129 error(R.string.error_screen_pinning_did_not_exit); 130 } 131 } 132 }; 133 134 // Unpin from code and check that it worked. 135 private final Test mCheckUnpinFromCode = new Test(0) { 136 protected void run() { 137 if (!mActivityManager.isInLockTaskMode()) { 138 error(R.string.error_screen_pinning_did_not_start); 139 return; 140 } 141 stopLockTask(); 142 for (int retry = MAX_TASK_MODE_CHECK_COUNT; retry > 0; retry--) { 143 try { 144 Thread.sleep(TASK_MODE_CHECK_DELAY); 145 } catch (InterruptedException e) { 146 } 147 Log.d(TAG, "Check unpin ... " + retry); 148 if (!mActivityManager.isInLockTaskMode()) { 149 succeed(); 150 break; 151 } else if (retry == 1) { 152 error(R.string.error_screen_pinning_couldnt_exit); 153 } 154 } 155 } 156 }; 157 } 158