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 
17 package com.android.cts.verifier.tv;
18 
19 import android.app.Activity;
20 import android.view.View;
21 import android.widget.TextView;
22 
23 import com.android.cts.verifier.R;
24 
25 import com.google.common.truth.FailureStrategy;
26 import com.google.common.truth.StandardSubjectBuilder;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /** Encapsulates the logic of a test step, which displays human instructions. */
32 public abstract class TestStepBase {
33     protected final Activity mContext;
34 
35     private boolean mHasPassed;
36     private Runnable mOnDoneListener;
37     private String mFailureDetails;
38     private StandardSubjectBuilder mAsserter;
39     private View mInstructionView;
40     private String mInstructionText;
41 
42     /**
43      * Constructs a test step containing instruction to the user and a button.
44      *
45      * @param context The test activity which this test step is part of.
46      * @param instructionText The text of the test instruction visible to the user.
47      */
TestStepBase(Activity context, String instructionText)48     public TestStepBase(Activity context, String instructionText) {
49         this.mContext = context;
50 
51         FailureStrategy failureStrategy =
52                 assertionError -> {
53                     appendFailureDetails(assertionError.getMessage());
54                     mHasPassed = false;
55                 };
56         mAsserter = StandardSubjectBuilder.forCustomFailureStrategy(failureStrategy);
57         mHasPassed = true;
58         mInstructionText = instructionText;
59     }
60 
hasPassed()61     public boolean hasPassed() {
62         return mHasPassed;
63     }
64 
65     /** Creates the View for this test step in the context {@link TvAppVerifierActivity}. */
createUiElements()66     public List<View> createUiElements() {
67         mInstructionView = TvAppVerifierActivity
68                 .createAutoItem(mContext.getLayoutInflater(), mInstructionText, null);
69         List<View> list = new ArrayList<>();
70         list.add(mInstructionView);
71         return list;
72     }
73 
74     /** Enables interactivity for this test step - for example, it enables buttons. */
enableInteractivity()75     public abstract void enableInteractivity();
76 
77     /** Disables interactivity for this test step - for example, it disables buttons. */
disableInteractivity()78     public abstract void disableInteractivity();
79 
setOnDoneListener(Runnable listener)80     public void setOnDoneListener(Runnable listener) {
81         mOnDoneListener = listener;
82     }
83 
getFailureDetails()84     public String getFailureDetails() {
85         return mFailureDetails;
86     }
87 
done()88     protected void done() {
89         TvAppVerifierActivity.setPassState(mInstructionView, mHasPassed);
90         if (mOnDoneListener != null) {
91             mOnDoneListener.run();
92         }
93     }
94 
getAsserter()95     protected StandardSubjectBuilder getAsserter() {
96         return mAsserter;
97     }
98 
appendInfoDetails(String infoFormat, Object... args)99     protected void appendInfoDetails(String infoFormat, Object... args) {
100         String info = String.format(infoFormat, args);
101         String details = String.format("Info: %s", info);
102         appendDetails(details);
103     }
104 
appendFailureDetails(String failure)105     protected void appendFailureDetails(String failure) {
106         String details = String.format("Failure: %s", failure);
107         appendDetails(details);
108 
109         appendMessageToView(mInstructionView, details);
110     }
111 
appendDetails(String details)112     protected void appendDetails(String details) {
113         if (mFailureDetails == null) {
114             mFailureDetails = new String();
115         }
116         mFailureDetails += details + "\n";
117     }
118 
appendMessageToView(View item, String message)119     private static void appendMessageToView(View item, String message) {
120         TextView instructions = item.findViewById(R.id.instructions);
121         instructions.setText(instructions.getText() + "\n" + message);
122     }
123 }
124