1 /* 2 * Copyright (C) 2014 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.cts.verifier.tv; 18 19 import android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.Button; 24 import android.widget.ImageView; 25 import android.widget.TextView; 26 27 import com.android.cts.verifier.PassFailButtons; 28 import com.android.cts.verifier.R; 29 30 /** 31 * Base class for TV app tests. 32 */ 33 public abstract class TvAppVerifierActivity extends PassFailButtons.Activity { 34 private static final String TAG = "TvAppVerifierActivity"; 35 36 private LayoutInflater mInflater; 37 private ViewGroup mItemList; 38 private View mPostTarget; 39 getPostTarget()40 public View getPostTarget() { 41 return mPostTarget; 42 } 43 44 @Override onCreate(Bundle savedInstanceState)45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 mInflater = getLayoutInflater(); 49 // Reusing location_mode_main. 50 View view = mInflater.inflate(R.layout.location_mode_main, null); 51 mPostTarget = mItemList = (ViewGroup) view.findViewById(R.id.test_items); 52 createTestItems(); 53 setContentView(view); 54 setPassFailButtonClickListeners(); 55 setInfoResources(); 56 57 getPassButton().setEnabled(false); 58 } 59 setButtonEnabled(View item, boolean enabled)60 public static void setButtonEnabled(View item, boolean enabled) { 61 View button = item.findViewById(R.id.user_action_button); 62 button.setFocusable(enabled); 63 button.setClickable(enabled); 64 button.setEnabled(enabled); 65 } 66 setPassState(View item, boolean passed)67 public static void setPassState(View item, boolean passed) { 68 ImageView status = (ImageView) item.findViewById(R.id.status); 69 status.setImageResource(passed ? R.drawable.fs_good : R.drawable.fs_error); 70 setButtonEnabled(item, false); 71 status.invalidate(); 72 } 73 createTestItems()74 protected abstract void createTestItems(); 75 setInfoResources()76 protected abstract void setInfoResources(); 77 78 /** 79 * Call this to create a test step where the user must perform some action. 80 */ createUserItem(int instructionTextId, int buttonTextId, View.OnClickListener l)81 public View createUserItem(int instructionTextId, int buttonTextId, View.OnClickListener l) { 82 View item = mInflater.inflate(R.layout.tv_item, mItemList, false); 83 TextView instructions = (TextView) item.findViewById(R.id.instructions); 84 instructions.setText(instructionTextId); 85 Button button = (Button) item.findViewById(R.id.user_action_button); 86 button.setVisibility(View.VISIBLE); 87 button.setText(buttonTextId); 88 button.setOnClickListener(l); 89 mItemList.addView(item); 90 return item; 91 } 92 93 /** 94 * Call this to create a test step where the user must perform some action. 95 */ createUserItem(CharSequence instructionCharSequence, int buttonTextId, View.OnClickListener l)96 public View createUserItem(CharSequence instructionCharSequence, 97 int buttonTextId, View.OnClickListener l) { 98 View item = mInflater.inflate(R.layout.tv_item, mItemList, false); 99 TextView instructions = (TextView) item.findViewById(R.id.instructions); 100 instructions.setText(instructionCharSequence); 101 Button button = (Button) item.findViewById(R.id.user_action_button); 102 button.setVisibility(View.VISIBLE); 103 button.setText(buttonTextId); 104 button.setOnClickListener(l); 105 mItemList.addView(item); 106 return item; 107 } 108 109 /** 110 * Call this to create a test step where the test automatically evaluates whether 111 * an expected condition is satisfied. 112 */ createAutoItem(int stringId)113 protected View createAutoItem(int stringId) { 114 View item = mInflater.inflate(R.layout.tv_item, mItemList, false); 115 TextView instructions = (TextView) item.findViewById(R.id.instructions); 116 instructions.setText(stringId); 117 mItemList.addView(item); 118 return item; 119 } 120 121 /** 122 * Call this to create alternative choice for the previous test step. 123 */ createButtonItem(int buttonTextId, View.OnClickListener l)124 protected View createButtonItem(int buttonTextId, View.OnClickListener l) { 125 View item = mInflater.inflate(R.layout.tv_item, mItemList, false); 126 Button button = (Button) item.findViewById(R.id.user_action_button); 127 button.setVisibility(View.VISIBLE); 128 button.setText(buttonTextId); 129 button.setOnClickListener(l); 130 ImageView status = (ImageView) item.findViewById(R.id.status); 131 status.setVisibility(View.INVISIBLE); 132 TextView instructions = (TextView) item.findViewById(R.id.instructions); 133 instructions.setVisibility(View.GONE); 134 mItemList.addView(item); 135 return item; 136 } 137 containsButton(View item, View button)138 static boolean containsButton(View item, View button) { 139 return item == null ? false : item.findViewById(R.id.user_action_button) == button; 140 } 141 } 142