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      */
createAndAttachUserItem(int instructionTextId, int buttonTextId, View.OnClickListener l)81     public View createAndAttachUserItem(int instructionTextId, int buttonTextId,
82             View.OnClickListener l) {
83         View item = mInflater.inflate(R.layout.tv_item, mItemList, false);
84         TextView instructions = (TextView) item.findViewById(R.id.instructions);
85         instructions.setText(instructionTextId);
86         Button button = (Button) item.findViewById(R.id.user_action_button);
87         button.setVisibility(View.VISIBLE);
88         button.setText(buttonTextId);
89         button.setOnClickListener(l);
90         mItemList.addView(item);
91         return item;
92     }
93 
94     /**
95      * Call this to create a test step where the user must perform some action.
96      */
createAndAttachUserItem(CharSequence instructionCharSequence, int buttonTextId, View.OnClickListener l)97     public View createAndAttachUserItem(CharSequence instructionCharSequence,
98                                   int buttonTextId, View.OnClickListener l) {
99         View item = mInflater.inflate(R.layout.tv_item, mItemList, false);
100         TextView instructions = item.findViewById(R.id.instructions);
101         instructions.setText(instructionCharSequence);
102         Button button = item.findViewById(R.id.user_action_button);
103         button.setVisibility(View.VISIBLE);
104         button.setText(buttonTextId);
105         button.setOnClickListener(l);
106         mItemList.addView(item);
107         return item;
108     }
109 
110     /**
111      * Call this to create a test step where the test automatically evaluates whether
112      * an expected condition is satisfied.
113      */
createAndAttachAutoItem(int stringId)114     public View createAndAttachAutoItem(int stringId) {
115         View item = mInflater.inflate(R.layout.tv_item, mItemList, false);
116         TextView instructions = item.findViewById(R.id.instructions);
117         instructions.setText(stringId);
118         mItemList.addView(item);
119         return item;
120     }
121 
122     /**
123      * Call this to create a test step where the test automatically evaluates whether
124      * an expected condition is satisfied.
125      */
createAutoItem(LayoutInflater inflater, CharSequence instructionCharSequence, ViewGroup root)126     public static View createAutoItem(LayoutInflater inflater,
127             CharSequence instructionCharSequence, ViewGroup root) {
128         View item = inflater.inflate(R.layout.tv_item, root, false);
129         TextView instructions = item.findViewById(R.id.instructions);
130         instructions.setText(instructionCharSequence);
131         return item;
132     }
133 
134     /**
135      * Call this to create a test step where the test automatically evaluates whether
136      * an expected condition is satisfied, and to attach it to the activity.
137      */
createAndAttachAutoItem(CharSequence instructionCharSequence)138     public View createAndAttachAutoItem(CharSequence instructionCharSequence) {
139         View item = createAutoItem(mInflater, instructionCharSequence, mItemList);
140         mItemList.addView(item);
141         return item;
142     }
143 
144     /**
145      * Call this to create alternative choice for the previous test step.
146      */
createButtonItem(LayoutInflater inflater, ViewGroup root, int buttonTextId, View.OnClickListener l)147     public static View createButtonItem(LayoutInflater inflater, ViewGroup root, int buttonTextId,
148             View.OnClickListener l) {
149         View item = inflater.inflate(R.layout.tv_item, root, false);
150         Button button = item.findViewById(R.id.user_action_button);
151         button.setVisibility(View.VISIBLE);
152         button.setText(buttonTextId);
153         button.setOnClickListener(l);
154         ImageView status = item.findViewById(R.id.status);
155         status.setVisibility(View.INVISIBLE);
156         TextView instructions = item.findViewById(R.id.instructions);
157         instructions.setVisibility(View.GONE);
158         return item;
159     }
160 
161     /**
162      * Call this to create alternative choice for the previous test step and to attach it to the
163      * activity.
164      */
createAndAttachButtonItem(int buttonTextId, View.OnClickListener l)165     public View createAndAttachButtonItem(int buttonTextId, View.OnClickListener l) {
166         View item = createButtonItem(mInflater, mItemList, buttonTextId, l);
167         mItemList.addView(item);
168         return item;
169     }
170 
171     /**
172      * Adds an item to the activity.
173      */
addItem(View item)174     public void addItem(View item) {
175         mItemList.addView(item);
176     }
177 
containsButton(View item, View button)178     static boolean containsButton(View item, View button) {
179         return item == null ? false : item.findViewById(R.id.user_action_button) == button;
180     }
181 }
182