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