1 /* 2 * Copyright (C) 2019 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; 17 18 import android.os.Bundle; 19 import android.view.View; 20 import android.widget.Button; 21 import android.widget.TextView; 22 import android.widget.Toast; 23 24 /** 25 * {@link PassFailButtons.Activity} that supports showing a series of tests in order. 26 */ 27 public abstract class OrderedTestActivity extends PassFailButtons.Activity { 28 private static final String KEY_CURRENT_TEST = "current_test"; 29 30 private Test[] mTests; 31 private int mTestIndex; 32 33 protected Button mNextButton; 34 protected TextView mInstructions; 35 onCreate(Bundle savedInstanceState)36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.ordered_test); 39 setPassFailButtonClickListeners(); 40 41 mTests = getTests(); 42 43 mInstructions = findViewById(R.id.txt_instruction); 44 45 mNextButton = findViewById(R.id.btn_next); 46 mNextButton.setOnClickListener(v -> { 47 if ((mTestIndex >= 0) && (mTestIndex < mTests.length)) { 48 mTests[mTestIndex].onNextClick(); 49 } 50 }); 51 52 // Don't allow pass until all tests complete. 53 findViewById(R.id.pass_button).setVisibility(View.GONE); 54 55 // Figure out if we are in a test or starting from the beginning. 56 if (savedInstanceState != null && savedInstanceState.containsKey(KEY_CURRENT_TEST)) { 57 mTestIndex = savedInstanceState.getInt(KEY_CURRENT_TEST); 58 } else { 59 mTestIndex = 0; 60 } 61 } 62 63 @Override onStart()64 protected void onStart() { 65 super.onStart(); 66 processCurrentTest(); 67 } 68 69 /** Returns a list of tests to run in order. */ getTests()70 protected abstract Test[] getTests(); 71 72 @Override onSaveInstanceState(Bundle outState)73 protected void onSaveInstanceState(Bundle outState) { 74 super.onSaveInstanceState(outState); 75 76 outState.putInt(KEY_CURRENT_TEST, mTestIndex); 77 } 78 succeed()79 protected void succeed() { 80 runOnUiThread(() -> { 81 mTestIndex++; 82 processCurrentTest(); 83 }); 84 } 85 error(int stringResId)86 protected void error(int stringResId) { 87 Toast.makeText(this, stringResId, Toast.LENGTH_SHORT).show(); 88 } 89 90 /** 91 * Must be invoked on the main thread 92 */ processCurrentTest()93 private void processCurrentTest() { 94 if (mTestIndex < mTests.length) { 95 mTests[mTestIndex].run(); 96 } else { 97 // On test completion, hide "next" and "fail" buttons, and show "pass" button 98 // instead. 99 mInstructions.setText(R.string.tests_completed_successfully); 100 mNextButton.setVisibility(View.GONE); 101 findViewById(R.id.pass_button).setVisibility(View.VISIBLE); 102 findViewById(R.id.fail_button).setVisibility(View.GONE); 103 } 104 } 105 106 protected abstract class Test { 107 private final int mStringId; 108 Test(int stringResId)109 public Test(int stringResId) { 110 mStringId = stringResId; 111 } 112 run()113 protected void run() { 114 showText(); 115 } 116 showText()117 protected void showText() { 118 if (mStringId == 0) { 119 return; 120 } 121 mInstructions.setText(mStringId); 122 } 123 onNextClick()124 protected void onNextClick() { 125 } 126 } 127 128 } 129